Merge https://github.com/bigeagle/godns into domain-specs-name-server

# Conflicts:
#	handler.go
#	hosts.go
#	hosts_test.go
#	resolver.go
#	settings.go
This commit is contained in:
kenshinx 2018-02-01 15:39:47 +08:00
commit 2e97278cb9
9 changed files with 252 additions and 44 deletions

5
etc/china.conf Normal file
View File

@ -0,0 +1,5 @@
server=8.8.8.8#53
server=127.0.0.1#5553
server=/baidu.com/114.114.114.114
# refer https://github.com/felixonmars/dnsmasq-china-list

View File

@ -12,6 +12,8 @@ host = "127.0.0.1"
port = 53 port = 53
[resolv] [resolv]
#Domain-specific nameservers configuration, formatting keep compatible with Dnsmasq
server-list-file = "./etc/china.conf"
resolv-file = "/etc/resolv.conf" resolv-file = "/etc/resolv.conf"
timeout = 5 # 5 seconds timeout = 5 # 5 seconds
# The concurrency interval request upstream recursive server # The concurrency interval request upstream recursive server
@ -21,6 +23,7 @@ interval = 200 # 200 milliseconds
setedns0 = false #Support for larger UDP DNS responses setedns0 = false #Support for larger UDP DNS responses
[redis] [redis]
enable = true
host = "127.0.0.1" host = "127.0.0.1"
port = 6379 port = 6379
db = 0 db = 0

View File

