54 lines
1.1 KiB
Go

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
}