-
Notifications
You must be signed in to change notification settings - Fork 2
/
domains.go
79 lines (66 loc) · 1.55 KB
/
domains.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
package warnlist
import (
"bufio"
"io"
"net/http"
"os"
"strings"
)
const (
DomainFileFormatHostfile = "hostfile"
DomainFileFormatTextList = "text"
DomainSourceTypeFile = "file"
DomainSourceTypeURL = "url"
)
func domainsFromSource(source string, sourceType string, sourceFormat string) chan string {
c := make(chan string)
go func() {
defer close(c)
var sourceData io.Reader
{
if sourceType == DomainSourceTypeFile {
log.Infof("Loading from file: %s", source)
file, err := os.Open(source)
if err != nil {
log.Error(err)
}
defer file.Close()
sourceData = file
} else if sourceType == DomainSourceTypeURL {
// TODO
log.Infof("Loading from URL: %s", source)
// Load the domain list from the URL
resp, err := http.Get(source) // nolint: gosec
if err != nil {
log.Error(err)
}
defer resp.Body.Close()
sourceData = resp.Body
}
}
scanner := bufio.NewScanner(sourceData)
for scanner.Scan() {
domain := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(domain, "#") {
// Skip comment lines
continue
}
if domain == "" {
// Skip empty lines
continue
}
if sourceFormat == DomainFileFormatHostfile {
domain = strings.Fields(domain)[1] // Assumes hostfile format: 127.0.0.1 some.host
}
// Assume all domains are global origin, with trailing dot (e.g. example.com.)
if !strings.HasSuffix(domain, ".") {
domain += "."
}
c <- domain
}
if err := scanner.Err(); err != nil {
log.Error(err)
}
}()
return c
}