forked from looterz/grimd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.go
153 lines (120 loc) · 3.13 KB
/
updater.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
)
var timesSeen = make(map[string]int)
var whitelist = make(map[string]bool)
// Update downloads all of the blocklists and imports them into the database
func Update() error {
if _, err := os.Stat("sources"); os.IsNotExist(err) {
if err := os.Mkdir("sources", 0700); err != nil {
return fmt.Errorf("error creating sources directory: %s", err)
}
}
for _, entry := range Config.Whitelist {
whitelist[entry] = true
}
for _, entry := range Config.Blocklist {
BlockCache.Set(entry, true)
}
if err := fetchSources(); err != nil {
return fmt.Errorf("error fetching sources: %s", err)
}
return nil
}
func downloadFile(uri string, name string) error {
filePath := filepath.FromSlash(fmt.Sprintf("sources/%s", name))
output, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("error creating file: %s", err)
}
defer output.Close()
response, err := http.Get(uri)
if err != nil {
return fmt.Errorf("error downloading source: %s", err)
}
defer response.Body.Close()
if _, err := io.Copy(output, response.Body); err != nil {
return fmt.Errorf("error copying output: %s", err)
}
return nil
}
func fetchSources() error {
var wg sync.WaitGroup
for _, uri := range Config.Sources {
wg.Add(1)
u, _ := url.Parse(uri)
host := u.Host
timesSeen[host] = timesSeen[host] + 1
fileName := fmt.Sprintf("%s.%d.list", host, timesSeen[host])
go func(uri string, name string) {
log.Printf("fetching source %s\n", uri)
if err := downloadFile(uri, name); err != nil {
fmt.Println(err)
}
wg.Done()
}(uri, fileName)
}
wg.Wait()
return nil
}
// UpdateBlockCache updates the BlockCache
func UpdateBlockCache() error {
log.Printf("loading blocked domains from %d locations...\n", len(Config.SourceDirs))
for _, dir := range Config.SourceDirs {
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Printf("directory %s not found, skipping\n", dir)
continue
}
err := filepath.Walk(dir, func(path string, f os.FileInfo, _ error) error {
if !f.IsDir() {
file, err := os.Open(filepath.FromSlash(path))
if err != nil {
return fmt.Errorf("error opening file: %s", err)
}
defer file.Close()
if err = parseHostFile(file); err != nil {
return fmt.Errorf("error parsing hostfile %s", err)
}
}
return nil
})
if err != nil {
return fmt.Errorf("error walking location %s\n", err)
}
}
log.Printf("%d domains loaded from sources\n", BlockCache.Length())
return nil
}
func parseHostFile(file *os.File) error {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
isComment := strings.HasPrefix(line, "#")
if !isComment && line != "" {
fields := strings.Fields(line)
if len(fields) > 1 && !strings.HasPrefix(fields[1], "#") {
line = fields[1]
} else {
line = fields[0]
}
if !BlockCache.Exists(line) && !whitelist[line] {
BlockCache.Set(line, true)
}
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error scanning hostfile: %s", err)
}
return nil
}