1
0
Fork 0
mirror of https://github.com/Luzifer/named-blacklist.git synced 2024-11-09 16:00:09 +00:00
named-blacklist/provider_hostFile.go
2019-08-25 20:38:08 +02:00

72 lines
1.4 KiB
Go

package main
import (
"bufio"
"fmt"
"regexp"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func init() {
registerProvider("hosts-file", providerHostFile{})
}
type providerHostFile struct{}
func (p providerHostFile) GetDomainList(d providerDefinition) ([]entry, error) {
r, err := d.GetContent()
if err != nil {
return nil, errors.Wrap(err, "Unable to get source content")
}
defer r.Close()
logger := log.WithField("provider", d.Name)
var (
entries []entry
matcher = regexp.MustCompile(`^(?:[0-9.]+|[a-z0-9:]+)\s+([^\s]+)(?:\s+#(.+))?$`)
)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if lineIsComment(line) {
continue
}
if !matcher.MatchString(line) {
logger.WithField("line", line).Warn("Invalid line found (format)")
continue
}
groups := matcher.FindStringSubmatch(line)
if len(groups) < 2 {
logger.WithField("line", line).Warn("Invalid line found (groups)")
continue
}
if isBlacklisted(groups[1]) {
logger.WithField("domain", groups[1]).Debug("Skipping because of blacklist")
continue
}
comment := fmt.Sprintf("From: %q", d.Name)
if len(groups) == 3 && strings.Trim(groups[2], "#") != "" {
comment = fmt.Sprintf("%s, Comment: %q",
comment,
strings.TrimSpace(strings.Trim(groups[2], "#")),
)
}
entries = append(entries, entry{
Domain: groups[1],
Comment: comment,
})
}
return entries, nil
}