-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinit_policy.go
More file actions
382 lines (342 loc) · 10.6 KB
/
init_policy.go
File metadata and controls
382 lines (342 loc) · 10.6 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
File: init_policy.go
Version: 1.0.6
Updated: 11-May-2026 14:31 CEST
Description:
Parses and maps RType and Domain policies for sdproxy.
Handles remote list fetching, local file parsing, and label boundary
calculations for optimized suffix walking.
Changes:
1.0.6 - [REFACTOR] Inherited `ParsePrefixUnmapped` from `process_helpers.go`.
Enforces uniform CIDR extraction boundaries cleanly natively.
1.0.5 - [SECURITY/FIX] Implemented strict `Is4In6()` unmapping heuristics across
Domain Policy CIDR boundaries natively. Eradicates evasion strategies
exploiting hybrid IPv6 payloads.
*/
package main
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"io"
"log"
"net/http"
"net/netip"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/miekg/dns"
)
var (
policyHTTPMeta = make(map[string]catListHeaders)
policyFileMeta = make(map[string]int64)
policyMu sync.Mutex
)
// loadPolicyMeta retrieves the metadata for domain policies from the cache directory.
func loadPolicyMeta() map[string]catListHeaders {
meta := make(map[string]catListHeaders)
if cfg.Server.PolicyCacheDir == "" {
return meta
}
b, err := os.ReadFile(filepath.Join(cfg.Server.PolicyCacheDir, "policy-meta.json"))
if err == nil {
json.Unmarshal(b, &meta)
}
return meta
}
// savePolicyMeta persists the metadata for domain policies to the cache directory.
func savePolicyMeta(meta map[string]catListHeaders) {
if cfg.Server.PolicyCacheDir == "" {
return
}
os.MkdirAll(cfg.Server.PolicyCacheDir, 0755)
b, err := json.Marshal(meta)
if err == nil {
path := filepath.Join(cfg.Server.PolicyCacheDir, "policy-meta.json")
tmp := path + ".tmp"
if os.WriteFile(tmp, b, 0644) == nil {
os.Rename(tmp, path)
}
}
}
// pollDomainPolicies rebuilds the domain policy map from all configured sources.
func pollDomainPolicies(force bool) {
policyMu.Lock()
defer policyMu.Unlock()
changed := false
activeFiles := make(map[string]bool)
activeURLs := make(map[string]bool)
if force {
policyHTTPMeta = make(map[string]catListHeaders)
} else if len(policyHTTPMeta) == 0 && cfg.Server.PolicyCacheDir != "" {
policyHTTPMeta = loadPolicyMeta()
}
for filePath := range cfg.DomainPolicyFiles {
activeFiles[filePath] = true
info, err := os.Stat(filePath)
if err != nil {
if _, exists := policyFileMeta[filePath]; exists {
delete(policyFileMeta, filePath)
changed = true
if logRouting {
log.Printf("[POLICY] File %s was removed. Flagging policy maps for rebuild.", filePath)
}
}
continue
}
mtime := info.ModTime().UnixNano()
if lastMtime, ok := policyFileMeta[filePath]; !ok || lastMtime != mtime {
policyFileMeta[filePath] = mtime
changed = true
}
}
urlData := make(map[string][]string)
fetchFailed := make(map[string]bool)
for urlStr := range cfg.DomainPolicyURLs {
activeURLs[urlStr] = true
req, err := http.NewRequest(http.MethodGet, urlStr, nil)
if err != nil {
fetchFailed[urlStr] = true
continue
}
req.Header.Set("User-Agent", cfg.Server.UserAgent)
var cachePath string
hasCacheFile := false
if cfg.Server.PolicyCacheDir != "" {
os.MkdirAll(cfg.Server.PolicyCacheDir, 0755)
h := sha256.Sum256([]byte(urlStr))
cachePath = filepath.Join(cfg.Server.PolicyCacheDir, "policy-"+hex.EncodeToString(h[:8])+".raw")
if _, errStat := os.Stat(cachePath); errStat == nil {
hasCacheFile = true
}
}
if !force {
if m, ok := policyHTTPMeta[urlStr]; ok && hasCacheFile {
if m.LastModified != "" {
req.Header.Set("If-Modified-Since", m.LastModified)
}
if m.ETag != "" {
req.Header.Set("If-None-Match", m.ETag)
}
}
}
resp, err := catHTTPClient.Do(req)
if err != nil {
fetchFailed[urlStr] = true
if logRouting {
if !hasCacheFile {
log.Printf("[POLICY] Fetch failed for %s: %v", urlStr, err)
} else {
log.Printf("[POLICY] Fetch failed for %s: %v — falling back to local cache", urlStr, err)
}
}
continue
}
if resp.StatusCode == http.StatusNotModified {
resp.Body.Close()
if hasCacheFile && logRouting {
log.Printf("[POLICY] Not modified (304) for %s — queued local cache", urlStr)
}
continue
} else if resp.StatusCode == http.StatusOK {
policyHTTPMeta[urlStr] = catListHeaders{
LastModified: resp.Header.Get("Last-Modified"),
ETag: resp.Header.Get("ETag"),
}
var lines []string
lr := io.LimitReader(resp.Body, 10*1024*1024)
scanner := bufio.NewScanner(lr)
buf := make([]byte, 64*1024)
scanner.Buffer(buf, 2*1024*1024)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if idx := strings.IndexByte(line, '#'); idx >= 0 {
line = strings.TrimSpace(line[:idx])
}
if line != "" {
lines = append(lines, line)
}
}
resp.Body.Close()
urlData[urlStr] = lines
if cachePath != "" {
tmp := cachePath + ".tmp"
f, err := os.Create(tmp)
if err == nil {
w := bufio.NewWriter(f)
for _, l := range lines {
w.WriteString(l)
w.WriteByte('\n')
}
w.Flush()
f.Close()
os.Rename(tmp, cachePath)
}
if logRouting {
log.Printf("[POLICY] Fetched %d entries from %s — saved to cache", len(lines), urlStr)
}
}
changed = true
} else {
fetchFailed[urlStr] = true
resp.Body.Close()
if hasCacheFile && logRouting {
log.Printf("[POLICY] HTTP %d for %s — falling back to local cache", resp.StatusCode, urlStr)
}
}
}
for f := range policyFileMeta {
if !activeFiles[f] {
delete(policyFileMeta, f)
changed = true
}
}
for u := range policyHTTPMeta {
if !activeURLs[u] {
delete(policyHTTPMeta, u)
changed = true
}
}
if domainPolicySnap.Load() == nil || changed {
savePolicyMeta(policyHTTPMeta)
newMap := make(map[string]int)
var newCIDRs []dpCidrEntry
processDomainPolicy := func(rawKey string, actionName string) int {
discarded := 0
for _, domainStr := range strings.Split(rawKey, ",") {
domainStr = strings.TrimSpace(domainStr)
if domainStr == "" {
continue
}
actionStr := strings.ToUpper(actionName)
var rcode int
if actionStr == "DROP" { rcode = PolicyActionDrop } else if actionStr == "BLOCK" { rcode = PolicyActionBlock } else {
rc, ok := dns.StringToRcode[actionStr]
if !ok { continue }
rcode = rc
}
if !cfg.Server.FilterIPs {
if _, err := netip.ParseAddr(domainStr); err == nil { discarded++; continue }
if _, err := netip.ParsePrefix(domainStr); err == nil { discarded++; continue }
} else {
if addr, err := netip.ParseAddr(domainStr); err == nil {
var prefix netip.Prefix
addr = addr.Unmap()
if addr.Is4() { prefix = netip.PrefixFrom(addr, 32) } else { prefix = netip.PrefixFrom(addr, 128) }
newCIDRs = append(newCIDRs, dpCidrEntry{prefix: prefix, action: rcode})
continue
}
if prefix, err := ParsePrefixUnmapped(domainStr); err == nil {
newCIDRs = append(newCIDRs, dpCidrEntry{prefix: prefix, action: rcode})
continue
}
}
clean := strings.ToLower(strings.TrimSuffix(domainStr, "."))
if clean != "" { newMap[clean] = rcode }
}
return discarded
}
for rawKey, actionName := range cfg.DomainPolicy { processDomainPolicy(rawKey, actionName) }
for filePath, actionName := range cfg.DomainPolicyFiles {
lines, err := readConfigListFile(filePath)
if err == nil {
for _, line := range lines { processDomainPolicy(line, actionName) }
}
}
for urlStr, actionName := range cfg.DomainPolicyURLs {
var lines []string
if data, ok := urlData[urlStr]; ok {
lines = data
} else if cfg.Server.PolicyCacheDir != "" {
h := sha256.Sum256([]byte(urlStr))
cachePath := filepath.Join(cfg.Server.PolicyCacheDir, "policy-"+hex.EncodeToString(h[:8])+".raw")
lines, _ = readConfigListFile(cachePath)
}
if len(lines) > 0 {
for _, line := range lines { processDomainPolicy(line, actionName) }
}
}
if len(newCIDRs) > 0 {
sort.SliceStable(newCIDRs, func(i, j int) bool {
return newCIDRs[i].prefix.Bits() > newCIDRs[j].prefix.Bits()
})
}
domainPolicySnap.Store(&newMap)
domainPolicyCIDRSnap.Store(&newCIDRs)
hasDomainPolicy.Store(len(newMap) > 0 || len(newCIDRs) > 0)
computeDomainLabelBounds(&newMap)
if logRouting {
log.Printf("[POLICY] Rebuilt domain policy map. Loaded %d rule(s) globally.", len(newMap)+len(newCIDRs))
}
}
}
// initPolicies prepares the fast-path blocking structures for QTypes and Domains.
func initPolicies() {
rtypePolicy = make(map[uint16]int, len(cfg.RtypePolicy))
processRtype := func(rawKey string, actionName string) {
for _, typeName := range strings.Split(rawKey, ",") {
typeName = strings.TrimSpace(typeName)
if typeName == "" { continue }
qtype, ok := dns.StringToType[strings.ToUpper(typeName)]
if !ok { continue }
actionStr := strings.ToUpper(actionName)
if actionStr == "DROP" { rtypePolicy[qtype] = PolicyActionDrop; continue }
if actionStr == "BLOCK" { rtypePolicy[qtype] = PolicyActionBlock; continue }
if rcode, ok := dns.StringToRcode[actionStr]; ok { rtypePolicy[qtype] = rcode }
}
}
for rawKey, actionName := range cfg.RtypePolicy { processRtype(rawKey, actionName) }
for filePath, actionName := range cfg.RtypePolicyFiles {
lines, err := readConfigListFile(filePath)
if err == nil {
for _, line := range lines { processRtype(line, actionName) }
}
}
if cfg.Server.FastStart {
go pollDomainPolicies(forceRefreshStartup)
} else {
pollDomainPolicies(forceRefreshStartup)
}
pollStr := cfg.Server.PolicyPollInterval
if pollStr == "" { pollStr = "6h" }
if interval, err := time.ParseDuration(pollStr); err == nil && interval > 0 {
go func() {
t := time.NewTicker(interval)
for range t.C { pollDomainPolicies(false) }
}()
}
if cfg.Server.BlockObsoleteQtypes {
for qtype := range obsoleteQtypes {
if _, userSet := rtypePolicy[qtype]; !userSet { rtypePolicy[qtype] = dns.RcodeNotImplemented }
}
blockUnknownQtypes = true
}
hasRtypePolicy = len(rtypePolicy) > 0
}
func computeDomainLabelBounds(dpMap *map[string]int) {
countLabels := func(k string) int { return strings.Count(k, ".") + 1 }
if len(*dpMap) > 0 {
minP, maxP := 128, 1
for k := range *dpMap {
n := countLabels(k)
if n < minP { minP = n }
if n > maxP { maxP = n }
}
domainPolicyMinLabels.Store(int32(minP))
domainPolicyMaxLabels.Store(int32(maxP))
}
if len(domainRoutes) > 0 {
minR, maxR := 128, 1
for k := range domainRoutes {
n := countLabels(k)
if n < minR { minR = n }
if n > maxR { maxR = n }
}
domainRoutesMinLabels.Store(int32(minR))
domainRoutesMaxLabels.Store(int32(maxR))
}
}