-
Notifications
You must be signed in to change notification settings - Fork 15
/
update.go
334 lines (305 loc) · 8.74 KB
/
update.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/missdeer/blocklist/utils"
"github.com/missdeer/golib/semaphore"
)
var (
sourceURLValidatorMap = map[string]lineValidator{
`https://raw.githubusercontent.com/notracking/hosts-blocklists/master/hostnames.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/yous/YousList/master/hosts.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/koala0529/adhost/master/adhosts`: hostLine("127.0.0.1"),
`https://raw.githubusercontent.com/azet12/KADhosts/master/KADhosts.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/lack006/Android-Hosts-L/master/hosts_files/2016_hosts/AD`: hostLine("127.0.0.1"),
`https://adaway.org/hosts.txt`: hostLine("127.0.0.1"),
`http://sysctl.org/cameleon/hosts`: hostLine("127.0.0.1"),
`https://download.dnscrypt.info/blacklists/domains/mybase.txt`: domainListLine(),
`https://anti-ad.net/domains.txt`: domainListLine(),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/AdguardMobileAds.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/AdguardMobileSpyware.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/AdguardTracking.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/AdguardCNAMEAds.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/AdguardCNAMEClickthroughs.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/AdguardCNAMEMicrosites.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/AdguardCNAME.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/AdguardDNS.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/EasyPrivacyCNAME.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/EasyPrivacySpecific.txt`: hostLine("0.0.0.0"),
`https://raw.githubusercontent.com/r-a-y/mobile-hosts/master/EasyPrivacy3rdParty.txt`: hostLine("0.0.0.0"),
`https://gitlab.com/ZeroDot1/CoinBlockerLists/raw/master/hosts`: hostLine("0.0.0.0"),
}
shortURLs = []string{
`db.tt`,
`www.db.tt`,
`j.mp`,
`www.j.mp`,
`bit.ly`,
`www.bit.ly`,
`pix.bit.ly`,
`goo.gl`,
`www.goo.gl`,
`t.co`,
`git.io`,
}
tlds = NewTLDs()
mutex sync.Mutex
sema = semaphore.New(50)
finalDomains = make(map[string]struct{})
blockDomain = make(chan string, 20)
quit = make(chan bool)
)
const (
blocklist = `toblock.lst`
blocklistWithoutShortURL = `toblock-without-shorturl.lst`
blocklistOptimized = `toblock-optimized.lst`
blocklistWithoutShortURLOptimized = `toblock-without-shorturl-optimized.lst`
)
func downloadRemoteContent(remoteLink string) (io.ReadCloser, error) {
response, err := http.Get(remoteLink)
if err != nil {
log.Println(err)
return nil, err
}
return response.Body, nil
}
func existent(domain string) (bool, error) {
req, err := http.NewRequest("GET", "https://dns.google.com/resolve", nil)
if err != nil {
log.Println("creating request failed:", domain, err)
return true, err
}
q := req.URL.Query()
q.Add("name", domain)
req.URL.RawQuery = q.Encode()
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
log.Println("doing request failed:", domain, err)
return true, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Println(domain, resp.Status)
return true, fmt.Errorf("unexpected status code:%s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("reading response failed:", domain, err)
return true, err
}
var response struct {
Status int `json:"Status"`
}
if err = json.Unmarshal(body, &response); err != nil {
log.Println("unmarshalling response failed:", domain, err)
return true, err
}
if response.Status == 3 {
return false, nil
}
return true, nil
}
func generateTLDs(wg *sync.WaitGroup) {
err := os.ErrNotExist
var r io.ReadCloser
for i := 0; i < 10 && err != nil; time.Sleep(5 * time.Second) {
r, err = downloadRemoteContent(tldsURL)
i++
}
if err == nil {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
tlds.insert(strings.ToLower(scanner.Text()))
}
r.Close()
}
wg.Done()
}
func generateEffectiveTLDsNames(wg *sync.WaitGroup) {
err := os.ErrNotExist
var r io.ReadCloser
for i := 0; i < 10 && err != nil; time.Sleep(5 * time.Second) {
r, err = downloadRemoteContent(effectiveTLDsNamesURL)
i++
}
if err == nil {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := strings.ToLower(scanner.Text())
if len(line) == 0 {
continue
}
c := line[0]
if c >= byte('a') && c <= byte('z') || c >= byte('0') && c <= byte('9') {
if strings.IndexByte(line, byte('.')) < 0 {
tlds.insert(line)
} else {
effectiveTLDsNames = append(effectiveTLDsNames, "."+line)
}
}
}
r.Close()
}
wg.Done()
}
func process(r io.ReadCloser, validator lineValidator) (domains []string, err error) {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
// extract valid lines
domain := validator(strings.ToLower(scanner.Text()))
if domain == "" {
continue
}
// remove items that don't match TLDs
if !tlds.match(domain) {
log.Println("don't match TLDs:", domain)
continue
}
// remove items in white list
if utils.InWhitelist(domain) {
log.Println("in whitelist:", domain)
continue
}
domains = append(domains, domain)
}
r.Close()
return
}
func saveToFile(content string, path string) error {
file, err := os.OpenFile(path, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644)
if err == nil {
file.WriteString(content)
file.Close()
return nil
}
log.Println(err)
return err
}
func getDomains(u string, v lineValidator, domains map[string]struct{}, wg *sync.WaitGroup) {
// download hosts
err := os.ErrNotExist
var r io.ReadCloser
for i := 0; i < 10 && err != nil; time.Sleep(5 * time.Second) {
r, err = downloadRemoteContent(u)
i++
}
if err == nil {
d, _ := process(r, v)
for _, domain := range d {
// so could remove duplicates
mutex.Lock()
domains[domain] = struct{}{}
mutex.Unlock()
}
}
wg.Done()
}
func receiveDomains() {
for {
select {
case domain := <-blockDomain:
finalDomains[domain] = struct{}{}
case <-quit:
return
}
}
}
func checkExistent(domain string, wg *sync.WaitGroup) {
// remove items that doesn't exist actually
for i := 0; i < 10; time.Sleep(3 * time.Second) {
exists, err := existent(domain)
if err != nil {
continue
}
if exists {
blockDomain <- domain
} else {
log.Println("google dns reports as non-exist:", domain)
}
break
}
sema.Release()
wg.Done()
}
func main() {
var wg sync.WaitGroup
// generate TLDs
wg.Add(2)
go generateTLDs(&wg)
go generateEffectiveTLDsNames(&wg)
wg.Wait()
// get blocked domain names
domains := make(map[string]struct{})
wg.Add(len(sourceURLValidatorMap))
for u, v := range sourceURLValidatorMap {
go getDomains(u, v, domains, &wg)
}
wg.Wait()
// remove non-exist domain names
go receiveDomains()
wg.Add(len(domains))
for domain := range domains {
sema.Acquire()
go checkExistent(domain, &wg)
}
wg.Wait()
quit <- true
// handle domain names of short URL services
for _, v := range shortURLs {
delete(finalDomains, v)
}
d := make([]string, len(finalDomains))
i := 0
for k := range finalDomains {
d[i] = k
i++
}
// save to file in order
sort.Strings(d)
c := strings.Join(d, "\n")
saveToFile(c, blocklistWithoutShortURL)
d = append(d, shortURLs...)
sort.Strings(d)
c = strings.Join(d, "\n")
saveToFile(c, blocklist)
// optimized
d = make([]string, len(finalDomains))
i = 0
for k := range finalDomains {
pick := true
kk := strings.Split(k, ".")
for l := 1; l < len(kk); l++ {
dn := strings.Join(kk[l:], ".")
if _, ok := finalDomains[dn]; ok {
pick = false
break
}
}
if pick {
d[i] = k
i++
}
}
d = d[:i]
// save to file in order
sort.Strings(d)
c = strings.Join(d, "\n")
saveToFile(c, blocklistWithoutShortURLOptimized)
d = append(d, shortURLs...)
sort.Strings(d)
c = strings.Join(d, "\n")
saveToFile(c, blocklistOptimized)
}