-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (148 loc) · 4.53 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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net"
"os"
"strings"
"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/inserter"
"github.com/maxmind/mmdbwriter/mmdbtype"
)
var (
writer *mmdbwriter.Tree
suffix = "/32"
name_field = "city_name"
floor_as_timezone = true
default_continent_name = "Europe"
default_continent_geonames_id = 6255148
default_continent_code = "EU"
default_country_name = "Germany"
default_country_geonames_id = 2921044
default_country_code = "DE"
default_is_in_european_union = true
default_city_name = "Göttingen"
default_city_geoname_id = 2918632
default_lat float64 = 51.5441
default_lon float64 = 9.9254
default_accuracy float32 = 5
)
type Ip struct {
Ip string `json:"ip"`
Name string `json:"name"`
Floor string `json:"floor"`
Accuracy float32 `json:"accuracy_radius"`
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
Coordinates [2]float64 `json:"coordinates"`
}
func main() {
database := flag.String("d", "GeoLite2-Country.mmdb", "database")
ips := flag.String("i", "ips.ndjson", "ips")
output := flag.String("o", "geoip.mmdb", "output")
dbtype := flag.String("t", "GeoIP-City", "type")
flag.Parse()
//Init source
var err error
if *database != "" {
// Load the database we wish to enrich.
writer, err = mmdbwriter.Load(*database, mmdbwriter.Options{IncludeReservedNetworks: true})
} else {
description := map[string]string{"en": "Simple Database for GeoIP lookups"}
writer, err = mmdbwriter.New(mmdbwriter.Options{IncludeReservedNetworks: true, IPVersion: 4, DatabaseType: *dbtype, Languages: []string{"en", "de"}, Description: description})
}
if err != nil {
log.Fatal(err)
} else {
err = nil
}
reader, err := os.Open(*ips)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
decoder := json.NewDecoder(reader)
for decoder.More() {
ip := Ip{Accuracy: default_accuracy, Coordinates: [2]float64{default_lon, default_lat}}
if err := decoder.Decode(&ip); err != nil {
log.Fatal(err)
}
var ipv4Net *net.IPNet
var err error
if !strings.Contains(ip.Ip, "/") {
_, ipv4Net, err = net.ParseCIDR(ip.Ip + suffix)
} else {
_, ipv4Net, err = net.ParseCIDR(ip.Ip)
}
if err != nil {
log.Fatal(err)
}
var lon float64
var lat float64
if ip.Lon != 0 && ip.Lat != 0 {
lon = ip.Lon
lat = ip.Lat
} else {
lon = ip.Coordinates[0]
lat = ip.Coordinates[1]
}
location := mmdbtype.Map{
"longitude": mmdbtype.Float64(lon),
"latitude": mmdbtype.Float64(lat),
}
if floor_as_timezone {
location["time_zone"] = mmdbtype.String(ip.Floor)
}
location["accuracy_radius"] = mmdbtype.Int32(ip.Accuracy)
locData := mmdbtype.Map{
"continent": mmdbtype.Map{
"geoname_id": mmdbtype.Uint32(default_continent_geonames_id),
"code": mmdbtype.String(default_continent_code),
"names": mmdbtype.Map{
"en": mmdbtype.String(default_continent_name),
},
},
"country": mmdbtype.Map{
"geoname_id": mmdbtype.Uint32(default_country_geonames_id),
"iso_code": mmdbtype.String(default_country_code),
"is_in_european_union": mmdbtype.Bool(default_is_in_european_union),
"names": mmdbtype.Map{
"en": mmdbtype.String(default_country_name),
},
},
"city": mmdbtype.Map{
"geoname_id": mmdbtype.Uint32(default_city_geoname_id),
"names": mmdbtype.Map{
"en": mmdbtype.String(default_city_name),
"de": mmdbtype.String(default_city_name),
}},
"name": mmdbtype.String(ip.Name),
"location": location,
"subdivisions": mmdbtype.Slice{
mmdbtype.Map{
"iso_code": mmdbtype.String(fmt.Sprintf("%2s", ip.Floor)),
"name": mmdbtype.String(ip.Name),
"names": mmdbtype.Map{
"en": mmdbtype.String(ip.Name),
"de": mmdbtype.String(ip.Name),
}}},
"floor": mmdbtype.String(ip.Floor),
}
err = writer.InsertFunc(ipv4Net, inserter.TopLevelMergeWith(locData))
if err != nil {
log.Fatal(err)
}
log.Printf("Inserted %s, name '%s': {lon: %f, lat:%f}, floor: %s, accuracy_radius: %f", ipv4Net, ip.Name, lon, lat, ip.Floor, ip.Accuracy)
}
// Write results
fh, err := os.Create(*output)
if err != nil {
log.Fatal(err)
}
_, err = writer.WriteTo(fh)
if err != nil {
log.Fatal(err)
}
}