resolver now can be compatible with /etc/resolv.conf
This commit is contained in:
parent
d9cce6332c
commit
1e24898e87
80
resolver.go
80
resolver.go
@ -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,62 +202,76 @@ 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 {
|
||||||
var ns NameServer
|
r.domain = ss[1]
|
||||||
switch len(ss) {
|
}
|
||||||
case 1:
|
case "search", "sortlist", "options": // we don't support these features in /etc/resolv.conf
|
||||||
ns.Addr = ss[0]
|
case "nameserver": // nameserver option, compatible with /etc/resolv.conf
|
||||||
case 2:
|
if len(ss) <= 1 {
|
||||||
ns.Addr = ss[0]
|
break
|
||||||
ns.Protocol = ss[1]
|
}
|
||||||
|
ss = ss[1:]
|
||||||
|
fallthrough
|
||||||
default:
|
default:
|
||||||
ns.Addr = ss[0]
|
var ns NameServer
|
||||||
ns.Protocol = ss[1]
|
switch len(ss) {
|
||||||
ns.Hostname = ss[2]
|
case 0:
|
||||||
|
break
|
||||||
|
case 1:
|
||||||
|
ns.Addr = ss[0]
|
||||||
|
case 2:
|
||||||
|
ns.Addr = ss[0]
|
||||||
|
ns.Protocol = ss[1]
|
||||||
|
default:
|
||||||
|
ns.Addr = ss[0]
|
||||||
|
ns.Protocol = ss[1]
|
||||||
|
ns.Hostname = ss[2]
|
||||||
|
}
|
||||||
|
nss = append(nss, ns)
|
||||||
}
|
}
|
||||||
nss = append(nss, ns)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user