2019-08-25 18:38:08 +00:00
|
|
|
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
|
2019-08-25 19:18:43 +00:00
|
|
|
matcher = regexp.MustCompile(`^(?:[0-9.]+|[a-z0-9:]+)\s+([^\s]+)(?:\s+#(.+)|\s+#)?$`)
|
2019-08-25 18:38:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|