zabov/hostfile.go

82 lines
1.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()
2020-10-08 15:14:07 -05:00
}
func ingestLocalBlacklists() {
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
2020-10-08 15:14:07 -05:00
}
for ZabovHostsFile, configs := range _files {
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 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
}