-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgeoip.go
More file actions
44 lines (35 loc) · 674 Bytes
/
geoip.go
File metadata and controls
44 lines (35 loc) · 674 Bytes
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
package main
import (
"errors"
"net"
"github.com/oschwald/maxminddb-golang"
)
var db *maxminddb.Reader
var IsActive bool
func GeoIpInit(FilePath string) error {
var err error
db, err = maxminddb.Open(FilePath)
if err == nil {
IsActive = true
}
return err
}
func GeoIpClose() error {
if db != nil {
return db.Close()
}
return nil
}
func GeoIpLookup(IP string) (string, error) {
if IsActive {
ip := net.ParseIP(IP)
var record struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
}
err := db.Lookup(ip, &record)
return record.Country.ISOCode, err
}
return "", errors.New("MMDB Uninitialized")
}