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
|
package blocklist
import (
"bufio"
"net/http"
"os"
"strings"
"sync"
"sync/atomic"
)
type ResponseAction int
const (
ResponseZeroIP ResponseAction = iota
ResponseNXDOMAIN
)
type Blocklist struct {
mu sync.RWMutex
blocked *trie
exceptions *trie
response ResponseAction
TotalRules int32
Hits int64
}
func New(action ResponseAction) *Blocklist {
return &Blocklist{
blocked: newTrie(),
exceptions: newTrie(),
response: action,
}
}
func (b *Blocklist) Response() ResponseAction {
return b.response
}
func (b *Blocklist) IsBlocked(domain string) bool {
b.mu.RLock()
defer b.mu.RUnlock()
labels := splitDomain(domain)
if !b.blocked.match(labels) {
return false
}
if b.exceptions.match(labels) {
return false
}
atomic.AddInt64(&b.Hits, 1)
return true
}
func (b *Blocklist) LoadFile(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
var n int32
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || line[0] == '#' || line[0] == '!' {
continue
}
if b.addRule(line) {
n++
}
}
atomic.StoreInt32(&b.TotalRules, n)
return scanner.Err()
}
func (b *Blocklist) LoadURL(url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
var n int32
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || line[0] == '#' {
continue
}
if b.addRule(line) {
n++
}
}
atomic.AddInt32(&b.TotalRules, n)
return scanner.Err()
}
func (b *Blocklist) addRule(line string) bool {
b.mu.Lock()
defer b.mu.Unlock()
if strings.HasPrefix(line, "@@") {
domain := strings.TrimPrefix(line, "@@")
domain = strings.TrimPrefix(domain, "||")
if idx := strings.Index(domain, "^"); idx > 0 {
domain = domain[:idx]
}
b.exceptions.insert(splitDomain(domain))
return true
}
fields := strings.Fields(line)
if len(fields) >= 2 {
ip := fields[0]
if ip == "0.0.0.0" || ip == "127.0.0.1" || ip == "::1" || ip == "::" {
b.blocked.insert(splitDomain(fields[len(fields)-1]))
return true
}
}
if strings.HasPrefix(line, "||") {
domain := strings.TrimPrefix(line, "||")
if idx := strings.Index(domain, "^"); idx > 0 {
domain = domain[:idx]
}
b.blocked.insert(splitDomain(domain))
return true
}
if strings.Contains(line, ".") && !strings.ContainsAny(line, " /") {
b.blocked.insert(splitDomain(line))
return true
}
return false
}
func splitDomain(domain string) []string {
domain = strings.TrimSuffix(domain, ".")
return strings.Split(domain, ".")
}
type trieNode struct {
children map[string]*trieNode
terminal bool
}
type trie struct{ root *trieNode }
func newTrie() *trie {
return &trie{
root: &trieNode{
children: make(map[string]*trieNode),
},
}
}
func (t *trie) insert(labels []string) {
node := t.root
for i := len(labels) - 1; i >= 0; i-- {
child, ok := node.children[labels[i]]
if !ok {
child = &trieNode{children: make(map[string]*trieNode)}
node.children[labels[i]] = child
}
node = child
}
node.terminal = true
}
func (t *trie) match(labels []string) bool {
node := t.root
for i := len(labels) - 1; i >= 0; i-- {
if node.terminal {
return true
}
child, ok := node.children[labels[i]]
if !ok {
return false
}
node = child
}
return node.terminal
}
|