Repository navigation
Expand file tree
/
Copy pathtransform.go
More file actions
499 lines (454 loc) · 13 KB
/
Copy pathtransform.go
File metadata and controls
499 lines (454 loc) · 13 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
/*
File: transform.go
Version: 1.18.0
Last Updated: 12-Nov-2026 16:55 CET
Description:
Outbound response mutation functions for sdproxy. Applied in ProcessDNS
(process.go) after a response arrives from the cache or upstream, before
it is written to the client.
transformResponse — dispatcher; calls the active transforms in order.
flattenCNAME — collapses a CNAME/DNAME chain into a single A/AAAA record
under the original query name (flatten_cname: true).
applyAnswerSort — dynamically rearranges contiguous blocks of A/AAAA
response records to simulate load-balancing techniques
like Round-Robin, Random, IP-Sort, or Proximity ordering
without breaking message structure dependencies.
All functions operate on an existing *dns.Msg — no upstream calls, no I/O.
Changes:
1.18.0 - [FIX] Normalized `*dns.AAAA` addresses with `.Unmap()` in both
`minProximityV6` and the `"ip-sort"` comparator inside `applyAnswerSort`
as promised in 1.17.0.
1.17.0 - [FIX] Normalized IPv4-mapped IPv6 addresses (`ip.Unmap()`) before
extracting 16-byte slices in `minProximityV6` and before comparing
addresses in `ip-sort` within `applyAnswerSort`.
1.16.0 - [FEAT] Added `"proximity"` sorting to `applyAnswerSort`, ordering A and
AAAA answers from closest to farthest in the 32-bit/128-bit address space
relative to the local listener and outbound query source addresses
(`proximityRefV4`, `proximityRefV6`, plus the per-request local socket IP).
1.15.0 - [SECURITY/FIX] Extended `flattenCNAME` to recognize RFC 6672 `DNAME`
records when walking the alias chain and computing `minTTL`.
1.14.0 - [SECURITY/FIX] Cleared `out.Ns` in `flattenCNAME` when terminal A/AAAA
records are collapsed onto `queryName`, preventing target-zone `NS`/`SOA`
authority records from leaking alongside flattened answers.
1.13.0 - [SECURITY/FIX] Cleared `out.AuthenticatedData = false` in `flattenCNAME`
when terminal A/AAAA records are rewritten onto `queryName`.
1.12.0 - [SECURITY/FIX] Bypassed `flattenCNAME` when `doBit` (`DO=1`) is set,
and omitted `RRSIG` records when flattening for `DO=0` clients.
1.11.0 - [SECURITY/FIX] Preserved matching `RRSIG` records (`r.TypeCovered == qtype`)
in `flattenCNAME` when `doBit` (`DO=1`) is requested.
1.10.0 - [SECURITY/FIX] Hardened `flattenCNAME` to verify that terminal A/AAAA
records belong to the CNAME chain rooted at `queryName` before rewriting
their owner names.
1.9.0 - [SECURITY/FIX] Preserved non-CNAME/non-A/AAAA records during `flattenCNAME`.
*/
package main
import (
"encoding/binary"
"math/bits"
"math/rand/v2"
"net/netip"
"slices"
"strings"
"github.com/miekg/dns"
)
// transformResponse applies the configured response transforms in order:
// 1. flattenCNAME — when flatten_cname: true, qtype is A or AAAA, and DO=0.
// 2. minimizeAnswer — strips Ns and Additional sections when minimize_answer: true.
func transformResponse(msg *dns.Msg, qtype uint16, doBit bool, inPlace bool) *dns.Msg {
if !cfg.Server.FlattenCNAME && !cfg.Server.MinimizeAnswer {
return msg
}
out := msg
if !inPlace {
out = msg.Copy()
}
if cfg.Server.FlattenCNAME && !doBit && (qtype == dns.TypeA || qtype == dns.TypeAAAA) {
flattenCNAME(out, qtype)
}
if cfg.Server.MinimizeAnswer {
if len(out.Ns) > 0 {
needFilter := false
for _, rr := range out.Ns {
rt := rr.Header().Rrtype
if !(rt == dns.TypeSOA || (doBit && (rt == dns.TypeNSEC || rt == dns.TypeNSEC3 || rt == dns.TypeRRSIG))) {
needFilter = true
break
}
}
if needFilter {
keptNs := make([]dns.RR, 0, len(out.Ns))
for _, rr := range out.Ns {
rt := rr.Header().Rrtype
if rt == dns.TypeSOA || (doBit && (rt == dns.TypeNSEC || rt == dns.TypeNSEC3 || rt == dns.TypeRRSIG)) {
keptNs = append(keptNs, rr)
}
}
out.Ns = keptNs
}
}
if len(out.Extra) > 0 {
needFilter := false
for _, rr := range out.Extra {
if rr.Header().Rrtype != dns.TypeOPT {
needFilter = true
break
}
}
if needFilter {
keptExtra := make([]dns.RR, 0, len(out.Extra))
for _, rr := range out.Extra {
if rr.Header().Rrtype == dns.TypeOPT {
keptExtra = append(keptExtra, rr)
}
}
out.Extra = keptExtra
}
}
}
return out
}
// flattenCNAME collapses a CNAME/DNAME chain in out.Answer into terminal A or AAAA
// records under the original query name. TTL is set to the chain minimum so
// the result expires no later than the shortest-lived link in the chain.
func flattenCNAME(out *dns.Msg, qtype uint16) {
if len(out.Answer) < 2 {
return
}
var firstAliasTTL uint32
hasAlias := false
queryName := ""
if len(out.Question) > 0 {
queryName = out.Question[0].Name
}
qLower := strings.ToLower(queryName)
for _, rr := range out.Answer {
switch r := rr.(type) {
case *dns.CNAME:
if queryName == "" || strings.EqualFold(r.Hdr.Name, queryName) {
firstAliasTTL = r.Hdr.Ttl
hasAlias = true
if queryName == "" {
queryName = r.Hdr.Name
qLower = strings.ToLower(queryName)
}
}
case *dns.DNAME:
ownerLower := strings.ToLower(r.Hdr.Name)
if qLower != "" && hasDotSuffix(qLower, ownerLower) {
firstAliasTTL = r.Hdr.Ttl
hasAlias = true
}
}
if hasAlias {
break
}
}
if !hasAlias || queryName == "" {
return
}
var chainStack [64]string
chainNames := chainStack[:0]
chainNames = append(chainNames, qLower)
minTTL := firstAliasTTL
for depth := 0; depth < 16; depth++ {
added := false
for _, rr := range out.Answer {
owner := strings.ToLower(rr.Header().Name)
switch r := rr.(type) {
case *dns.CNAME:
if isValidBailiwickName(chainNames, owner) {
if r.Hdr.Ttl < minTTL {
minTTL = r.Hdr.Ttl
}
target := strings.ToLower(r.Target)
if !isValidBailiwickName(chainNames, target) && len(chainNames) < 64 {
chainNames = append(chainNames, target)
added = true
}
}
case *dns.DNAME:
for _, vn := range chainNames {
if hasDotSuffix(vn, owner) {
if r.Hdr.Ttl < minTTL {
minTTL = r.Hdr.Ttl
}
if !isValidBailiwickName(chainNames, owner) && len(chainNames) < 64 {
chainNames = append(chainNames, owner)
added = true
}
prefix := vn[:len(vn)-len(owner)]
synthTarget := prefix + strings.ToLower(r.Target)
if len(synthTarget) <= 254 && !isValidBailiwickName(chainNames, synthTarget) && len(chainNames) < 64 {
chainNames = append(chainNames, synthTarget)
added = true
}
}
}
}
}
if !added {
break
}
}
finals := make([]dns.RR, 0, len(out.Answer)-1)
hasTerminal := false
for _, rr := range out.Answer {
switch r := rr.(type) {
case *dns.A:
if qtype == dns.TypeA && isValidBailiwickName(chainNames, strings.ToLower(r.Hdr.Name)) {
if r.Hdr.Ttl < minTTL {
minTTL = r.Hdr.Ttl
}
cp := *r
cp.Hdr.Name = queryName
finals = append(finals, &cp)
hasTerminal = true
}
case *dns.AAAA:
if qtype == dns.TypeAAAA && isValidBailiwickName(chainNames, strings.ToLower(r.Hdr.Name)) {
if r.Hdr.Ttl < minTTL {
minTTL = r.Hdr.Ttl
}
cp := *r
cp.Hdr.Name = queryName
finals = append(finals, &cp)
hasTerminal = true
}
}
}
if !hasTerminal {
return
}
for _, rr := range finals {
rr.Header().Ttl = minTTL
}
out.AuthenticatedData = false
out.Answer = finals
out.Ns = nil
}
// ---------------------------------------------------------------------------
// Address-Space Proximity Helpers (32-bit IPv4 & 128-bit IPv6)
// ---------------------------------------------------------------------------
func absDiff32(a, b uint32) uint32 {
if a >= b {
return a - b
}
return b - a
}
func absDiff128(a, b [2]uint64) [2]uint64 {
if a[0] > b[0] || (a[0] == b[0] && a[1] >= b[1]) {
lo, borrow := bits.Sub64(a[1], b[1], 0)
hi, _ := bits.Sub64(a[0], b[0], borrow)
return [2]uint64{hi, lo}
}
lo, borrow := bits.Sub64(b[1], a[1], 0)
hi, _ := bits.Sub64(b[0], a[0], borrow)
return [2]uint64{hi, lo}
}
func cmpU128(a, b [2]uint64) int {
if a[0] < b[0] {
return -1
}
if a[0] > b[0] {
return 1
}
if a[1] < b[1] {
return -1
}
if a[1] > b[1] {
return 1
}
return 0
}
func minProximityV4(ip netip.Addr, localV4 uint32, hasLocalV4 bool) (uint32, uint32) {
if !ip.IsValid() {
return ^uint32(0), ^uint32(0)
}
b := ip.Unmap().As4()
val := binary.BigEndian.Uint32(b[:])
minDist := ^uint32(0)
if hasLocalV4 {
minDist = absDiff32(val, localV4)
}
for _, ref := range proximityRefV4 {
if d := absDiff32(val, ref); d < minDist {
minDist = d
}
}
return minDist, val
}
func minProximityV6(ip netip.Addr, localV6 [2]uint64, hasLocalV6 bool) ([2]uint64, [2]uint64) {
maxU128 := [2]uint64{^uint64(0), ^uint64(0)}
if !ip.IsValid() {
return maxU128, maxU128
}
b := ip.Unmap().As16()
val := [2]uint64{
binary.BigEndian.Uint64(b[0:8]),
binary.BigEndian.Uint64(b[8:16]),
}
minDist := maxU128
if hasLocalV6 {
minDist = absDiff128(val, localV6)
}
for _, ref := range proximityRefV6 {
if d := absDiff128(val, ref); cmpU128(d, minDist) < 0 {
minDist = d
}
}
return minDist, val
}
// applyAnswerSort sorts the A and AAAA records in the Answer section natively.
func applyAnswerSort(msg *dns.Msg, method string, localAddrs ...netip.Addr) bool {
if method == "" || method == "none" || len(msg.Answer) < 2 {
return false
}
var (
hasLocalV4 bool
localV4 uint32
hasLocalV6 bool
localV6 [2]uint64
)
if method == "proximity" && len(localAddrs) > 0 && localAddrs[0].IsValid() {
la := localAddrs[0].Unmap()
if !la.IsUnspecified() {
if la.Is4() {
b := la.As4()
localV4 = binary.BigEndian.Uint32(b[:])
hasLocalV4 = true
} else if la.Is6() {
b := la.As16()
localV6 = [2]uint64{
binary.BigEndian.Uint64(b[0:8]),
binary.BigEndian.Uint64(b[8:16]),
}
hasLocalV6 = true
}
}
}
type rrItem struct {
idx int
rr dns.RR
}
var aRecordsBuf [16]rrItem
var aaaaRecordsBuf [16]rrItem
aRecords := aRecordsBuf[:0]
aaaaRecords := aaaaRecordsBuf[:0]
for i, rr := range msg.Answer {
if rr.Header().Rrtype == dns.TypeA {
aRecords = append(aRecords, rrItem{i, rr})
} else if rr.Header().Rrtype == dns.TypeAAAA {
aaaaRecords = append(aaaaRecords, rrItem{i, rr})
}
}
changed := false
processGroup := func(group []rrItem) {
if len(group) < 2 {
return
}
var origBuf [16]dns.RR
var original []dns.RR
if len(group) <= 16 {
original = origBuf[:len(group)]
} else {
original = make([]dns.RR, len(group))
}
for i, item := range group {
original[i] = item.rr
}
var sortBuf [16]dns.RR
var sorted []dns.RR
if len(group) <= 16 {
sorted = sortBuf[:len(group)]
} else {
sorted = make([]dns.RR, len(group))
}
copy(sorted, original)
switch method {
case "round-robin":
first := sorted[0]
copy(sorted, sorted[1:])
sorted[len(sorted)-1] = first
case "random":
rand.Shuffle(len(sorted), func(i, j int) {
sorted[i], sorted[j] = sorted[j], sorted[i]
})
case "ip-sort":
slices.SortStableFunc(sorted, func(a, b dns.RR) int {
var ipI, ipJ netip.Addr
if rr, ok := a.(*dns.A); ok {
if addr, ok := netip.AddrFromSlice(rr.A); ok {
ipI = addr.Unmap()
}
} else if rr, ok := a.(*dns.AAAA); ok {
if addr, ok := netip.AddrFromSlice(rr.AAAA); ok {
ipI = addr.Unmap()
}
}
if rr, ok := b.(*dns.A); ok {
if addr, ok := netip.AddrFromSlice(rr.A); ok {
ipJ = addr.Unmap()
}
} else if rr, ok := b.(*dns.AAAA); ok {
if addr, ok := netip.AddrFromSlice(rr.AAAA); ok {
ipJ = addr.Unmap()
}
}
return ipI.Compare(ipJ)
})
case "proximity":
slices.SortStableFunc(sorted, func(a, b dns.RR) int {
if rrA, okA := a.(*dns.A); okA {
if rrB, okB := b.(*dns.A); okB {
ipA, _ := netip.AddrFromSlice(rrA.A)
ipB, _ := netip.AddrFromSlice(rrB.A)
distA, valA := minProximityV4(ipA, localV4, hasLocalV4)
distB, valB := minProximityV4(ipB, localV4, hasLocalV4)
if distA < distB {
return -1
}
if distA > distB {
return 1
}
if valA < valB {
return -1
}
if valA > valB {
return 1
}
return 0
}
}
if rrA, okA := a.(*dns.AAAA); okA {
if rrB, okB := b.(*dns.AAAA); okB {
ipA, _ := netip.AddrFromSlice(rrA.AAAA)
ipB, _ := netip.AddrFromSlice(rrB.AAAA)
distA, valA := minProximityV6(ipA, localV6, hasLocalV6)
distB, valB := minProximityV6(ipB, localV6, hasLocalV6)
if c := cmpU128(distA, distB); c != 0 {
return c
}
return cmpU128(valA, valB)
}
}
return 0
})
}
groupChanged := false
for i := range sorted {
if sorted[i] != original[i] {
groupChanged = true
break
}
}
if groupChanged {
changed = true
for i, item := range group {
msg.Answer[item.idx] = sorted[i]
}
}
}
processGroup(aRecords)
processGroup(aaaaRecords)
return changed
}