-
Notifications
You must be signed in to change notification settings - Fork 78
/
main.go
209 lines (169 loc) · 4.31 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
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
package main
import (
"encoding/csv"
"flag"
"log"
"net"
"os"
"path/filepath"
"strconv"
"time"
"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/mmdbtype"
)
var (
writer *mmdbwriter.Tree
isNew bool
workDir string
out string
)
func init() {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
flag.BoolVar(&isNew, "new", false, "Do not re using official database and create new mmdb. Default false.")
flag.StringVar(&workDir, "dir", dir, "The directory which contains ip list file. Default executable file directory.")
flag.StringVar(&out, "out", "china_ip_list.mmdb", "The output mmdb file name. Default china_ip_list.mmdb.")
flag.Parse()
}
func parseIpNets(strIps []string) []*net.IPNet {
var ipNets = make([]*net.IPNet, 0, 50)
for _, strIp := range strIps {
_, ipNet, err := net.ParseCIDR(strIp)
if err != nil || ipNet == nil {
log.Printf("%s fail to parse to CIDR\n", strIp)
continue
}
ipNets = append(ipNets, ipNet)
}
return ipNets
}
func insertIps(strIps []string, data mmdbtype.DataType) {
ipList := parseIpNets(strIps)
for _, ip := range ipList {
err := writer.Insert(ip, data)
if err != nil {
log.Fatalf("fail to insert to writer %v\n", err)
}
}
}
func testResult(mmdbFile string) {
log.Print("Sleep 5 minutes before test ips.")
time.Sleep(time.Duration(5) * time.Second)
//testAllIPDiff("GeoLite2-Country.mmdb", out)
testSingleIp("1.4.9.249", mmdbFile)
testSingleIp("2400:da00::1", mmdbFile)
}
func insertCsvSkipCN(csvName string) {
ipCsvFile, err := os.Open(filepath.Join(workDir, "mindmax", csvName))
if err != nil {
log.Fatalf("fail to open %s\n", err)
}
reader := csv.NewReader(ipCsvFile)
ipCsvLines, err := reader.ReadAll()
if err != nil {
log.Printf("fail to read csv %s\n", err)
return
}
for index, value := range ipCsvLines {
if index == 0 {
continue
}
_, ipNet, err := net.ParseCIDR(value[0])
if err != nil || ipNet == nil {
log.Printf("%s fail to parse to CIDR\n", value[0])
continue
}
geoNameId, _ := strconv.ParseUint(value[1], 10, 32)
if geoNameId == 1814991 {
continue
}
err = writer.Insert(ipNet, liteCountryMap[geoNameId])
if err != nil {
log.Fatalf("fail to insert to writer %v\n", err)
}
}
}
func buildAll() {
log.Print("Start build all.")
var err error
if isNew {
writer, err = mmdbwriter.New(
mmdbwriter.Options{
DatabaseType: "GeoIP2-Country",
RecordSize: 24,
Languages: []string{"de", "ru", "pt-BR", "ja", "en", "fr", "zh-CN", "es"},
},
)
} else {
writer, err = mmdbwriter.Load(filepath.Join(workDir, "GeoLite2-Country.mmdb"), mmdbwriter.Options{})
}
if err != nil {
log.Fatal(err)
}
if isNew {
// 0 mindmax data
insertCsvSkipCN("GeoLite2-Country-Blocks-IPv4.csv")
insertCsvSkipCN("GeoLite2-Country-Blocks-IPv6.csv")
}
// 1
insertIps(chinaIpList, cnData)
insertIps(clangIpV6List, cnData)
// 2
insertIps(clangIpV4List, cnData)
insertIps(chunzhenIpList, cnData)
// 3
insertIps(chinaOperatorIpV4List, cnData)
insertIps(chinaOperatorIpV6List, cnData)
// 4
insertIps(cloudCNListV4List, cnData)
insertIps(cloudCNListV6List, cnData)
fh, err := os.Create(filepath.Join(workDir, out))
if err != nil {
log.Fatal(err)
}
_, err = writer.WriteTo(fh)
if err != nil {
log.Fatal(err)
}
log.Print("End build all.")
testResult(out)
}
func buildLite() {
log.Print("Start build lite.")
var err error
writer, _ = mmdbwriter.New(
mmdbwriter.Options{
DatabaseType: "GeoIP2-Country",
RecordSize: 24,
Languages: []string{"zh-CN"},
},
)
// 0 mindmax data
// insertCsvSkipCN("GeoLite2-Country-Blocks-IPv4.csv")
// insertCsvSkipCN("GeoLite2-Country-Blocks-IPv6.csv")
// 1
insertIps(chinaIpList, liteCountryMap[1814991])
insertIps(clangIpV6List, liteCountryMap[1814991])
// 2
insertIps(clangIpV4List, liteCountryMap[1814991])
insertIps(chunzhenIpList, liteCountryMap[1814991])
// 3
insertIps(chinaOperatorIpV4List, liteCountryMap[1814991])
insertIps(chinaOperatorIpV6List, liteCountryMap[1814991])
// 4
insertIps(cloudCNListV4List, cnData)
insertIps(cloudCNListV6List, cnData)
fh, err := os.Create(filepath.Join(workDir, "lite_"+out))
if err != nil {
log.Fatal(err)
}
_, err = writer.WriteTo(fh)
if err != nil {
log.Fatal(err)
}
log.Print("End build lite.")
testResult("lite_" + out)
}
func main() {
buildAll()
buildLite()
}