-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
214 lines (176 loc) · 4.62 KB
/
convert.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
)
type BlockList struct {
Description string `json:"description"`
Name string `json:"name"`
DeniedRemoteAddress []string `json:"denied-remote-addresses,omitempty"`
}
type Filter struct {
CountryID string
}
func extractIPs(source string, file io.Reader, filter Filter) ([]string, error) {
switch source {
case "maxmind":
r := csv.NewReader(file)
return extractIPsFromCSV(r, filter, CSVStructure{
IPCol: 0,
CountryIDCol: 2,
})
case "ip2location_cidr":
s := bufio.NewScanner(file)
return extractIPsFromList(s)
case "ipinfo":
r := csv.NewReader(file)
return extractIPsFromCSV(r, filter, CSVStructure{
IPRange: true,
IPCol: 0,
IPEndCol: 1,
CountryIDCol: 2,
})
}
return nil, fmt.Errorf("source not supported (options: maxmind, ip2location")
}
func main() {
var inputFile string
flag.StringVar(&inputFile, "input_csv", "", "path to maxmind csv file")
var source string
flag.StringVar(&source, "input_source", "maxmind", "source of the ip addresses")
var dir string
flag.StringVar(&dir, "output_dir", "./", "output directory for resulting files")
var filename string
flag.StringVar(&filename, "output_file", "ip_block_list", "filename of the resulting files")
var name string
flag.StringVar(&name, "list_name", "", "name used in blocklist")
var desc string
flag.StringVar(&desc, "list_desc", "", "description used in blocklist")
var filterCountryID string
flag.StringVar(&filterCountryID, "countryid", "", "filter by maxmind country id (supported by maxmind input source only")
flag.Parse()
file, err := os.Open(inputFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
addrs, err := extractIPs(source, file, Filter{CountryID: filterCountryID})
if err != nil {
log.Fatal(err)
}
lists := buildBlockList(name, desc, addrs)
err = writeListsToFile(dir, filename, lists)
if err != nil {
log.Fatal(err)
}
}
// writeListsToFile takes a slice of block lists and writes them to the given directory
func writeListsToFile(dir, filename string, lists []BlockList) error {
for i, list := range lists {
suf := ""
if len(lists) > 1 {
suf = fmt.Sprintf("_%d", i+1)
}
err := writeJSONToFile(list, fmt.Sprintf("%s/%s%s.lsrules", dir, filename, suf))
if err != nil {
return err
}
}
return nil
}
// buildBlockList creates one or more LittleSnitch block list structs for the given slice of IP addresses
func buildBlockList(name, desc string, ips []string) []BlockList {
sets := make([]BlockList, 0)
maxSize := 200000
var j int
for i := 0; i < len(ips); i += maxSize {
j += maxSize
if j > len(ips) {
j = len(ips)
}
batch := ips[i:j]
sets = append(sets, BlockList{
Description: name,
Name: desc,
DeniedRemoteAddress: batch,
})
}
return sets
}
// writeJSONToFile marshals given struct and dumps directly to given path
func writeJSONToFile(j interface{}, path string) error {
contents, err := json.Marshal(j)
if err != nil {
return err
}
return os.WriteFile(path, contents, 0644)
}
type CSVStructure struct {
IPRange bool
IPCol int
IPStartCol int
IPEndCol int
CountryIDCol int
}
// extractIPsFromCSV reads in a CSV file and extracts IPs for a given structure.
func extractIPsFromCSV(r *csv.Reader, filter Filter, structure CSVStructure) ([]string, error) {
var ipNetworks []string
firstRow := true
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if firstRow {
firstRow = false
continue
}
if filter.CountryID != "" && record[structure.CountryIDCol] != filter.CountryID {
continue
}
if structure.IPRange == true {
start := net.ParseIP(record[structure.IPStartCol])
end := net.ParseIP(record[structure.IPEndCol])
if start.To4() == nil || end.To4() == nil {
continue
}
r := fmt.Sprintf("%s-%s", start.String(), end.String())
ipNetworks = append(ipNetworks, r)
continue
}
ipStr := record[structure.IPCol]
_, ipv4Net, err := net.ParseCIDR(ipStr)
if err != nil {
return nil, err
}
ipNetworks = append(ipNetworks, ipv4Net.String())
}
return ipNetworks, nil
}
// extractIPsFromList extracts and returns IPs from a single text list.
func extractIPsFromList(s *bufio.Scanner) ([]string, error) {
var ipNetworks []string
for s.Scan() {
t := s.Text()
if strings.Trim(t, " ")[0] == '#' {
continue
}
_, ipv4Net, err := net.ParseCIDR(t)
if err != nil {
return nil, err
}
ipNetworks = append(ipNetworks, ipv4Net.String())
}
return ipNetworks, nil
}