zabov/hostfile.go

126 lines
2.4 KiB
Go
Raw Normal View History

2020-10-08 15:14:07 -05:00
package main
import (
"bufio"
"fmt"
"os"
"strings"
2020-10-08 15:14:07 -05:00
)
func init() {
fmt.Println("Ingesting local hosts file")
ingestLocalBlacklists()
ingestLocalWhiteLists()
2020-10-08 15:14:07 -05:00
}
func ingestLocalBlacklists() {
fmt.Println("ingestLocalBlacklist: collecting urls from all configs...")
_HostsFiles := urlsMap{}
for config := range ZabovConfigs {
ZabovHostsFile := ZabovConfigs[config].ZabovHostsFile
if len(ZabovHostsFile) == 0 {
continue
}
configs := _HostsFiles[ZabovHostsFile]
if configs == nil {
configs = stringarray{}
_HostsFiles[ZabovHostsFile] = configs
}
configs = append(configs, config)
_HostsFiles[ZabovHostsFile] = configs
2020-10-08 15:14:07 -05:00
}
for ZabovHostsFile, configs := range _HostsFiles {
file, err := os.Open(ZabovHostsFile)
if err != nil {
fmt.Println(err.Error())
}
defer file.Close()
2020-10-08 15:14:07 -05:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
d := scanner.Text()
if len(d) == 0 || strings.TrimSpace(d)[0] == '#' {
continue
}
DomainKill(d, ZabovHostsFile, configs)
incrementStats("Blacklist", 1)
2020-10-08 15:14:07 -05:00
}
if err := scanner.Err(); err != nil {
fmt.Println(err.Error())
}
2020-10-08 15:14:07 -05:00
}
}
func ingestLocalWhiteLists() {
fmt.Println("ingestLocalWhiteLists: collecting urls from all configs...")
_WhiteListFiles := urlsMap{}
for config := range ZabovConfigs {
ZabovWhiteList := ZabovConfigs[config].ZabovWhiteList
if len(ZabovWhiteList) == 0 {
continue
}
configs := _WhiteListFiles[ZabovWhiteList]
if configs == nil {
configs = stringarray{}
_WhiteListFiles[ZabovWhiteList] = configs
}
configs = append(configs, config)
_WhiteListFiles[ZabovWhiteList] = configs
}
for ZabovWhiteList, configs := range _WhiteListFiles {
file, err := os.Open(ZabovWhiteList)
if err != nil {
fmt.Println(err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
d := scanner.Text()
if len(d) == 0 || strings.TrimSpace(d)[0] == '#' {
continue
}
DomainWhiteList(d, ZabovWhiteList, configs)
incrementStats("WhiteList", 1)
}
if err := scanner.Err(); err != nil {
fmt.Println(err.Error())
}
}
}
2020-10-08 15:14:07 -05:00
func fileByLines(filename string) (blurls []string) {
file, err := os.Open(filename)
if err != nil {
fmt.Println(err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
d := scanner.Text()
blurls = append(blurls, d)
}
if err := scanner.Err(); err != nil {
fmt.Println(err.Error())
}
return
}