This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
350 lines (324 loc) · 9.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"bufio"
"io"
"log"
"net/http"
"net/url"
"os"
"regexp"
"sort"
"strings"
"sync"
"text/template"
"time"
)
// Structs
type blacklist struct {
IgnoreCount int
WhitelistedCount int
Data []string
}
type source map[string]*blacklist
type name struct {
URL string
Value string
}
type timeRestriction struct {
Pass map[string]string
Ignore []string
}
type dataTemp struct {
TimeRestrictions timeRestriction
Sources map[string]*blacklist
}
func getBlacklistURLs(fileName string) ([]url.URL, error) {
file, err := os.Open(fileName)
if err != nil {
log.Println("Error: readFile", err)
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
urls := []url.URL{}
for scanner.Scan() {
line := scanner.Text()
if line == "" || line[:1] == "#" {
continue
}
u, err := url.Parse(line)
if err != nil {
log.Println("Error: Parse", err)
return nil, err
}
urls = append(urls, *u)
}
return urls, nil
}
func parseBlacklist(url string, r io.Reader, names chan<- name, trust bool) error {
rx_comment := regexp.MustCompile(`^(#|$)`)
rx_inline_comment := regexp.MustCompile(`\s*#\s*[a-z0-9-].*$`)
rx_u := regexp.MustCompile(
`^@*\|\|([a-z0-9.-]+[.][a-z]{2,})\^?(\$(popup|third-party))?$`)
rx_l := regexp.MustCompile(`^([a-z0-9.-]+[.][a-z]{2,})$`)
rx_h := regexp.MustCompile(
`^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}\s+([a-z0-9.-]+[.][a-z]{2,})$`)
rx_mdl := regexp.MustCompile(`^"[^"]+","([a-z0-9.-]+[.][a-z]{2,})",`)
rx_b := regexp.MustCompile(`^([a-z0-9.-]+[.][a-z]{2,}),.+,[0-9: /-]+,`)
rx_dq := regexp.MustCompile(`^address=/([a-z0-9.-]+[.][a-z]{2,})/.`)
rx_trusted := regexp.MustCompile(`^([*a-z0-9.-]+)\s*(@\S+)?$`)
// Trusted
// time_restrictions = {}
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
rx_set := []*regexp.Regexp{rx_u, rx_l, rx_h, rx_mdl, rx_b, rx_dq}
rx_setTrust := []*regexp.Regexp{rx_trusted}
for scanner.Scan() {
line := strings.TrimSpace(strings.ToLower(scanner.Text()))
if rx_comment.MatchString(line) {
continue
}
line = rx_inline_comment.ReplaceAllLiteralString(line, "")
if trust {
for _, rx := range rx_setTrust {
if !rx.MatchString(line) {
continue
}
res := rx.FindStringSubmatch(line)
names <- name{URL: url, Value: res[1]}
}
} else {
for _, rx := range rx_set {
if !rx.MatchString(line) {
continue
}
res := rx.FindStringSubmatch(line)
names <- name{URL: url, Value: res[1]}
}
}
}
if err := scanner.Err(); err != nil {
log.Println("Error: parseList", err)
return err
}
return nil
}
func parseLocalList(fileName string) (map[string]struct{}, map[string]string, error) {
file, err := os.Open(fileName)
if err != nil {
log.Println("Error: readFile", err)
return nil, nil, err
}
defer file.Close()
rx_comment := regexp.MustCompile(`^(#|$)`)
rx_inline_comment := regexp.MustCompile(`\s*#\s*[a-z0-9-].*$`)
rx_trusted := regexp.MustCompile(`^([*a-z0-9.-]+)\s*(@\S+)?$`)
// https://emersion.fr/blog/2017/sets-in-go/
namesSet := make(map[string]struct{})
timeRestriction := make(map[string]string)
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
rx_setTrust := []*regexp.Regexp{rx_trusted}
for scanner.Scan() {
line := strings.TrimSpace(strings.ToLower(scanner.Text()))
if rx_comment.MatchString(line) {
continue
}
line = rx_inline_comment.ReplaceAllLiteralString(line, "")
for _, rx := range rx_setTrust {
if !rx.MatchString(line) {
continue
}
res := rx.FindStringSubmatch(line)
namesSet[res[1]] = struct{}{}
if res[2] != "" {
timeRestriction[res[1]] = res[2]
}
}
}
if err := scanner.Err(); err != nil {
log.Println("Error: whitelistNames", err)
return nil, nil, err
}
return namesSet, timeRestriction, nil
}
// https://stackoverflow.com/questions/34464146/the-idiomatic-way-to-implement-generators-yield-in-golang-for-recursive-functi
func callURL(urls []url.URL) <-chan name {
var wg sync.WaitGroup
names := make(chan name)
go func() {
for _, u := range urls {
wg.Add(1)
go func(u url.URL) {
defer wg.Done()
if u.Scheme == "file" {
file, err := os.Open(u.String()[5:])
if err != nil {
log.Println("Error: readFile", err)
return
}
defer file.Close()
parseBlacklist(u.String(), file, names, true)
} else if u.Scheme == "https" || u.Scheme == "http" {
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
log.Println("Error: NewRequest", err)
return
}
body, err := send(req)
if err != nil {
log.Println("Error: Send", err)
return
}
defer body.Close()
parseBlacklist(u.String(), body, names, false)
} else {
log.Println("Error: Scheme not supported", u.Scheme)
}
}(u)
}
wg.Wait()
close(names)
}()
return names
}
func hasSuffix(namesSet map[string]struct{}, name string) bool {
parts := strings.Split(name, ".")
for range parts {
parts = parts[1:]
if _, ok := namesSet[strings.Join(parts, ".")]; ok {
return true
}
}
return false
}
func main() {
start := time.Now()
urls, err := getBlacklistURLs("domains-blacklist.conf")
if err != nil {
log.Println("Error: getBlacklistURLs", err)
return
}
namesSet := make(map[string]struct{})
whitelistsSet, _, err := parseLocalList("domains-whitelist.txt")
if err != nil {
log.Println("Error: parseLocalList", err)
return
}
timeRestrictionSet, timeRestrictions, err := parseLocalList("domains-time-restricted.txt")
if err != nil {
log.Println("Error: parseLocalList", err)
return
}
for url := range timeRestrictionSet {
if _, ok := whitelistsSet[url]; !ok {
whitelistsSet[url] = struct{}{}
}
}
sources := make(source)
tmpSources := make(source)
for _, url := range urls {
sources[url.String()] = &blacklist{}
tmpSources[url.String()] = &blacklist{}
}
for name := range callURL(urls) {
if _, ok := namesSet[name.Value]; ok {
sources[name.URL].IgnoreCount++
continue
}
if _, ok := whitelistsSet[name.Value]; ok {
sources[name.URL].WhitelistedCount++
continue
}
namesSet[name.Value] = struct{}{}
tmpSources[name.URL].Data = append(tmpSources[name.URL].Data, name.Value)
}
// another loop for has_suffix Because maybe in other list suffix appear before main site when fetching and main site already in the list
for url := range tmpSources {
for _, name := range tmpSources[url].Data {
if hasSuffix(namesSet, name) {
sources[url].IgnoreCount++
continue
}
if hasSuffix(whitelistsSet, name) {
sources[url].WhitelistedCount++
continue
}
sources[url].Data = append(sources[url].Data, name)
}
}
// Sort by reverse domain
for url := range sources {
sort.Slice(sources[url].Data, func(i, j int) bool {
iParts := strings.Split(sources[url].Data[i], ".")
jParts := strings.Split(sources[url].Data[j], ".")
for i := len(iParts)/2 - 1; i >= 0; i-- {
opp := len(iParts) - 1 - i
iParts[i], iParts[opp] = iParts[opp], iParts[i]
}
for i := len(jParts)/2 - 1; i >= 0; i-- {
opp := len(jParts) - 1 - i
jParts[i], jParts[opp] = jParts[opp], jParts[i]
}
return strings.Join(iParts, ".") < strings.Join(jParts, ".")
})
}
var totalIgnoreCount, totalWhitelistedCount, totalData int
for url := range sources {
totalIgnoreCount += sources[url].IgnoreCount
totalWhitelistedCount += sources[url].WhitelistedCount
totalData += len(sources[url].Data)
}
log.Println("Total Data:", totalData)
log.Println("Total Ignored duplicates:", totalIgnoreCount)
log.Println("Total Ignored whitelisted :", totalWhitelistedCount)
// Template
tpl, err := template.New("blacklists").Parse(`{{ if or .TimeRestrictions.Pass .TimeRestrictions.Ignore }}########## Time-based blacklist ##########
{{ range $key, $value := .TimeRestrictions.Pass -}}
{{ $key }} {{ $value }}
{{ end }}{{ range $key := .TimeRestrictions.Ignore -}}
# ignored: [{{ $key }}] was in the time-restricted list, but without a time restriction label
{{ end }}
{{ end }}{{ range $key, $value := .Sources -}}
########## Blacklist from {{ $key}} ##########
{{ if $value.IgnoreCount -}}
# Ignored duplicates: {{ $value.IgnoreCount }}
{{ end }}
{{- if $value.WhitelistedCount -}}
# Ignored entries due to the whitelist: {{ $value.WhitelistedCount }}
{{ end }}
{{ range $index, $name := $value.Data -}}
{{ $name }}
{{ end }}
{{ end }}`)
if err != nil {
log.Println("Error: template", err)
return
}
f, err := os.Create("results.txt")
if err != nil {
log.Println("Error: Create file", err)
return
}
defer f.Close()
data := dataTemp{Sources: sources, TimeRestrictions: timeRestriction{Pass: make(map[string]string)}}
for url := range timeRestrictionSet {
if _, ok := timeRestrictions[url]; ok {
data.TimeRestrictions.Pass[url] = timeRestrictions[url]
} else {
data.TimeRestrictions.Ignore = append(data.TimeRestrictions.Ignore, url)
}
}
if err = tpl.Execute(f, data); err != nil {
log.Println("Error: Execute", err)
return
}
// https://www.joeshaw.org/dont-defer-close-on-writable-files/
if err = f.Sync(); err != nil {
log.Println("Error: Sync", err)
return
}
elapsed := time.Since(start)
log.Printf("It tooks %s", elapsed)
}