Compare commits

..

No commits in common. "master" and "bc2470e344e2dbc725efba29691be9a8462dd84d" have entirely different histories.

5 changed files with 45 additions and 130 deletions

View File

@ -124,28 +124,7 @@ func init() {
} else { } else {
conf.ZabovCache = true conf.ZabovCache = true
} }
ZabovDNSArray := fileByLines(conf.ZabovUpDNS) conf.ZabovDNSArray = fileByLines(conf.ZabovUpDNS)
conf.ZabovDNSArray = []string{}
for _, value := range ZabovDNSArray {
tvalue := strings.TrimSpace(value)
if tvalue != "" && !strings.HasPrefix(tvalue, "#") {
conf.ZabovDNSArray = append(conf.ZabovDNSArray, tvalue)
}
}
if confRaw["parallelqueries"] != nil {
conf.ZabovParallelQueries = int(confRaw["parallelqueries"].(float64))
}
if conf.ZabovParallelQueries > len(conf.ZabovDNSArray) {
conf.ZabovParallelQueries = len(conf.ZabovDNSArray)
}
if conf.ZabovParallelQueries < 1 {
conf.ZabovParallelQueries = 1
}
fmt.Println("ZabovParallelQueries:", conf.ZabovParallelQueries)
ZabovConfigs[name] = &conf ZabovConfigs[name] = &conf
} }
@ -303,8 +282,7 @@ func init() {
if localresponder["responder"] != nil { if localresponder["responder"] != nil {
ZabovLocalResponder = localresponder["responder"].(string) ZabovLocalResponder = localresponder["responder"].(string)
if len(ZabovLocalResponder) > 0 { if len(ZabovLocalResponder) > 0 {
local := ZabovConfig{ZabovDNSArray: []string{ZabovLocalResponder}, local := ZabovConfig{ZabovDNSArray: []string{ZabovLocalResponder}, references: 1}
references: 1, ZabovParallelQueries: 1}
ZabovConfigs[localresponderConfigName] = &local ZabovConfigs[localresponderConfigName] = &local
fmt.Println("ZabovLocalResponder:", ZabovLocalResponder) fmt.Println("ZabovLocalResponder:", ZabovLocalResponder)
} }

View File

@ -36,9 +36,7 @@
"singlefilters":"./urls-domains-updated.txt", "singlefilters":"./urls-domains-updated.txt",
"doublefilters":"./urls-hosts-normal.txt", "doublefilters":"./urls-hosts-normal.txt",
"blackholeip":"127.0.0.1", "blackholeip":"127.0.0.1",
"hostsfile":"./urls-local-normal.txt", "hostsfile":"./urls-local-normal.txt"
"whitelist":"./urls-wl-normal.txt",
"parallelqueries":4
}, },
"children":{ "children":{
"upstream":"./dns-familyscreen.txt", "upstream":"./dns-familyscreen.txt",

View File

@ -225,6 +225,7 @@
212.89.128.28:53 212.89.128.28:53
213.133.116.14:53 213.133.116.14:53
213.166.247.100:53 213.166.247.100:53
217.243.173.82:53
217.5.182.118:53 217.5.182.118:53
217.7.80.40:53 217.7.80.40:53
217.7.81.136:53 217.7.81.136:53
@ -361,6 +362,7 @@
85.214.62.160:53 85.214.62.160:53
85.93.91.101:53 85.93.91.101:53
87.106.63.208:53 87.106.63.208:53
87.118.126.225:53
88.198.37.146:53 88.198.37.146:53
88.99.66.18:53 88.99.66.18:53
89.163.150.209:53 89.163.150.209:53

View File

@ -11,77 +11,6 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func SingleResolve(query *dns.Msg, d string, lfqdn string, ch chan *dns.Msg) {
c := new(dns.Client)
c.ReadTimeout = 500 * time.Millisecond
c.WriteTimeout = 500 * time.Millisecond
in, _, err := c.Exchange(query, d)
if err != nil {
fmt.Printf("SingleResolve: Problem with DNS %s : %s\n", d, err.Error())
go incrementStats("DNS Problems "+d, 1)
ch <- nil
} else {
go incrementStats(d, 1)
Rcode := in.MsgHdr.Rcode
in.SetReply(query)
in.MsgHdr.Rcode = Rcode
in.Authoritative = true
in.Compress = true
go DomainCache(lfqdn, in)
if ZabovDebug {
log.Println("SingleResolve: OK:", d, lfqdn)
}
ch <- in
}
}
func ParallelResolve(query *dns.Msg, config string, lfqdn string) *dns.Msg {
var resCur *dns.Msg
var res *dns.Msg
ch := make(chan *dns.Msg)
dnss := oneTimeDNS(config, ZabovConfigs[config].ZabovParallelQueries)
for _, d := range dnss {
if ZabovDebug {
log.Println("ParallelResolve: running SingleResolve on:", d, lfqdn)
}
go SingleResolve(query, d, lfqdn, ch)
}
if ZabovDebug {
log.Println("ParallelResolve: wait for results...")
}
for range dnss {
resCur = <-ch
if resCur != nil {
if res == nil {
if ZabovDebug {
log.Println("ParallelResolve: got first result!")
}
res = resCur
} else if resCur.Rcode == dns.RcodeSuccess {
if ZabovDebug {
log.Println("ParallelResolve: got next result, RcodeSuccess, replacing previous...")
}
res = resCur
} else {
if ZabovDebug {
log.Println("ParallelResolve: got next result, discarding...")
}
}
if res.Rcode == dns.RcodeSuccess {
break
}
}
}
return res
}
//ForwardQuery forwards the query to the upstream server //ForwardQuery forwards the query to the upstream server
//first server to answer wins //first server to answer wins
//accepts config name to select the UP DNS source list //accepts config name to select the UP DNS source list
@ -110,9 +39,15 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
} }
cached.Compress = true cached.Compress = true
return cached return cached
} }
} }
c := new(dns.Client)
c.ReadTimeout = 500 * time.Millisecond
c.WriteTimeout = 500 * time.Millisecond
for { for {
// round robin with retry // round robin with retry
@ -123,10 +58,27 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
continue continue
} }
in := ParallelResolve(query, config, lfqdn) d := oneTimeDNS(config)
in, _, err := c.Exchange(query, d)
if err != nil {
fmt.Printf("Problem with DNS %s : %s\n", d, err.Error())
go incrementStats("DNS Problems "+d, 1)
continue
} else {
go incrementStats(d, 1)
Rcode := in.MsgHdr.Rcode
in.SetReply(query)
in.MsgHdr.Rcode = Rcode
in.Authoritative = true
in.Compress = true
go DomainCache(lfqdn, in)
if ZabovDebug {
log.Println("ForwardQuery: OK!")
}
if in != nil {
return in return in
} }
} }
@ -146,12 +98,9 @@ func init() {
} }
func oneTimeDNS(config string, count int) (dns []string) { func oneTimeDNS(config string) (dns string) {
if count == 0 { rand.Seed(time.Now().Unix())
count = 1
}
rand.Seed(time.Now().UnixNano())
upl := ZabovConfigs[config].ZabovDNSArray upl := ZabovConfigs[config].ZabovDNSArray
@ -159,27 +108,16 @@ func oneTimeDNS(config string, count int) (dns []string) {
if len(ZabovLocalResponder) > 0 { if len(ZabovLocalResponder) > 0 {
fmt.Println("No DNS defined, fallback to local responder:", ZabovLocalResponder) fmt.Println("No DNS defined, fallback to local responder:", ZabovLocalResponder)
return []string{ZabovLocalResponder} return ZabovLocalResponder
} }
fmt.Println("No DNS defined, using default 127.0.0.53:53. Hope it works!") fmt.Println("No DNS defined, using default 127.0.0.53:53. Hope it works!")
return []string{"127.0.0.53:53"} return "127.0.0.53:53"
} }
n := rand.Intn(128*len(upl)) % len(upl) n := rand.Intn(128*len(upl)) % len(upl)
res := []string{} dns = upl[n]
for i := 0; i < count; i++ {
res = append(res, upl[(n+i)%len(upl)])
}
check := make(map[string]int) return
for _, val := range res {
check[val] = 1
}
res = []string{}
for d, _ := range check {
res = append(res, d)
}
return res
} }

