1
0
Fork 0
mirror of https://github.com/Luzifer/named-blacklist.git synced 2024-11-08 07:20:07 +00:00
named-blacklist/provider_hostFile.go
Knut Ahlers 09170e8f8d
Fix comments, speed up
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2020-02-11 16:13:37 +01: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+#(.+)|\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("%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
}