Skip to content

Commit e3ee4e0

Browse files
committed
Add "randomize" setting for controlling whether or not the "nameservers" list gets randomized on lookups
1 parent daa2225 commit e3ee4e0

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

cmd/rawdns/forwarding.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/miekg/dns"
1616
)
1717

18-
func handleForwardingRaw(nameservers []string, req *dns.Msg, remote net.Addr) *dns.Msg {
18+
func handleForwardingRaw(nameservers []string, randomize bool, req *dns.Msg, remote net.Addr) *dns.Msg {
1919
if len(nameservers) == 0 {
2020
log.Printf("no nameservers defined, can not forward\n")
2121
m := new(dns.Msg)
@@ -31,13 +31,17 @@ func handleForwardingRaw(nameservers []string, req *dns.Msg, remote net.Addr) *d
3131
tcp = true
3232
}
3333

34+
nsid := 0
35+
if randomize {
36+
// Use request Id for "random" nameserver selection.
37+
nsid = int(req.Id) % len(nameservers)
38+
}
39+
3440
var (
3541
r *dns.Msg
3642
err error
3743
try int
3844
)
39-
// Use request Id for "random" nameserver selection.
40-
nsid := int(req.Id) % len(nameservers)
4145
dnsClient := &dns.Client{Net: "udp", Timeout: 4 * time.Second, SingleInflight: true}
4246
if tcp {
4347
dnsClient.Net = "tcp"
@@ -68,6 +72,6 @@ Redo:
6872
}
6973

7074
// ServeDNSForward forwards a request to a nameservers and returns the response.
71-
func handleForwarding(nameservers []string, w dns.ResponseWriter, req *dns.Msg) {
72-
w.WriteMsg(handleForwardingRaw(nameservers, req, w.RemoteAddr()))
75+
func handleForwarding(nameservers []string, randomize bool, w dns.ResponseWriter, req *dns.Msg) {
76+
w.WriteMsg(handleForwardingRaw(nameservers, randomize, req, w.RemoteAddr()))
7377
}

cmd/rawdns/main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type DomainConfig struct {
3535

3636
// "type": "forwarding"
3737
Nameservers []string `json:"nameservers"` // [ "8.8.8.8", "8.8.4.4" ]
38+
Randomize *bool `json:"randomize"` // whether to randomize recursive lookups to spread upstream load (defaults to true)
3839

3940
// "type": "static"
4041
Addrs []string `json:"addrs"`
@@ -74,6 +75,13 @@ func main() {
7475
}
7576

7677
for domain := range config {
78+
if config[domain].Randomize == nil {
79+
domainConfig := config[domain]
80+
randomize := true
81+
domainConfig.Randomize = &randomize
82+
config[domain] = domainConfig
83+
}
84+
7785
switch config[domain].Type {
7886
case "containers":
7987
// TODO there must be a better way to pass "domain" along without an anonymous function AND copied variable
@@ -93,8 +101,9 @@ func main() {
93101
case "forwarding":
94102
// TODO there must be a better way to pass "domain" along without an anonymous function AND copied variable
95103
nameservers := config[domain].Nameservers
104+
randomize := *config[domain].Randomize
96105
dns.HandleFunc(domain, func(w dns.ResponseWriter, r *dns.Msg) {
97-
handleForwarding(nameservers, w, r)
106+
handleForwarding(nameservers, randomize, w, r)
98107
})
99108
case "static":
100109
cCopy := config[domain]
@@ -252,7 +261,7 @@ func handleStaticRequest(config DomainConfig, w dns.ResponseWriter, r *dns.Msg)
252261
},
253262
Question: []dns.Question{recQ},
254263
}
255-
recM := handleForwardingRaw(config.Nameservers, recR, w.RemoteAddr())
264+
recM := handleForwardingRaw(config.Nameservers, *config.Randomize, recR, w.RemoteAddr())
256265
for _, rr := range recM.Answer {
257266
dnsAppend(recQ, m, rr)
258267
}

0 commit comments

Comments
 (0)