-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
142 lines (125 loc) · 3.17 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
package main
import (
"bufio"
"log"
"net"
"os"
"strings"
"github.com/Netnod/go-cidrman"
"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/mmdbtype"
"github.com/spf13/pflag"
)
func main() {
var (
cidrSource []string
cidrOutput string
mmdbOutput string
showHelp bool
)
pflag.StringSliceVarP(&cidrSource, "source", "i", nil, "CIDR source files")
pflag.StringVarP(&cidrOutput, "cidr-output", "o", "cidr.txt", "CIDR ouput")
pflag.StringVarP(&mmdbOutput, "mmdb-output", "m", "Country.mmdb", "MMDB ouput")
pflag.BoolVarP(&showHelp, "help", "h", false, "Show usage")
pflag.CommandLine.SortFlags = false
pflag.Parse()
if showHelp {
pflag.Usage()
os.Exit(0)
}
if len(cidrSource) < 1 {
log.Fatal("at least one cidr source file is required")
}
data, err := merge(cidrSource)
if err != nil {
log.Fatal("merge cidr: " + err.Error())
}
if err := saveFile(data, cidrOutput); err != nil {
log.Fatal(err)
}
if err := buildMMDB(data, mmdbOutput); err != nil {
log.Fatal(err)
}
}
func merge(source []string) ([]*net.IPNet, error) {
cidrMap := make(map[string]struct{})
for _, file := range source {
f, err := os.Open(file)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
cidrMap[line] = struct{}{}
}
f.Close()
}
ips := make([]*net.IPNet, 0, len(cidrMap))
for cidr := range cidrMap {
_, c, err := net.ParseCIDR(cidr)
if err != nil {
log.Println(err)
continue
}
ips = append(ips, c)
}
return cidrman.MergeIPNets(ips)
}
func saveFile(data []*net.IPNet, output string) error {
outputF, err := os.Create(output)
if err != nil {
return err
}
defer outputF.Close()
writer := bufio.NewWriter(outputF)
for _, v := range data {
writer.WriteString(v.String() + "\n")
}
return writer.Flush()
}
func buildMMDB(data []*net.IPNet, output string) error {
outputF, err := os.Create(output)
if err != nil {
return err
}
defer outputF.Close()
writer, err := mmdbwriter.New(mmdbwriter.Options{
DatabaseType: "GeoIP2-Country",
RecordSize: 24,
})
if err != nil {
return err
}
// https://github.com/Hackl0us/GeoIP2-CN/blob/c053afa7ef3d092b1ea84aa229fe035a49fe3603/main.go#L64
// https://dev.maxmind.com/geoip/docs/databases/city-and-country
// https://dev.maxmind.com/static/pdf/GeoLite2-and-GeoIP2-Precision-Web-Services-Comparison.pdf
dataType := 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("中国"),
},
},
}
for _, v := range data {
if err := writer.Insert(v, dataType); err != nil {
log.Println("fail to insert " + v.String())
}
}
_, err = writer.WriteTo(outputF)
return err
}