2020-10-08 22:14:07 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-13 22:30:04 +01:00
|
|
|
"fmt"
|
2020-10-08 22:14:07 +02:00
|
|
|
"net"
|
|
|
|
"strings"
|
2021-01-13 22:30:04 +01:00
|
|
|
"time"
|
2020-10-08 22:14:07 +02:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2021-01-13 22:30:04 +01: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 {
|
|
|
|
fmt.Println("confFromTimeTable: return default")
|
|
|
|
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 23:38:33 +01:00
|
|
|
go incrementStats("TIMETABLE IN: "+timetable, 1)
|
2021-01-13 22:30:04 +01:00
|
|
|
fmt.Println("confFromTimeTable: return IN", tt.cfgin)
|
|
|
|
return tt.cfgin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 23:38:33 +01:00
|
|
|
go incrementStats("TIMETABLE OUT: "+timetable, 1)
|
2021-01-13 22:30:04 +01:00
|
|
|
fmt.Println("confFromTimeTable: return OUT", tt.cfgout)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
fmt.Println("confFromIP: ipgroup.cfg")
|
|
|
|
return ipgroup.cfg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("confFromIP: return default")
|
|
|
|
return "default"
|
|
|
|
}
|
2020-10-08 22:14:07 +02: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-12 00:04:34 +01:00
|
|
|
go incrementStats("CLIENT ERROR: "+remIP, 1)
|
|
|
|
} else {
|
2020-10-08 22:14:07 +02:00
|
|
|
go incrementStats("CLIENT: "+remIP, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := dns.Msg{}
|
|
|
|
msg.SetReply(r)
|
|
|
|
|
2021-01-13 22:30:04 +01:00
|
|
|
config := confFromIP(net.ParseIP(remIP))
|
2021-01-12 00:04:34 +01:00
|
|
|
|
2021-01-13 22:30:04 +01:00
|
|
|
ZabovConfig := ZabovConfigs[config]
|
2020-10-08 22:14:07 +02:00
|
|
|
switch r.Question[0].Qtype {
|
|
|
|
case dns.TypeA:
|
|
|
|
msg.Authoritative = true
|
|
|
|
domain := msg.Question[0].Name
|
|
|
|
fqdn := strings.TrimRight(domain, ".")
|
|
|
|
|
2021-01-13 23:38:33 +01: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-12 00:04:34 +01:00
|
|
|
if domainInKillfile(fqdn, config) {
|
2020-10-08 22:14:07 +02: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 22:30:04 +01:00
|
|
|
A: net.ParseIP(ZabovConfig.ZabovAddBL),
|
2020-10-08 22:14:07 +02:00
|
|
|
})
|
|
|
|
} else {
|
2021-01-13 23:38:33 +01:00
|
|
|
ret := ForwardQuery(r, config, false)
|
2020-10-08 22:14:07 +02:00
|
|
|
w.WriteMsg(ret)
|
|
|
|
}
|
|
|
|
default:
|
2021-01-13 23:38:33 +01:00
|
|
|
ret := ForwardQuery(r, config, false)
|
2020-10-08 22:14:07 +02:00
|
|
|
w.WriteMsg(ret)
|
|
|
|
}
|
2021-01-13 23:38:33 +01:00
|
|
|
go incrementStats("CONFIG: "+config, 1)
|
2020-10-08 22:14:07 +02:00
|
|
|
w.WriteMsg(&msg)
|
|
|
|
|
|
|
|
}
|