Specify server in server-list-file
This commit is contained in:
parent
b3b3939122
commit
3df6ec09f3
|
@ -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
|
|
@ -12,9 +12,9 @@ host = "127.0.0.1"
|
|||
port = 53
|
||||
|
||||
[resolv]
|
||||
server-list-file = "/etc/godns.d/servers"
|
||||
resolv-file = "/etc/resolv.conf"
|
||||
timeout = 5 # 5 seconds
|
||||
domain-server-file = "/etc/godns.d/servers"
|
||||
|
||||
[redis]
|
||||
enable = true
|
60
resolver.go
60
resolver.go
|
@ -4,6 +4,7 @@ import (
|
|||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -21,30 +22,40 @@ func (e ResolvError) Error() string {
|
|||
}
|
||||
|
||||
type Resolver struct {
|
||||
config *dns.ClientConfig
|
||||
servers []string
|
||||
domain_server *suffixTreeNode
|
||||
config *ResolvSettings
|
||||
}
|
||||
|
||||
func NewResolver(c ResolvSettings) *Resolver {
|
||||
var clientConfig *dns.ClientConfig
|
||||
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.Printf(":%s is not a valid resolv.conf file\n", c.ResolvFile)
|
||||
logger.Println(err)
|
||||
panic(err)
|
||||
}
|
||||
clientConfig.Timeout = c.Timeout
|
||||
|
||||
domain_server := newSuffixTreeRoot()
|
||||
r := &Resolver{clientConfig, domain_server}
|
||||
|
||||
if len(c.DomainServerFile) > 0 {
|
||||
r.ReadDomainServerFile(c.DomainServerFile)
|
||||
for _, server := range clientConfig.Servers {
|
||||
nameserver := server + ":" + clientConfig.Port
|
||||
r.servers = append(r.servers, nameserver)
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Resolver) ReadDomainServerFile(file string) {
|
||||
func (r *Resolver) ReadServerListFile(file string) {
|
||||
buf, err := os.Open(file)
|
||||
if err != nil {
|
||||
panic("Can't open " + file)
|
||||
|
@ -66,15 +77,35 @@ func (r *Resolver) ReadDomainServerFile(file string) {
|
|||
line = strings.TrimSpace(sli[1])
|
||||
|
||||
tokens := strings.Split(line, "/")
|
||||
if len(tokens) != 3 {
|
||||
continue
|
||||
}
|
||||
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)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -118,8 +149,7 @@ func (r *Resolver) Nameservers(qname string) []string {
|
|||
ns = append(ns, nameserver)
|
||||
}
|
||||
|
||||
for _, server := range r.config.Servers {
|
||||
nameserver := server + ":" + r.config.Port
|
||||
for _, nameserver := range r.servers {
|
||||
ns = append(ns, nameserver)
|
||||
}
|
||||
return ns
|
||||
|
|
|
@ -25,8 +25,8 @@ type Settings struct {
|
|||
}
|
||||
|
||||
type ResolvSettings struct {
|
||||
ServerListFile string `toml:"server-list-file"`
|
||||
ResolvFile string `toml:"resolv-file"`
|
||||
DomainServerFile string `toml:"domain-server-file"`
|
||||
Timeout int
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue