-
Notifications
You must be signed in to change notification settings - Fork 9
/
list_loader.go
84 lines (77 loc) · 2.06 KB
/
list_loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package blocklist
import (
"bufio"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/coredns/caddy"
)
func loadList(c *caddy.Controller, location string) ([]string, error) {
log.Infof("Loading from %s", location)
if strings.HasPrefix(location, "http://") || strings.HasPrefix(location, "https://") {
return loadListFromUrl(c, location)
}
return loadListFromFile(c, location)
}
func loadListFromUrl(c *caddy.Controller, name string) ([]string, error) {
response, err := http.Get(name)
if err != nil {
return nil, err
}
defer response.Body.Close()
return collectDomains(response.Body, name)
}
func loadListFromFile(c *caddy.Controller, name string) ([]string, error) {
if !filepath.IsAbs(name) {
name = filepath.Join(
filepath.Dir(c.File()),
name,
)
}
readFile, err := os.Open(name)
if err != nil {
return nil, err
}
defer readFile.Close()
return collectDomains(readFile, name)
}
func collectDomains(r io.Reader, name string) ([]string, error) {
var domains []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
domains = append(domains, scanner.Text())
}
if scanner.Err() != nil {
return nil, scanner.Err()
}
log.Infof("Loaded %d domains from %s", len(domains), name)
domainCount.WithLabelValues(name).Set(float64(len(domains)))
return domains, nil
}
func toMap(domains []string) map[string]bool {
domainsMap := map[string]bool{}
fullLineCommentRegex := regexp.MustCompile(`^[ ]*#`)
inlineCommentRegex := regexp.MustCompile(`[ ]*#.*$`)
for _, line := range domains {
if fullLineCommentRegex.MatchString(line) {
log.Debugf("Filtered out comment '%s' from blocklist", line)
continue
}
line = inlineCommentRegex.ReplaceAllString(line, "")
line = strings.Replace(line, "0.0.0.0 ", "", 1)
line = strings.Replace(line, "127.0.0.1 ", "", 1)
if line == "" || line == "localhost" || strings.Contains(line, " ") {
continue
}
log.Debugf("Loaded '%s' into blocklist", line)
if strings.HasSuffix(line, ".") {
domainsMap[line] = true
} else {
domainsMap[line+"."] = true
}
}
return domainsMap
}