some library updates and adds support for adblock lists
This commit is contained in:
53
cmd/bind/parsing-adblock.go
Normal file
53
cmd/bind/parsing-adblock.go
Normal file
@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"regexp"
|
||||
|
||||
"github.com/asaskevich/govalidator"
|
||||
|
||||
"gitlab.smoothnet.org/nhyatt/bind-response-policy-zone-creator/internal/log"
|
||||
)
|
||||
|
||||
func parseAdBlock(data []byte) []string {
|
||||
var domains []string
|
||||
|
||||
// convert data to reader for line-by-line reading
|
||||
r := bytes.NewReader(data)
|
||||
|
||||
// process combined files line-by-line
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
// skip lines with the AdBlock header
|
||||
if regexp.MustCompile(`^\[Adblock Plus\]`).MatchString(line) {
|
||||
continue
|
||||
}
|
||||
|
||||
// skip lines where the first non-whitespace character is '#' or '//'
|
||||
if regexp.MustCompile(`^(\s+)?(#|\/\/|\!)`).MatchString(line) {
|
||||
continue
|
||||
}
|
||||
|
||||
// skip lines with no characters and/or whitespace only
|
||||
if regexp.MustCompile(`^(\s+)?$`).MatchString(line) {
|
||||
continue
|
||||
}
|
||||
|
||||
// remove line header
|
||||
d := regexp.MustCompile(`^\|\|`).ReplaceAllString(line, "")
|
||||
|
||||
// remove line footer
|
||||
d = regexp.MustCompile(`\^$`).ReplaceAllString(d, "")
|
||||
|
||||
if govalidator.IsDNSName(d) {
|
||||
domains = append(domains, d)
|
||||
} else {
|
||||
log.Debug("host invalid", "host", d)
|
||||
}
|
||||
}
|
||||
|
||||
return domains
|
||||
}
|
Reference in New Issue
Block a user