-
Notifications
You must be signed in to change notification settings - Fork 0
/
reload.go
54 lines (47 loc) · 1.05 KB
/
reload.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
package matchrelay
import (
"io/ioutil"
"strings"
"strconv"
"bufio"
"bytes"
"github.com/coredns/coredns/plugin/pkg/log"
)
// Reload - function which reloads the rules
func (mr *MatchRelay) Reload(buf []byte) {
mr.rules = nil
mr.domains = make(map[string]string)
r := rule{}
scanner := bufio.NewScanner(bytes.NewReader(buf))
for scanner.Scan() {
line := scanner.Text()
fields := strings.Split(line, " ")
if fields[0] == "net" {
id := fields[0]
fields = fields[1:]
p := makePolicy(fields)
if p.filter != nil {
p.ftype = id
r.policies = append(r.policies, p)
}
} else if fields[0] == "domain" {
if fields[1] != "" {
mr.domains[fields[1]] = strconv.Itoa(len(buf))
}
}
}
if err := scanner.Err(); err != nil {
log.Errorf("read line error %v", err)
}
if len(r.policies) > 0 {
mr.rules = append(mr.rules, r)
}
}
func fileOpen(fileName string) ([]byte, error) {
file, err := ioutil.ReadFile(fileName)
if err != nil {
log.Errorf("error opening file %s", fileName)
return file, err
}
return file, nil
}