- sanity checks
  - parsing of timetables
- removed unused global variables (moved at config level)
- added new stat: DNS request for each "CONFIG"
- implemented configuration from ip groups / timetables
pull/1/head
bloved 2021-01-13 22:30:04 +01:00
parent 5e94032cd0
commit e86ead83b7
7 changed files with 148 additions and 73 deletions

View File

@ -5,7 +5,9 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"os" "os"
"strconv"
"strings" "strings"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -46,13 +48,15 @@ func init() {
configs := MyConf["configs"].(map[string]interface{}) configs := MyConf["configs"].(map[string]interface{})
defaultConf := configs["default"].(map[string]interface{}) if len(configs) == 0 {
log.Println("you shall set at least default config")
os.Exit(1)
}
ZabovUpDNS = defaultConf["upstream"].(string) if configs["default"] == nil {
ZabovSingleBL = defaultConf["singlefilters"].(string) log.Println("default config is required")
ZabovDoubleBL = defaultConf["doublefilters"].(string) os.Exit(1)
ZabovAddBL = defaultConf["blackholeip"].(string) }
ZabovHostsFile = defaultConf["hostsfile"].(string)
zabovString := ZabovAddr + ":" + ZabovPort zabovString := ZabovAddr + ":" + ZabovPort
@ -60,13 +64,11 @@ func init() {
MyDNS.Addr = zabovString MyDNS.Addr = zabovString
MyDNS.Net = ZabovType MyDNS.Net = ZabovType
ZabovDNSArray = fileByLines(ZabovUpDNS)
ZabovConfigs = map[string]ZabovConfig{} ZabovConfigs = map[string]ZabovConfig{}
ZabovIPGroups = []ZabovIPGroup{} ZabovIPGroups = []ZabovIPGroup{}
ZabovTimetables = map[string]ZabovTimetable{} ZabovTimetables = map[string]*ZabovTimetable{}
ZabovIPAliases = map[string]string{} ZabovIPAliases = map[string]string{}
ZabovDNSArrays = map[string][]string{}
IPAliasesRaw := MyConf["ipaliases"].(map[string]interface{}) IPAliasesRaw := MyConf["ipaliases"].(map[string]interface{})
for alias, ip := range IPAliasesRaw { for alias, ip := range IPAliasesRaw {
@ -84,11 +86,8 @@ func init() {
conf.ZabovAddBL = confRaw["blackholeip"].(string) conf.ZabovAddBL = confRaw["blackholeip"].(string)
conf.ZabovHostsFile = confRaw["hostsfile"].(string) conf.ZabovHostsFile = confRaw["hostsfile"].(string)
ZabovDNSArrays[name] = fileByLines(conf.ZabovUpDNS) conf.ZabovDNSArray = fileByLines(conf.ZabovUpDNS)
ZabovConfigs[name] = conf ZabovConfigs[name] = conf
if name == "default" {
ZabovConfigDefault = conf
}
ZabovCreateKDB(name) ZabovCreateKDB(name)
} }
@ -111,13 +110,13 @@ func init() {
_, ok := ZabovConfigs[timetable.cfgin] _, ok := ZabovConfigs[timetable.cfgin]
if !ok { if !ok {
log.Println("inexistent cfgin:", timetable.cfgin) log.Println("timetable: inexistent cfgin:", timetable.cfgin)
os.Exit(1) os.Exit(1)
} }
_, ok = ZabovConfigs[timetable.cfgout] _, ok = ZabovConfigs[timetable.cfgout]
if !ok { if !ok {
log.Println("inexistent cfgout:", timetable.cfgout) log.Println("timetable: inexistent cfgout:", timetable.cfgout)
os.Exit(1) os.Exit(1)
} }
@ -126,11 +125,36 @@ func init() {
for i := range tables { for i := range tables {
table := tables[i].(map[string]interface{}) table := tables[i].(map[string]interface{})
var ttEntry ZabovTimetableEntry var ttEntry ZabovTimetableEntry
ttEntry.times = strings.Split(table["times"].(string), ";") ttEntry.times = []*ZabovTimeRange{}
ttEntry.days = strings.Split(table["days"].(string), ";") for _, tRaw := range strings.Split(table["times"].(string), ";") {
timetable.table = append(timetable.table, ttEntry) tRawArr := strings.Split(tRaw, "-")
if len(tRawArr) > 1 {
startArr := strings.Split(tRawArr[0], ":")
stopArr := strings.Split(tRawArr[1], ":")
if len(startArr) > 1 && len(stopArr) > 1 {
hourStart, _ := strconv.Atoi(startArr[0])
minuteStart, _ := strconv.Atoi(startArr[1])
start := ZabovTime{hour: hourStart, minute: minuteStart}
hourStop, _ := strconv.Atoi(stopArr[0])
minuteStop, _ := strconv.Atoi(stopArr[1])
stop := ZabovTime{hour: hourStop, minute: minuteStop}
t := ZabovTimeRange{start: start, stop: stop}
ttEntry.times = append(ttEntry.times, &t)
}
}
}
ttEntry.days = map[string]bool{}
for _, day := range strings.Split(table["days"].(string), ";") {
ttEntry.days[day] = true
}
timetable.table = append(timetable.table, &ttEntry)
} }
ZabovTimetables[name] = timetable ZabovTimetables[name] = &timetable
} }
IPGroups := MyConf["ipgroups"].([]interface{}) IPGroups := MyConf["ipgroups"].([]interface{})
@ -141,15 +165,16 @@ func init() {
var groupStruct ZabovIPGroup var groupStruct ZabovIPGroup
groupMap := IPGroups[i].(map[string]interface{}) groupMap := IPGroups[i].(map[string]interface{})
IPsRaw := groupMap["ips"].([]interface{}) IPsRaw := groupMap["ips"].([]interface{})
groupStruct.ips = []string{} groupStruct.ips = []net.IP{}
for x := range IPsRaw { for x := range IPsRaw {
ip := IPsRaw[x].(string) ipRaw := IPsRaw[x].(string)
fmt.Println("adding IP ", ip) ip := net.ParseIP(ipRaw)
fmt.Println("adding IP ", ipRaw)
alias, ok := ZabovIPAliases[ip] alias, ok := ZabovIPAliases[ipRaw]
if ok { if ok {
fmt.Println("IP alias: ", ip, alias) fmt.Println("IP alias: ", ipRaw, alias)
ip = alias ip = net.ParseIP(alias)
} }
groupStruct.ips = append(groupStruct.ips, ip) groupStruct.ips = append(groupStruct.ips, ip)
} }
@ -164,9 +189,9 @@ func init() {
} }
ZabovIPGroups = append(ZabovIPGroups, groupStruct) ZabovIPGroups = append(ZabovIPGroups, groupStruct)
} }
fmt.Println("ZabovConfigs:", ZabovConfigs) //fmt.Println("ZabovConfigs:", ZabovConfigs)
fmt.Println("ZabovTimetables:", ZabovTimetables) //fmt.Println("ZabovTimetables:", ZabovTimetables)
fmt.Println("ZabovIPAliases:", ZabovIPAliases) //fmt.Println("ZabovIPAliases:", ZabovIPAliases)
fmt.Println("ZabovIPGroups:", ZabovIPGroups) //fmt.Println("ZabovIPGroups:", ZabovIPGroups)
} }

