zabov/01.killfile.go

91 lines
1.4 KiB
Go
Raw Normal View History

2020-10-08 22:14:07 +02:00
package main
import (
"fmt"
"strings"
)
type killfileItem struct {
Kdomain string
Ksource string
Kconfigs stringarray
2020-10-08 22:14:07 +02:00
}
var bChannel chan killfileItem
func init() {
bChannel = make(chan killfileItem, 1024)
fmt.Println("Initializing kill channel engine.")
go bWriteThread()
}
func bWriteThread() {
for item := range bChannel {
alreadyInSomeDB := false
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("TOTAL", 1)
}
2020-10-08 22:14:07 +02:00
}
}
//DomainKill stores a domain name inside the killfile
func DomainKill(s, durl string, configs stringarray) {
2020-10-08 22:14:07 +02:00
if len(s) > 2 {
s = strings.ToLower(s)
var k killfileItem
k.Kdomain = s
k.Ksource = durl
k.Kconfigs = configs
2020-10-08 22:14:07 +02:00
bChannel <- k
}
}
func writeInKillfile(key, value string, config string) {
2020-10-08 22:14:07 +02:00
stK := []byte(key)
stV := []byte(value)
MyZabovKDB := MyZabovKDBs[config]
2020-10-08 22:14:07 +02:00
err := MyZabovKDB.Put(stK, stV, nil)
if err != nil {
fmt.Println("Cannot write to Killfile DB: ", err.Error())
}
}
func domainInKillfile(domain string, config string) bool {
2020-10-08 22:14:07 +02:00
s := strings.ToLower(domain)
MyZabovKDB := MyZabovKDBs[config]
2020-10-08 22:14:07 +02:00
has, err := MyZabovKDB.Has([]byte(s), nil)
if err != nil {
fmt.Println("Cannot read from Killfile DB: ", err.Error())
}
return has
}