Compare commits

..

No commits in common. "15ec9f49ac2fde58dd792bebec8156b4b8f7d33b" and "5c5e4e441702069e93aff7eb63cd5bc7f3bfb0fb" have entirely different histories.

11 changed files with 123 additions and 551 deletions

View File

@ -7,15 +7,12 @@ import (
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
) )
//MyZabovKDB is the storage where we'll put domains to block (obsolete) //MyZabovKDB is the storage where we'll put domains to block
//var MyZabovKDB *leveldb.DB var MyZabovKDB *leveldb.DB
//MyZabovCDB is the storage where we'll put domains to cache (global for all configs) //MyZabovCDB is the storage where we'll put domains to cache
var MyZabovCDB *leveldb.DB var MyZabovCDB *leveldb.DB
//MyZabovKDBs is the storage where we'll put domains to block (one for each config)
var MyZabovKDBs map[string]*leveldb.DB
func init() { func init() {
var err error var err error
@ -23,13 +20,13 @@ func init() {
os.RemoveAll("./db") os.RemoveAll("./db")
os.MkdirAll("./db", 0755) os.MkdirAll("./db", 0755)
/*
MyZabovKDB, err = leveldb.OpenFile("./db/killfile", nil) MyZabovKDB, err = leveldb.OpenFile("./db/killfile", nil)
if err != nil { if err != nil {
fmt.Println("Cannot create Killfile db: ", err.Error()) fmt.Println("Cannot create Killfile db: ", err.Error())
} else { } else {
fmt.Println("Killfile DB created") fmt.Println("Killfile DB created")
}*/ }
MyZabovCDB, err = leveldb.OpenFile("./db/cache", nil) MyZabovCDB, err = leveldb.OpenFile("./db/cache", nil)
if err != nil { if err != nil {
@ -38,21 +35,4 @@ func init() {
fmt.Println("Cache DB created") fmt.Println("Cache DB created")
} }
MyZabovKDBs = map[string]*leveldb.DB{}
}
// ZabovCreateKDB creates Kill DBs
func ZabovCreateKDB(conf string) {
var err error
dbname := "./db/killfile_" + conf
KDB, err := leveldb.OpenFile(dbname, nil)
if err != nil {
fmt.Println("Cannot create Killfile db: ", err.Error())
} else {
fmt.Println("Killfile DB created")
}
MyZabovKDBs[conf] = KDB
} }

View File

@ -5,19 +5,30 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"os" "os"
"strconv"
"strings"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
type stringarray []string
type urlsMap map[string]stringarray
func init() { func init() {
var MyConfRaw interface{}
//ZabovConf describes the Json we use for configuration
type ZabovConf struct {
Zabov struct {
Port string `json:"port"`
Proto string `json:"proto"`
Ipaddr string `json:"ipaddr"`
Upstream string `json:"upstream"`
Cachettl int `json:"cachettl"`
Killfilettl int `json:"killfilettl"`
Singlefilters string `json:"singlefilters"`
Doublefilters string `json:"doublefilters"`
Blackholeip string `json:"blackholeip"`
Hostsfile string `json:"hostsfile"`
} `json:"zabov"`
}
var MyConf ZabovConf
file, err := ioutil.ReadFile("config.json") file, err := ioutil.ReadFile("config.json")
@ -26,42 +37,26 @@ func init() {
os.Exit(1) os.Exit(1)
} }
err = json.Unmarshal([]byte(file), &MyConfRaw) err = json.Unmarshal([]byte(file), &MyConf)
if err != nil { if err != nil {
log.Println("Cannot unmarshal json: ", err.Error()) log.Println("Cannot marshal json: ", err.Error())
os.Exit(1) os.Exit(1)
} }
// now we read configuration file // now we read configuration file
fmt.Println("Reading configuration file...") fmt.Println("Reading configuration file...")
MyConf := MyConfRaw.(map[string]interface{}) ZabovPort := MyConf.Zabov.Port
ZabovType := MyConf.Zabov.Proto
zabov := MyConf["zabov"].(map[string]interface{}) ZabovAddr := MyConf.Zabov.Ipaddr
ZabovUpDNS = MyConf.Zabov.Upstream
ZabovPort := zabov["port"].(string) ZabovSingleBL = MyConf.Zabov.Singlefilters
ZabovType := zabov["proto"].(string) ZabovDoubleBL = MyConf.Zabov.Doublefilters
ZabovAddr := zabov["ipaddr"].(string) ZabovAddBL = MyConf.Zabov.Blackholeip
ZabovCacheTTL = int(zabov["cachettl"].(float64)) ZabovCacheTTL = MyConf.Zabov.Cachettl
ZabovKillTTL = int(zabov["killfilettl"].(float64)) ZabovKillTTL = MyConf.Zabov.Killfilettl
ZabovHostsFile = MyConf.Zabov.Hostsfile
if MyConf["configs"] == nil {
log.Println("configs not set: you shall set at least 'default' config")
os.Exit(1)
}
configs := MyConf["configs"].(map[string]interface{})
if len(configs) == 0 {
log.Println("you shall set at least 'default' config")
os.Exit(1)
}
if configs["default"] == nil {
log.Println("'default' config is required")
os.Exit(1)
}
zabovString := ZabovAddr + ":" + ZabovPort zabovString := ZabovAddr + ":" + ZabovPort
@ -69,151 +64,6 @@ func init() {
MyDNS.Addr = zabovString MyDNS.Addr = zabovString
MyDNS.Net = ZabovType MyDNS.Net = ZabovType
ZabovConfigs = map[string]ZabovConfig{} ZabovDNSArray = fileByLines(ZabovUpDNS)
ZabovIPGroups = []ZabovIPGroup{}
ZabovTimetables = map[string]*ZabovTimetable{}
ZabovIPAliases = map[string]string{}
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)
conf.ZabovDNSArray = fileByLines(conf.ZabovUpDNS)
ZabovConfigs[name] = conf
ZabovCreateKDB(name)
}
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"
}
_, ok := ZabovConfigs[timetable.cfgin]
if !ok {
log.Println("timetable: inexistent cfgin:", timetable.cfgin)
os.Exit(1)
}
_, ok = ZabovConfigs[timetable.cfgout]
if !ok {
log.Println("timetable: inexistent cfgout:", timetable.cfgout)
os.Exit(1)
}
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)
}
}
}
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
}
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)
}
groupStruct.cfg = groupMap["cfg"].(string)
groupStruct.timetable = groupMap["timetable"].(string)
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)
}
localresponder := MyConf["localresponder"].(map[string]interface{})
if localresponder != nil {
if localresponder["responder"] != nil {
ZabovLocalResponder = localresponder["responder"].(string)
if len(ZabovLocalResponder) > 0 {
local := ZabovConfig{}
local.ZabovDNSArray = []string{ZabovLocalResponder}
ZabovConfigs["__localresponder__"] = local
fmt.Println("ZabovLocalResponder:", ZabovLocalResponder)
}
}
if localresponder["localdomain"] != nil {
ZabovLocalDomain = localresponder["localdomain"].(string)
}
}
//fmt.Println("ZabovConfigs:", ZabovConfigs)
//fmt.Println("ZabovTimetables:", ZabovTimetables)
//fmt.Println("ZabovIPAliases:", ZabovIPAliases)
//fmt.Println("ZabovIPGroups:", ZabovIPGroups)
} }