View File

@ -89,6 +89,7 @@ func getDoubleFilters(urls urlsMap) {
for url, configs := range urls { for url, configs := range urls {
DoubleIndexFilter(url, configs) DoubleIndexFilter(url, configs)
} }
fmt.Println("getDoubleFilters: DONE!")
} }

View File

@ -87,6 +87,7 @@ func getSingleFilters(urls urlsMap) {
for url, configs := range urls { for url, configs := range urls {
SingleIndexFilter(url, configs) SingleIndexFilter(url, configs)
} }
fmt.Println("getSingleFilters: DONE!")
} }

View File

@ -7,18 +7,19 @@
"killfilettl": 12 "killfilettl": 12
}, },
"ipaliases":{ "ipaliases":{
"pc8":"192.168.178.29" "pc8":"192.168.178.29",
"localhost":"127.0.0.1"
}, },
"ipgroups":[ "ipgroups":[
{ {
"ips":["192.168.178.30/32", "192.168.178.31/32", "pc8"], "ips":["localhost", "::1", "192.168.178.30", "192.168.178.31", "pc8"],
"cfg":"", "cfg":"",
"timetable":"tt_children" "timetable":"tt_children"
} }
], ],
"timetables":{ "timetables":{
"tt_children":{ "tt_children":{
"tables":[{"times":"8:30-12:30;18:30-22:30", "days":"Mo;Tu;We;Th;Fr;Sa;Su"}], "tables":[{"times":"00:00-05:00;8:30-12:30;18:30-22:59", "days":"Mo;Tu;We;Th;Fr;Sa;Su"}],
"cfgin":"children_restricted", "cfgin":"children_restricted",
"cfgout":"children" "cfgout":"children"
} }

View File

@ -83,8 +83,7 @@ func oneTimeDNS(config string) (dns string) {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
ZabovDNSArray := ZabovDNSArrays[config] upl := ZabovConfigs[config].ZabovDNSArray
upl := ZabovDNSArray
if len(upl) < 1 { if len(upl) < 1 {
fmt.Println("No DNS defined, using default 127.0.0.53:53. Hope it works!") fmt.Println("No DNS defined, using default 127.0.0.53:53. Hope it works!")

View File

@ -1,12 +1,68 @@
package main package main
import ( import (
"fmt"
"net" "net"
"strings" "strings"
"time"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
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)) {
incrementStats("TIMETABLE IN: "+timetable, 1)
fmt.Println("confFromTimeTable: return IN", tt.cfgin)
return tt.cfgin
}
}
}
}
incrementStats("TIMETABLE OUT: "+timetable, 1)
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"
}
func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
go incrementStats("TotalQueries", 1) go incrementStats("TotalQueries", 1)
@ -20,8 +76,10 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
msg := dns.Msg{} msg := dns.Msg{}
msg.SetReply(r) msg.SetReply(r)
config := "default" // TODO: get config from client IP & timetable config := confFromIP(net.ParseIP(remIP))
incrementStats("CONFIG: "+config, 1)
ZabovConfig := ZabovConfigs[config]
switch r.Question[0].Qtype { switch r.Question[0].Qtype {
case dns.TypeA: case dns.TypeA:
msg.Authoritative = true msg.Authoritative = true
@ -33,7 +91,7 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
msg.Answer = append(msg.Answer, &dns.A{ msg.Answer = append(msg.Answer, &dns.A{
Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60}, Hdr: dns.RR_Header{Name: domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
A: net.ParseIP(ZabovAddBL), A: net.ParseIP(ZabovConfig.ZabovAddBL),
}) })
} else { } else {
ret := ForwardQuery(r, config) ret := ForwardQuery(r, config)

62
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"log" "log"
"net"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -9,53 +10,30 @@ import (
//MyDNS is my dns server //MyDNS is my dns server
var MyDNS *dns.Server var MyDNS *dns.Server
//ZabovUpDNS keeps the name of upstream DNSs //ZabovCacheTTL is the amount of hours we cache records of DNS (global)
var ZabovUpDNS string
//ZabovSingleBL list of urls returning a file with just names of domains
var ZabovSingleBL string
//ZabovDoubleBL list of urls returning a file with IP<space>domain
var ZabovDoubleBL string
//ZabovAddBL is the IP we want to send all the clients to. Usually is 127.0.0.1
var ZabovAddBL string
//ZabovCacheTTL is the amount of hours we cache records of DNS
var ZabovCacheTTL int var ZabovCacheTTL int
//ZabovKillTTL is the amount of hours we cache the killfile //ZabovKillTTL is the amount of hours we cache the killfile (global)
var ZabovKillTTL int var ZabovKillTTL int
//ZabovHostsFile is the file we use to keep our hosts
var ZabovHostsFile string
//ZabovDNSArray is the array containing all the DNS we mention
var ZabovDNSArray []string
type handler struct{} type handler struct{}
//ZabovDNSArrays contains the arrays containing all the DNS we mention
var ZabovDNSArrays map[string][]string
// ZabovConfig contains all Zabov configs // ZabovConfig contains all Zabov configs
type ZabovConfig struct { type ZabovConfig struct {
ZabovUpDNS string // json:upstream -> ZabovDNSArray ZabovSingleBL string // json:singlefilters -> ZabovSingleBL list of urls returning a file with just names of domains
ZabovSingleBL string // json:singlefilters ZabovDoubleBL string // json:doublefilters -> ZabovDoubleBL list of urls returning a file with IP<space>domain
ZabovDoubleBL string // json:doublefilters ZabovAddBL string // json:blackholeip -> ZabovAddBL is the IP we want to send all the clients to. Usually is 127.0.0.1
ZabovAddBL string // json:blackholeip ZabovHostsFile string // json:hostsfile -> ZabovHostsFile is the file we use to keep our hosts
ZabovHostsFile string // json:hostsfile ZabovUpDNS string // json:upstream -> ZabovUpDNS keeps the name of upstream DNSs
ZabovDNSArray []string // contains all the DNS we mention, parsed from ZabovUpDNS file
} }
// ZabovConfigs contains all Zabov configs // ZabovConfigs contains all Zabov configs
var ZabovConfigs map[string]ZabovConfig var ZabovConfigs map[string]ZabovConfig
// ZabovConfigDefault contains only "default" config
var ZabovConfigDefault ZabovConfig
// ZabovIPGroup contains Zabov groups of IPs // ZabovIPGroup contains Zabov groups of IPs
type ZabovIPGroup struct { type ZabovIPGroup struct {
ips []string // IPs in this group ips []net.IP // IPs in this group
cfg string // config name to be used if there is no timetable cfg string // config name to be used if there is no timetable
timetable string // timetable name to be used for this group; timetable SHALL reference to config name to use timetable string // timetable name to be used for this group; timetable SHALL reference to config name to use
} }
@ -63,21 +41,33 @@ type ZabovIPGroup struct {
// ZabovIPGroups contains an array of all Zabov groups of IP rules // ZabovIPGroups contains an array of all Zabov groups of IP rules
var ZabovIPGroups []ZabovIPGroup var ZabovIPGroups []ZabovIPGroup
// ZabovTime contains Zabov single time
type ZabovTime struct {
hour int
minute int
}
// ZabovTimeRange contains Zabov single time range
type ZabovTimeRange struct {
start ZabovTime
stop ZabovTime
}
// ZabovTimetableEntry contains Zabov single time table entry // ZabovTimetableEntry contains Zabov single time table entry
type ZabovTimetableEntry struct { type ZabovTimetableEntry struct {
times []string times []*ZabovTimeRange
days []string days map[string]bool
} }
// ZabovTimetable contains a Zabov time table // ZabovTimetable contains a Zabov time table
type ZabovTimetable struct { type ZabovTimetable struct {
table []ZabovTimetableEntry table []*ZabovTimetableEntry
cfgin string // configuration name to be used if "inside" timetable cfgin string // configuration name to be used if "inside" timetable
cfgout string // configuration name to be used if "outiside" timetable cfgout string // configuration name to be used if "outiside" timetable
} }
// ZabovTimetables contains all Zabov time tables, by name // ZabovTimetables contains all Zabov time tables, by name
var ZabovTimetables map[string]ZabovTimetable var ZabovTimetables map[string]*ZabovTimetable
// ZabovIPAliases contains an array of all Zabov IP aliases // ZabovIPAliases contains an array of all Zabov IP aliases
var ZabovIPAliases map[string]string var ZabovIPAliases map[string]string