-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.go
151 lines (124 loc) · 3.23 KB
/
database.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
package geoip
import (
"sync"
"github.com/oschwald/maxminddb-golang"
"github.com/pkg/errors"
)
// Database is the main wrapper around the MaxMind GeoIP database
type Database struct {
path string
lookupCache map[string]*IPLocation
lookupCacheMutex sync.RWMutex
}
// NewDatabase returns a new Database instance with the given database path
func NewDatabase(path string) *Database {
return &Database{
path: path,
lookupCache: map[string]*IPLocation{},
}
}
// ClearCache clears the cache for the lookups
func (database *Database) ClearCache() {
database.lookupCacheMutex.Lock()
defer database.lookupCacheMutex.Unlock()
database.lookupCache = map[string]*IPLocation{}
}
// Lookup returns the full country information for a specific IP address
func (database *Database) Lookup(ipaddress string) (*IPLocation, error) {
if ipaddress == "" {
return nil, nil
}
location, err := database.lookupFromCache(ipaddress)
if err != nil {
return nil, err
}
if location != nil {
return location, nil
}
var record interface{}
db, err := maxminddb.Open(database.path)
if err != nil {
return location, err
}
defer db.Close()
ip, private, err := isPrivateIP(ipaddress)
if err != nil {
return nil, err
}
if private {
return nil, nil
}
if err := db.Lookup(ip, &location); err != nil {
return location, err
}
_ = db.Lookup(ip, &record)
if location == nil {
return location, errors.New("No info for: " + ipaddress)
}
location.IPAddress = ipaddress
location.IsCached = false
database.lookupCacheMutex.Lock()
defer database.lookupCacheMutex.Unlock()
database.lookupCache[ipaddress] = location
return location, nil
}
func (database *Database) lookupFromCache(ipaddress string) (*IPLocation, error) {
database.lookupCacheMutex.RLock()
defer database.lookupCacheMutex.RUnlock()
if location, cached := database.lookupCache[ipaddress]; cached {
location.IsCached = true
return location, nil
}
return nil, nil
}
// CountryCode returns the country code for a specific IP address
func (database *Database) CountryCode(ipaddress string) (string, error) {
location, err := database.Lookup(ipaddress)
if err != nil {
return "", err
}
if location == nil {
return "", nil
}
return location.CountryCode(), nil
}
// CountryName returns the country name for a specific IP address
func (database *Database) CountryName(ipaddress string) (string, error) {
location, err := database.Lookup(ipaddress)
if err != nil {
return "", err
}
if location == nil {
return "", nil
}
return location.CountryName(), nil
}
// RegionName returns the region name for a specific IP address
//
// Region can be:
// - west-us
// - south-brazil
// - japan-east
// - southeast-asia
// - west-europe (the default)
func (database *Database) RegionName(ipaddress string) (string, error) {
location, err := database.Lookup(ipaddress)
if err != nil {
return "", err
}
if location == nil {
return "", nil
}
return location.RegionName(), nil
}
// TimeZone returns the timezone for a specific IP address
func (database *Database) TimeZone(ipaddress string) (string, error) {
location, err := database.Lookup(ipaddress)
if err != nil {
return "", err
}
if location == nil {
return "", nil
}
return location.TimeZone(), nil
}