19
main.go
View File

@ -35,16 +35,15 @@ type handler struct{}
// ZabovConfig contains all Zabov configs // ZabovConfig contains all Zabov configs
type ZabovConfig struct { type ZabovConfig struct {
ZabovSingleBL string // json:singlefilters -> ZabovSingleBL list of urls returning a file with just names of domains ZabovSingleBL string // json:singlefilters -> ZabovSingleBL list of urls returning a file with just names of domains
ZabovDoubleBL string // json:doublefilters -> ZabovDoubleBL list of urls returning a file with IP<space>domain ZabovDoubleBL string // json:doublefilters -> ZabovDoubleBL list of urls returning a file with IP<space>domain
ZabovAddBL net.IP // json:blackholeip -> ZabovAddBL is the IP we want to send all the clients to. Usually is 127.0.0.1 ZabovAddBL net.IP // json:blackholeip -> ZabovAddBL is the IP we want to send all the clients to. Usually is 127.0.0.1
ZabovHostsFile string // json:hostsfile -> ZabovHostsFile is the file we use to keep our hosts ZabovHostsFile string // json:hostsfile -> ZabovHostsFile is the file we use to keep our hosts
ZabovWhiteList string // json:hostsfile -> ZabovWhiteList is the file we use to keep white listed hosts ZabovWhiteList string // json:hostsfile -> ZabovWhiteList is the file we use to keep white listed hosts
ZabovUpDNS string // json:upstream -> ZabovUpDNS keeps the name of upstream DNSs ZabovUpDNS string // json:upstream -> ZabovUpDNS keeps the name of upstream DNSs
ZabovDNSArray []string // contains all the DNS we mention, parsed from ZabovUpDNS file ZabovDNSArray []string // contains all the DNS we mention, parsed from ZabovUpDNS file
ZabovCache bool // allows to disable cache ZabovCache bool // allows to disable cache
ZabovParallelQueries int // contains max number of parallel queries to multiple DNS servers references int // contains references to this config; if zero, config shall be removed
references int // contains references to this config; if zero, config shall be removed
} }
// ZabovConfigs contains all Zabov configs // ZabovConfigs contains all Zabov configs