better fallback logical

nghttp2
fangdingjun 9 years ago
parent 9f9a78555e
commit 667dc1ff49

@ -12919,5 +12919,7 @@
"mmbang.info": 1, "mmbang.info": 1,
"fequan.com": 1, "fequan.com": 1,
"fangdd.com": 1, "fangdd.com": 1,
"qianzhan.com": 1 "qianzhan.com": 1,
"simicloud.com": 1,
"i4season.com": 1
} }

@ -84,8 +84,6 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
var res *dns.Msg var res *dns.Msg
domain := r.Question[0].Name domain := r.Question[0].Name
var done int
/* /*
reply from hosts reply from hosts
*/ */
@ -135,7 +133,6 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
// forward to upstream server // forward to upstream server
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
done = 0
for _, sv := range Servers { for _, sv := range Servers {
if sv.match(domain) { if sv.match(domain) {
@ -165,47 +162,39 @@ 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 return
break
} }
} }
} }
}
// fallback to default upstream server // fallback to default upstream server
if done != 1 { for i := 0; i < 2; i++ {
logger.Debug("%s query %s %s %s, use default server\n", logger.Debug("%s query %s %s %s, use default server\n",
w.RemoteAddr(), w.RemoteAddr(),
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],
) )
res := query(r) res := query(r)
if res != nil { if res != nil {
//logger.Debug("get: %s", res) //logger.Debug("get: %s", res)
if enable_cache && res.Rcode == dns.RcodeSuccess && if enable_cache && res.Rcode == dns.RcodeSuccess &&
len(res.Answer) > 0 { len(res.Answer) > 0 {
// add to cache // add to cache
v := []string{} v := []string{}
for _, as := range res.Answer { for _, as := range res.Answer {
v = append(v, as.String()) v = append(v, as.String())
}
dns_cache.Add(key, strings.Join(v, "|"))
} }
w.WriteMsg(res) dns_cache.Add(key, strings.Join(v, "|"))
done = 1
break
} }
w.WriteMsg(res)
return
} }
if done == 1 {
break
}
} }
if done != 1 { dns.HandleFailed(w, r)
dns.HandleFailed(w, r)
}
} }
func main() { func main() {

Loading…
Cancel
Save