Repository navigation
Expand file tree
/
Copy pathprocess.go
More file actions
634 lines (567 loc) · 22.7 KB
/
Copy pathprocess.go
File metadata and controls
634 lines (567 loc) · 22.7 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
File: process.go
Version: 3.132.0
Last Updated: 15-Nov-2026 16:05 CET
Description:
Master orchestrator for the sdproxy per-query DNS resolution pipeline.
Changes:
3.132.0 - [FIX] Re-evaluated `handleSpoofedRecords` (`groupRRs` / `groupRRRegexes`)
when `determineRouting` updates `clientName` and causes
`ResolveStateKeyAndGroup` to resolve a new `clientGroup`
(`clientGroup != prevClientGroup`).
3.131.0 - [FIX] Applied `clientGroup` upstream and `bypass_local` fallback onto
`routeCtx` when `determineRouting` updates `clientName` and causes
`ResolveStateKeyAndGroup` to resolve a new `clientGroup` while no
explicit route or domain upstream was selected.
3.130.0 - [SECURITY/FIX] Stopped copying unforced primary `clientRoute` fields
(`Upstream`, `HasRcode`/`Rcode`, `BypassLocal`, `BypassGlobal`) onto a
forced `cr2` (`cr2.Force == true`) when `clientRoute.Force == false`
so a forced `client-name` route that assigns a parental `group:` can
properly inherit that group's `upstream` instead of being shadowed by
an unforced MAC/IP route's upstream; and re-evaluated `sk` and
`clientGroup` if `determineRouting` changes `clientName`.
3.129.0 - [FEAT] Wired `qc.enforceRFCComplianceCachedOnly()` at step 3.7 and
`qc.enforceRFCComplianceCheck()` at step 7.7 to enforce RFC 2181 NS
CNAME indirection checks across cached and uncached queries.
3.128.0 - [SECURITY/FIX] Re-evaluated `clientGroup` upstream/bypass_local
fallback in `ProcessDNS` when a `cr2` route overrides `client_name`
a second time (`cr2.ClientName != clientName`), and synced
`clientName`, `clientNameLower`, and `clientID` with
`routeCtx.clientName` after `determineRouting`.
3.127.0 - [FIX] Synced `clientName`, `clientNameLower`, and `clientID` with
`routeCtx.clientName` after `determineRouting` when a forced
`client-name` route overrides `client_name`.
3.126.0 - [SECURITY/FIX] Permitted a forced `client-name` route (`cr2.Force == true`)
to override an initial route even when the initial route also had
`force: true` and set a `client_name` override.
3.125.0 - [SECURITY/FIX] Preserved non-empty `Upstream`, `HasRcode`/`Rcode`,
`BypassLocal`, and `BypassGlobal` fields from the primary route when
a `client_name` override triggers a secondary `cr2` match with
`cr2.Force == true` that leaves those fields unset.
3.124.0 - [SECURITY/FIX] Re-evaluated `clientGroup` upstream/bypass_local
fallback in `ProcessDNS` unconditionally after a `client_name`
override re-runs `ResolveStateKeyAndGroup`.
3.123.0 - [SECURITY/FIX] Re-evaluated `resolveClientRoute` unconditionally when
a matched client route overrides `client_name`.
*/
package main
import (
"fmt"
"log"
"net"
"net/netip"
"strconv"
"strings"
"github.com/miekg/dns"
)
func recordBlockMsgStats(resp *dns.Msg, qNameTrimmed string) {
if resp == nil {
return
}
isNull := false
for _, rr := range resp.Answer {
switch a := rr.(type) {
case *dns.A:
if a.A.IsUnspecified() {
isNull = true
}
case *dns.AAAA:
if a.AAAA.IsUnspecified() {
isNull = true
}
}
}
IncrReturnCode(resp.Rcode, isNull)
if resp.Rcode == dns.RcodeNameError && qNameTrimmed != "" {
IncrNXDomain(qNameTrimmed)
}
}
func ProcessDNS(w dns.ResponseWriter, r *dns.Msg, clientIP, protocol, sni, path string) {
var clientAddr netip.Addr
if clientIP != "" {
if a, err := netip.ParseAddr(clientIP); err == nil {
clientAddr = a.Unmap().WithZone("")
if clientAddr != a {
clientIP = clientAddr.String()
}
if clientAddr.IsValid() && (clientAddr.IsMulticast() || clientAddr.IsUnspecified() || (clientAddr.Is4() && clientAddr.As4() == [4]byte{255, 255, 255, 255})) {
IncrDroppedRateLimit()
return
}
}
}
defer func() {
if rec := recover(); rec != nil {
log.Printf("[PANIC] Recovered in ProcessDNS (%s / %s): %v", protocol, clientIP, rec)
func() {
defer func() {
if r2 := recover(); r2 != nil {
log.Printf("[PANIC] Secondary panic recovered during error handling: %v", r2)
}
}()
PenalizeClient(clientIP, clientAddr, -1)
if r != nil {
IncrReturnCode(dns.RcodeServerFailure, false)
dns.HandleFailed(w, r)
}
}()
}
}()
// ── 0. Sanity + throttle ──────────────────────────────────────────────
if r == nil || len(r.Question) != 1 {
if r != nil {
dns.HandleFailed(w, r)
}
return
}
if !AcquireQuery() {
if protocol != "UDP" {
dns.HandleFailed(w, r)
}
return
}
defer ReleaseQuery()
q := r.Question[0]
qNameTrimmed := lowerTrimDot(q.Name)
originalQName := q.Name
originalQNameTrimmed := qNameTrimmed
originalID := r.Id
doBit := false
var ecsAddr netip.Addr
var ecsPrefix netip.Prefix
var ednsMAC string
trustedForwarder := isTrustedProxy(clientAddr)
if !cfg.Server.AllowCDBit && !trustedForwarder {
r.CheckingDisabled = false
}
if opt := r.IsEdns0(); opt != nil {
doBit = opt.Do()
if trustedForwarder {
for _, o := range opt.Option {
switch v := o.(type) {
case *dns.EDNS0_SUBNET:
if !ecsAddr.IsValid() && v.Address != nil {
if addr, ok := netip.AddrFromSlice(v.Address); ok {
ecsAddr = addr.Unmap()
bits := int(v.SourceNetmask)
maxBits := 128
if ecsAddr.Is4() {
maxBits = 32
}
if bits >= 0 && bits <= maxBits {
if p, err := ecsAddr.Prefix(bits); err == nil {
ecsPrefix = p.Masked()
}
}
}
}
case *dns.EDNS0_LOCAL:
if v.Code == edns0OptMAC && ednsMAC == "" {
ednsMAC = parseEDNS0MAC(v.Data)
}
}
}
}
}
clientMAC := LookupMAC(clientIP)
lookupMAC := clientMAC
if lookupMAC == "" {
lookupMAC = ednsMAC
}
clientName := LookupNameByMACOrIP(lookupMAC, clientIP)
var localPort string
if w.LocalAddr() != nil {
if _, p, err := net.SplitHostPort(w.LocalAddr().String()); err == nil {
localPort = p
} else {
if p, err := strconv.ParseUint(w.LocalAddr().String(), 10, 16); err == nil {
localPort = strconv.FormatUint(p, 10)
}
}
}
protoEndpoint := formatProtoEndpoint(protocol, localPort, path)
clientNameLower := ""
if clientName != "" {
clientNameLower = strings.ToLower(clientName)
}
sniLower := ""
if sni != "" {
sniLower = strings.ToLower(sni)
}
pathLower := ""
if path != "" {
pathLower = strings.ToLower(path)
}
clientID := appendClientEDNS(buildClientID(clientIP, clientName, clientAddr), ecsAddr, ecsPrefix, ednsMAC)
// ── 1. Security & Admission ───────────────────────────────────────────
if enforceSecurityGuards(w, r, q, originalQNameTrimmed, clientIP, clientAddr, clientMAC, ednsMAC, clientName, clientID, protocol, protoEndpoint) {
return
}
// ── 1.5 DDR interception ──────────────────────────────────────────────
if handleDDR(w, r, q, originalQNameTrimmed, clientIP, clientID, protocol, protoEndpoint) {
return
}
sk, clientGroup := ResolveStateKeyAndGroup(clientMAC, ednsMAC, clientIP, clientAddr, ecsAddr, ecsPrefix, clientName, clientNameLower, sni, sniLower, path, pathLower, localPort)
clientRoute, clientRouteMatched, routeOriginType := resolveClientRoute(clientMAC, ednsMAC, clientIP, clientAddr, ecsAddr, ecsPrefix, clientNameLower, sniLower, pathLower, localPort)
bypassGlobal := clientRouteMatched && clientRoute.BypassGlobal
if clientRouteMatched && clientRoute.ClientName != "" && clientRoute.ClientName != clientName {
clientName = clientRoute.ClientName
clientNameLower = strings.ToLower(clientName)
clientID = appendClientEDNS(buildClientID(clientIP, clientName, clientAddr), ecsAddr, ecsPrefix, ednsMAC)
sk, clientGroup = ResolveStateKeyAndGroup(clientMAC, ednsMAC, clientIP, clientAddr, ecsAddr, ecsPrefix, clientName, clientNameLower, sni, sniLower, path, pathLower, localPort)
if cr2, ok2, orig2 := resolveClientRoute(clientMAC, ednsMAC, clientIP, clientAddr, ecsAddr, ecsPrefix, clientNameLower, sniLower, pathLower, localPort); ok2 {
if cr2.Force && (!clientRoute.Force || orig2 == "CLIENT-NAME") {
if cr2.ClientName == "" {
cr2.ClientName = clientName
} else if cr2.ClientName != clientName {
clientName = cr2.ClientName
clientNameLower = strings.ToLower(clientName)
clientID = appendClientEDNS(buildClientID(clientIP, clientName, clientAddr), ecsAddr, ecsPrefix, ednsMAC)
sk, clientGroup = ResolveStateKeyAndGroup(clientMAC, ednsMAC, clientIP, clientAddr, ecsAddr, ecsPrefix, clientName, clientNameLower, sni, sniLower, path, pathLower, localPort)
}
if clientRoute.Force {
if cr2.Upstream == "" && clientRoute.Upstream != "" && !cr2.HasRcode {
cr2.Upstream = clientRoute.Upstream
}
if !cr2.HasRcode && clientRoute.HasRcode {
cr2.HasRcode = true
cr2.Rcode = clientRoute.Rcode
}
if !cr2.BypassLocal && clientRoute.BypassLocal {
cr2.BypassLocal = true
}
if !cr2.BypassGlobal && clientRoute.BypassGlobal {
cr2.BypassGlobal = true
}
}
clientRoute, clientRouteMatched, routeOriginType = cr2, ok2, orig2
} else {
if clientRoute.Upstream == "" && cr2.Upstream != "" && !clientRoute.HasRcode {
clientRoute.Upstream = cr2.Upstream
}
if !clientRoute.HasRcode && cr2.HasRcode {
clientRoute.HasRcode = true
clientRoute.Rcode = cr2.Rcode
}
if !clientRoute.BypassLocal && cr2.BypassLocal {
clientRoute.BypassLocal = true
}
if !clientRoute.BypassGlobal && cr2.BypassGlobal {
clientRoute.BypassGlobal = true
}
}
bypassGlobal = clientRoute.BypassGlobal
}
}
if clientRouteMatched && clientRoute.Upstream == "" && clientGroup != "" && !clientRoute.HasRcode {
if grp, ok := cfg.Groups[clientGroup]; ok {
if grp.Upstream != "" {
clientRoute.Upstream = grp.Upstream
}
if !clientRoute.BypassLocal && grp.BypassLocal {
clientRoute.BypassLocal = true
}
}
}
var spoofedAlias string
// ── 1.6 Spoofed Records (RRs) ─────────────────────────────────────────
if hasRRs {
var intercepted bool
intercepted, spoofedAlias = handleSpoofedRecords(&w, r, &q, &qNameTrimmed, clientGroup, clientIP, clientID, protocol, protoEndpoint, bypassGlobal)
if intercepted {
return
}
}
// ── 1.8 Custom Rules Engine ───────────────────────────────────────────
ruleAction, ruleMatch := CheckRules(originalQNameTrimmed, clientGroup)
if ruleAction == "" && qNameTrimmed != originalQNameTrimmed {
ruleAction, ruleMatch = CheckRules(qNameTrimmed, clientGroup)
}
if ruleAction == "BLOCK" {
IncrPolicyBlock()
reason := "Custom Rule (" + ruleMatch + ")"
if clientGroup != "" && clientGroup != "global" {
reason += " [" + clientGroup + "]"
}
RecordBlockEvent(clientIP, originalQNameTrimmed, reason)
if globalBlockAction == BlockActionDrop {
if logQueries {
if logSimple {
logQuerySimple(protoEndpoint, clientIP, originalQName, qtypeName(q.Qtype), "DROP", reason, false)
} else {
log.Printf("[DNS] [%s] %s -> %s %s | CUSTOM RULE DROP | DROP", protocol, clientID, originalQName, qtypeName(q.Qtype))
}
}
return
} else if globalBlockAction == BlockActionLog {
if logQueries && !logSimple {
log.Printf("[DNS] [%s] %s -> %s %s | CUSTOM RULE BLOCK (LOG ONLY) | %s", protocol, clientID, originalQName, qtypeName(q.Qtype), getBlockActionLogStr(q.Qtype))
}
} else {
resp := generateBlockMsg(r, syntheticTTL)
recordBlockMsgStats(resp, originalQNameTrimmed)
enforceUDPDefenses(r, resp, protocol, netip.Addr{}, nil)
w.WriteMsg(resp)
msgPool.Put(resp)
if logQueries {
if logSimple {
logQuerySimple(protoEndpoint, clientIP, originalQName, qtypeName(q.Qtype), getBlockActionLogStr(q.Qtype), reason, false)
} else {
log.Printf("[DNS] [%s] %s -> %s %s | CUSTOM RULE BLOCK | %s", protocol, clientID, originalQName, qtypeName(q.Qtype))
}
}
return
}
}
bypassPolicies := (ruleAction == "ALLOW")
if bypassPolicies && logQueries && !logSimple {
log.Printf("[DNS] [%s] %s -> %s %s | CUSTOM RULE ALLOW | Bypassing policies", protocol, clientID, originalQName, qtypeName(q.Qtype))
}
// ── 2. Routing Engine ─────────────────────────────────────────────────
routeCtx, intercepted := determineRouting(w, r, q, qNameTrimmed, originalQName, originalQNameTrimmed, clientIP, clientAddr, ecsAddr, ecsPrefix, ednsMAC, clientName, clientID, protocol, protoEndpoint, bypassPolicies, bypassGlobal, clientRoute, clientRouteMatched, routeOriginType)
if intercepted {
return
}
if routeCtx.clientName != "" && routeCtx.clientName != clientName {
prevClientGroup := clientGroup
clientName = routeCtx.clientName
clientNameLower = strings.ToLower(clientName)
clientID = appendClientEDNS(buildClientID(clientIP, clientName, clientAddr), ecsAddr, ecsPrefix, ednsMAC)
sk, clientGroup = ResolveStateKeyAndGroup(clientMAC, ednsMAC, clientIP, clientAddr, ecsAddr, ecsPrefix, clientName, clientNameLower, sni, sniLower, path, pathLower, localPort)
if clientGroup != "" && routeCtx.routeOriginType != "DOMAIN" && clientRoute.Upstream == "" {
if grp, ok := cfg.Groups[clientGroup]; ok {
if grp.Upstream != "" {
routeCtx.routeName = grp.Upstream
routeCtx.routeIdx = getRouteIdx(grp.Upstream)
}
if !routeCtx.bypassLocal && grp.BypassLocal {
routeCtx.bypassLocal = true
}
}
}
if hasRRs && clientGroup != "" && clientGroup != prevClientGroup {
var rrIntercepted bool
var newAlias string
rrIntercepted, newAlias = handleSpoofedRecords(&w, r, &q, &qNameTrimmed, clientGroup, clientIP, clientID, protocol, protoEndpoint, bypassGlobal)
if rrIntercepted {
return
}
if newAlias != "" {
spoofedAlias = newAlias
}
}
}
// ── 3. Parental Controls ──────────────────────────────────────────────
var parentalForcedTTL uint32
var parentalReason string
var parentalCategory string
var parentalMatchedApex string
if hasParental && (clientMAC != "" || ednsMAC != "" || clientIP != "" || ecsAddr.IsValid() || clientName != "" || sni != "" || path != "" || localPort != "") {
checkTarget := originalQNameTrimmed
isPTR := false
var targetAddr netip.Addr
if q.Qtype == dns.TypePTR {
if extractedIP := extractIPFromPTR(originalQNameTrimmed); extractedIP != "" {
if a, err := netip.ParseAddr(extractedIP); err == nil {
targetAddr = a.Unmap()
checkTarget = ""
} else {
checkTarget = extractedIP
}
isPTR = true
}
}
blocked, blockTTL, forcedTTL, reason, cat, matchedApex := CheckParental(sk, clientGroup, clientMAC, ednsMAC, clientIP, clientAddr, ecsAddr, clientName, clientNameLower, sni, sniLower, path, pathLower, localPort, checkTarget, targetAddr, false, bypassPolicies)
if !blocked && qNameTrimmed != originalQNameTrimmed {
b2, btl2, ftl2, r2, c2, ma2 := CheckParental(sk, clientGroup, clientMAC, ednsMAC, clientIP, clientAddr, ecsAddr, clientName, clientNameLower, sni, sniLower, path, pathLower, localPort, qNameTrimmed, netip.Addr{}, true, bypassPolicies)
if b2 {
blocked, blockTTL, forcedTTL, reason, cat, matchedApex = b2, btl2, ftl2, r2, c2, ma2
isPTR = false
} else if c2 != "" && cat == "" {
forcedTTL, reason, cat, matchedApex = ftl2, r2, c2, ma2
}
}
if blocked {
IncrParentalBlock()
blockReason := reason
if isPTR {
ptrTarget := checkTarget
if targetAddr.IsValid() {
ptrTarget = targetAddr.String()
}
blockReason += " (PTR IP: " + ptrTarget + ")"
}
RecordBlockEvent(clientIP, originalQNameTrimmed, blockReason)
if globalBlockAction == BlockActionDrop {
if logQueries {
if logSimple {
logQuerySimple(protoEndpoint, clientIP, originalQName, qtypeName(q.Qtype), "DROP", "Parental ("+blockReason+")", false)
} else {
log.Printf("[DNS] [%s] %s -> %s %s | PARENTAL DROP | DROP", protocol, clientID, originalQName, qtypeName(q.Qtype))
}
}
return
} else if globalBlockAction == BlockActionLog {
if logQueries && !logSimple {
catStr := ""
if cat != "" {
catStr = fmt.Sprintf(" (Category: %s, apex: %s)", cat, matchedApex)
}
log.Printf("[DNS] [%s] %s -> %s %s | PARENTAL BLOCK (LOG ONLY)%s | %s",
protocol, clientID, originalQName, qtypeName(q.Qtype), catStr, getBlockActionLogStr(q.Qtype))
}
} else {
resp := generateBlockMsg(r, blockTTL)
recordBlockMsgStats(resp, originalQNameTrimmed)
enforceUDPDefenses(r, resp, protocol, netip.Addr{}, nil)
w.WriteMsg(resp)
if logQueries {
rcodeStr := getBlockActionLogStr(q.Qtype)
if logSimple {
logQuerySimple(protoEndpoint, clientIP, originalQName, qtypeName(q.Qtype), rcodeStr, "Parental ("+blockReason+")", false)
} else {
catStr := ""
if cat != "" {
catStr = fmt.Sprintf(" (Category: %s, apex: %s)", cat, matchedApex)
}
ptrLogStr := ""
if isPTR {
ptrTarget := checkTarget
if targetAddr.IsValid() {
ptrTarget = targetAddr.String()
}
ptrLogStr = " [PTR Target IP: " + ptrTarget + "]"
}
log.Printf("[DNS] [%s] %s -> %s %s%s | PARENTAL BLOCK%s | %s",
protocol, clientID,
originalQName, qtypeName(q.Qtype), ptrLogStr, catStr, rcodeStr)
}
}
msgPool.Put(resp)
return
}
}
parentalForcedTTL = forcedTTL
parentalReason = reason
parentalCategory = cat
parentalMatchedApex = matchedApex
} else {
IncrGroup("default")
}
// ── 3.5 Assemble the query context ────────────────────────────────────
qc := &queryCtx{
w: w, r: r, q: q,
qNameTrimmed: qNameTrimmed,
originalQName: originalQName,
originalQNameTrimmed: originalQNameTrimmed,
originalID: originalID,
doBit: doBit,
clientIP: clientIP,
clientAddr: clientAddr,
ecsAddr: ecsAddr,
ecsPrefix: ecsPrefix,
clientMAC: clientMAC,
ednsMAC: ednsMAC,
clientName: clientName,
clientNameLower: clientNameLower,
clientID: clientID,
protocol: protocol,
protoEndpoint: protoEndpoint,
sni: sni,
sniLower: sniLower,
path: path,
pathLower: pathLower,
localPort: localPort,
sk: sk,
clientGroup: clientGroup,
bypassGlobal: bypassGlobal,
bypassPolicies: bypassPolicies,
spoofedAlias: spoofedAlias,
route: routeCtx,
parentalForcedTTL: parentalForcedTTL,
parentalReason: parentalReason,
parentalCategory: parentalCategory,
parentalMatchedApex: parentalMatchedApex,
}
qc.deriveCacheKey()
// ── 3.6 Immediate Cached RFC 10023 "_for-sale." Parent/Apex Check ─────
if qc.enforceForSaleCachedOnly() {
return
}
// ── 3.65 Immediate Cached Parked Parent/Apex Check ────────────────────
if qc.enforceParkedCachedOnly() {
return
}
// ── 3.7 Immediate Cached RFC Compliance Parent/Apex Check ─────────────
if qc.enforceRFCComplianceCachedOnly() {
return
}
// ── 4. Cache lookup ───────────────────────────────────────────────────
if qc.serveFromCache() {
return
}
// ── 5. Policy exits ───────────────────────────────────────────────────
if !qc.bypassPolicies && !qc.bypassGlobal && handlePolicyExits(qc.w, qc.r, qc.q, qc.qNameTrimmed, qc.clientIP, qc.clientID, qc.protocol, qc.protoEndpoint, qc.cacheKey, qc.originalQName, qc.originalQNameTrimmed, qc.spoofedAlias) {
return
}
// ── 6. Local A/AAAA/PTR answers ───────────────────────────────────────
if handleLocalIdentity(qc.w, qc.r, qc.q, qc.qNameTrimmed, qc.clientID, qc.clientIP, qc.protocol, qc.protoEndpoint, qc.route.routeName, qc.route.routeOriginType, qc.route.bypassLocal, qc.cacheKey, qc.parentalForcedTTL, qc.originalQName, qc.spoofedAlias) {
return
}
// ── 6.5 Strict PTR Leakage Prevention ─────────────────────────────────
if cfg.Server.StrictPTR && qc.q.Qtype == dns.TypePTR && !qc.bypassPolicies && !qc.bypassGlobal {
if ipStr := extractIPFromPTR(qc.qNameTrimmed); ipStr != "" {
if addr, err := netip.ParseAddr(ipStr); err == nil {
if addr.IsPrivate() || addr.IsLoopback() || addr.IsLinkLocalUnicast() {
IncrPolicyBlock()
RecordBlockEvent(qc.clientIP, qc.originalQNameTrimmed, "Strict PTR (LAN Leakage Prevention)")
dropped := writePolicyAction(qc.w, qc.r, dns.RcodeNameError)
if cacheSynthFlag && !dropped && globalBlockAction != BlockActionDrop {
synthMsg := buildSynthCacheMsg(qc.r, qc.q, dns.RcodeNameError)
CacheSetSynth(qc.cacheKey, synthMsg)
msgPool.Put(synthMsg)
}
if logQueries {
if logSimple {
rcStr := "NXDOMAIN"
if dropped {
rcStr = "DROP"
}
logQuerySimple(qc.protoEndpoint, clientIP, qc.originalQName, "PTR", rcStr, "Strict PTR", false)
} else {
statusMark := "POLICY BLOCK"
if dropped {
statusMark = "POLICY DROP"
}
statusMark = qc.annotateAlias(statusMark)
log.Printf("[DNS] [%s] %s -> %s PTR | %s (Strict PTR Leakage) | NXDOMAIN",
qc.protocol, qc.clientID, qc.originalQName, statusMark)
}
}
return
}
}
}
}
// ── 7. Upstream group resolution & QNAME label bounds ─────────────────
if !qc.resolveUpstreamGroup() {
return
}
if qc.enforceQnameLabelBounds() {
return
}
// ── 7.5 RFC 10023 "_for-sale." domain check (upstream probe on miss) ──
if qc.enforceForSaleCheck() {
return
}
// ── 7.6 Parked domain check (upstream NS/SOA/TXT probe on miss) ───────
if qc.enforceParkedCheck() {
return
}
// ── 7.7 RFC compliance & NS CNAME indirection check (on miss) ─────────
if qc.enforceRFCComplianceCheck() {
return
}
// ── 8. Upstream exchange ──────────────────────────────────────────────
qc.serveFromUpstream()
}