forked from andyjack/geoip2-haproxy-ranges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (58 loc) · 1.18 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 (
"fmt"
"log"
"os"
"path"
maxminddb "github.com/oschwald/maxminddb-golang"
"github.com/schollz/progressbar/v3"
)
var db string
var destDir string
func main() {
getFlags()
mmdb, err := maxminddb.Open(db)
if err != nil {
log.Fatal(err)
}
defer mmdb.Close()
record := struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
}{}
if err = os.Mkdir(destDir, os.ModePerm); err != nil && !os.IsExist(err) {
log.Fatal(err)
}
isoToFile := make(map[string](*os.File))
networks := mmdb.Networks()
i := 0
bar := progressbar.Default(-1)
for networks.Next() {
subnet, err := networks.Network(&record)
if err != nil {
log.Fatal(err)
}
country := record.Country.ISOCode
var file = isoToFile[country]
if file == nil {
file, err = os.Create(path.Join(destDir, country+".txt"))
if err != nil {
log.Fatal(err)
}
isoToFile[country] = file
}
_, err = file.WriteString(subnet.String() + "\n")
if err != nil {
log.Fatal(err)
}
bar.Add(1)
i++
}
for _, file := range isoToFile {
if err = file.Close(); err != nil {
log.Fatal(err)
}
}
fmt.Printf("\nWrote %d records from %s\n", i, db)
}