forked from abh/geodns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoip.go
142 lines (116 loc) · 2.7 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
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 (
"github.com/abh/geodns/Godeps/_workspace/src/github.com/abh/geoip"
"github.com/abh/geodns/countries"
"log"
"net"
"strings"
"time"
)
type GeoIP struct {
country *geoip.GeoIP
hasCountry bool
countryLastLoad time.Time
city *geoip.GeoIP
cityLastLoad time.Time
hasCity bool
asn *geoip.GeoIP
hasAsn bool
asnLastLoad time.Time
}
var geoIP = new(GeoIP)
func (g *GeoIP) GetCountry(ip net.IP) (country, continent string, netmask int) {
if g.country == nil {
return "", "", 0
}
country, netmask = geoIP.country.GetCountry(ip.String())
if len(country) > 0 {
country = strings.ToLower(country)
continent = countries.CountryContinent[country]
}
return
}
func (g *GeoIP) GetCountryRegion(ip net.IP) (country, continent, regionGroup, region string, netmask int) {
if g.city == nil {
log.Println("No city database available")
country, continent, netmask = g.GetCountry(ip)
return
}
record := geoIP.city.GetRecord(ip.String())
if record == nil {
return
}
country = record.CountryCode
region = record.Region
if len(country) > 0 {
country = strings.ToLower(country)
continent = countries.CountryContinent[country]
if len(region) > 0 {
region = country + "-" + strings.ToLower(region)
regionGroup = countries.CountryRegionGroup(country, region)
}
}
return
}
func (g *GeoIP) GetASN(ip net.IP) (asn string, netmask int) {
if g.asn == nil {
log.Println("No asn database available")
return
}
name, netmask := g.asn.GetName(ip.String())
if len(name) > 0 {
index := strings.Index(name, " ")
if index > 0 {
asn = strings.ToLower(name[:index])
}
}
return
}
func (g *GeoIP) setDirectory() {
directory := Config.GeoIPDirectory()
if len(directory) > 0 {
geoip.SetCustomDirectory(directory)
}
}
func (g *GeoIP) setupGeoIPCountry() {
if g.country != nil {
return
}
g.setDirectory()
gi, err := geoip.OpenType(geoip.GEOIP_COUNTRY_EDITION)
if gi == nil || err != nil {
log.Printf("Could not open country GeoIP database: %s\n", err)
return
}
g.countryLastLoad = time.Now()
g.hasCity = true
g.country = gi
}
func (g *GeoIP) setupGeoIPCity() {
if g.city != nil {
return
}
g.setDirectory()
gi, err := geoip.OpenType(geoip.GEOIP_CITY_EDITION_REV1)
if gi == nil || err != nil {
log.Printf("Could not open city GeoIP database: %s\n", err)
return
}
g.cityLastLoad = time.Now()
g.hasCity = true
g.city = gi
}
func (g *GeoIP) setupGeoIPASN() {
if g.asn != nil {
return
}
g.setDirectory()
gi, err := geoip.OpenType(geoip.GEOIP_ASNUM_EDITION)
if gi == nil || err != nil {
log.Printf("Could not open ASN GeoIP database: %s\n", err)
return
}
g.asnLastLoad = time.Now()
g.hasAsn = true
g.asn = gi
}