resolver now can be compatible with /etc/resolv.conf

This commit is contained in:
ginuerzh 2018-11-12 20:20:41 +08:00
parent d9cce6332c
commit 1e24898e87

View File

@ -67,6 +67,7 @@ type resolver struct {
Timeout time.Duration Timeout time.Duration
TTL time.Duration TTL time.Duration
period time.Duration period time.Duration
domain string
} }
// NewResolver create a new Resolver with the given name servers and resolution timeout. // NewResolver create a new Resolver with the given name servers and resolution timeout.
@ -99,6 +100,9 @@ func (r *resolver) Resolve(host string) (ips []net.IP, err error) {
return []net.IP{ip}, nil return []net.IP{ip}, nil
} }
if !strings.Contains(host, ".") && r.domain != "" {
host = host + "." + r.domain
}
ips = r.loadCache(host) ips = r.loadCache(host)
if len(ips) > 0 { if len(ips) > 0 {
if Debug { if Debug {
@ -198,50 +202,62 @@ func (r *resolver) storeCache(name string, ips []net.IP) {
func (r *resolver) Reload(rd io.Reader) error { func (r *resolver) Reload(rd io.Reader) error {
var nss []NameServer var nss []NameServer
scanner := bufio.NewScanner(rd) split := func(line string) []string {
for scanner.Scan() { if line == "" {
line := scanner.Text() return nil
}
if n := strings.IndexByte(line, '#'); n >= 0 { if n := strings.IndexByte(line, '#'); n >= 0 {
line = line[:n] line = line[:n]
} }
line = strings.Replace(line, "\t", " ", -1) line = strings.Replace(line, "\t", " ", -1)
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if line == "" {
continue
}
var ss []string var ss []string
for _, s := range strings.Split(line, " ") { for _, s := range strings.Split(line, " ") {
if s = strings.TrimSpace(s); s != "" { if s = strings.TrimSpace(s); s != "" {
ss = append(ss, s) ss = append(ss, s)
} }
} }
return ss
}
scanner := bufio.NewScanner(rd)
for scanner.Scan() {
line := scanner.Text()
ss := split(line)
if len(ss) == 0 { if len(ss) == 0 {
continue continue
} }
if len(ss) >= 2 { switch ss[0] {
// timeout option case "timeout": // timeout option
if strings.ToLower(ss[0]) == "timeout" { if len(ss) > 1 {
r.Timeout, _ = time.ParseDuration(ss[1]) r.Timeout, _ = time.ParseDuration(ss[1])
continue
} }
case "ttl": // ttl option
// ttl option if len(ss) > 1 {
if strings.ToLower(ss[0]) == "ttl" {
r.TTL, _ = time.ParseDuration(ss[1]) r.TTL, _ = time.ParseDuration(ss[1])
continue
} }
case "reload": // reload option
// reload option if len(ss) > 1 {
if strings.ToLower(ss[0]) == "reload" {
r.period, _ = time.ParseDuration(ss[1]) r.period, _ = time.ParseDuration(ss[1])
continue
} }
case "domain":
if len(ss) > 1 {
r.domain = ss[1]
} }
case "search", "sortlist", "options": // we don't support these features in /etc/resolv.conf
case "nameserver": // nameserver option, compatible with /etc/resolv.conf
if len(ss) <= 1 {
break
}
ss = ss[1:]
fallthrough
default:
var ns NameServer var ns NameServer
switch len(ss) { switch len(ss) {
case 0:
break
case 1: case 1:
ns.Addr = ss[0] ns.Addr = ss[0]
case 2: case 2:
@ -254,6 +270,8 @@ func (r *resolver) Reload(rd io.Reader) error {
} }
nss = append(nss, ns) nss = append(nss, ns)
} }
}
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
return err return err
} }