-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildhostnames.go
81 lines (77 loc) · 2.16 KB
/
buildhostnames.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
package blacklist
import (
"context"
"strings"
)
//nolint:lll
const (
adsBlockListHostnamesURL = "https://raw.githubusercontent.com/qdm12/files/master/ads-hostnames.updated"
maliciousBlockListHostnamesURL = "https://raw.githubusercontent.com/qdm12/files/master/malicious-hostnames.updated"
surveillanceBlockListHostnamesURL = "https://raw.githubusercontent.com/qdm12/files/master/surveillance-hostnames.updated"
)
func (b *builder) Hostnames(ctx context.Context,
blockMalicious, blockAds, blockSurveillance bool,
additionalBlockedHostnames, allowedHostnames []string) (
blockedHostnames []string, errs []error) {
chResults := make(chan []string)
chError := make(chan error)
listsLeftToFetch := 0
if blockMalicious {
listsLeftToFetch++
go func() {
results, err := getList(ctx, b.client, string(maliciousBlockListHostnamesURL))
chResults <- results
chError <- err
}()
}
if blockAds {
listsLeftToFetch++
go func() {
results, err := getList(ctx, b.client, string(adsBlockListHostnamesURL))
chResults <- results
chError <- err
}()
}
if blockSurveillance {
listsLeftToFetch++
go func() {
results, err := getList(ctx, b.client, string(surveillanceBlockListHostnamesURL))
chResults <- results
chError <- err
}()
}
uniqueResults := make(map[string]struct{})
for listsLeftToFetch > 0 {
select {
case results := <-chResults:
for _, result := range results {
uniqueResults[result] = struct{}{}
}
case err := <-chError:
listsLeftToFetch--
if err != nil {
errs = append(errs, err)
}
}
}
for _, blockedHostname := range additionalBlockedHostnames {
allowed := false
for _, allowedHostname := range allowedHostnames {
if blockedHostname == allowedHostname || strings.HasSuffix(blockedHostname, "."+allowedHostname) {
allowed = true
}
}
if allowed {
continue
}
uniqueResults[blockedHostname] = struct{}{}
}
for _, allowedHostname := range allowedHostnames {
delete(uniqueResults, allowedHostname)
}
blockedHostnames = make([]string, 0, len(uniqueResults))
for result := range uniqueResults {
blockedHostnames = append(blockedHostnames, result)
}
return blockedHostnames, errs
}