-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeoip.go
61 lines (55 loc) · 1.42 KB
/
geoip.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
package main
import (
// import "github.com/IncSW/geoip2"
"github.com/oschwald/geoip2-golang"
"net"
)
type GeoIPCountryStateCityFun func(ip net.IP) (string, string, string)
var GeoIPCountryStateCity GeoIPCountryStateCityFun
//func InitGeoipReader(maxmindDbPath string) {
// reader, err := geoip2.NewCityReaderFromFile(maxmindDbPath)
// if err != nil {
// log.Info(err)
// GeoIPCountryStateCity = func(ip net.IP) (string, string, string) {
// return "", "", ""
// }
// return
// }
//
// GeoIPCountryStateCity = func(ip net.IP) (countryCode, stateName, city string) {
// record, err := reader.Lookup(ip)
// if err != nil {
// log.Warn(err)
// return "", "", ""
// }
// countryCode = record.Country.ISOCode
// if len(record.Subdivisions) != 0 {
// stateName = record.Subdivisions[0].Names["en"]
// }
// city = record.City.Names["en"]
// return
// }
//}
func InitGeoipReader(maxmindDbPath string) {
db_, err := geoip2.Open(maxmindDbPath)
if err != nil {
log.Info(err)
GeoIPCountryStateCity = func(ip net.IP) (string, string, string) {
return "", "", ""
}
return
}
GeoIPCountryStateCity = func(ip net.IP) (countryCode, stateName, city string) {
record, err := db_.City(ip)
if err != nil {
log.Warn(err)
return "", "", ""
}
countryCode = record.Country.IsoCode
if len(record.Subdivisions) != 0 {
stateName = record.Subdivisions[0].Names["en"]
}
city = record.City.Names["en"]
return
}
}