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