Skip to content

Commit c7f5d33

Browse files
author
Brian Picciano
committed
Merge pull request #1 from LevenLabs/cachingGroups
Added support for groups of addresses and defaulted to serial queries
2 parents ad99725 + 865dc0e commit c7f5d33

2 files changed

Lines changed: 52 additions & 11 deletions

File tree

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ non-empty answer section. If so that is returned, otherwise the server at
3030
`8.8.8.8:53` will be checked. The order of checking is always in the order the
3131
`--fwd-to` parameters are specified.
3232

33-
In reality struggledns will make a dns request for every `--fwd-to` at the same
34-
time, for every incoming request. It then checks responses in order. This way
35-
there's not much of a speed penalty, at the cost of more bandwidth and more hits
36-
to the remote servers.
33+
### Groups
34+
35+
struggledns --fwd-to 10.0.0.5:53,8.8.8.8:53 --fwd-to 10.0.0.6:53,8.8.4.4:53
36+
37+
Each `--fwd-to` can be a group of comma-delimited addresses to query in
38+
parallel. Every address in a group is queried in parallel and groups are queried
39+
in serial unless `--parallel` is specified, which causes **all** sent addresses
40+
from all groups to be queried in parallel. The response still respects the order
41+
of the addresses despite being sent in parallel. In the above example, when a
42+
query is received it will be first sent to both `10.0.0.5:53` and `8.8.8.8:53`
43+
and if both of those fail then it will make a request to both `10.0.0.6:53` and
44+
`8.8.4.4:53`.

main.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55

66
"github.com/mediocregopher/lever"
77
"github.com/miekg/dns"
8+
"strings"
89
)
910

10-
var dnsServers []string
11+
var dnsServerGroups [][]string
1112

1213
func tryProxy(m *dns.Msg, addr string) *dns.Msg {
1314
aM, err := dns.Exchange(m, addr)
@@ -20,21 +21,32 @@ func tryProxy(m *dns.Msg, addr string) *dns.Msg {
2021
return aM
2122
}
2223

23-
func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
24-
chs := make([]chan *dns.Msg, len(dnsServers))
25-
for i := range dnsServers {
24+
func queryServers(r *dns.Msg, servers []string) *dns.Msg {
25+
chs := make([]chan *dns.Msg, len(servers))
26+
for i := range servers {
2627
chs[i] = make(chan *dns.Msg, 1)
2728
go func(ch chan *dns.Msg, addr string) {
2829
ch <- tryProxy(r, addr)
29-
}(chs[i], dnsServers[i])
30+
}(chs[i], servers[i])
3031
}
3132

3233
var m *dns.Msg
33-
for i := range dnsServers {
34+
for i := range servers {
3435
if m = <-chs[i]; m != nil {
3536
break
3637
}
3738
}
39+
return m
40+
}
41+
42+
func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
43+
var m *dns.Msg
44+
for i := range dnsServerGroups {
45+
m = queryServers(r, dnsServerGroups[i])
46+
if m != nil {
47+
break
48+
}
49+
}
3850
if m == nil {
3951
dns.HandleFailed(w, r)
4052
return
@@ -55,10 +67,31 @@ func main() {
5567
Name: "--fwd-to",
5668
Description: "Address (ip:port) of a dns server to attempt forward requests to. Specify multiple times to make multiple request attempts. Order specified dictates precedence should more than one server respond for a request",
5769
})
70+
l.Add(lever.Param{
71+
Name: "--parallel",
72+
Description: "If sent the query will be sent to all addresses in parallel",
73+
Flag: true,
74+
})
5875
l.Parse()
5976

6077
addr, _ := l.ParamStr("--listen-addr")
61-
dnsServers, _ = l.ParamStrs("--fwd-to")
78+
dnsServers, _ := l.ParamStrs("--fwd-to")
79+
combineGroups := l.ParamFlag("--parallel")
80+
81+
if combineGroups {
82+
//combine all the servers sent into one group
83+
dnsServerGroups = make([][]string, 1)
84+
var groupServers []string
85+
for i := range dnsServers {
86+
groupServers = strings.Split(dnsServers[i], ",")
87+
dnsServerGroups[0] = append(dnsServerGroups[0], groupServers...)
88+
}
89+
} else {
90+
dnsServerGroups = make([][]string, len(dnsServers))
91+
for i := range dnsServers {
92+
dnsServerGroups[i] = strings.Split(dnsServers[i], ",")
93+
}
94+
}
6295

6396
handler := dns.HandlerFunc(handleRequest)
6497
go func() {

0 commit comments

Comments
 (0)