-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
88 lines (78 loc) · 1.54 KB
/
db.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
package country_ip
import (
"bufio"
"bytes"
"errors"
"io"
"log"
"math"
"net"
"strconv"
"strings"
cidr "github.com/yl2chen/cidranger"
)
var (
mCidr map[country]cidr.Ranger
// list of country names
cList []string
)
func initDB() {
mCidr = make(map[country]cidr.Ranger)
cList = make([]string, 0, 100)
data, err := Asset("apnic.txt")
if err != nil {
log.Fatal(err)
}
r := bufio.NewReader(bytes.NewReader(data))
for {
line, _, err := r.ReadLine()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
log.Fatal(err)
}
c, n, ok := parseLineV4(string(line))
if !ok {
continue
}
ranger, ok := mCidr[country(c)]
if !ok {
ranger = cidr.NewPCTrieRanger()
mCidr[country(c)] = ranger
cList = append(cList, c)
}
err = ranger.Insert(cidr.NewBasicRangerEntry(*n))
if err != nil {
log.Fatal(err)
}
}
lanRanger := cidr.NewPCTrieRanger()
mCidr["LAN"] = lanRanger
for _, n := range lanCidr {
_, r, _ := net.ParseCIDR(n)
err := lanRanger.Insert(cidr.NewBasicRangerEntry(*r))
if err != nil {
log.Fatal(err)
}
}
}
func parseLineV4(line string) (string, *net.IPNet, bool) {
if !strings.HasPrefix(line, "apnic|") {
return "", nil, false
}
tokens := strings.Split(line, "|")
if tokens[2] != "ipv4" {
return "", nil, false
}
mask, err := strconv.Atoi(tokens[4])
if err != nil {
return "", nil, false
}
mask = 32 - int(math.Log(float64(mask))/math.Log(2))
_, r, err := net.ParseCIDR(tokens[3] + "/" + strconv.Itoa(mask))
if err != nil {
return "", nil, false
}
return tokens[1], r, true
}