-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
147 lines (112 loc) · 2.76 KB
/
utils.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
package main
import (
"bufio"
"fmt"
"net"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"github.com/wisepythagoras/geoip-service/extension"
)
func sliceContains[T any](arr []T, thing T) bool {
for _, v := range arr {
if reflect.DeepEqual(v, thing) {
return true
}
}
return false
}
// ParseIPList parses a list of IP addresses and CIDR ranges into two lists which could be used
// as a whitelist or blacklist.
func ParseIPList(file *os.File) ([]*net.IPNet, []net.IP, error) {
scanner := bufio.NewScanner(file)
ipRanges := []*net.IPNet{}
ipAddresses := []net.IP{
net.ParseIP("127.0.0.1"),
}
for scanner.Scan() {
line := scanner.Text()
re := regexp.MustCompile(`(#(?:[^\n]+)?)`)
line = strings.Trim(re.ReplaceAllLiteralString(line, ""), " ")
if len(line) == 0 {
continue
}
isCidrRange := strings.Contains(line, "/")
if isCidrRange {
_, ipRange, err := net.ParseCIDR(line)
if err != nil {
return nil, nil, err
}
ipRanges = append(ipRanges, ipRange)
} else {
ipAddresses = append(ipAddresses, net.ParseIP(line))
}
}
if err := scanner.Err(); err != nil {
return nil, nil, err
}
return ipRanges, ipAddresses, nil
}
// ParseDNSServerList parses a file which has a DNS server on each line. The format is:
// 1.1.1.1:53 for the DNS server. Comments are allowed on the same line preceeded by #.
func ParseDNSServerList(file *os.File) ([]string, error) {
scanner := bufio.NewScanner(file)
dnsServerList := []string{}
for scanner.Scan() {
line := scanner.Text()
re := regexp.MustCompile(`(#(?:[^\n]+)?)`)
line = strings.Trim(re.ReplaceAllLiteralString(line, ""), " ")
if len(line) == 0 {
continue
}
if !strings.Contains(line, ":") {
line = fmt.Sprintf("%s:53", line)
}
dnsServerList = append(dnsServerList, line)
}
return dnsServerList, nil
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if err != nil || os.IsNotExist(err) {
return false
}
return true
}
func parseExtensions(path string) ([]*extension.Extension, error) {
files, err := os.ReadDir(path)
extensions := []*extension.Extension{}
if err != nil {
return extensions, err
}
for _, f := range files {
if !f.IsDir() {
continue
}
extFiles, err := os.ReadDir(filepath.Join(path, f.Name()))
if err != nil {
return extensions, err
}
var entry os.DirEntry = nil
for _, extFile := range extFiles {
if extFile.IsDir() {
continue
}
if extFile.Name() == "index.js" {
entry = extFile
}
}
if entry == nil {
return extensions, fmt.Errorf("extension folder %q doesn't have an entry point (index.js)", f.Name())
}
extension := &extension.Extension{
ExtDir: path,
Dir: f,
Entry: entry,
}
extensions = append(extensions, extension)
}
return extensions, nil
}