Adaptive with IPV6 in hosts records.

This commit is contained in:
kenshinx 2015-02-12 14:54:02 +08:00
parent 81450a3983
commit 8e504da15a
2 changed files with 19 additions and 12 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"net"
"sync" "sync"
"time" "time"
@ -87,8 +86,7 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
// Query hosts // Query hosts
if settings.Hosts.Enable && IPQuery > 0 { if settings.Hosts.Enable && IPQuery > 0 {
if sip, ok := h.hosts.Get(Q.qname); ok { if ip, ok := h.hosts.Get(Q.qname, IPQuery); ok {
var ip net.IP
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(req) m.SetReply(req)
@ -100,7 +98,6 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
Class: dns.ClassINET, Class: dns.ClassINET,
Ttl: settings.Hosts.TTL, Ttl: settings.Hosts.TTL,
} }
ip = net.ParseIP(sip).To4()
a := &dns.A{rr_header, ip} a := &dns.A{rr_header, ip}
m.Answer = append(m.Answer, a) m.Answer = append(m.Answer, a)
case _IP6Query: case _IP6Query:
@ -110,7 +107,6 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
Class: dns.ClassINET, Class: dns.ClassINET,
Ttl: settings.Hosts.TTL, Ttl: settings.Hosts.TTL,
} }
ip = net.ParseIP(sip).To16()
aaaa := &dns.AAAA{rr_header, ip} aaaa := &dns.AAAA{rr_header, ip}
m.Answer = append(m.Answer, aaaa) m.Answer = append(m.Answer, aaaa)
} }

View File

@ -36,18 +36,29 @@ func NewHosts(hs HostsSettings, rs RedisSettings) Hosts {
2. Fetch hosts records from /etc/hosts file and redis per minute 2. Fetch hosts records from /etc/hosts file and redis per minute
*/ */
func (h *Hosts) Get(domain string) (ip string, ok bool) { func (h *Hosts) Get(domain string, family int) (ip net.IP, ok bool) {
if ip, ok = h.fileHosts.Get(domain); ok { var sip string
return
if sip, ok = h.fileHosts.Get(domain); !ok {
if h.redisHosts != nil {
sip, ok = h.redisHosts.Get(domain)
}
} }
if h.redisHosts != nil { if sip == "" {
ip, ok = h.redisHosts.Get(domain) return nil, false
return
} }
return ip, false switch family {
case _IP4Query:
ip = net.ParseIP(sip).To4()
case _IP6Query:
ip = net.ParseIP(sip).To16()
default:
return nil, false
}
return ip, (ip != nil)
} }
func (h *Hosts) refresh() { func (h *Hosts) refresh() {