Specify server in server-list-file

This commit is contained in:
bigeagle 2015-02-04 16:21:08 +08:00
parent b3b3939122
commit 3df6ec09f3
4 changed files with 63 additions and 28 deletions

5
examples/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,9 +12,9 @@ host = "127.0.0.1"
port = 53 port = 53
[resolv] [resolv]
server-list-file = "/etc/godns.d/servers"
resolv-file = "/etc/resolv.conf" resolv-file = "/etc/resolv.conf"
timeout = 5 # 5 seconds timeout = 5 # 5 seconds
domain-server-file = "/etc/godns.d/servers"
[redis] [redis]
enable = true enable = true

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
@ -21,30 +22,40 @@ func (e ResolvError) Error() string {
} }
type Resolver struct { type Resolver struct {
config *dns.ClientConfig servers []string
domain_server *suffixTreeNode domain_server *suffixTreeNode
config *ResolvSettings
} }
func NewResolver(c ResolvSettings) *Resolver { func NewResolver(c ResolvSettings) *Resolver {
var clientConfig *dns.ClientConfig r := &Resolver{
clientConfig, err := dns.ClientConfigFromFile(c.ResolvFile) servers: []string{},
if err != nil { domain_server: newSuffixTreeRoot(),
logger.Printf(":%s is not a valid resolv.conf file\n", c.ResolvFile) config: &c,
logger.Println(err)
panic(err)
} }
clientConfig.Timeout = c.Timeout
domain_server := newSuffixTreeRoot() if len(c.ServerListFile) > 0 {
r := &Resolver{clientConfig, domain_server} r.ReadServerListFile(c.ServerListFile)
// Debug("%v", r.servers)
if len(c.DomainServerFile) > 0 {
r.ReadDomainServerFile(c.DomainServerFile)
} }
if len(c.ResolvFile) > 0 {
clientConfig, err := dns.ClientConfigFromFile(c.ResolvFile)
if err != nil {
logger.Printf(":%s is not a valid resolv.conf file\n", c.ResolvFile)
logger.Println(err)
panic(err)
}
for _, server := range clientConfig.Servers {
nameserver := server + ":" + clientConfig.Port
r.servers = append(r.servers, nameserver)
}
}
return r return r
} }
func (r *Resolver) ReadDomainServerFile(file string) { func (r *Resolver) ReadServerListFile(file string) {
buf, err := os.Open(file) buf, err := os.Open(file)
if err != nil { if err != nil {
panic("Can't open " + file) panic("Can't open " + file)
@ -66,15 +77,35 @@ func (r *Resolver) ReadDomainServerFile(file string) {
line = strings.TrimSpace(sli[1]) line = strings.TrimSpace(sli[1])
tokens := strings.Split(line, "/") tokens := strings.Split(line, "/")
if len(tokens) != 3 { switch len(tokens) {
continue 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)
} }
domain := tokens[1]
ip := tokens[2]
if !isDomain(domain) || !isIP(ip) {
continue
}
r.domain_server.sinsert(strings.Split(domain, "."), ip)
} }
} }
@ -118,8 +149,7 @@ func (r *Resolver) Nameservers(qname string) []string {
ns = append(ns, nameserver) ns = append(ns, nameserver)
} }
for _, server := range r.config.Servers { for _, nameserver := range r.servers {
nameserver := server + ":" + r.config.Port
ns = append(ns, nameserver) ns = append(ns, nameserver)
} }
return ns return ns

View File

@ -25,9 +25,9 @@ type Settings struct {
} }
type ResolvSettings struct { type ResolvSettings struct {
ResolvFile string `toml:"resolv-file"` ServerListFile string `toml:"server-list-file"`
DomainServerFile string `toml:"domain-server-file"` ResolvFile string `toml:"resolv-file"`
Timeout int Timeout int
} }
type DNSServerSettings struct { type DNSServerSettings struct {