2020-10-08 16:14:07 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strings"
|
2021-01-13 16:30:04 -05:00
|
|
|
"time"
|
2020-10-08 16:14:07 -04:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
var weekdays []string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
weekdays = []string{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCurTime() (time.Time, error) {
|
|
|
|
return time.Parse("15:04", time.Now().Format("15:04"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func confFromTimeTable(timetable string) string {
|
|
|
|
tt := ZabovTimetables[timetable]
|
|
|
|
if tt == nil {
|
2021-01-14 11:15:27 -05:00
|
|
|
//fmt.Println("confFromTimeTable: return default")
|
2021-01-13 16:30:04 -05:00
|
|
|
return "default"
|
|
|
|
}
|
|
|
|
for _, ttentry := range tt.table {
|
|
|
|
now := time.Now()
|
|
|
|
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 11:15:27 -05:00
|
|
|
//fmt.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 11:15:27 -05:00
|
|
|
//fmt.Println("confFromTimeTable: return OUT", tt.cfgout)
|
2021-01-13 16:30:04 -05:00
|
|
|
return tt.cfgout
|
|
|
|
}
|
|
|
|
|
|
|
|
func confFromIP(clientIP net.IP) string {
|
|
|
|
|
|
|
|
for _, ipgroup := range ZabovIPGroups {
|
|
|
|
for _, ip := range ipgroup.ips {
|
|
|
|
if clientIP.Equal(ip) {
|
|
|
|
if len(ipgroup.timetable) > 0 {
|
|
|
|
return confFromTimeTable(ipgroup.timetable)
|
|
|
|
}
|
2021-01-14 11:15:27 -05:00
|
|
|
//fmt.Println("confFromIP: ipgroup.cfg")
|
2021-01-13 16:30:04 -05:00
|
|
|
return ipgroup.cfg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 11:15:27 -05:00
|
|
|
//fmt.Println("confFromIP: return default")
|
2021-01-13 16:30:04 -05:00
|
|
|
return "default"
|
|
|
|
}
|
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-13 16:30:04 -05:00
|
|
|
config := confFromIP(net.ParseIP(remIP))
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
ZabovConfig := ZabovConfigs[config]
|
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, ".")
|
|
|
|
|
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]),
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
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
|
|
|
})
|
|
|
|
} else {
|
2021-01-13 17:38:33 -05:00
|
|
|
ret := ForwardQuery(r, config, false)
|
2020-10-08 16:14:07 -04:00
|
|
|
w.WriteMsg(ret)
|
|
|
|
}
|
|
|
|
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-13 17:38:33 -05:00
|
|
|
go incrementStats("CONFIG: "+config, 1)
|
2020-10-08 16:14:07 -04:00
|
|
|
w.WriteMsg(&msg)
|
|
|
|
|
|
|
|
}
|