send query to all upstream server

nghttp2
fangdingjun 9 years ago
parent 5cae50d369
commit 783790557e

@ -21,6 +21,10 @@ var DefaultServer []*UpstreamServer
var blacklist_file string var blacklist_file string
var enable_cache = false
var region_file = ""
func (s *ArgSrvs) String() string { func (s *ArgSrvs) String() string {
//Sprintf("%s", s) //Sprintf("%s", s)
return "filter1.txt,udp:8.8.8.8:53" return "filter1.txt,udp:8.8.8.8:53"
@ -86,6 +90,10 @@ func parse_flags() {
log.Fatal(err) log.Fatal(err)
} }
} }
if region_file != "" {
ip_region = parse_net(region_file)
}
} }
func init() { func init() {
@ -105,4 +113,6 @@ func init() {
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")
flag.StringVar(&hostfile, "hosts", "", "load special ip from hosts or /etc/hosts") flag.StringVar(&hostfile, "hosts", "", "load special ip from hosts or /etc/hosts")
flag.BoolVar(&enable_cache, "enable_cache", false, "enable cache or not")
flag.StringVar(&region_file, "region_file", "", "local country region ip range file")
} }

@ -0,0 +1,527 @@
{
"0.0.0.0": 1,
"1.1.1.1": 1,
"1.1.127.45": 1,
"1.1.67.51": 1,
"1.2.3.4": 1,
"1.209.208.200": 1,
"1.226.83.147": 1,
"1.234.21.83": 1,
"1.234.29.40": 1,
"1.234.39.14": 1,
"1.234.4.91": 1,
"1.234.70.80": 1,
"1.234.83.104": 1,
"1.244.115.172": 1,
"1.33.170.68": 1,
"1.33.188.62": 1,
"1.33.190.228": 1,
"1.33.190.70": 1,
"1.33.191.58": 1,
"2.1.1.2": 1,
"2.187.253.121": 1,
"2.228.123.7": 1,
"2.228.154.8": 1,
"4.17.143.131": 1,
"4.193.80.0": 1,
"4.21.70.9": 1,
"4.30.13.168": 1,
"4.30.187.9": 1,
"4.30.235.229": 1,
"4.31.139.146": 1,
"4.34.180.178": 1,
"4.35.100.20": 1,
"4.35.234.200": 1,
"4.36.66.178": 1,
"4.53.17.215": 1,
"4.59.79.206": 1,
"4.78.167.196": 1,
"4.79.129.122": 1,
"4.59.90.226": 1,
"4.59.90.212": 1,
"4.59.90.237": 1,
"4.59.90.221": 1,
"4.59.90.246": 1,
"4.59.90.232": 1,
"4.59.90.242": 1,
"4.59.90.241": 1,
"4.59.90.217": 1,
"4.59.90.227": 1,
"4.59.90.247": 1,
"4.59.90.231": 1,
"4.59.90.236": 1,
"4.59.90.251": 1,
"4.59.90.216": 1,
"4.59.90.222": 1,
"5.10.105.41": 1,
"5.10.68.187": 1,
"5.10.68.188": 1,
"5.10.69.29": 1,
"5.10.77.72": 1,
"5.100.152.24": 1,
"5.100.225.204": 1,
"5.100.228.206": 1,
"5.100.231.27": 1,
"5.100.248.208": 1,
"5.144.129.20": 1,
"5.35.251.108": 1,
"5.9.118.111": 1,
"5.9.120.140": 1,
"5.9.136.210": 1,
"5.9.242.232": 1,
"5.9.5.26": 1,
"5.9.65.105": 1,
"8.105.84.0": 1,
"8.34.161.150": 1,
"8.7.198.45": 1,
"12.87.133.0": 1,
"14.102.249.18": 1,
"16.63.155.0": 1,
"20.139.56.0": 1,
"23.23.14.192": 1,
"23.89.5.60": 1,
"24.51.184.0": 1,
"28.121.126.139": 1,
"28.13.216.0": 1,
"31.169.90.4": 1,
"31.170.8.8": 1,
"31.210.156.212": 1,
"31.22.4.60": 1,
"31.222.185.202": 1,
"31.25.191.134": 1,
"34.254.247.151": 1,
"37.1.205.21": 1,
"37.1.207.129": 1,
"37.140.238.35": 1,
"37.187.134.150": 1,
"37.187.149.129": 1,
"37.187.251.35": 1,
"37.208.111.120": 1,
"37.252.122.184": 1,
"37.58.78.79": 1,
"37.59.25.95": 1,
"37.61.54.158": 1,
"37.99.194.148": 1,
"38.117.98.231": 1,
"41.79.20.9": 1,
"43.253.199.12": 1,
"46.137.219.7": 1,
"46.165.231.144": 1,
"46.20.126.252": 1,
"46.20.13.100": 1,
"46.229.175.95": 1,
"46.243.6.170": 1,
"46.30.212.198": 1,
"46.38.24.209": 1,
"46.82.174.68": 1,
"49.2.123.56": 1,
"49.212.153.128": 1,
"50.116.6.162": 1,
"50.18.183.233": 1,
"50.57.11.12": 1,
"50.63.202.13": 1,
"50.87.148.140": 1,
"50.87.169.77": 1,
"50.93.207.101": 1,
"50.97.134.91": 1,
"54.174.40.182": 1,
"54.187.136.30": 1,
"54.187.39.38": 1,
"54.191.193.138": 1,
"54.200.3.32": 1,
"54.206.98.127": 1,
"54.209.238.28": 1,
"54.209.87.186": 1,
"54.218.38.198": 1,
"54.229.147.183": 1,
"54.235.199.154": 1,
"54.244.22.77": 1,
"54.246.169.32": 1,
"54.246.202.250": 1,
"54.68.166.130": 1,
"54.76.135.1": 1,
"54.83.51.191": 1,
"54.86.21.64": 1,
"54.86.223.202": 1,
"54.88.252.91": 1,
"59.124.74.28": 1,
"59.24.3.173": 1,
"61.54.28.6": 1,
"62.138.115.35": 1,
"62.75.221.31": 1,
"62.92.17.213": 1,
"64.14.72.41": 1,
"64.150.184.98": 1,
"64.22.110.34": 1,
"64.33.88.161": 1,
"64.33.99.47": 1,
"64.34.161.142": 1,
"64.50.179.133": 1,
"64.66.163.251": 1,
"64.79.69.250": 1,
"64.79.84.141": 1,
"64.91.254.97": 1,
"65.104.202.252": 1,
"65.160.219.113": 1,
"65.183.39.139": 1,
"66.146.2.241": 1,
"66.187.204.50": 1,
"66.206.11.194": 1,
"66.39.61.161": 1,
"66.45.252.237": 1,
"66.55.151.148": 1,
"66.85.134.186": 1,
"66.96.147.160": 1,
"67.137.227.11": 1,
"67.225.220.248": 1,
"68.71.58.18": 1,
"69.16.196.113": 1,
"69.167.172.162": 1,
"69.171.13.49": 1,
"69.174.244.221": 1,
"69.175.75.202": 1,
"69.195.124.90": 1,
"69.30.23.10": 1,
"69.50.192.218": 1,
"69.61.60.122": 1,
"70.42.243.33": 1,
"72.14.205.104": 1,
"72.14.205.99": 1,
"72.167.32.10": 1,
"72.20.110.50": 1,
"72.29.94.240": 1,
"72.32.4.243": 1,
"72.47.228.79": 1,
"72.5.1.109": 1,
"72.52.244.56": 1,
"74.117.117.122": 1,
"74.117.57.138": 1,
"74.124.195.73": 1,
"74.125.127.102": 1,
"74.125.155.102": 1,
"74.125.204.121": 1,
"74.125.39.102": 1,
"74.125.39.113": 1,
"74.207.236.174": 1,
"74.208.125.184": 1,
"74.220.215.67": 1,
"74.82.166.166": 1,
"75.98.175.166": 1,
"76.164.217.116": 1,
"77.4.7.92": 1,
"78.108.178.26": 1,
"78.140.172.33": 1,
"78.16.49.15": 1,
"78.24.135.99": 1,
"79.127.127.68": 1,
"79.136.125.49": 1,
"79.98.34.60": 1,
"80.190.96.26": 1,
"80.241.209.19": 1,
"80.241.92.180": 1,
"80.245.171.70": 1,
"80.70.184.118": 1,
"80.72.41.146": 1,
"80.82.117.209": 1,
"80.82.201.154": 1,
"80.92.117.132": 1,
"82.145.47.117": 1,
"83.125.118.122": 1,
"83.222.124.187": 1,
"83.222.5.171": 1,
"84.124.59.165": 1,
"85.111.18.138": 1,
"85.190.0.110": 1,
"85.25.171.103": 1,
"85.92.134.229": 1,
"87.106.57.209": 1,
"87.230.46.50": 1,
"88.198.69.101": 1,
"88.214.195.67": 1,
"89.108.118.129": 1,
"89.111.181.74": 1,
"89.186.95.11": 1,
"89.30.125.204": 1,
"89.31.55.106": 1,
"90.156.201.42": 1,
"91.121.245.154": 1,
"91.186.28.41": 1,
"91.198.129.47": 1,
"91.217.73.22": 1,
"91.221.37.35": 1,
"91.223.175.25": 1,
"91.238.30.54": 1,
"91.239.201.16": 1,
"92.53.106.175": 1,
"92.53.96.9": 1,
"92.63.110.174": 1,
"93.115.240.148": 1,
"93.158.121.72": 1,
"93.187.205.2": 1,
"93.46.8.89": 1,
"93.93.187.49": 1,
"94.136.188.30": 1,
"94.141.31.140": 1,
"94.23.147.142": 1,
"94.23.156.11": 1,
"94.23.193.224": 1,
"94.23.199.144": 1,
"95.163.95.47": 1,
"95.211.150.70": 1,
"95.211.229.156": 1,
"95.211.58.97": 1,
"95.85.22.163": 1,
"96.126.97.15": 1,
"96.127.172.221": 1,
"96.30.51.148": 1,
"97.74.80.22": 1,
"98.129.229.202": 1,
"98.158.152.159": 1,
"98.158.178.141": 1,
"104.200.31.226": 1,
"104.28.1.22": 1,
"104.28.14.112": 1,
"104.28.20.14": 1,
"104.28.30.59": 1,
"106.186.120.157": 1,
"106.187.39.80": 1,
"107.6.34.101": 1,
"108.168.215.230": 1,
"108.168.250.3": 1,
"108.179.196.77": 1,
"108.179.250.106": 1,
"108.61.250.218": 1,
"109.123.115.205": 1,
"109.206.173.212": 1,
"109.234.159.38": 1,
"109.71.81.130": 1,
"110.173.154.142": 1,
"110.74.163.40": 1,
"112.175.60.31": 1,
"113.11.194.190": 1,
"113.160.102.90": 1,
"118.145.17.184": 1,
"118.219.253.245": 1,
"118.5.49.6": 1,
"119.18.62.130": 1,
"119.235.57.82": 1,
"119.245.217.155": 1,
"119.9.94.83": 1,
"120.196.0.5": 1,
"120.198.243.113": 1,
"120.198.243.114": 1,
"120.198.243.115": 1,
"120.198.243.116": 1,
"120.198.243.14": 1,
"120.198.243.15": 1,
"120.198.243.151": 1,
"120.198.243.48": 1,
"120.198.243.52": 1,
"120.198.243.53": 1,
"120.89.93.248": 1,
"122.214.2.171": 1,
"122.218.101.190": 1,
"123.126.249.238": 1,
"123.30.175.29": 1,
"123.50.49.171": 1,
"125.230.148.48": 1,
"127.0.0.2": 1,
"128.121.126.139": 1,
"128.199.180.162": 1,
"133.192.181.66": 1,
"133.242.165.24": 1,
"133.42.48.3": 1,
"137.135.129.175": 1,
"141.101.118.102": 1,
"141.8.195.47": 1,
"141.8.195.78": 1,
"141.8.225.80": 1,
"142.4.5.109": 1,
"144.76.106.232": 1,
"144.76.127.114": 1,
"144.76.21.13": 1,
"145.253.183.23": 1,
"147.87.244.32": 1,
"155.92.182.118": 1,
"157.205.32.64": 1,
"157.7.143.209": 1,
"159.106.121.75": 1,
"159.24.3.173": 1,
"159.253.20.179": 1,
"159.50.88.77": 1,
"162.159.243.101": 1,
"162.243.137.163": 1,
"162.253.33.134": 1,
"164.109.96.232": 1,
"164.138.221.68": 1,
"168.156.168.21": 1,
"169.132.13.103": 1,
"171.17.130.53": 1,
"171.25.204.141": 1,
"173.192.219.59": 1,
"173.194.127.144": 1,
"173.201.216.6": 1,
"173.224.209.14": 1,
"173.236.228.108": 1,
"173.244.184.10": 1,
"173.255.194.174": 1,
"173.255.230.196": 1,
"174.142.113.142": 1,
"174.142.22.25": 1,
"176.10.37.81": 1,
"176.57.216.145": 1,
"178.18.82.216": 1,
"178.236.177.77": 1,
"178.32.111.136": 1,
"178.32.156.59": 1,
"178.32.247.82": 1,
"178.33.212.162": 1,
"178.49.132.135": 1,
"178.62.242.156": 1,
"178.62.75.99": 1,
"178.79.182.248": 1,
"180.153.225.168": 1,
"180.179.171.121": 1,
"180.87.182.227": 1,
"181.224.155.41": 1,
"183.111.141.95": 1,
"184.154.10.146": 1,
"184.169.132.244": 1,
"184.72.253.232": 1,
"185.25.150.45": 1,
"185.53.61.50": 1,
"188.132.250.186": 1,
"188.165.31.24": 1,
"188.226.207.251": 1,
"188.40.108.13": 1,
"188.5.4.96": 1,
"189.163.17.5": 1,
"192.104.44.6": 1,
"192.121.151.106": 1,
"192.67.198.6": 1,
"192.95.98.202": 1,
"193.105.145.158": 1,
"193.169.66.88": 1,
"193.203.48.18": 1,
"193.234.233.149": 1,
"193.238.151.98": 1,
"193.239.132.44": 1,
"193.48.96.218": 1,
"193.57.244.117": 1,
"193.91.26.132": 1,
"194.149.250.20": 1,
"194.185.115.1": 1,
"194.187.94.6": 1,
"194.67.144.70": 1,
"195.146.235.33": 1,
"195.149.210.211": 1,
"195.154.243.151": 1,
"195.191.149.103": 1,
"195.2.88.68": 1,
"195.211.72.200": 1,
"195.43.82.170": 1,
"195.49.201.30": 1,
"195.50.195.15": 1,
"195.74.38.62": 1,
"195.74.78.21": 1,
"195.77.241.242": 1,
"195.8.125.64": 1,
"197.4.4.12": 1,
"198.143.143.36": 1,
"198.57.205.133": 1,
"198.57.222.88": 1,
"198.58.124.68": 1,
"199.167.31.142": 1,
"199.21.68.222": 1,
"199.79.63.83": 1,
"200.229.206.115": 1,
"200.98.234.14": 1,
"201.77.211.143": 1,
"202.106.1.2": 1,
"202.181.7.85": 1,
"202.218.219.10": 1,
"202.6.96.25": 1,
"203.113.173.22": 1,
"203.133.238.172": 1,
"203.161.230.171": 1,
"203.199.57.81": 1,
"203.98.7.65": 1,
"206.108.51.91": 1,
"206.113.150.70": 1,
"207.12.88.98": 1,
"207.126.59.27": 1,
"207.140.149.247": 1,
"207.58.177.166": 1,
"208.109.138.55": 1,
"208.109.205.232": 1,
"208.112.102.122": 1,
"208.43.134.107": 1,
"208.43.33.194": 1,
"208.56.31.43": 1,
"208.73.211.164": 1,
"208.86.154.112": 1,
"208.93.0.150": 1,
"209.116.71.109": 1,
"209.126.106.182": 1,
"209.141.48.35": 1,
"209.145.54.50": 1,
"209.188.7.186": 1,
"209.204.148.22": 1,
"209.220.30.174": 1,
"209.235.224.25": 1,
"209.36.73.33": 1,
"209.43.1.130": 1,
"209.56.158.42": 1,
"209.62.154.94": 1,
"209.85.229.138": 1,
"210.175.255.154": 1,
"210.209.110.199": 1,
"210.230.192.183": 1,
"211.139.136.73": 1,
"211.139.204.59": 1,
"211.43.203.33": 1,
"211.5.133.18": 1,
"211.8.69.27": 1,
"211.94.66.147": 1,
"212.227.98.130": 1,
"212.45.52.219": 1,
"212.68.42.67": 1,
"212.77.104.29": 1,
"213.108.66.21": 1,
"213.133.111.102": 1,
"213.169.251.35": 1,
"213.174.158.108": 1,
"213.186.33.5": 1,
"213.19.161.141": 1,
"213.207.85.148": 1,
"213.238.166.227": 1,
"216.12.205.2": 1,
"216.139.213.144": 1,
"216.178.241.101": 1,
"216.198.246.103": 1,
"216.221.188.182": 1,
"216.234.179.13": 1,
"216.250.115.144": 1,
"216.38.0.92": 1,
"216.70.88.29": 1,
"216.92.58.37": 1,
"217.160.42.85": 1,
"217.172.183.9": 1,
"217.30.184.161": 1,
"218.44.251.212": 1,
"220.110.150.90": 1,
"220.247.224.8": 1,
"220.250.64.24": 1,
"221.213.49.149": 1,
"221.179.46.190": 1,
"221.179.46.194": 1,
"221.8.69.27": 1,
"222.122.56.219": 1,
"243.185.187.3": 1,
"243.185.187.30": 1,
"243.185.187.39": 1,
"249.129.46.48": 1,
"253.157.14.165": 1,
"255.255.255.255": 1
}

@ -0,0 +1,80 @@
package main
import (
//"fmt"
"github.com/miekg/dns"
"net"
"time"
)
var ip_region []*net.IPNet
type res struct {
m *dns.Msg
err error
}
func query(m *dns.Msg) *dns.Msg {
resch := make(chan *res, len(DefaultServer))
for _, s := range DefaultServer {
go query_one(s, m, resch)
}
delayed := []*dns.Msg{}
slen := len(DefaultServer)
got := 0
loop:
for {
select {
case r := <-resch:
r1 := *r
if r1.err != nil {
logger.Error("error %s\n", r1.err.Error())
continue
}
if in_blacklist(r1.m) {
continue
}
if answer_in_region(r1.m, ip_region) {
return r1.m
} else {
delayed = append(delayed, r1.m)
}
got += 1
if got >= slen {
break loop
}
case <-time.After(900 * time.Millisecond):
break loop
}
}
if len(delayed) == 0 {
logger.Error("empty delayed list")
return nil
}
// return first ok result
for _, m1 := range delayed {
if m1.Rcode == dns.RcodeSuccess {
return m1
}
}
// return NXDOMAIN result
for _, m1 := range delayed {
if m1.Rcode == dns.RcodeNameError {
return m1
}
}
// errror
return nil
}
func query_one(srv *UpstreamServer, m *dns.Msg, ch chan *res) {
m1, err := srv.query(m)
select {
case ch <- &res{m1, err}:
}
}

@ -0,0 +1,36 @@
#!/usr/bin/env python
import math
import netaddr
import urllib2
url = "http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest"
data = urllib2.urlopen(url).read()
with open("delegated-apnic-lastest", "w") as fp:
fp.write(data)
results = []
with open("delegated-apnic-latest") as fp:
for line in fp:
# format
# apnic|CN|ipv4|1.0.0.0|256|2012232|allocated
l = line.strip()
if not l:
continue
if l[0] == '#':
continue
lns = l.split("|")
if len(lns) != 7:
continue
if lns[2] != 'ipv4':
continue
if lns[1] != 'CN':
continue
ip = lns[3]
mask = 32-int(math.log(int(lns[4]), 2))
cidr = "%s/%d" % (ip, mask)
results.append(cidr)
r = netaddr.cidr_merge(results)
with open("region_cn.txt", "w") as fp:
for a in r:
fp.write("%s,%s,%d\n" % (a.ip,a.netmask, a.prefixlen))

@ -0,0 +1,84 @@
package main
import (
"bufio"
"fmt"
"github.com/miekg/dns"
"log"
"net"
"os"
"strings"
"unicode"
)
// parse ip range region file
// format
// ip, netmask, prefixlen
func parse_net(fn string) []*net.IPNet {
fp, err := os.Open(fn)
if err != nil {
log.Fatal(err)
return []*net.IPNet{}
}
defer fp.Close()
nets := []*net.IPNet{}
reader := bufio.NewReader(fp)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
line = strings.Trim(line, "\r\n")
fnds := strings.FieldsFunc(line, func(c rune) bool {
if unicode.IsSpace(c) {
return true
}
if c == ',' {
return true
}
return false
})
if len(fnds) != 3 {
continue
}
if _, net1, err := net.ParseCIDR(
fmt.Sprintf("%s/%s", fnds[0], fnds[2])); err == nil {
nets = append(nets, net1)
} else {
log.Fatal(err)
}
}
return nets
}
// test ip in ip range region
func in_region(ip net.IP, nets []*net.IPNet) bool {
for _, n := range nets {
if n.Contains(ip) {
return true
}
}
return false
}
// test dns reply A or AAAA in special ip range region
func answer_in_region(m *dns.Msg, nets []*net.IPNet) bool {
for _, rr := range m.Answer {
if a, ok := rr.(*dns.A); ok {
if in_region(a.A, nets) {
return true
}
}
if aaaa, ok := rr.(*dns.AAAA); ok {
if in_region(aaaa.AAAA, nets) {
return true
}
}
}
return false
}

