forked from JMVoid/ipip2mmdb
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (78 loc) · 2 KB
/
Copy pathmain.go
File metadata and controls
90 lines (78 loc) · 2 KB
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
package main
import (
"bufio"
"flag"
"os"
"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/mmdbtype"
log "github.com/sirupsen/logrus"
)
var (
srcFile string
dstFile string
databaseType string
cnRecord = mmdbtype.Map{
"country": mmdbtype.Map{
"geoname_id": mmdbtype.Uint32(1814991),
"is_in_european_union": mmdbtype.Bool(false),
"iso_code": mmdbtype.String("CN"),
"names": mmdbtype.Map{
"de": mmdbtype.String("China"),
"en": mmdbtype.String("China"),
"es": mmdbtype.String("China"),
"fr": mmdbtype.String("Chine"),
"ja": mmdbtype.String("中国"),
"pt-BR": mmdbtype.String("China"),
"ru": mmdbtype.String("Китай"),
"zh-CN": mmdbtype.String("中国"),
},
},
}
)
func init() {
flag.StringVar(&srcFile, "s", "ipip_cn.txt", "specify source ip list file")
flag.StringVar(&dstFile, "d", "Country.mmdb", "specify destination mmdb file")
flag.StringVar(&databaseType, "t", "GeoIP2-Country", "specify MaxMind database type")
flag.Parse()
}
func main() {
writer, err := mmdbwriter.New(
mmdbwriter.Options{
DatabaseType: databaseType,
RecordSize: 24,
},
)
if err != nil {
log.Fatalf("fail to new writer %v\n", err)
}
var ipTxtList []string
fh, err := os.Open(srcFile)
if err != nil {
log.Fatalf("fail to open %s: %v\n", srcFile, err)
}
defer fh.Close()
scanner := bufio.NewScanner(fh)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
ipTxtList = append(ipTxtList, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatalf("fail to scan file: %v\n", err)
}
ipList := parseCIDRs(ipTxtList)
for _, ip := range ipList {
err = writer.Insert(ip, cnRecord)
if err != nil {
log.Fatalf("fail to insert to writer %v\n", err)
}
}
outFh, err := os.Create(dstFile)
if err != nil {
log.Fatalf("fail to create output file %v\n", err)
}
defer outFh.Close()
_, err = writer.WriteTo(outFh)
if err != nil {
log.Fatalf("fail to write to file %v\n", err)
}
}