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 bind_addr string
var default_server string var default_server ArgSrvs
var srv ArgSrvs var srv ArgSrvs
@ -17,7 +17,7 @@ var logfile string
type ArgSrvs []string type ArgSrvs []string
var DefaultServer *UpstreamServer var DefaultServer []*UpstreamServer
var blacklist_file string 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -56,11 +57,17 @@ func parse_flags() {
c = client_tcp c = client_tcp
} }
DefaultServer = &UpstreamServer{ upsrv := &UpstreamServer{
Addr: addr, Addr: addr,
Proto: proto, Proto: proto,
client: c, client: c,
} }
DefaultServer = append(DefaultServer, upsrv)
}
if len(DefaultServer) == 0 {
log.Fatal("please special a -upstream")
}
a, err := load_domain(blacklist_file) a, err := load_domain(blacklist_file)
if err != nil { if err != nil {
@ -93,7 +100,7 @@ func init() {
`) `)
flag.StringVar(&bind_addr, "bind", ":53", "the address bind to") 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(&logfile, "logfile", "", "the logfile, default stdout")
flag.StringVar(&blacklist_file, "blacklist", "", "the blacklist file") flag.StringVar(&blacklist_file, "blacklist", "", "the blacklist file")
flag.BoolVar(&debug, "debug", false, "output debug log, default false") 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 // fallback to default upstream server
if done != 1 { if done != 1 {
res, err = DefaultServer.query(r) for _, dfsrv := range DefaultServer {
res, err = dfsrv.query(r)
if err != nil { if err != nil {
logger.Error("%s", err) logger.Error("%s", err)
continue continue
@ -179,7 +180,7 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
domain, domain,
dns.ClassToString[r.Question[0].Qclass], dns.ClassToString[r.Question[0].Qclass],
dns.TypeToString[r.Question[0].Qtype], dns.TypeToString[r.Question[0].Qtype],
DefaultServer.Proto, DefaultServer.Addr, dfsrv.Proto, dfsrv.Addr,
dns.RcodeToString[res.Rcode], dns.RcodeToString[res.Rcode],
) )
@ -192,6 +193,8 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
dns_cache.Add(key, strings.Join(v, "|")) dns_cache.Add(key, strings.Join(v, "|"))
w.WriteMsg(res) w.WriteMsg(res)
done = 1 done = 1
break
}
} }
} }

Loading…
Cancel
Save