-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparental_parser.go
More file actions
236 lines (216 loc) · 6.01 KB
/
parental_parser.go
File metadata and controls
236 lines (216 loc) · 6.01 KB
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
/*
File: parental_parser.go
Version: 1.1.0
Updated: 21-Apr-2026 14:42 CEST
Description:
String and byte parsing for Parental Category domain lists.
Extracted from parental_categories.go to isolate formatting constraints.
Changes:
1.1.0 - [FEAT] Integrated `ipVersionSupport` check into `parseCategoryLine`
to selectively ignore IP addresses and subnets that do not match
the filtered IP version setting.
*/
package main
import (
"bufio"
"io"
"net/netip"
"os"
"strings"
)
// ---------------------------------------------------------------------------
// Service-label stripping
// ---------------------------------------------------------------------------
var serviceLabels = map[string]struct{}{
"www": {}, "www2": {}, "www3": {}, "web": {},
"cdn": {}, "static": {}, "assets": {}, "media": {}, "img": {}, "images": {},
"api": {}, "app": {}, "apps": {},
"mail": {}, "smtp": {}, "pop": {}, "pop3": {}, "imap": {}, "mx": {},
"ftp": {}, "sftp": {},
"vpn": {}, "proxy": {}, "gate": {},
"ns": {}, "ns1": {}, "ns2": {}, "ns3": {}, "ns4": {},
"download": {}, "downloads": {}, "update": {}, "updates": {},
"secure": {}, "ssl": {},
"dev": {}, "staging": {}, "stage": {}, "test": {}, "beta": {}, "alpha": {},
"blog": {}, "shop": {}, "store": {}, "portal": {},
}
func stripServiceLabel(d string) (string, bool) {
idx := strings.IndexByte(d, '.')
if idx < 0 {
return "", false
}
label := d[:idx]
rest := d[idx+1:]
if rest == "" {
return "", false
}
if isPublicSuffix(rest) {
return "", false
}
if len(label) == 1 {
return rest, true
}
if _, ok := serviceLabels[label]; ok {
return rest, true
}
return "", false
}
// ---------------------------------------------------------------------------
// Source format helpers
// ---------------------------------------------------------------------------
// isIPOrCIDR safely tests whether a string parses successfully as an IP or Subnet.
func isIPOrCIDR(s string) bool {
if _, err := netip.ParseAddr(s); err == nil {
return true
}
if _, err := netip.ParsePrefix(s); err == nil {
return true
}
return false
}
func parseCategoryLine(line string) []string {
line = strings.TrimSpace(line)
if line == "" || line[0] == '#' || line[0] == '!' {
return nil
}
pseudoHost := func(s string) bool {
switch s {
case "localhost", "local", "broadcasthost",
"ip6-localhost", "ip6-loopback", "ip6-localnet",
"ip6-mcastprefix", "ip6-allnodes", "ip6-allrouters",
"ip6-allhosts", "0.0.0.0", "::", "127.0.0.1", "::1":
return true
}
return false
}
switch {
case strings.HasPrefix(line, "@@||"):
return nil
case strings.HasPrefix(line, "||"):
d := line[2:]
if i := strings.IndexByte(d, '^'); i >= 0 {
d = d[:i]
}
d = strings.TrimPrefix(d, "*.")
if strings.ContainsAny(d, "/*?") {
return nil
}
d = strings.ToLower(strings.TrimSuffix(d, "."))
if d == "" {
return nil
}
// [PERF] Filter entry based on global SupportIPVersion setting to save memory.
if addr, err := netip.ParseAddr(d); err == nil {
if ipVersionSupport != "both" {
if ipVersionSupport == "ipv4" && !addr.Is4() { return nil }
if ipVersionSupport == "ipv6" && !addr.Is6() { return nil }
}
return nil // Purposely ignore IP rules in Adblock lists
}
if prefix, err := netip.ParsePrefix(d); err == nil {
if ipVersionSupport != "both" {
if ipVersionSupport == "ipv4" && !prefix.Addr().Is4() { return nil }
if ipVersionSupport == "ipv6" && !prefix.Addr().Is6() { return nil }
}
return nil // Purposely ignore CIDR rules in Adblock lists
}
return []string{d}
case len(line) > 0 && (line[0] >= '0' && line[0] <= '9' || line[0] == ':'):
cleanLine := line
if i := strings.IndexByte(cleanLine, '#'); i >= 0 {
cleanLine = strings.TrimSpace(cleanLine[:i])
}
if cleanLine == "" {
return nil
}
fields := strings.Fields(cleanLine)
if len(fields) == 0 {
return nil
}
if len(fields) == 1 {
d := strings.ToLower(strings.TrimSuffix(fields[0], "."))
if d == "" {
return nil
}
// Check version support
if addr, err := netip.ParseAddr(d); err == nil {
if ipVersionSupport != "both" {
if ipVersionSupport == "ipv4" && !addr.Is4() { return nil }
if ipVersionSupport == "ipv6" && !addr.Is6() { return nil }
}
}
if prefix, err := netip.ParsePrefix(d); err == nil {
if ipVersionSupport != "both" {
if ipVersionSupport == "ipv4" && !prefix.Addr().Is4() { return nil }
if ipVersionSupport == "ipv6" && !prefix.Addr().Is6() { return nil }
}
}
return []string{d}
}
if addr, err := netip.ParseAddr(fields[0]); err == nil {
if ipVersionSupport != "both" {
if ipVersionSupport == "ipv4" && !addr.Is4() { return nil }
if ipVersionSupport == "ipv6" && !addr.Is6() { return nil }
}
var out []string
for _, f := range fields[1:] {
f = strings.ToLower(strings.TrimSuffix(f, "."))
if f == "" || pseudoHost(f) {
continue
}
if isIPOrCIDR(f) {
continue
}
out = append(out, f)
}
return out
}
var out []string
for _, f := range fields {
f = strings.ToLower(strings.TrimSuffix(f, "."))
if f == "" || pseudoHost(f) {
continue
}
out = append(out, f)
}
return out
default:
if i := strings.IndexByte(line, '#'); i >= 0 {
line = strings.TrimSpace(line[:i])
}
if line == "" || strings.ContainsAny(line, "[]") {
return nil
}
d := strings.ToLower(strings.TrimSuffix(line, "."))
if d == "" {
return nil
}
return []string{d}
}
}
func isSourceURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}
func parseSourceBody(r io.Reader) ([]string, int) {
var lines []string
eTLDs := 0
sc := bufio.NewScanner(r)
for sc.Scan() {
domains := parseCategoryLine(sc.Text())
for _, d := range domains {
if !isIPOrCIDR(d) && isPublicSuffix(d) {
eTLDs++
}
lines = append(lines, d)
}
}
return lines, eTLDs
}
func parseLocalFile(path string) ([]string, int) {
f, err := os.Open(path)
if err != nil {
return nil, 0
}
defer f.Close()
return parseSourceBody(f)
}