@ -0,0 +1,74 @@
package main
import (
"fmt"
"github.com/miekg/dns"
"testing"
//"os"
)
func TestParseNet(t *testing.T) {
nets := parse_net("net_china.txt")
fmt.Printf("get %d networks\n", len(nets))
fmt.Printf("1st %s\n", nets[0].String())
}
func TestQuery(t *testing.T) {
ip_region = parse_net("net_china.txt")
var c *dns.Client
for _, srv := range []string{
"tcp:114.114.114.114:53",
"udp:8.8.8.8:53",
"udp:192.168.41.1:53",
"udp:4.2.2.2:53",
} {
proto, addr, err := parse_addr(srv)
if err != nil {
t.Error(err)
}
if proto == "tcp" {
c = client_tcp
} else {
c = client_udp
}
upsrv := &UpstreamServer{
Addr: addr,
Proto: proto,
client: c,
}
DefaultServer = append(DefaultServer, upsrv)
}
blacklist_file = "blacklist.txt"
a, err := load_domain(blacklist_file)
if err != nil {
fmt.Println(err)
} else {
Blacklist_ips = a
}
logger = NewLogger("", true)
for _, dn := range []string{
"www.google.com",
"www.sina.com.cn",
"www.taobao.com",
"www.ifeng.com",
"twitter.com",
"www.facebook.com",
"plus.google.com",
"drive.google.com",
"dongtaiwang.com",
"www.ratafee.nl",
"cc.ratafee.nl",
"noddcade.xx.ffs.aafde",
"ndfddcade.xx.ffs.aafde",
"sddf32dsf.comd.ffdasdf.fdsd3eaaaaa",
"www.google.com.hk",
} {
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(dn), dns.TypeA)
m1 := query(m)
if m1 == nil {
t.Error("query failed")
}
fmt.Printf("%s\n", m1)
}
}

