-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxmind.go
65 lines (54 loc) · 1.46 KB
/
maxmind.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
package handlers
import (
_ "embed"
"fmt"
"net"
"github.com/gofiber/fiber/v2"
geoip2 "github.com/oschwald/geoip2-golang"
)
// See https://pkg.go.dev/github.com/oschwald/geoip2-golang#City for a full list of options you can use here to modify
// what data is returned for a specific IP.
type ipLookup struct {
GeoNameID uint `json:"geoname_id"`
City string `json:"city"`
Country string `json:"country"`
IsoCode string `json:"iso_code"`
AccuracyRadius uint16 `json:"accuracy_radius"`
}
//go:embed GeoLite2-City.mmdb
var mmdb []byte
var geoIPDb *geoip2.Reader
func init() {
// Load MaxMind DB
var err error
geoIPDb, err = geoip2.FromBytes(mmdb)
if err != nil {
fmt.Println("Unable to load 'GeoLite2-City.mmdb'.")
panic(err)
}
}
// GeoIP is a handler for IP address lookups
func GeoIP() fiber.Handler {
// Return handler
return func(c *fiber.Ctx) error {
ipAddr := c.Params("ip", c.IP())
// Check IP address format
ip := net.ParseIP(ipAddr)
if ip == nil {
return c.Status(400).JSON(map[string]string{"status": "error", "message": "Invalid IP address"})
}
// Perform lookup
record, err := geoIPDb.City(ip)
if err != nil {
return err
}
// Send response
return c.JSON(ipLookup{
GeoNameID: record.City.GeoNameID,
City: record.City.Names["en"],
IsoCode: record.Country.IsoCode,
AccuracyRadius: record.Location.AccuracyRadius,
Country: record.Country.Names["en"],
})
}
}