2020-10-08 16:14:07 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2021-01-13 16:30:04 -05:00
|
|
|
"net"
|
2020-10-08 16:14:07 -04:00
|
|
|
"os"
|
2021-01-13 16:30:04 -05:00
|
|
|
"strconv"
|
2021-01-11 18:04:34 -05:00
|
|
|
"strings"
|
2020-10-08 16:14:07 -04:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2021-01-11 18:04:34 -05:00
|
|
|
type stringarray []string
|
|
|
|
type urlsMap map[string]stringarray
|
2020-10-08 16:14:07 -04:00
|
|
|
|
2021-01-11 18:04:34 -05:00
|
|
|
func init() {
|
|
|
|
var MyConfRaw interface{}
|
2020-10-08 16:14:07 -04:00
|
|
|
|
|
|
|
file, err := ioutil.ReadFile("config.json")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Cannot open config file", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-01-11 18:04:34 -05:00
|
|
|
err = json.Unmarshal([]byte(file), &MyConfRaw)
|
2020-10-08 16:14:07 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2021-01-11 18:04:34 -05:00
|
|
|
log.Println("Cannot unmarshal json: ", err.Error())
|
2020-10-08 16:14:07 -04:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we read configuration file
|
|
|
|
fmt.Println("Reading configuration file...")
|
|
|
|
|
2021-01-11 18:04:34 -05:00
|
|
|
MyConf := MyConfRaw.(map[string]interface{})
|
|
|
|
|
2021-01-16 17:26:29 -05:00
|
|
|
//******************************
|
|
|
|
// zabov section (global config)
|
|
|
|
//******************************
|
2021-01-11 18:04:34 -05:00
|
|
|
zabov := MyConf["zabov"].(map[string]interface{})
|
|
|
|
|
|
|
|
ZabovPort := zabov["port"].(string)
|
|
|
|
ZabovType := zabov["proto"].(string)
|
|
|
|
ZabovAddr := zabov["ipaddr"].(string)
|
2021-01-15 12:50:59 -05:00
|
|
|
|
2021-01-11 18:04:34 -05:00
|
|
|
ZabovCacheTTL = int(zabov["cachettl"].(float64))
|
|
|
|
ZabovKillTTL = int(zabov["killfilettl"].(float64))
|
|
|
|
|
2021-01-15 12:50:59 -05:00
|
|
|
if zabov["debug"] != nil {
|
|
|
|
ZabovDebug = zabov["debug"].(string) == "true"
|
|
|
|
}
|
|
|
|
if zabov["debugdbpath"] != nil {
|
|
|
|
ZabovDebugDBPath = (zabov["debugdbpath"].(string))
|
|
|
|
}
|
2021-01-14 14:25:10 -05:00
|
|
|
|
2021-01-13 17:38:33 -05:00
|
|
|
if MyConf["configs"] == nil {
|
|
|
|
log.Println("configs not set: you shall set at least 'default' config")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-01-11 18:04:34 -05:00
|
|
|
configs := MyConf["configs"].(map[string]interface{})
|
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
if len(configs) == 0 {
|
2021-01-13 17:38:33 -05:00
|
|
|
log.Println("you shall set at least 'default' config")
|
2021-01-13 16:30:04 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
if configs["default"] == nil {
|
2021-01-13 17:38:33 -05:00
|
|
|
log.Println("'default' config is required")
|
2021-01-13 16:30:04 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-10-08 16:14:07 -04:00
|
|
|
|
|
|
|
zabovString := ZabovAddr + ":" + ZabovPort
|
|
|
|
|
|
|
|
MyDNS = new(dns.Server)
|
|
|
|
MyDNS.Addr = zabovString
|
|
|
|
MyDNS.Net = ZabovType
|
|
|
|
|
2021-01-14 09:38:46 -05:00
|
|
|
ZabovConfigs = map[string]*ZabovConfig{}
|
2021-01-11 18:04:34 -05:00
|
|
|
ZabovIPGroups = []ZabovIPGroup{}
|
2021-01-13 16:30:04 -05:00
|
|
|
ZabovTimetables = map[string]*ZabovTimetable{}
|
2021-01-11 18:04:34 -05:00
|
|
|
ZabovIPAliases = map[string]string{}
|
2021-01-13 16:30:04 -05:00
|
|
|
|
2021-01-16 17:26:29 -05:00
|
|
|
//*******************
|
|
|
|
// IP aliases section
|
|
|
|
//*******************
|
2021-01-17 09:28:20 -05:00
|
|
|
if MyConf["ipaliases"] != nil {
|
|
|
|
IPAliasesRaw := MyConf["ipaliases"].(map[string]interface{})
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2021-01-17 09:28:20 -05:00
|
|
|
for alias, ip := range IPAliasesRaw {
|
|
|
|
fmt.Println("IP Alias:", alias, ip)
|
|
|
|
ZabovIPAliases[alias] = ip.(string)
|
|
|
|
}
|
2021-01-11 18:04:34 -05:00
|
|
|
}
|
|
|
|
|
2021-01-16 17:26:29 -05:00
|
|
|
//****************
|
|
|
|
// configs section
|
|
|
|
//****************
|
2021-01-11 18:04:34 -05:00
|
|
|
for name, v := range configs {
|
|
|
|
fmt.Println("evaluaing config name:", name)
|
|
|
|
confRaw := v.(map[string]interface{})
|
|
|
|
var conf ZabovConfig
|
|
|
|
conf.ZabovUpDNS = confRaw["upstream"].(string)
|
|
|
|
conf.ZabovSingleBL = confRaw["singlefilters"].(string)
|
|
|
|
conf.ZabovDoubleBL = confRaw["doublefilters"].(string)
|
|
|
|
conf.ZabovAddBL = confRaw["blackholeip"].(string)
|
|
|
|
conf.ZabovHostsFile = confRaw["hostsfile"].(string)
|
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
conf.ZabovDNSArray = fileByLines(conf.ZabovUpDNS)
|
2021-01-14 09:38:46 -05:00
|
|
|
ZabovConfigs[name] = &conf
|
2021-01-16 17:26:29 -05:00
|
|
|
|
2021-01-11 18:04:34 -05:00
|
|
|
}
|
|
|
|
|
2021-01-16 17:26:29 -05:00
|
|
|
// default config is mandatory
|
2021-01-14 09:38:46 -05:00
|
|
|
ZabovConfigs["default"].references++
|
|
|
|
|
2021-01-16 17:26:29 -05:00
|
|
|
//*******************
|
|
|
|
// timetables section
|
|
|
|
//*******************
|
2021-01-17 09:28:20 -05:00
|
|
|
if MyConf["timetables"] != nil {
|
|
|
|
timetables := MyConf["timetables"].(map[string]interface{})
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2021-01-17 09:28:20 -05:00
|
|
|
for name, v := range timetables {
|
|
|
|
fmt.Println("evaluaing timetable name:", name)
|
|
|
|
timetableRaw := v.(map[string]interface{})
|
|
|
|
var timetable ZabovTimetable
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2021-01-17 09:28:20 -05:00
|
|
|
timetable.cfgin = timetableRaw["cfgin"].(string)
|
|
|
|
timetable.cfgout = timetableRaw["cfgout"].(string)
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2021-01-17 09:28:20 -05:00
|
|
|
if timetable.cfgin == "" {
|
|
|
|
timetable.cfgin = "default"
|
|
|
|
}
|
|
|
|
if timetable.cfgout == "" {
|
|
|
|
timetable.cfgout = "default"
|
|
|
|
}
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2021-01-17 09:28:20 -05:00
|
|
|
refConfig, ok := ZabovConfigs[timetable.cfgin]
|
|
|
|
if !ok {
|
|
|
|
log.Println("timetable: inexistent cfgin:", timetable.cfgin)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2021-01-17 09:28:20 -05:00
|
|
|
refConfig.references++
|
|
|
|
refConfig, ok = ZabovConfigs[timetable.cfgout]
|
|
|
|
if !ok {
|
|
|
|
log.Println("timetable: inexistent cfgout:", timetable.cfgout)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
refConfig.references++
|
|
|
|
|
|
|
|
tables := timetableRaw["tables"].([]interface{})
|
|
|
|
|
|
|
|
for i := range tables {
|
|
|
|
table := tables[i].(map[string]interface{})
|
|
|
|
var ttEntry ZabovTimetableEntry
|
|
|
|
ttEntry.times = []*ZabovTimeRange{}
|
|
|
|
for _, tRaw := range strings.Split(table["times"].(string), ";") {
|
|
|
|
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)
|
|
|
|
}
|
2021-01-13 16:30:04 -05:00
|
|
|
}
|
2021-01-17 09:28:20 -05:00
|
|
|
|
2021-01-13 16:30:04 -05:00
|
|
|
}
|
|
|
|
|
2021-01-17 09:28:20 -05:00
|
|
|
ttEntry.days = map[string]bool{}
|
|
|
|
for _, day := range strings.Split(table["days"].(string), ";") {
|
|
|
|
ttEntry.days[day] = true
|
|
|
|
}
|
2021-01-13 16:30:04 -05:00
|
|
|
|
2021-01-17 09:28:20 -05:00
|
|
|
timetable.table = append(timetable.table, &ttEntry)
|
2021-01-13 16:30:04 -05:00
|
|
|
}
|
2021-01-17 09:28:20 -05:00
|
|
|
ZabovTimetables[name] = &timetable
|
2021-01-11 18:04:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 17:26:29 -05:00
|
|
|
//******************
|
|
|
|
// IP groups section
|
|
|
|
//******************
|
2021-01-17 09:28:20 -05:00
|
|
|
if MyConf["ipgroups"] != nil {
|
|
|
|
IPGroups := MyConf["ipgroups"].([]interface{})
|
|
|
|
|
|
|
|
fmt.Println("evaluating IP Groups: ", len(IPGroups))
|
|
|
|
for i := range IPGroups {
|
|
|
|
fmt.Println("evaluating IP Group n.", i)
|
|
|
|
var groupStruct ZabovIPGroup
|
|
|
|
groupMap := IPGroups[i].(map[string]interface{})
|
|
|
|
IPsRaw := groupMap["ips"].([]interface{})
|
|
|
|
groupStruct.ips = []net.IP{}
|
|
|
|
for x := range IPsRaw {
|
|
|
|
ipRaw := IPsRaw[x].(string)
|
|
|
|
ip := net.ParseIP(ipRaw)
|
|
|
|
fmt.Println("adding IP ", ipRaw)
|
|
|
|
|
|
|
|
alias, ok := ZabovIPAliases[ipRaw]
|
|
|
|
if ok {
|
|
|
|
fmt.Println("IP alias: ", ipRaw, alias)
|
|
|
|
ip = net.ParseIP(alias)
|
|
|
|
}
|
|
|
|
groupStruct.ips = append(groupStruct.ips, ip)
|
2021-01-11 18:04:34 -05:00
|
|
|
}
|
2021-01-17 09:28:20 -05:00
|
|
|
groupStruct.cfg = groupMap["cfg"].(string)
|
|
|
|
groupStruct.timetable = groupMap["timetable"].(string)
|
|
|
|
if len(groupStruct.cfg) > 0 {
|
|
|
|
refConfig, ok := ZabovConfigs[groupStruct.cfg]
|
|
|
|
if !ok {
|
|
|
|
log.Println("ipgroups: inexistent cfg:", groupStruct.cfg)
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
refConfig.references++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("cfg:", groupStruct.cfg)
|
|
|
|
fmt.Println("timetable:", groupStruct.timetable)
|
|
|
|
_, ok := ZabovTimetables[groupStruct.timetable]
|
2021-01-14 09:38:46 -05:00
|
|
|
if !ok {
|
2021-01-17 09:28:20 -05:00
|
|
|
log.Println("inexistent timetable:", groupStruct.timetable)
|
2021-01-14 09:38:46 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-17 09:28:20 -05:00
|
|
|
ZabovIPGroups = append(ZabovIPGroups, groupStruct)
|
2021-01-14 09:38:46 -05:00
|
|
|
}
|
2021-01-11 18:04:34 -05:00
|
|
|
}
|
2021-01-13 17:38:33 -05:00
|
|
|
|
2021-01-16 12:54:31 -05:00
|
|
|
if zabov["timetable"] != nil {
|
|
|
|
ZabovDefaultTimetable = zabov["timetable"].(string)
|
|
|
|
_, ok := ZabovTimetables[ZabovDefaultTimetable]
|
|
|
|
if !ok {
|
|
|
|
log.Println("inexistent timetable:", ZabovDefaultTimetable)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 17:26:29 -05:00
|
|
|
//************************
|
|
|
|
// Local responser section
|
|
|
|
//************************
|
2021-01-17 09:28:20 -05:00
|
|
|
if MyConf["localresponder"] != nil {
|
|
|
|
localresponder := MyConf["localresponder"].(map[string]interface{})
|
|
|
|
|
|
|
|
if localresponder != nil {
|
|
|
|
if localresponder["responder"] != nil {
|
|
|
|
ZabovLocalResponder = localresponder["responder"].(string)
|
|
|
|
if len(ZabovLocalResponder) > 0 {
|
|
|
|
local := ZabovConfig{ZabovDNSArray: []string{ZabovLocalResponder}, references: 1}
|
|
|
|
ZabovConfigs["__localresponder__"] = &local
|
|
|
|
fmt.Println("ZabovLocalResponder:", ZabovLocalResponder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if localresponder["localdomain"] != nil {
|
|
|
|
ZabovLocalDomain = localresponder["localdomain"].(string)
|
2021-01-13 17:38:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 17:26:29 -05:00
|
|
|
//******************************************
|
2021-01-17 09:28:20 -05:00
|
|
|
// clearing unused configs to save resources
|
2021-01-16 17:26:29 -05:00
|
|
|
//******************************************
|
2021-01-14 09:38:46 -05:00
|
|
|
for name, conf := range ZabovConfigs {
|
|
|
|
if conf.references == 0 {
|
|
|
|
log.Println("WARNING: disabling unused configuration:", name)
|
|
|
|
delete(ZabovConfigs, name)
|
2021-01-16 17:26:29 -05:00
|
|
|
} else {
|
|
|
|
ZabovCreateKDB(name)
|
2021-01-14 09:38:46 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-11 18:04:34 -05:00
|
|
|
|
2020-10-08 16:14:07 -04:00
|
|
|
}
|