2020-10-08 22:14:07 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2021-01-13 22:30:04 +01:00
|
|
|
"net"
|
2020-10-08 22:14:07 +02:00
|
|
|
"os"
|
2021-01-13 22:30:04 +01:00
|
|
|
"strconv"
|
2021-01-12 00:04:34 +01:00
|
|
|
"strings"
|
2020-10-08 22:14:07 +02:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2021-01-12 00:04:34 +01:00
|
|
|
type stringarray []string
|
|
|
|
type urlsMap map[string]stringarray
|
2020-10-08 22:14:07 +02:00
|
|
|
|
2021-01-12 00:04:34 +01:00
|
|
|
func init() {
|
|
|
|
var MyConfRaw interface{}
|
2020-10-08 22:14:07 +02:00
|
|
|
|
|
|
|
file, err := ioutil.ReadFile("config.json")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Cannot open config file", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-01-12 00:04:34 +01:00
|
|
|
err = json.Unmarshal([]byte(file), &MyConfRaw)
|
2020-10-08 22:14:07 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2021-01-12 00:04:34 +01:00
|
|
|
log.Println("Cannot unmarshal json: ", err.Error())
|
2020-10-08 22:14:07 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we read configuration file
|
|
|
|
fmt.Println("Reading configuration file...")
|
|
|
|
|
2021-01-12 00:04:34 +01:00
|
|
|
MyConf := MyConfRaw.(map[string]interface{})
|
|
|
|
|
|
|
|
zabov := MyConf["zabov"].(map[string]interface{})
|
|
|
|
|
|
|
|
ZabovPort := zabov["port"].(string)
|
|
|
|
ZabovType := zabov["proto"].(string)
|
|
|
|
ZabovAddr := zabov["ipaddr"].(string)
|
2021-01-14 20:25:10 +01:00
|
|
|
DebugStr := (zabov["debug"].(string))
|
2021-01-12 00:04:34 +01:00
|
|
|
ZabovCacheTTL = int(zabov["cachettl"].(float64))
|
|
|
|
ZabovKillTTL = int(zabov["killfilettl"].(float64))
|
|
|
|
|
2021-01-14 20:25:10 +01:00
|
|
|
ZabovDebug = DebugStr == "true"
|
|
|
|
|
2021-01-13 23:38:33 +01:00
|
|
|
if MyConf["configs"] == nil {
|
|
|
|
log.Println("configs not set: you shall set at least 'default' config")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-01-12 00:04:34 +01:00
|
|
|
configs := MyConf["configs"].(map[string]interface{})
|
|
|
|
|
2021-01-13 22:30:04 +01:00
|
|
|
if len(configs) == 0 {
|
2021-01-13 23:38:33 +01:00
|
|
|
log.Println("you shall set at least 'default' config")
|
2021-01-13 22:30:04 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-12 00:04:34 +01:00
|
|
|
|
2021-01-13 22:30:04 +01:00
|
|
|
if configs["default"] == nil {
|
2021-01-13 23:38:33 +01:00
|
|
|
log.Println("'default' config is required")
|
2021-01-13 22:30:04 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-10-08 22:14:07 +02:00
|
|
|
|
|
|
|
zabovString := ZabovAddr + ":" + ZabovPort
|
|
|
|
|
|
|
|
MyDNS = new(dns.Server)
|
|
|
|
MyDNS.Addr = zabovString
|
|
|
|
MyDNS.Net = ZabovType
|
|
|
|
|
2021-01-14 15:38:46 +01:00
|
|
|
ZabovConfigs = map[string]*ZabovConfig{}
|
2021-01-12 00:04:34 +01:00
|
|
|
ZabovIPGroups = []ZabovIPGroup{}
|
2021-01-13 22:30:04 +01:00
|
|
|
ZabovTimetables = map[string]*ZabovTimetable{}
|
2021-01-12 00:04:34 +01:00
|
|
|
ZabovIPAliases = map[string]string{}
|
2021-01-13 22:30:04 +01:00
|
|
|
|
2021-01-12 00:04:34 +01:00
|
|
|
IPAliasesRaw := MyConf["ipaliases"].(map[string]interface{})
|
|
|
|
|
|
|
|
for alias, ip := range IPAliasesRaw {
|
|
|
|
fmt.Println("IP Alias:", alias, ip)
|
|
|
|
ZabovIPAliases[alias] = ip.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 22:30:04 +01:00
|
|
|
conf.ZabovDNSArray = fileByLines(conf.ZabovUpDNS)
|
2021-01-14 15:38:46 +01:00
|
|
|
ZabovConfigs[name] = &conf
|
2021-01-12 00:04:34 +01:00
|
|
|
ZabovCreateKDB(name)
|
|
|
|
}
|
|
|
|
|
2021-01-14 15:38:46 +01:00
|
|
|
ZabovConfigs["default"].references++
|
|
|
|
|
2021-01-12 00:04:34 +01:00
|
|
|
timetables := MyConf["timetables"].(map[string]interface{})
|
|
|
|
|
|
|
|
for name, v := range timetables {
|
|
|
|
fmt.Println("evaluaing timetable name:", name)
|
|
|
|
timetableRaw := v.(map[string]interface{})
|
|
|
|
var timetable ZabovTimetable
|
|
|
|
|
|
|
|
timetable.cfgin = timetableRaw["cfgin"].(string)
|
|
|
|
timetable.cfgout = timetableRaw["cfgout"].(string)
|
|
|
|
|
|
|
|
if timetable.cfgin == "" {
|
|
|
|
timetable.cfgin = "default"
|
|
|
|
}
|
|
|
|
if timetable.cfgout == "" {
|
|
|
|
timetable.cfgout = "default"
|
|
|
|
}
|
|
|
|
|
2021-01-14 15:38:46 +01:00
|
|
|
refConfig, ok := ZabovConfigs[timetable.cfgin]
|
2021-01-12 00:04:34 +01:00
|
|
|
if !ok {
|
2021-01-13 22:30:04 +01:00
|
|
|
log.Println("timetable: inexistent cfgin:", timetable.cfgin)
|
2021-01-12 00:04:34 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-01-14 15:38:46 +01:00
|
|
|
refConfig.references++
|
|
|
|
refConfig, ok = ZabovConfigs[timetable.cfgout]
|
2021-01-12 00:04:34 +01:00
|
|
|
if !ok {
|
2021-01-13 22:30:04 +01:00
|
|
|
log.Println("timetable: inexistent cfgout:", timetable.cfgout)
|
2021-01-12 00:04:34 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-14 15:38:46 +01:00
|
|
|
refConfig.references++
|
2021-01-12 00:04:34 +01:00
|
|
|
|
|
|
|
tables := timetableRaw["tables"].([]interface{})
|
|
|
|
|
|
|
|
for i := range tables {
|
|
|
|
table := tables[i].(map[string]interface{})
|
|
|
|
var ttEntry ZabovTimetableEntry
|
2021-01-13 22:30:04 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ttEntry.days = map[string]bool{}
|
|
|
|
for _, day := range strings.Split(table["days"].(string), ";") {
|
|
|
|
ttEntry.days[day] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
timetable.table = append(timetable.table, &ttEntry)
|
2021-01-12 00:04:34 +01:00
|
|
|
}
|
2021-01-13 22:30:04 +01:00
|
|
|
ZabovTimetables[name] = &timetable
|
2021-01-12 00:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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{})
|
2021-01-13 22:30:04 +01:00
|
|
|
groupStruct.ips = []net.IP{}
|
2021-01-12 00:04:34 +01:00
|
|
|
for x := range IPsRaw {
|
2021-01-13 22:30:04 +01:00
|
|
|
ipRaw := IPsRaw[x].(string)
|
|
|
|
ip := net.ParseIP(ipRaw)
|
|
|
|
fmt.Println("adding IP ", ipRaw)
|
2021-01-12 00:04:34 +01:00
|
|
|
|
2021-01-13 22:30:04 +01:00
|
|
|
alias, ok := ZabovIPAliases[ipRaw]
|
2021-01-12 00:04:34 +01:00
|
|
|
if ok {
|
2021-01-13 22:30:04 +01:00
|
|
|
fmt.Println("IP alias: ", ipRaw, alias)
|
|
|
|
ip = net.ParseIP(alias)
|
2021-01-12 00:04:34 +01:00
|
|
|
}
|
|
|
|
groupStruct.ips = append(groupStruct.ips, ip)
|
|
|
|
}
|
|
|
|
groupStruct.cfg = groupMap["cfg"].(string)
|
|
|
|
groupStruct.timetable = groupMap["timetable"].(string)
|
2021-01-14 15:38:46 +01:00
|
|
|
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++
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 00:04:34 +01:00
|
|
|
fmt.Println("cfg:", groupStruct.cfg)
|
|
|
|
fmt.Println("timetable:", groupStruct.timetable)
|
|
|
|
_, ok := ZabovTimetables[groupStruct.timetable]
|
|
|
|
if !ok {
|
|
|
|
log.Println("inexistent timetable:", groupStruct.timetable)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
ZabovIPGroups = append(ZabovIPGroups, groupStruct)
|
|
|
|
}
|
2021-01-13 23:38:33 +01:00
|
|
|
|
|
|
|
localresponder := MyConf["localresponder"].(map[string]interface{})
|
|
|
|
|
|
|
|
if localresponder != nil {
|
|
|
|
if localresponder["responder"] != nil {
|
|
|
|
ZabovLocalResponder = localresponder["responder"].(string)
|
|
|
|
if len(ZabovLocalResponder) > 0 {
|
2021-01-14 15:38:46 +01:00
|
|
|
local := ZabovConfig{ZabovDNSArray: []string{ZabovLocalResponder}, references: 1}
|
|
|
|
ZabovConfigs["__localresponder__"] = &local
|
2021-01-13 23:38:33 +01:00
|
|
|
fmt.Println("ZabovLocalResponder:", ZabovLocalResponder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if localresponder["localdomain"] != nil {
|
|
|
|
ZabovLocalDomain = localresponder["localdomain"].(string)
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 15:38:46 +01:00
|
|
|
|
|
|
|
for name, conf := range ZabovConfigs {
|
|
|
|
if conf.references == 0 {
|
|
|
|
log.Println("WARNING: disabling unused configuration:", name)
|
|
|
|
delete(ZabovConfigs, name)
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 22:30:04 +01:00
|
|
|
//fmt.Println("ZabovConfigs:", ZabovConfigs)
|
|
|
|
//fmt.Println("ZabovTimetables:", ZabovTimetables)
|
|
|
|
//fmt.Println("ZabovIPAliases:", ZabovIPAliases)
|
|
|
|
//fmt.Println("ZabovIPGroups:", ZabovIPGroups)
|
2021-01-12 00:04:34 +01:00
|
|
|
|
2020-10-08 22:14:07 +02:00
|
|
|
}
|