-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoip.go
94 lines (77 loc) · 1.89 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
package geoip
import (
"fmt"
"net"
"sync"
"time"
"github.com/IncSW/geoip2"
)
// GeoIPHandler will keep itself up-to-date with the latest GeoIP database by updating once every 24 hours
// Provides methods to lookup IP addresses and return the associated latitude and longitude
// The structure should be created with it's maxmind license key
type GeoIPHandler struct {
sync.RWMutex
// The stop channel is used to end the goroutine that updates the database
stop chan struct{}
// Maxmind license key can be created at https://www.maxmind.com
licenseKey string
// underlying database object
db *geoip2.CityReader
}
// NewGeoIPHandler creates a new GeoIPHandler with the given license key
func NewGeoIPHandler(licenseKey string) (*GeoIPHandler, error) {
// Download the database
bytes, err := downloadAndCheckHash(licenseKey)
if err != nil {
return nil, err
}
// Create the database
db, err := geoip2.NewCityReader(bytes)
if err != nil {
return nil, err
}
// Create the handler
handler := &GeoIPHandler{
stop: make(chan struct{}),
licenseKey: licenseKey,
db: db,
}
// update the database every 24 hours
go handler.update(handler.stop)
return handler, nil
}
func (g *GeoIPHandler) update(stop chan struct{}) {
// update the database every 24 hours
for {
select {
case <-stop:
return
case <-time.After(24 * time.Hour):
// Lock the database
g.Lock()
// Download the database
bytes, err := downloadAndCheckHash(g.licenseKey)
if err != nil {
fmt.Println(err)
g.Unlock()
continue
}
// Create the database
db, err := geoip2.NewCityReader(bytes)
if err != nil {
fmt.Println(err)
g.Unlock()
continue
}
// Create the handler
g.db = db
g.Unlock()
}
}
}
func (g *GeoIPHandler) Close() {
g.stop <- struct{}{}
}
func (g *GeoIPHandler) Lookup(ip net.IP) (*geoip2.CityResult, error) {
return g.db.Lookup(ip)
}