1
0
mirror of https://github.com/Luzifer/named-blacklist.git synced 2024-09-16 14:58:29 +00:00
named-blacklist/provider_hostFile.go
Knut Ahlers b565234570
Make comments sortable for diffing generated files
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2020-02-15 14:22:35 +01:00

73 lines
1.5 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],
Comments: []string{comment},
})
}
return entries, nil
}