support more than one upstream dns server

nghttp2
fangdingjun 9 years ago
parent 922a51b106
commit 5cae50d369

@ -9,7 +9,7 @@ import (
var bind_addr string
var default_server string
var default_server ArgSrvs
var srv ArgSrvs
@ -17,7 +17,7 @@ var logfile string
type ArgSrvs []string
var DefaultServer *UpstreamServer
var DefaultServer []*UpstreamServer
var blacklist_file string
@ -44,7 +44,8 @@ func parse_flags() {
}
}
proto, addr, err := parse_addr(default_server)
for _, dsvr := range default_server {
proto, addr, err := parse_addr(dsvr)
if err != nil {
log.Fatal(err)
}
@ -56,11 +57,17 @@ func parse_flags() {
c = client_tcp
}
DefaultServer = &UpstreamServer{
upsrv := &UpstreamServer{
Addr: addr,
Proto: proto,
client: c,
}
DefaultServer = append(DefaultServer, upsrv)
}
if len(DefaultServer) == 0 {
log.Fatal("please special a -upstream")
}
a, err := load_domain(blacklist_file)
if err != nil {
@ -93,7 +100,7 @@ func init() {
`)
flag.StringVar(&bind_addr, "bind", ":53", "the address bind to")
flag.StringVar(&default_server, "upstream", "udp:114.114.114.114:53", "the default upstream server to use")
flag.Var(&default_server, "upstream", "special the upstream server to use")
flag.StringVar(&logfile, "logfile", "", "the logfile, default stdout")
flag.StringVar(&blacklist_file, "blacklist", "", "the blacklist file")
flag.BoolVar(&debug, "debug", false, "output debug log, default false")

@ -168,7 +168,8 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
// fallback to default upstream server
if done != 1 {
res, err = DefaultServer.query(r)
for _, dfsrv := range DefaultServer {
res, err = dfsrv.query(r)
if err != nil {
logger.Error("%s", err)
continue
@ -179,7 +180,7 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
domain,
dns.ClassToString[r.Question[0].Qclass],
dns.TypeToString[r.Question[0].Qtype],
DefaultServer.Proto, DefaultServer.Addr,
dfsrv.Proto, dfsrv.Addr,
dns.RcodeToString[res.Rcode],
)
@ -192,6 +193,8 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
dns_cache.Add(key, strings.Join(v, "|"))
w.WriteMsg(res)
done = 1
break
}
}
}

Loading…
Cancel
Save