2020-10-08 16:14:07 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-15 12:50:59 -05:00
|
|
|
"fmt"
|
2021-01-14 14:25:10 -05:00
|
|
|
"log"
|
2020-10-08 16:14:07 -04:00
|
|
|
"net"
|
2021-01-15 12:50:59 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-10-08 16:14:07 -04:00
|
|
|
"strings"
|
2021-01-13 16:30:04 -05:00
|
|
|
"time"
|
2020-10-08 16:14:07 -04:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2021-01-15 12:50:59 -05:00
|
|
|
var reqTypes map[uint16]string
|
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
var weekdays []string
|
|
|
|
|
2021-01-17 10:15:12 -05:00
|
|
|
type logItem struct {
|
|
|
|
clientIP string
|
|
|
|
name string
|
|
|
|
reqType uint16
|
|
|
|
config string
|
|
|
|
timetable string
|
|
|
|
killed string
|
|
|
|
}
|
|
|
|
|
|
|
|
// logChannel used by logging thread
|
|
|
|
var logChannel chan logItem
|
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
func init() {
|
2021-01-17 10:15:12 -05:00
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
weekdays = []string{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}
|
2021-01-15 12:50:59 -05:00
|
|
|
|
|
|
|
if len(ZabovDebugDBPath) > 0 {
|
|
|
|
os.MkdirAll(ZabovDebugDBPath, 0755)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqTypes = map[uint16]string{
|
|
|
|
dns.TypeNone: "TypeNone",
|
|
|
|
dns.TypeA: "TypeA",
|
|
|
|
dns.TypeNS: "TypeNS",
|
|
|
|
dns.TypeMD: "TypeMD",
|
|
|
|
dns.TypeMF: "TypeMF",
|
|
|
|
dns.TypeCNAME: "TypeCNAME",
|
|
|
|
dns.TypeSOA: "TypeSOA",
|
|
|
|
dns.TypeMB: "TypeMB",
|
|
|
|
dns.TypeMG: "TypeMG",
|
|
|
|
dns.TypeMR: "TypeMR",
|
|
|
|
dns.TypeNULL: "TypeNULL",
|
|
|
|
dns.TypePTR: "TypePTR",
|
|
|
|
dns.TypeHINFO: "TypeHINFO",
|
|
|
|
dns.TypeMINFO: "TypeMINFO",
|
|
|
|
dns.TypeMX: "TypeMX",
|
|
|
|
dns.TypeTXT: "TypeTXT",
|
|
|
|
dns.TypeRP: "TypeRP",
|
|
|
|
dns.TypeAFSDB: "TypeAFSDB",
|
|
|
|
dns.TypeX25: "TypeX25",
|
|
|
|
dns.TypeISDN: "TypeISDN",
|
|
|
|
dns.TypeRT: "TypeRT",
|
|
|
|
dns.TypeNSAPPTR: "TypeNSAPPTR",
|
|
|
|
dns.TypeSIG: "TypeSIG",
|
|
|
|
dns.TypeKEY: "TypeKEY",
|
|
|
|
dns.TypePX: "TypePX",
|
|
|
|
dns.TypeGPOS: "TypeGPOS",
|
|
|
|
dns.TypeAAAA: "TypeAAAA",
|
|
|
|
dns.TypeLOC: "TypeLOC",
|
|
|
|
dns.TypeNXT: "TypeNXT",
|
|
|
|
dns.TypeEID: "TypeEID",
|
|
|
|
dns.TypeNIMLOC: "TypeNIMLOC",
|
|
|
|
dns.TypeSRV: "TypeSRV",
|
|
|
|
dns.TypeATMA: "TypeATMA",
|
|
|
|
dns.TypeNAPTR: "TypeNAPTR",
|
|
|
|
dns.TypeKX: "TypeKX",
|
|
|
|
dns.TypeCERT: "TypeCERT",
|
|
|
|
dns.TypeDNAME: "TypeDNAME",
|
|
|
|
dns.TypeOPT: "TypeOPT",
|
|
|
|
dns.TypeAPL: "TypeAPL",
|
|
|
|
dns.TypeDS: "TypeDS",
|
|
|
|
dns.TypeSSHFP: "TypeSSHFP",
|
|
|
|
dns.TypeRRSIG: "TypeRRSIG",
|
|
|
|
dns.TypeNSEC: "TypeNSEC",
|
|
|
|
dns.TypeDNSKEY: "TypeDNSKEY",
|
|
|
|
dns.TypeDHCID: "TypeDHCID",
|
|
|
|
dns.TypeNSEC3: "TypeNSEC3",
|
|
|
|
dns.TypeNSEC3PARAM: "TypeNSEC3PARAM",
|
|
|
|
dns.TypeTLSA: "TypeTLSA",
|
|
|
|
dns.TypeSMIMEA: "TypeSMIMEA",
|
|
|
|
dns.TypeHIP: "TypeHIP",
|
|
|
|
dns.TypeNINFO: "TypeNINFO",
|
|
|
|
dns.TypeRKEY: "TypeRKEY",
|
|
|
|
dns.TypeTALINK: "TypeTALINK",
|
|
|
|
dns.TypeCDS: "TypeCDS",
|
|
|
|
dns.TypeCDNSKEY: "TypeCDNSKEY",
|
|
|
|
dns.TypeOPENPGPKEY: "TypeOPENPGPKEY",
|
|
|
|
dns.TypeCSYNC: "TypeCSYNC",
|
|
|
|
dns.TypeSPF: "TypeSPF",
|
|
|
|
dns.TypeUINFO: "TypeUINFO",
|
|
|
|
dns.TypeUID: "TypeUID",
|
|
|
|
dns.TypeGID: "TypeGID",
|
|
|
|
dns.TypeUNSPEC: "TypeUNSPEC",
|
|
|
|
dns.TypeNID: "TypeNID",
|
|
|
|
dns.TypeL32: "TypeL32",
|
|
|
|
dns.TypeL64: "TypeL64",
|
|
|
|
dns.TypeLP: "TypeLP",
|
|
|
|
dns.TypeEUI48: "TypeEUI48",
|
|
|
|
dns.TypeEUI64: "TypeEUI64",
|
|
|
|
dns.TypeURI: "TypeURI",
|
|
|
|
dns.TypeCAA: "TypeCAA",
|
|
|
|
dns.TypeAVC: "TypeAVC",
|
|
|
|
dns.TypeTKEY: "TypeTKEY",
|
|
|
|
dns.TypeTSIG: "TypeTSIG",
|
|
|
|
dns.TypeIXFR: "TypeIXFR",
|
|
|
|
dns.TypeAXFR: "TypeAXFR",
|
|
|
|
dns.TypeMAILB: "TypeMAILB",
|
|
|
|
dns.TypeMAILA: "TypeMAILA",
|
|
|
|
dns.TypeANY: "TypeANY",
|
|
|
|
dns.TypeTA: "TypeTA",
|
|
|
|
dns.TypeDLV: "TypeDLV",
|
|
|
|
dns.TypeReserved: "TypeReserved"}
|
2021-01-15 18:11:54 -05:00
|
|
|
|
|
|
|
fmt.Println("Local Time:", getLocalTime().Format(time.ANSIC))
|
2021-01-15 12:50:59 -05:00
|
|
|
|
|
|
|
if len(ZabovDebugDBPath) > 0 {
|
2021-01-17 10:15:12 -05:00
|
|
|
logChannel = make(chan logItem, 1024)
|
|
|
|
go logWriteThread()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func logWriteThread() {
|
|
|
|
for item := range logChannel {
|
2021-01-15 12:50:59 -05:00
|
|
|
var header string
|
|
|
|
d := time.Now().Format("2006-01-02")
|
2021-01-17 10:15:12 -05:00
|
|
|
logpath := path.Join(ZabovDebugDBPath, strings.Replace(item.clientIP, ":", "_", -1)+"-"+d+".log")
|
2021-01-15 12:50:59 -05:00
|
|
|
|
|
|
|
_, err1 := os.Stat(logpath)
|
|
|
|
if os.IsNotExist(err1) {
|
|
|
|
header = strings.Join([]string{"time", "clientIP", "name", "reqType", "config", "timetable", "killed"}, "\t")
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(logpath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err == nil {
|
2021-01-17 10:15:12 -05:00
|
|
|
reqTypeName, err := reqTypes[item.reqType]
|
2021-01-15 12:50:59 -05:00
|
|
|
if !err {
|
2021-01-17 10:15:12 -05:00
|
|
|
reqTypeName = fmt.Sprintf("%d", item.reqType)
|
2021-01-15 12:50:59 -05:00
|
|
|
}
|
|
|
|
ct := time.Now().Format(time.RFC3339)
|
2021-01-17 10:15:12 -05:00
|
|
|
log := strings.Join([]string{ct, item.clientIP, strings.TrimRight(item.name, "."), reqTypeName, item.config, item.timetable, item.killed}, "\t")
|
2021-01-15 12:50:59 -05:00
|
|
|
if len(header) > 0 {
|
|
|
|
f.Write([]byte(header))
|
|
|
|
f.Write([]byte("\n"))
|
|
|
|
}
|
|
|
|
f.Write([]byte(log))
|
|
|
|
f.Write([]byte("\n"))
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 16:30:04 -05:00
|
|
|
}
|
|
|
|
|
2021-01-17 10:15:12 -05:00
|
|
|
func logQuery(clientIP string, name string, reqType uint16, config string, timetable string, killed string) {
|
|
|
|
if len(ZabovDebugDBPath) > 0 {
|
|
|
|
k := logItem{clientIP: clientIP, name: name, reqType: reqType, config: config, timetable: timetable, killed: killed}
|
|
|
|
|
|
|
|
logChannel <- k
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:11:54 -05:00
|
|
|
func getLocalTime() time.Time {
|
|
|
|
return time.Now().Local()
|
2021-01-13 16:30:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func confFromTimeTable(timetable string) string {
|
|
|
|
tt := ZabovTimetables[timetable]
|
|
|
|
if tt == nil {
|
2021-01-14 14:25:10 -05:00
|
|
|
if ZabovDebug {
|
|
|
|
log.Println("confFromTimeTable: return default")
|
|
|
|
}
|
2021-01-13 16:30:04 -05:00
|
|
|
return "default"
|
|
|
|
}
|
|
|
|
for _, ttentry := range tt.table {
|
2021-01-15 18:11:54 -05:00
|
|
|
now := getLocalTime()
|
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
nowHour := now.Hour()
|
|
|
|
nowMinute := now.Minute()
|
|
|
|
weekday := weekdays[now.Weekday()]
|
|
|
|
if ttentry.days == nil || len(ttentry.days) == 0 || ttentry.days[weekday] || ttentry.days[strings.ToLower(weekday)] {
|
|
|
|
for _, t := range ttentry.times {
|
|
|
|
|
|
|
|
if (nowHour > t.start.hour || (nowHour == t.start.hour && nowMinute >= t.start.minute)) &&
|
|
|
|
(nowHour < t.stop.hour || (nowHour == t.stop.hour && nowMinute <= t.stop.minute)) {
|
2021-01-13 17:38:33 -05:00
|
|
|
go incrementStats("TIMETABLE IN: "+timetable, 1)
|
2021-01-14 14:25:10 -05:00
|
|
|
if ZabovDebug {
|
|
|
|
log.Println("confFromTimeTable: return IN", tt.cfgin)
|
|
|
|
}
|
2021-01-13 16:30:04 -05:00
|
|
|
return tt.cfgin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 17:38:33 -05:00
|
|
|
go incrementStats("TIMETABLE OUT: "+timetable, 1)
|
2021-01-14 14:25:10 -05:00
|
|
|
if ZabovDebug {
|
|
|
|
log.Println("confFromTimeTable: return OUT", tt.cfgout)
|
|
|
|
}
|
2021-01-13 16:30:04 -05:00
|
|
|
return tt.cfgout
|
|
|
|
}
|
|
|
|
|
2021-01-15 12:50:59 -05:00
|
|
|
func confFromIP(clientIP net.IP) (string, string) {
|
2021-01-13 16:30:04 -05:00
|
|
|
for _, ipgroup := range ZabovIPGroups {
|
|
|
|
for _, ip := range ipgroup.ips {
|
|
|
|
if clientIP.Equal(ip) {
|
|
|
|
if len(ipgroup.timetable) > 0 {
|
2021-01-15 12:50:59 -05:00
|
|
|
return confFromTimeTable(ipgroup.timetable), ipgroup.timetable
|
2021-01-13 16:30:04 -05:00
|
|
|
}
|
2021-01-14 14:25:10 -05:00
|
|
|
if ZabovDebug {
|
2021-01-16 12:54:31 -05:00
|
|
|
log.Println("confFromIP: ipgroup.cfg", ipgroup.cfg)
|
2021-01-14 14:25:10 -05:00
|
|
|
}
|
2021-01-15 12:50:59 -05:00
|
|
|
return ipgroup.cfg, ""
|
2021-01-13 16:30:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 12:54:31 -05:00
|
|
|
if len(ZabovDefaultTimetable) > 0 {
|
|
|
|
return confFromTimeTable(ZabovDefaultTimetable), ZabovDefaultTimetable
|
|
|
|
}
|
|
|
|
|
2021-01-14 14:25:10 -05:00
|
|
|
if ZabovDebug {
|
|
|
|
log.Println("confFromIP: return default")
|
|
|
|
}
|
2021-01-15 12:50:59 -05:00
|
|
|
return "default", ""
|
2021-01-13 16:30:04 -05:00
|
|
|
}
|
2020-10-08 16:14:07 -04:00
|
|
|
func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
|
|
|
|
go incrementStats("TotalQueries", 1)
|
|
|
|
|
|
|
|
remIP, _, e := net.SplitHostPort(w.RemoteAddr().String())
|
|
|
|
if e != nil {
|
2021-01-11 18:04:34 -05:00
|
|
|
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)
|
|
|
|
|
2021-01-15 12:50:59 -05:00
|
|
|
config, timetable := confFromIP(net.ParseIP(remIP))
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2021-01-14 14:25:10 -05:00
|
|
|
if ZabovDebug {
|
|
|
|
log.Println("REQUEST:", remIP, config)
|
|
|
|
}
|
2021-01-13 16:30:04 -05:00
|
|
|
ZabovConfig := ZabovConfigs[config]
|
2021-01-15 12:50:59 -05:00
|
|
|
QType := r.Question[0].Qtype
|
|
|
|
switch QType {
|
2020-10-08 16:14:07 -04:00
|
|
|
case dns.TypeA:
|
|
|
|
msg.Authoritative = true
|
|
|
|
domain := msg.Question[0].Name
|
|
|
|
fqdn := strings.TrimRight(domain, ".")
|
|
|
|
|
2021-01-14 14:25:10 -05:00
|
|
|
if ZabovDebug {
|
|
|
|
log.Println("TypeA: fqdn:", fqdn)
|
|
|
|
}
|
|
|
|
|
2021-01-13 17:38:33 -05:00
|
|
|
if len(ZabovIPAliases[fqdn]) > 0 {
|
|
|
|
config = "__aliases__"
|
|
|
|
msg.Answer = append(msg.Answer, &dns.A{
|
|
|
|
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
|
|
|
|
A: net.ParseIP(ZabovIPAliases[fqdn]),
|
|
|
|
})
|
2021-01-15 12:50:59 -05:00
|
|
|
go logQuery(remIP, fqdn, QType, config, timetable, "alias")
|
2021-01-13 17:38:33 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(ZabovLocalResponder) > 0 {
|
|
|
|
if !strings.Contains(fqdn, ".") ||
|
|
|
|
(len(ZabovLocalDomain) > 0 && strings.HasSuffix(fqdn, ZabovLocalDomain)) {
|
|
|
|
config = "__localresponder__"
|
|
|
|
ret := ForwardQuery(r, config, true)
|
|
|
|
w.WriteMsg(ret)
|
2021-01-15 12:50:59 -05:00
|
|
|
go logQuery(remIP, fqdn, QType, config, timetable, "localresponder")
|
2021-01-13 17:38:33 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-01-11 18:04:34 -05:00
|
|
|
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},
|
2021-01-13 16:30:04 -05:00
|
|
|
A: net.ParseIP(ZabovConfig.ZabovAddBL),
|
2020-10-08 16:14:07 -04:00
|
|
|
})
|
2021-01-15 12:50:59 -05:00
|
|
|
go logQuery(remIP, fqdn, QType, config, timetable, "killed")
|
2020-10-08 16:14:07 -04:00
|
|
|
} else {
|
2021-01-15 12:50:59 -05:00
|
|
|
go logQuery(remIP, fqdn, QType, config, timetable, "forwarded")
|
2021-01-13 17:38:33 -05:00
|
|
|
ret := ForwardQuery(r, config, false)
|
2020-10-08 16:14:07 -04:00
|
|
|
w.WriteMsg(ret)
|
|
|
|
}
|
2021-01-14 14:25:10 -05:00
|
|
|
case dns.TypePTR:
|
|
|
|
if ZabovDebug {
|
|
|
|
log.Println("TypePTR: Name:", msg.Question[0].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ZabovLocalResponder) > 0 {
|
|
|
|
// if set use local responder for reverse lookup (suffix ".in-addr.arpa.")
|
|
|
|
config = "__localresponder__"
|
|
|
|
}
|
|
|
|
ret := ForwardQuery(r, config, true)
|
|
|
|
w.WriteMsg(ret)
|
2021-01-15 12:50:59 -05:00
|
|
|
go logQuery(remIP, msg.Question[0].Name, QType, config, timetable, "localresponder")
|
2020-10-08 16:14:07 -04:00
|
|
|
default:
|
2021-01-13 17:38:33 -05:00
|
|
|
ret := ForwardQuery(r, config, false)
|
2020-10-08 16:14:07 -04:00
|
|
|
w.WriteMsg(ret)
|
2021-01-15 12:50:59 -05:00
|
|
|
if len(ZabovDebugDBPath) > 0 {
|
|
|
|
go logQuery(remIP, msg.Question[0].Name, QType, config, timetable, "forwarded")
|
|
|
|
}
|
2020-10-08 16:14:07 -04:00
|
|
|
}
|
2021-01-13 17:38:33 -05:00
|
|
|
go incrementStats("CONFIG: "+config, 1)
|
2020-10-08 16:14:07 -04:00
|
|
|
w.WriteMsg(&msg)
|
|
|
|
|
|
|
|
}
|