-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeoipinjecter.go
50 lines (42 loc) · 1.21 KB
/
geoipinjecter.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
package main
import (
"net"
"sync"
"github.com/cdreier/golang-snippets/snippets"
"github.com/fasibio/funk_agent/geoipdbupdater"
"github.com/fasibio/funk_agent/logger"
"github.com/oschwald/geoip2-golang"
)
var geoIPDatabase *geoip2.Reader
var mutex sync.Mutex
func InitGeoIP() GeoReader {
snippets.EnsureDir("./tmpassets")
snippets.EnsureDir("./tmpassets/geoip")
updateInfo := make(chan string, 2)
geoipdbupdater.NewGEOIPUpdateTicker(updateInfo)
go func() {
for newDBPath := range updateInfo {
mutex.Lock()
tmpGeoIPDatabase, err := geoip2.Open(newDBPath)
if err != nil {
logger.Get().Error("cannot open new database file: ", err.Error(), ", no updated Database is loaded!")
} else {
geoIPDatabase = tmpGeoIPDatabase
geoipdbupdater.CleanUpOldMaxmindDBs(newDBPath, geoipdbupdater.DefaultGeoIPurlParams)
logger.Get().Info("Updated newer geoIP db table")
}
mutex.Unlock()
}
}()
return &GeoDataReader{}
}
type GeoReader interface {
GetGeoDataByIP(IPaddress string) (*geoip2.City, error)
}
type GeoDataReader struct {
}
func (g *GeoDataReader) GetGeoDataByIP(IPaddress string) (*geoip2.City, error) {
mutex.Lock()
defer mutex.Unlock()
return geoIPDatabase.City(net.ParseIP(IPaddress))
}