File diff suppressed because it is too large Load Diff

@ -108,6 +108,7 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
key := fmt.Sprintf("%s_%s", domain, dns.TypeToString[r.Question[0].Qtype]) key := fmt.Sprintf("%s_%s", domain, dns.TypeToString[r.Question[0].Qtype])
if enable_cache {
// reply from cache // reply from cache
if a, ok := dns_cache.Get(key); ok { if a, ok := dns_cache.Get(key); ok {
msg := new(dns.Msg) msg := new(dns.Msg)
@ -130,6 +131,7 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
) )
return return
} }
}
// forward to upstream server // forward to upstream server
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
@ -153,12 +155,14 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
) )
if res.Rcode != dns.RcodeServerFailure && !in_blacklist(res) { if res.Rcode != dns.RcodeServerFailure && !in_blacklist(res) {
if enable_cache {
// 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, "|")) dns_cache.Add(key, strings.Join(v, "|"))
}
w.WriteMsg(res) w.WriteMsg(res)
done = 1 done = 1
break break
@ -168,34 +172,27 @@ func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
// fallback to default upstream server // fallback to default upstream server
if done != 1 { if done != 1 {
for _, dfsrv := range DefaultServer { logger.Debug("%s query %s %s %s, use default server\n",
res, err = dfsrv.query(r)
if err != nil {
logger.Error("%s", err)
continue
}
logger.Debug("%s query %s %s %s, use default server %s:%s, %s\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],
dfsrv.Proto, dfsrv.Addr,
dns.RcodeToString[res.Rcode],
) )
res := query(r)
if res.Rcode != dns.RcodeServerFailure && !in_blacklist(res) { if res != nil {
if enable_cache {
// 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, "|")) dns_cache.Add(key, strings.Join(v, "|"))
}
w.WriteMsg(res) w.WriteMsg(res)
done = 1 done = 1
break break
} }
}
} }
if done == 1 { if done == 1 {
@ -212,12 +209,13 @@ func main() {
parse_flags() parse_flags()
var err error var err error
if enable_cache {
// create cache // create cache
dns_cache, err = lru.New(1000) dns_cache, err = lru.New(1000)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
dns.HandleFunc(".", handleRoot) dns.HandleFunc(".", handleRoot)

Loading…
Cancel
Save