-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (56 loc) · 1.29 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
cidrman "github.com/EvilSuperstars/go-cidrman"
)
func main() {
hostPrefix := flag.Int("hostPrefix", 32, "prefix length for host routes summary")
listURL := flag.String("listURL", "https://raw.githubusercontent.com/zapret-info/z-i/master/dump.csv", "URL to get list of banned resources")
flag.Parse()
resp, err := http.Get(*listURL)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
dump, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
split := bytes.Split(dump, []byte("\n"))
var prefixes []string
for _, line := range split {
IPs := bytes.Split(line, []byte(";"))
IPs = bytes.Split(IPs[0], []byte(" | "))
for _, ip := range IPs {
prefixes = append(prefixes, string(ip))
}
}
var sanitized []string
for _, prefix := range prefixes {
if strings.Contains(prefix, "/") {
continue
}
if net.ParseIP(prefix).To4() == nil {
continue
}
sanitized = append(sanitized, fmt.Sprintf("%s/%d", prefix, *hostPrefix))
}
merged, err := cidrman.MergeCIDRs(sanitized)
if err != nil {
fmt.Println(err)
return
}
for _, v := range merged {
fmt.Println("route " + v + " reject;")
}
log.Println(len(sanitized), "=>>", len(merged))
}