View File

@ -5,10 +5,11 @@ import (
"strings" "strings"
) )
var zabovKbucket = []byte("killfile")
type killfileItem struct { type killfileItem struct {
Kdomain string Kdomain string
Ksource string Ksource string
Kconfigs stringarray
} }
var bChannel chan killfileItem var bChannel chan killfileItem
@ -26,25 +27,16 @@ func bWriteThread() {
for item := range bChannel { for item := range bChannel {
alreadyInSomeDB := false writeInKillfile(item.Kdomain, item.Ksource)
for _, config := range item.Kconfigs {
if !alreadyInSomeDB {
alreadyInSomeDB = domainInKillfile(item.Kdomain, config)
}
writeInKillfile(item.Kdomain, item.Ksource, config)
}
if !alreadyInSomeDB {
incrementStats("BL domains from "+item.Ksource, 1) incrementStats("BL domains from "+item.Ksource, 1)
incrementStats("TOTAL", 1) incrementStats("TOTAL", 1)
}
} }
} }
//DomainKill stores a domain name inside the killfile //DomainKill stores a domain name inside the killfile
func DomainKill(s, durl string, configs stringarray) { func DomainKill(s, durl string) {
if len(s) > 2 { if len(s) > 2 {
@ -54,7 +46,6 @@ func DomainKill(s, durl string, configs stringarray) {
k.Kdomain = s k.Kdomain = s
k.Ksource = durl k.Ksource = durl
k.Kconfigs = configs
bChannel <- k bChannel <- k
@ -62,12 +53,11 @@ func DomainKill(s, durl string, configs stringarray) {
} }
func writeInKillfile(key, value string, config string) { func writeInKillfile(key, value string) {
stK := []byte(key) stK := []byte(key)
stV := []byte(value) stV := []byte(value)
MyZabovKDB := MyZabovKDBs[config]
err := MyZabovKDB.Put(stK, stV, nil) err := MyZabovKDB.Put(stK, stV, nil)
if err != nil { if err != nil {
fmt.Println("Cannot write to Killfile DB: ", err.Error()) fmt.Println("Cannot write to Killfile DB: ", err.Error())
@ -75,11 +65,10 @@ func writeInKillfile(key, value string, config string) {
} }
func domainInKillfile(domain string, config string) bool { func domainInKillfile(domain string) bool {
s := strings.ToLower(domain) s := strings.ToLower(domain)
MyZabovKDB := MyZabovKDBs[config]
has, err := MyZabovKDB.Has([]byte(s), nil) has, err := MyZabovKDB.Has([]byte(s), nil)
if err != nil { if err != nil {
fmt.Println("Cannot read from Killfile DB: ", err.Error()) fmt.Println("Cannot read from Killfile DB: ", err.Error())

View File

@ -81,12 +81,7 @@ func statsThread() {
case "INC": case "INC":
ZabovStats[item.Payload] += item.Number ZabovStats[item.Payload] += item.Number
case "SET": case "SET":
if item.Number == 0 {
delete(ZabovStats, item.Payload)
} else {
ZabovStats[item.Payload] = item.Number ZabovStats[item.Payload] = item.Number
}
case "PRI": case "PRI":
statsPrint() statsPrint()
} }

View File

@ -16,12 +16,9 @@ func init() {
} }
//DoubleIndexFilter puts the domains inside file //DoubleIndexFilter puts the domains inside file
func DoubleIndexFilter(durl string, configs stringarray) error { func DoubleIndexFilter(durl string) error {
fmt.Println("DoubleIndexFilter: Retrieving HostFile from: ", durl) fmt.Println("Retrieving HostFile from: ", durl)
// resets malformed HostLines for url
setstatsvalue("Malformed HostLines "+durl, 0)
var err error var err error
@ -51,9 +48,6 @@ func DoubleIndexFilter(durl string, configs stringarray) error {
line := scanner.Text() line := scanner.Text()
if len(line) == 0 || strings.TrimSpace(line)[0] == '#' {
continue
}
h := strings.FieldsFunc(line, splitter) h := strings.FieldsFunc(line, splitter)
if h == nil { if h == nil {
@ -65,8 +59,7 @@ func DoubleIndexFilter(durl string, configs stringarray) error {
} }
if net.ParseIP(h[0]) != nil { if net.ParseIP(h[0]) != nil {
DomainKill(h[1], durl)
DomainKill(h[1], durl, configs)
// fmt.Println("MATCH: ", h[1]) // fmt.Println("MATCH: ", h[1])
numLines++ numLines++
@ -83,41 +76,20 @@ func DoubleIndexFilter(durl string, configs stringarray) error {
} }
func getDoubleFilters(urls urlsMap) { func getDoubleFilters() {
fmt.Println("getDoubleFilters: downloading all urls:", len(urls)) s := fileByLines(ZabovDoubleBL)
for url, configs := range urls {
DoubleIndexFilter(url, configs) for _, a := range s {
DoubleIndexFilter(a)
} }
fmt.Println("getDoubleFilters: DONE!")
} }
func downloadDoubleThread() { func downloadDoubleThread() {
fmt.Println("Starting updater of DOUBLE lists, each (hours):", ZabovKillTTL) fmt.Println("Starting updater of DOUBLE lists, each (hours):", ZabovKillTTL)
_urls := urlsMap{}
for { for {
fmt.Println("downloadDoubleThread: collecting urls from all configs...") getDoubleFilters()
for config := range ZabovConfigs {
ZabovDoubleBL := ZabovConfigs[config].ZabovDoubleBL
if len(ZabovDoubleBL) == 0 {
continue
}
s := fileByLines(ZabovDoubleBL)
for _, v := range s {
configs := _urls[v]
if configs == nil {
configs = stringarray{}
_urls[v] = configs
}
configs = append(configs, config)
_urls[v] = configs
}
}
getDoubleFilters(_urls)
time.Sleep(time.Duration(ZabovKillTTL) * time.Hour) time.Sleep(time.Duration(ZabovKillTTL) * time.Hour)
} }

View File

@ -14,13 +14,10 @@ func init() {
} }
//SingleIndexFilter puts the domains inside file //SingleIndexFilter puts the domains inside file
func SingleIndexFilter(durl string, configs stringarray) error { func SingleIndexFilter(durl string) error {
fmt.Println("Retrieving DomainFile from: ", durl) fmt.Println("Retrieving DomainFile from: ", durl)
// resets malformed HostLines for url
setstatsvalue("Malformed DomainLines "+durl, 0)
var err error var err error
// Get the data // Get the data
@ -49,9 +46,6 @@ func SingleIndexFilter(durl string, configs stringarray) error {
line := scanner.Text() line := scanner.Text()
if len(line) == 0 || strings.TrimSpace(line)[0] == '#' {
continue
}
h := strings.FieldsFunc(line, splitter) h := strings.FieldsFunc(line, splitter)
if h == nil { if h == nil {
@ -63,9 +57,7 @@ func SingleIndexFilter(durl string, configs stringarray) error {
} }
if !strings.Contains(h[0], "#") { if !strings.Contains(h[0], "#") {
DomainKill(h[0], durl)
DomainKill(h[0], durl, configs)
// fmt.Println("MATCH: ", h[1]) // fmt.Println("MATCH: ", h[1])
numLines++ numLines++
} else { } else {
@ -81,42 +73,20 @@ func SingleIndexFilter(durl string, configs stringarray) error {
} }
func getSingleFilters(urls urlsMap) { func getSingleFilters() {
fmt.Println("getSingleFilters: downloading all urls:", len(urls)) s := fileByLines(ZabovSingleBL)
for url, configs := range urls {
SingleIndexFilter(url, configs) for _, a := range s {
SingleIndexFilter(a)
} }
fmt.Println("getSingleFilters: DONE!")
} }
func downloadThread() { func downloadThread() {
fmt.Println("Starting updater of SINGLE lists, each (hours): ", ZabovKillTTL) fmt.Println("Starting updater of SINGLE lists, each (hours): ", ZabovKillTTL)
_urls := urlsMap{}
for { for {
fmt.Println("downloadThread: collecting urls from all configs...") getSingleFilters()
for config := range ZabovConfigs {
ZabovSingleBL := ZabovConfigs[config].ZabovSingleBL
if len(ZabovSingleBL) == 0 {
continue
}
s := fileByLines(ZabovSingleBL)
for _, v := range s {
configs := _urls[v]
if configs == nil {
configs = stringarray{}
_urls[v] = configs
}
configs = append(configs, config)
_urls[v] = configs
}
}
getSingleFilters(_urls)
time.Sleep(time.Duration(ZabovKillTTL) * time.Hour) time.Sleep(time.Duration(ZabovKillTTL) * time.Hour)
} }

View File

@ -3,53 +3,13 @@
"port":"53", "port":"53",
"proto":"udp", "proto":"udp",
"ipaddr":"0.0.0.0", "ipaddr":"0.0.0.0",
"upstream":"./dns-upstream.txt" ,
"cachettl": 1, "cachettl": 1,
"killfilettl": 12 "killfilettl": 12,
},
"localresponder":{
"responder":"192.168.178.1:53",
"localdomain":"fritz.box"
},
"ipaliases":{
"pc8":"192.168.178.29",
"localhost":"127.0.0.1"
},
"ipgroups":[
{
"ips":["localhost", "::1", "192.168.178.30", "192.168.178.31", "pc8"],
"cfg":"",
"timetable":"tt_children"
}
],
"timetables":{
"tt_children":{
"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",
"cfgout":"children"
}
},
"configs":{
"default":{
"upstream":"./dns-upstream.txt",
"singlefilters":"./urls-domains.txt" , "singlefilters":"./urls-domains.txt" ,
"doublefilters":"./urls-hosts.txt", "doublefilters":"./urls-hosts.txt",
"blackholeip":"127.0.0.1", "blackholeip":"127.0.0.1",
"hostsfile":"./urls-local.txt" "hostsfile":"./urls-local.txt"
},
"children":{
"upstream":"./dns-upstream.txt",
"singlefilters":"./urls-domains.txt",
"doublefilters":"./urls-hosts.txt",
"blackholeip":"127.0.0.1",
"hostsfile":"./urls-local.txt"
},
"children_restricted":{
"upstream":"./dns-upstream.txt",
"singlefilters":"./urls-domains.txt",
"doublefilters":"./urls-hosts.txt",
"blackholeip":"127.0.0.1",
"hostsfile":"./urls-local.txt"
}
} }
} }

View File

@ -12,8 +12,7 @@ import (
//ForwardQuery forwards the query to the upstream server //ForwardQuery forwards the query to the upstream server
//first server to answer wins //first server to answer wins
//accepts config name to select the UP DNS source list func ForwardQuery(query *dns.Msg) *dns.Msg {
func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
go incrementStats("ForwardQueries", 1) go incrementStats("ForwardQueries", 1)
@ -24,7 +23,6 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
fqdn := strings.TrimRight(query.Question[0].Name, ".") fqdn := strings.TrimRight(query.Question[0].Name, ".")
lfqdn := fmt.Sprintf("%d", query.Question[0].Qtype) + "." + fqdn lfqdn := fmt.Sprintf("%d", query.Question[0].Qtype) + "." + fqdn
if !nocache {
if cached := GetDomainFromCache(lfqdn); cached != nil { if cached := GetDomainFromCache(lfqdn); cached != nil {
go incrementStats("CacheHit", 1) go incrementStats("CacheHit", 1)
cached.SetReply(query) cached.SetReply(query)
@ -32,7 +30,6 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
return cached return cached
} }
}
c := new(dns.Client) c := new(dns.Client)
@ -48,7 +45,7 @@ func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
continue continue
} }
d := oneTimeDNS(config) d := oneTimeDNS()
in, _, err := c.Exchange(query, d) in, _, err := c.Exchange(query, d)
if err != nil { if err != nil {
@ -81,11 +78,11 @@ func init() {
} }
func oneTimeDNS(config string) (dns string) { func oneTimeDNS() (dns string) {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
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,124 +1,44 @@
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)) {
go incrementStats("TIMETABLE IN: "+timetable, 1)
fmt.Println("confFromTimeTable: return IN", tt.cfgin)
return tt.cfgin
}
}
}
}
go 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)
remIP, _, e := net.SplitHostPort(w.RemoteAddr().String()) remIP, _, e := net.SplitHostPort(w.RemoteAddr().String())
if e != nil { if e != nil {
go incrementStats("CLIENT ERROR: "+remIP, 1)
} else {
go incrementStats("CLIENT: "+remIP, 1) go incrementStats("CLIENT: "+remIP, 1)
} }
msg := dns.Msg{} msg := dns.Msg{}
msg.SetReply(r) msg.SetReply(r)
config := confFromIP(net.ParseIP(remIP))
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
domain := msg.Question[0].Name domain := msg.Question[0].Name
fqdn := strings.TrimRight(domain, ".") fqdn := strings.TrimRight(domain, ".")
if len(ZabovIPAliases[fqdn]) > 0 { if domainInKillfile(fqdn) {
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
}
}
if domainInKillfile(fqdn, config) {
go incrementStats("Killed", 1) go incrementStats("Killed", 1)
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(ZabovConfig.ZabovAddBL), A: net.ParseIP(ZabovAddBL),
}) })
} else { } else {
ret := ForwardQuery(r, config, false) ret := ForwardQuery(r)
w.WriteMsg(ret) w.WriteMsg(ret)
} }
default: default:
ret := ForwardQuery(r, config, false) ret := ForwardQuery(r)
w.WriteMsg(ret) w.WriteMsg(ret)
} }
go incrementStats("CONFIG: "+config, 1)
w.WriteMsg(&msg) w.WriteMsg(&msg)
} }

View File

@ -9,29 +9,12 @@ import (
func init() { func init() {
fmt.Println("Ingesting local hosts file") fmt.Println("Ingesting local hosts file")
ingestLocalBlacklists() ingestLocalBlacklist()
} }
func ingestLocalBlacklists() { func ingestLocalBlacklist() {
fmt.Println("ingestLocalBlacklist: collecting urls from all configs...")
_files := urlsMap{}
for config := range ZabovConfigs {
ZabovHostsFile := ZabovConfigs[config].ZabovHostsFile
if len(ZabovHostsFile) == 0 {
continue
}
configs := _files[ZabovHostsFile]
if configs == nil {
configs = stringarray{}
_files[ZabovHostsFile] = configs
}
configs = append(configs, config)
_files[ZabovHostsFile] = configs
}
for ZabovHostsFile, configs := range _files {
file, err := os.Open(ZabovHostsFile) file, err := os.Open(ZabovHostsFile)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
@ -41,7 +24,7 @@ func ingestLocalBlacklists() {
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
d := scanner.Text() d := scanner.Text()
DomainKill(d, ZabovHostsFile, configs) DomainKill(d, ZabovHostsFile)
incrementStats("Blacklist", 1) incrementStats("Blacklist", 1)
} }
@ -49,7 +32,6 @@ func ingestLocalBlacklists() {
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
}
} }

79
main.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"log" "log"
"net"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -10,74 +9,32 @@ import (
//MyDNS is my dns server //MyDNS is my dns server
var MyDNS *dns.Server var MyDNS *dns.Server
//ZabovCacheTTL is the amount of hours we cache records of DNS (global) //ZabovUpDNS keeps the name of upstream DNSs
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 (global) //ZabovKillTTL is the amount of hours we cache the killfile
var ZabovKillTTL int var ZabovKillTTL int
//ZabovLocalResponder is the default DNS server for loca domains //ZabovHostsFile is the file we use to keep our hosts
var ZabovLocalResponder string var ZabovHostsFile string
//ZabovLocalDomain is the default local domain //ZabovDNSArray is the array containing all the DNS we mention
var ZabovLocalDomain string var ZabovDNSArray []string
type handler struct{} type handler struct{}
// ZabovConfig contains all Zabov configs
type ZabovConfig struct {
ZabovSingleBL string // json:singlefilters -> ZabovSingleBL list of urls returning a file with just names of domains
ZabovDoubleBL string // json:doublefilters -> ZabovDoubleBL list of urls returning a file with IP<space>domain
ZabovAddBL string // json:blackholeip -> ZabovAddBL is the IP we want to send all the clients to. Usually is 127.0.0.1
ZabovHostsFile string // json:hostsfile -> ZabovHostsFile is the file we use to keep our hosts
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
var ZabovConfigs map[string]ZabovConfig
// ZabovIPGroup contains Zabov groups of IPs
type ZabovIPGroup struct {
ips []net.IP // IPs in this group
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
}
// ZabovIPGroups contains an array of all Zabov groups of IP rules
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
type ZabovTimetableEntry struct {
times []*ZabovTimeRange
days map[string]bool
}
// ZabovTimetable contains a Zabov time table
type ZabovTimetable struct {
table []*ZabovTimetableEntry
cfgin string // configuration name to be used if "inside" timetable
cfgout string // configuration name to be used if "outiside" timetable
}
// ZabovTimetables contains all Zabov time tables, by name
var ZabovTimetables map[string]*ZabovTimetable
// ZabovIPAliases contains an array of all Zabov IP aliases
var ZabovIPAliases map[string]string
func main() { func main() {
MyDNS.Handler = &handler{} MyDNS.Handler = &handler{}