/
geoip_cgo.go
336 lines (300 loc) · 9.67 KB
/
geoip_cgo.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2013 The Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build cgo
package geoip
//#include "GeoIP.h"
//#include "GeoIPCity.h"
//#include <stdio.h>
//#cgo LDFLAGS: -lGeoIP
import "C"
import (
"encoding/binary"
"errors"
"fmt"
"net"
"strconv"
"strings"
"syscall"
"time"
"unsafe"
)
const (
COUNTRY_EDITION = C.GEOIP_COUNTRY_EDITION
CITY_EDITION_REV1 = C.GEOIP_CITY_EDITION_REV1
REGION_EDITION_REV1 = C.GEOIP_REGION_EDITION_REV1
ISP_EDITION = C.GEOIP_ISP_EDITION
ORG_EDITION = C.GEOIP_ORG_EDITION
CITY_EDTION = C.GEOIP_CITY_EDITION_REV0
REGTION_EDITION_REV0 = C.GEOIP_REGION_EDITION_REV0
PROXY_EDTION = C.GEOIP_PROXY_EDITION
ASNUM_EDITION = C.GEOIP_ASNUM_EDITION
NETSPEED_EDITION = C.GEOIP_NETSPEED_EDITION
DOMAIN_EDITION = C.GEOIP_DOMAIN_EDITION
COUNTRY_EDITION_V6 = C.GEOIP_COUNTRY_EDITION_V6
LOCATIONA_EDITION = C.GEOIP_LOCATIONA_EDITION
ACCURACYRADIUS_EDITION = C.GEOIP_ACCURACYRADIUS_EDITION
CITYCONFIDENCE_EDITION = C.GEOIP_CITYCONFIDENCE_EDITION
CITYCONFIDENCEDIST_EDITION = C.GEOIP_CITYCONFIDENCEDIST_EDITION
LARGE_COUNTRY_EDITION = C.GEOIP_LARGE_COUNTRY_EDITION
LARGE_COUNTRY_EDITION_V6 = C.GEOIP_LARGE_COUNTRY_EDITION_V6
CITYCONFIDENCEDIST_ISP_ORG_EDITION = C.GEOIP_CITYCONFIDENCEDIST_ISP_ORG_EDITION
CCM_COUNTRY_EDITION = C.GEOIP_CCM_COUNTRY_EDITION
ASNUM_EDITION_V6 = C.GEOIP_ASNUM_EDITION_V6
ISP_EDITION_V6 = C.GEOIP_ISP_EDITION_V6
ORG_EDITION_V6 = C.GEOIP_ORG_EDITION_V6
DOMAIN_EDITION_V6 = C.GEOIP_DOMAIN_EDITION_V6
LOCATIONA_EDITION_V6 = C.GEOIP_LOCATIONA_EDITION_V6
REGISTRAR_EDITION = C.GEOIP_REGISTRAR_EDITION
REGISTRAR_EDITION_V6 = C.GEOIP_REGISTRAR_EDITION_V6
USERTYPE_EDITION = C.GEOIP_USERTYPE_EDITION
USERTYPE_EDITION_V6 = C.GEOIP_USERTYPE_EDITION_V6
CITY_EDITION_REV1_V6 = C.GEOIP_CITY_EDITION_REV1_V6
CITY_EDITION_REV0_V6 = C.GEOIP_CITY_EDITION_REV0_V6
NETSPEED_EDITION_REV1 = C.GEOIP_NETSPEED_EDITION_REV1
NETSPEED_EDITION_REV1_V6 = C.GEOIP_NETSPEED_EDITION_REV1_V6
)
func (gi *GeoIP) IsIPv4Database() bool {
switch gi.edition {
case COUNTRY_EDITION,
CITY_EDITION_REV1,
REGION_EDITION_REV1,
ISP_EDITION,
ORG_EDITION,
CITY_EDTION,
REGTION_EDITION_REV0,
PROXY_EDTION,
ASNUM_EDITION,
NETSPEED_EDITION,
DOMAIN_EDITION,
LOCATIONA_EDITION,
ACCURACYRADIUS_EDITION,
CITYCONFIDENCE_EDITION,
CITYCONFIDENCEDIST_EDITION,
LARGE_COUNTRY_EDITION,
CITYCONFIDENCEDIST_ISP_ORG_EDITION,
CCM_COUNTRY_EDITION,
NETSPEED_EDITION_REV1,
REGISTRAR_EDITION,
USERTYPE_EDITION:
return true
}
return false
}
func (gi *GeoIP) IsIPv6Database() bool {
switch gi.edition {
case COUNTRY_EDITION_V6,
LARGE_COUNTRY_EDITION_V6,
ASNUM_EDITION_V6,
ISP_EDITION_V6,
ORG_EDITION_V6,
DOMAIN_EDITION_V6,
LOCATIONA_EDITION_V6,
REGISTRAR_EDITION_V6,
USERTYPE_EDITION_V6,
CITY_EDITION_REV1_V6,
CITY_EDITION_REV0_V6,
NETSPEED_EDITION_REV1_V6:
return true
}
return false
}
func (gi *GeoIP) IsCountryDatabase() bool {
switch gi.edition {
case COUNTRY_EDITION,
COUNTRY_EDITION_V6,
LARGE_COUNTRY_EDITION,
LARGE_COUNTRY_EDITION_V6,
CCM_COUNTRY_EDITION:
return true
}
return false
}
func (gi *GeoIP) IsCityDatabase() bool {
switch gi.edition {
case CITY_EDITION_REV1,
CITY_EDTION,
CITYCONFIDENCE_EDITION,
CITYCONFIDENCEDIST_EDITION,
CITYCONFIDENCEDIST_ISP_ORG_EDITION,
CITY_EDITION_REV1_V6,
CITY_EDITION_REV0_V6:
return true
}
return false
}
type GeoIP struct {
gi *C.GeoIP
edition int
}
func checkedCString(goVal string) *C.char {
cVal := C.CString(goVal)
if cVal == nil {
panic(syscall.ENOMEM)
}
return cVal
}
func New() (gi *GeoIP, err error) {
gi = new(GeoIP)
gi.gi = C.GeoIP_new(C.GEOIP_MEMORY_CACHE)
if gi.gi == nil {
err = errors.New("GeoIP_new failed")
return
}
return
}
func Open(filename string) (gi *GeoIP, err error) {
cFilename := checkedCString(filename)
defer C.free(unsafe.Pointer(cFilename))
gi = new(GeoIP)
gi.gi = C.GeoIP_open(cFilename, C.GEOIP_MEMORY_CACHE)
if gi.gi == nil {
err = errors.New("GeoIP_open failed")
return
}
gi.edition = gi.DatabaseEdition()
return
}
func (gi *GeoIP) DatabaseInfo() string {
// this call returns a newly allocated CString
info := C.GeoIP_database_info(gi.gi)
defer C.free(unsafe.Pointer(info))
goinfo := C.GoString(info)
return goinfo
}
func (gi *GeoIP) DatabaseEdition() (code int) {
return int(C.GeoIP_database_edition(gi.gi))
}
func (gi *GeoIP) DatabaseCreateTime() (time.Time, error) {
info := gi.DatabaseInfo()
infos := strings.Split(info, " ")
if len(infos) < 2 {
return time.Time{}, errors.New(fmt.Sprintf("unable to parse time when database info = %v", info))
}
if len(infos[1]) != 8 {
return time.Time{}, errors.New(fmt.Sprintf("unable to parse date when database info = %v", info))
}
year, err := strconv.Atoi(infos[1][0:4])
if err != nil {
return time.Time{}, err
}
month, err := strconv.Atoi(infos[1][4:6])
if err != nil {
return time.Time{}, err
}
day, err := strconv.Atoi(infos[1][6:8])
if err != nil {
return time.Time{}, err
}
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC), nil
}
func (gi *GeoIP) DbAvail(typ int) (avail bool) {
return C.GeoIP_db_avail(C.int(typ)) == 1
}
func (gi *GeoIP) CountryCodeByIPv4(ip net.IP) (code string) {
return gi.CountryCodeByIPNum(binary.BigEndian.Uint32(ip.To4()))
}
func (gi *GeoIP) CountryCodeByIPNum(ipnum uint32) (code string) {
// this call returns a static CString
return C.GoString(C.GeoIP_country_code_by_ipnum(gi.gi, C.ulong(ipnum)))
}
func (gi *GeoIP) CountryCodeByIPv6(ip net.IP) (code string) {
cip := checkedCString(ip.String())
defer C.free(unsafe.Pointer(cip))
// this call returns a static CString
return C.GoString(C.GeoIP_country_code_by_name_v6(gi.gi, cip))
}
func (gi *GeoIP) CountryCode3ByIPv4(ip net.IP) (code3 string) {
return gi.CountryCode3ByIPNum(binary.BigEndian.Uint32(ip.To4()))
}
func (gi *GeoIP) CountryCode3ByIPNum(ipnum uint32) (code3 string) {
// this call returns a static CString
return C.GoString(C.GeoIP_country_code3_by_ipnum(gi.gi, C.ulong(ipnum)))
}
func (gi *GeoIP) CountryNameByIPv4(ip net.IP) (name string) {
return gi.CountryNameByIPNum(binary.BigEndian.Uint32(ip.To4()))
}
func (gi *GeoIP) CountryNameByIPNum(ipnum uint32) (name string) {
// this call returns a static CString
return C.GoString(C.GeoIP_country_name_by_ipnum(gi.gi, C.ulong(ipnum)))
}
func (gi *GeoIP) RecordByIPv4(ip net.IP) (gir *GeoIPRecord) {
return gi.RecordByIPNum(binary.BigEndian.Uint32(ip.To4()))
}
func (gi *GeoIP) RecordByIPNum(ipnum uint32) (gir *GeoIPRecord) {
cGir := C.GeoIP_record_by_ipnum(gi.gi, C.ulong(ipnum))
if cGir == nil {
return nil
}
// this call frees all the CStrings in cGir
defer C.GeoIPRecord_delete(cGir)
gir = new(GeoIPRecord)
gir.CountryCode = C.GoString(cGir.country_code)
gir.CountryCode3 = C.GoString(cGir.country_code3)
gir.CountryName = C.GoString(cGir.country_name)
gir.Region = C.GoString(cGir.region)
gir.City = latin1toUTF8([]byte(C.GoString(cGir.city)))
gir.PostalCode = C.GoString(cGir.postal_code)
gir.Latitude = float64(cGir.latitude)
gir.Longitude = float64(cGir.longitude)
gir.AreaCode = int(cGir.area_code)
gir.ContinentCode = C.GoString(cGir.continent_code)
return
}
func (gi *GeoIP) CityByIPNum(ipnum uint32) string {
cGir := C.GeoIP_record_by_ipnum(gi.gi, C.ulong(ipnum))
if cGir == nil {
return ""
}
// this call frees all the CStrings in cGir
defer C.GeoIPRecord_delete(cGir)
return latin1toUTF8([]byte(C.GoString(cGir.city)))
}
func (gi *GeoIP) RecordByIPv6(ip net.IP) (gir *GeoIPRecord) {
cip := checkedCString(ip.String())
defer C.free(unsafe.Pointer(cip))
cGir := C.GeoIP_record_by_addr_v6(gi.gi, cip)
if cGir == nil {
return nil
}
// this call frees all the CStrings in cGir
defer C.GeoIPRecord_delete(cGir)
gir = new(GeoIPRecord)
gir.CountryCode = C.GoString(cGir.country_code)
gir.CountryCode3 = C.GoString(cGir.country_code3)
gir.CountryName = C.GoString(cGir.country_name)
gir.Region = C.GoString(cGir.region)
gir.City = latin1toUTF8([]byte(C.GoString(cGir.city)))
gir.PostalCode = C.GoString(cGir.postal_code)
gir.Latitude = float64(cGir.latitude)
gir.Longitude = float64(cGir.longitude)
gir.AreaCode = int(cGir.area_code)
gir.ContinentCode = C.GoString(cGir.continent_code)
return
}
func (gi *GeoIP) Delete() {
C.GeoIP_delete(gi.gi)
}
func CodeByID(id int) (code string) {
// this call returns a static CString
return C.GoString(C.GeoIP_code_by_id(C.int(id)))
}
func Code3ByID(id int) (code3 string) {
// this call returns a static CString
return C.GoString(C.GeoIP_code3_by_id(C.int(id)))
}
func NameByID(id int) (name string) {
// this call returns a static CString
return C.GoString(C.GeoIP_name_by_id(C.int(id)))
}
func ContinentByID(id int) (continent string) {
// this call returns a static CString
return C.GoString(C.GeoIP_continent_by_id(C.int(id)))
}