@ -32,21 +32,12 @@ type GODNSHandler struct {
func NewHandler() *GODNSHandler { func NewHandler() *GODNSHandler {
var ( var (
clientConfig *dns.ClientConfig
cacheConfig CacheSettings cacheConfig CacheSettings
resolver *Resolver resolver *Resolver
cache, negCache Cache cache, negCache Cache
) )
resolvConfig := settings.ResolvConfig resolver = NewResolver(settings.ResolvConfig)
clientConfig, err := dns.ClientConfigFromFile(resolvConfig.ResolvFile)
if err != nil {
logger.Warn(":%s is not a valid resolv.conf file\n", resolvConfig.ResolvFile)
logger.Error(err.Error())
panic(err)
}
clientConfig.Timeout = resolvConfig.Timeout
resolver = &Resolver{clientConfig}
cacheConfig = settings.Cache cacheConfig = settings.Cache
switch cacheConfig.Backend { switch cacheConfig.Backend {

View File

@ -218,7 +218,7 @@ func (f *FileHosts) Refresh() {
} }
ip := sli[0] ip := sli[0]
if !f.isIP(ip) { if !isIP(ip) {
continue continue
} }
@ -241,14 +241,14 @@ func (f *FileHosts) clear() {
f.hosts = make(map[string]string) f.hosts = make(map[string]string)
} }
func (f *FileHosts) isDomain(domain string) bool { func isDomain(domain string) bool {
if f.isIP(domain) { if isIP(domain) {
return false return false
} }
match, _ := regexp.MatchString(`^([a-zA-Z0-9\*]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$`, domain) match, _ := regexp.MatchString(`^([a-zA-Z0-9\*]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$`, domain)
return match return match
} }
func (f *FileHosts) isIP(ip string) bool { func isIP(ip string) bool {
return (net.ParseIP(ip) != nil) return (net.ParseIP(ip) != nil)
} }

View File

@ -8,26 +8,24 @@ import (
func TestHostDomainAndIP(t *testing.T) { func TestHostDomainAndIP(t *testing.T) {
Convey("Test Host File Domain and IP regex", t, func() { Convey("Test Host File Domain and IP regex", t, func() {
f := &FileHosts{}
Convey("1.1.1.1 should be IP and not domain", func() { Convey("1.1.1.1 should be IP and not domain", func() {
So(f.isIP("1.1.1.1"), ShouldEqual, true) So(isIP("1.1.1.1"), ShouldEqual, true)
So(f.isDomain("1.1.1.1"), ShouldEqual, false) So(isDomain("1.1.1.1"), ShouldEqual, false)
}) })
Convey("2001:470:20::2 should be IP and not domain", func() { Convey("2001:470:20::2 should be IP and not domain", func() {
So(f.isIP("2001:470:20::2"), ShouldEqual, true) So(isIP("2001:470:20::2"), ShouldEqual, true)
So(f.isDomain("2001:470:20::2"), ShouldEqual, false) So(isDomain("2001:470:20::2"), ShouldEqual, false)
}) })
Convey("`host` should not be domain and not IP", func() { Convey("`host` should not be domain and not IP", func() {
So(f.isDomain("host"), ShouldEqual, false) So(isDomain("host"), ShouldEqual, false)
So(f.isIP("host"), ShouldEqual, false) So(isIP("host"), ShouldEqual, false)
}) })
Convey("`123.test` should be domain and not IP", func() { Convey("`123.test` should be domain and not IP", func() {
So(f.isDomain("123.test"), ShouldEqual, true) So(isDomain("123.test"), ShouldEqual, true)
So(f.isIP("123.test"), ShouldEqual, false) So(isIP("123.test"), ShouldEqual, false)
}) })
}) })

View File

@ -1,8 +1,10 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"net" "os"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -21,7 +23,92 @@ func (e ResolvError) Error() string {
} }
type Resolver struct { type Resolver struct {
config *dns.ClientConfig servers []string
domain_server *suffixTreeNode
config *ResolvSettings
}
func NewResolver(c ResolvSettings) *Resolver {
r := &Resolver{
servers: []string{},
domain_server: newSuffixTreeRoot(),
config: &c,
}
if len(c.ServerListFile) > 0 {
r.ReadServerListFile(c.ServerListFile)
// Debug("%v", r.servers)
}
if len(c.ResolvFile) > 0 {
clientConfig, err := dns.ClientConfigFromFile(c.ResolvFile)
if err != nil {
logger.Error(":%s is not a valid resolv.conf file\n", c.ResolvFile)
logger.Error("%s", err)
panic(err)
}
for _, server := range clientConfig.Servers {
nameserver := server + ":" + clientConfig.Port
r.servers = append(r.servers, nameserver)
}
}
return r
}
func (r *Resolver) ReadServerListFile(file string) {
buf, err := os.Open(file)
if err != nil {
panic("Can't open " + file)
}
scanner := bufio.NewScanner(buf)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "server") {
continue
}
sli := strings.Split(line, "=")
if len(sli) != 2 {
continue
}
line = strings.TrimSpace(sli[1])
tokens := strings.Split(line, "/")
switch len(tokens) {
case 3:
domain := tokens[1]
ip := tokens[2]
if !isDomain(domain) || !isIP(ip) {
continue
}
r.domain_server.sinsert(strings.Split(domain, "."), ip)
case 1:
srv_port := strings.Split(line, "#")
if len(srv_port) > 2 {
continue
}
ip := ""
if ip = srv_port[0]; !isIP(ip) {
continue
}
port := "53"
if len(srv_port) == 2 {
if _, err := strconv.Atoi(srv_port[1]); err != nil {
continue
}
port = srv_port[1]
}
r.servers = append(r.servers, ip+":"+port)
}
}
} }
// Lookup will ask each nameserver in top-to-bottom fashion, starting a new request // Lookup will ask each nameserver in top-to-bottom fashion, starting a new request
@ -60,7 +147,7 @@ func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error
return return
} }
} else { } else {
logger.Debug("%s resolv on %s (%s) ttl: %d", UnFqdn(qname), nameserver, net, rtt) logger.Debug("%s resolv on %s (%s) ttl: %v", UnFqdn(qname), nameserver, net, rtt)
} }
select { select {
case res <- r: case res <- r:
@ -71,12 +158,14 @@ func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error
ticker := time.NewTicker(time.Duration(settings.ResolvConfig.Interval) * time.Millisecond) ticker := time.NewTicker(time.Duration(settings.ResolvConfig.Interval) * time.Millisecond)
defer ticker.Stop() defer ticker.Stop()
// Start lookup on each nameserver top-down, in every second // Start lookup on each nameserver top-down, in every second
for _, nameserver := range r.Nameservers() { nameservers := r.Nameservers(qname)
for _, nameserver := range nameservers {
wg.Add(1) wg.Add(1)
go L(nameserver) go L(nameserver)
// but exit early, if we have an answer // but exit early, if we have an answer
select { select {
case r := <-res: case r := <-res:
// logger.Debug("%s resolv on %s rtt: %v", UnFqdn(qname), nameserver, rtt)
return r, nil return r, nil
case <-ticker.C: case <-ticker.C:
continue continue
@ -86,25 +175,32 @@ func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error
wg.Wait() wg.Wait()
select { select {
case r := <-res: case r := <-res:
// logger.Debug("%s resolv on %s rtt: %v", UnFqdn(qname), nameserver, rtt)
return r, nil return r, nil
default: default:
return nil, ResolvError{qname, net, r.Nameservers()} return nil, ResolvError{qname, net, nameservers}
} }
} }
// Namservers return the array of nameservers, with port number appended. // Namservers return the array of nameservers, with port number appended.
// '#' in the name is treated as port separator, as with dnsmasq. // '#' in the name is treated as port separator, as with dnsmasq.
func (r *Resolver) Nameservers() (ns []string) {
for _, server := range r.config.Servers { func (r *Resolver) Nameservers(qname string) []string {
if i := strings.IndexByte(server, '#'); i > 0 { queryKeys := strings.Split(qname, ".")
server = net.JoinHostPort(server[:i], server[i+1:]) queryKeys = queryKeys[:len(queryKeys)-1] // ignore last '.'
} else {
server = net.JoinHostPort(server, r.config.Port) ns := []string{}
if v, found := r.domain_server.search(queryKeys); found {
logger.Debug("found upstream: %v", v)
server := v
nameserver := server + ":53"
ns = append(ns, nameserver)
} }
ns = append(ns, server)
for _, nameserver := range r.servers {
ns = append(ns, nameserver)
} }
return return ns
} }
func (r *Resolver) Timeout() time.Duration { func (r *Resolver) Timeout() time.Duration {

View File

@ -34,10 +34,11 @@ type Settings struct {
} }
type ResolvSettings struct { type ResolvSettings struct {
ResolvFile string `toml:"resolv-file"`
Timeout int Timeout int
Interval int Interval int
SetEDNS0 bool SetEDNS0 bool
ServerListFile string `toml:"server-list-file"`
ResolvFile string `toml:"resolv-file"`
} }
type DNSServerSettings struct { type DNSServerSettings struct {
@ -93,7 +94,7 @@ func init() {
var configFile string var configFile string
flag.StringVar(&configFile, "c", "godns.conf", "Look for godns toml-formatting config file in this directory") flag.StringVar(&configFile, "c", "./etc/godns.conf", "Look for godns toml-formatting config file in this directory")
flag.Parse() flag.Parse()
if _, err := toml.DecodeFile(configFile, &settings); err != nil { if _, err := toml.DecodeFile(configFile, &settings); err != nil {

65
sfx_tree.go Normal file
View File

@ -0,0 +1,65 @@
package main
type suffixTreeNode struct {
key string
value string
children map[string]*suffixTreeNode
}
func newSuffixTreeRoot() *suffixTreeNode {
return newSuffixTree("", "")
}
func newSuffixTree(key string, value string) *suffixTreeNode {
root := &suffixTreeNode{
key: key,
value: value,
children: map[string]*suffixTreeNode{},
}
return root
}
func (node *suffixTreeNode) ensureSubTree(key string) {
if _, ok := node.children[key]; !ok {
node.children[key] = newSuffixTree(key, "")
}
}
func (node *suffixTreeNode) insert(key string, value string) {
if c, ok := node.children[key]; ok {
c.value = value
} else {
node.children[key] = newSuffixTree(key, value)
}
}
func (node *suffixTreeNode) sinsert(keys []string, value string) {
if len(keys) == 0 {
return
}
key := keys[len(keys)-1]
if len(keys) > 1 {
node.ensureSubTree(key)
node.children[key].sinsert(keys[:len(keys)-1], value)
return
}
node.insert(key, value)
}
func (node *suffixTreeNode) search(keys []string) (string, bool) {
if len(keys) == 0 {
return "", false
}
key := keys[len(keys)-1]
if n, ok := node.children[key]; ok {
if nextValue, found := n.search(keys[:len(keys)-1]); found {
return nextValue, found
}
return n.value, (n.value != "")
}
return "", false
}

49
sfx_tree_test.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Suffix_Tree(t *testing.T) {
root := newSuffixTreeRoot()
Convey("Google should not be found", t, func() {
root.insert("cn", "114.114.114.114")
root.sinsert([]string{"baidu", "cn"}, "166.111.8.28")
root.sinsert([]string{"sina", "cn"}, "114.114.114.114")
v, found := root.search(strings.Split("google.com", "."))
So(found, ShouldEqual, false)
v, found = root.search(strings.Split("baidu.cn", "."))
So(found, ShouldEqual, true)
So(v, ShouldEqual, "166.111.8.28")
})
Convey("Google should be found", t, func() {
root.sinsert(strings.Split("com", "."), "")
root.sinsert(strings.Split("google.com", "."), "8.8.8.8")
root.sinsert(strings.Split("twitter.com", "."), "8.8.8.8")
root.sinsert(strings.Split("scholar.google.com", "."), "208.67.222.222")
v, found := root.search(strings.Split("google.com", "."))
So(found, ShouldEqual, true)
So(v, ShouldEqual, "8.8.8.8")
v, found = root.search(strings.Split("scholar.google.com", "."))
So(found, ShouldEqual, true)
So(v, ShouldEqual, "208.67.222.222")
v, found = root.search(strings.Split("twitter.com", "."))
So(found, ShouldEqual, true)
So(v, ShouldEqual, "8.8.8.8")
v, found = root.search(strings.Split("baidu.cn", "."))
So(found, ShouldEqual, true)
So(v, ShouldEqual, "166.111.8.28")
})
}