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
|
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
|
60
resolver.go
60
resolver.go
|
@ -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{
|
||||||
|
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)
|
clientConfig, err := dns.ClientConfigFromFile(c.ResolvFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Printf(":%s is not a valid resolv.conf file\n", c.ResolvFile)
|
logger.Printf(":%s is not a valid resolv.conf file\n", c.ResolvFile)
|
||||||
logger.Println(err)
|
logger.Println(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
clientConfig.Timeout = c.Timeout
|
for _, server := range clientConfig.Servers {
|
||||||
|
nameserver := server + ":" + clientConfig.Port
|
||||||
domain_server := newSuffixTreeRoot()
|
r.servers = append(r.servers, nameserver)
|
||||||
r := &Resolver{clientConfig, domain_server}
|
|
||||||
|
|
||||||
if len(c.DomainServerFile) > 0 {
|
|
||||||
r.ReadDomainServerFile(c.DomainServerFile)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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]
|
domain := tokens[1]
|
||||||
ip := tokens[2]
|
ip := tokens[2]
|
||||||
if !isDomain(domain) || !isIP(ip) {
|
if !isDomain(domain) || !isIP(ip) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
r.domain_server.sinsert(strings.Split(domain, "."), ip)
|
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)
|
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
|
||||||
|
|
|
@ -25,8 +25,8 @@ type Settings struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResolvSettings struct {
|
type ResolvSettings struct {
|
||||||
|
ServerListFile string `toml:"server-list-file"`
|
||||||
ResolvFile string `toml:"resolv-file"`
|
ResolvFile string `toml:"resolv-file"`
|
||||||
DomainServerFile string `toml:"domain-server-file"`
|
|
||||||
Timeout int
|
Timeout int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue