forked from XTLS/RealiTLScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo.go
More file actions
41 lines (37 loc) · 675 Bytes
/
geo.go
File metadata and controls
41 lines (37 loc) · 675 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
package main
import (
"github.com/oschwald/geoip2-golang"
"log/slog"
"net"
"sync"
)
type Geo struct {
geoReader *geoip2.Reader
mu sync.Mutex
}
func NewGeo() *Geo {
geo := &Geo{
mu: sync.Mutex{},
}
reader, err := geoip2.Open("Country.mmdb")
if err != nil {
slog.Warn("Cannot open Country.mmdb")
return geo
}
slog.Info("Enabled GeoIP")
geo.geoReader = reader
return geo
}
func (o *Geo) GetGeo(ip net.IP) string {
if o.geoReader == nil {
return "N/A"
}
o.mu.Lock()
defer o.mu.Unlock()
country, err := o.geoReader.Country(ip)
if err != nil {
slog.Debug("Error reading geo", "err", err)
return "N/A"
}
return country.Country.IsoCode
}