Browse Source

- config:

- more sanity checks
  - added localresponder: if set use specified DNS server for local domains
  - ipaliases is now used in DNS responses (works same as /etc/hosts file)
- ForwardQuery(): accept param to avoid cache
master
bloved 1 month ago
parent
commit
945709f24e
8 changed files with 75 additions and 14 deletions
  1. +24
    -2
      01.conf.go
  2. +3
    -1
      adlist_hosts.go
  3. +4
    -0
      adlist_single.go
  4. +4
    -0
      config.json
  5. +8
    -6
      dns_client.go
  6. +23
    -5
      dns_handler.go
  7. +3
    -0
      hostfile.go
  8. +6
    -0
      main.go

+ 24
- 2
01.conf.go View File

@ -46,15 +46,20 @@ func init() {
ZabovCacheTTL = int(zabov["cachettl"].(float64))
ZabovKillTTL = int(zabov["killfilettl"].(float64))
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")
log.Println("you shall set at least 'default' config")
os.Exit(1)
}
if configs["default"] == nil {
log.Println("default config is required")
log.Println("'default' config is required")
os.Exit(1)
}
@ -189,6 +194,23 @@ func init() {
}
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)

+ 3
- 1
adlist_hosts.go View File

@ -102,7 +102,9 @@ func downloadDoubleThread() {
fmt.Println("downloadDoubleThread: collecting urls from all configs...")
for config := range ZabovConfigs {
ZabovDoubleBL := ZabovConfigs[config].ZabovDoubleBL
if len(ZabovDoubleBL) == 0 {
continue
}
s := fileByLines(ZabovDoubleBL)
for _, v := range s {
configs := _urls[v]

+ 4
- 0
adlist_single.go View File

@ -100,6 +100,10 @@ func downloadThread() {
for config := range ZabovConfigs {
ZabovSingleBL := ZabovConfigs[config].ZabovSingleBL
if len(ZabovSingleBL) == 0 {
continue
}
s := fileByLines(ZabovSingleBL)
for _, v := range s {
configs := _urls[v]

+ 4
- 0
config.json View File

@ -6,6 +6,10 @@
"cachettl": 1,
"killfilettl": 12
},
"localresponder":{
"responder":"192.168.178.1:53",
"localdomain":"fritz.box"
},
"ipaliases":{
"pc8":"192.168.178.29",
"localhost":"127.0.0.1"

+ 8
- 6
dns_client.go View File

@ -13,7 +13,7 @@ import (
//ForwardQuery forwards the query to the upstream server
//first server to answer wins
//accepts config name to select the UP DNS source list
func ForwardQuery(query *dns.Msg, config string) *dns.Msg {
func ForwardQuery(query *dns.Msg, config string, nocache bool) *dns.Msg {
go incrementStats("ForwardQueries", 1)
@ -24,12 +24,14 @@ func ForwardQuery(query *dns.Msg, config string) *dns.Msg {
fqdn := strings.TrimRight(query.Question[0].Name, ".")
lfqdn := fmt.Sprintf("%d", query.Question[0].Qtype) + "." + fqdn
if cached := GetDomainFromCache(lfqdn); cached != nil {
go incrementStats("CacheHit", 1)
cached.SetReply(query)
cached.Authoritative = true
return cached
if !nocache {
if cached := GetDomainFromCache(lfqdn); cached != nil {
go incrementStats("CacheHit", 1)
cached.SetReply(query)
cached.Authoritative = true
return cached
}
}
c := new(dns.Client)

+ 23
- 5
dns_handler.go View File

@ -35,14 +35,14 @@ func confFromTimeTable(timetable string) string {
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)
go incrementStats("TIMETABLE IN: "+timetable, 1)
fmt.Println("confFromTimeTable: return IN", tt.cfgin)
return tt.cfgin
}
}
}
}
incrementStats("TIMETABLE OUT: "+timetable, 1)
go incrementStats("TIMETABLE OUT: "+timetable, 1)
fmt.Println("confFromTimeTable: return OUT", tt.cfgout)
return tt.cfgout
}
@ -77,7 +77,6 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
msg.SetReply(r)
config := confFromIP(net.ParseIP(remIP))
incrementStats("CONFIG: "+config, 1)
ZabovConfig := ZabovConfigs[config]
switch r.Question[0].Qtype {
@ -86,6 +85,24 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
domain := msg.Question[0].Name
fqdn := strings.TrimRight(domain, ".")
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
}
}
if domainInKillfile(fqdn, config) {
go incrementStats("Killed", 1)
@ -94,13 +111,14 @@ func (mydns *handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
A: net.ParseIP(ZabovConfig.ZabovAddBL),
})
} else {
ret := ForwardQuery(r, config)
ret := ForwardQuery(r, config, false)
w.WriteMsg(ret)
}
default:
ret := ForwardQuery(r, config)
ret := ForwardQuery(r, config, false)
w.WriteMsg(ret)
}
go incrementStats("CONFIG: "+config, 1)
w.WriteMsg(&msg)
}

+ 3
- 0
hostfile.go View File

@ -19,6 +19,9 @@ func ingestLocalBlacklists() {
_files := urlsMap{}
for config := range ZabovConfigs {
ZabovHostsFile := ZabovConfigs[config].ZabovHostsFile
if len(ZabovHostsFile) == 0 {
continue
}
configs := _files[ZabovHostsFile]
if configs == nil {
configs = stringarray{}

+ 6
- 0
main.go View File

@ -16,6 +16,12 @@ var ZabovCacheTTL int
//ZabovKillTTL is the amount of hours we cache the killfile (global)
var ZabovKillTTL int
//ZabovLocalResponder is the default DNS server for loca domains
var ZabovLocalResponder string
//ZabovLocalDomain is the default local domain
var ZabovLocalDomain string
type handler struct{}
// ZabovConfig contains all Zabov configs

Loading…
Cancel
Save