zabov/dns_handler.go

49 lines
1002 B
Go
Raw Normal View History

2020-10-08 16:14:07 -04:00
package main
import (
"net"
"strings"
"github.com/miekg/dns"
)
func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
go incrementStats("TotalQueries", 1)
remIP, _, e := net.SplitHostPort(w.RemoteAddr().String())
if e != nil {
go incrementStats("CLIENT ERROR: "+remIP, 1)
} else {
2020-10-08 16:14:07 -04:00
go incrementStats("CLIENT: "+remIP, 1)
}
msg := dns.Msg{}
msg.SetReply(r)
config := "default" // TODO: get config from client IP & timetable
2020-10-08 16:14:07 -04:00
switch r.Question[0].Qtype {
case dns.TypeA:
msg.Authoritative = true
domain := msg.Question[0].Name
fqdn := strings.TrimRight(domain, ".")
if domainInKillfile(fqdn, config) {
2020-10-08 16:14:07 -04:00
go incrementStats("Killed", 1)
msg.Answer = append(msg.Answer, &dns.A{
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
A: net.ParseIP(ZabovAddBL),
})
} else {
ret := ForwardQuery(r, config)
2020-10-08 16:14:07 -04:00
w.WriteMsg(ret)
}
default:
ret := ForwardQuery(r, config)
2020-10-08 16:14:07 -04:00
w.WriteMsg(ret)
}
w.WriteMsg(&msg)
}