1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package geodb
- import (
- "net"
- "github.com/oschwald/geoip2-golang"
- "imuslab.com/arozos/ReverseProxy/mod/database"
- )
- type Store struct {
- geodb *geoip2.Reader
- sysdb *database.Database
- }
- type CountryInfo struct {
- CountryIsoCode string
- ContinetCode string
- }
- func NewGeoDb(sysdb *database.Database, dbfile string) (*Store, error) {
- db, err := geoip2.Open(dbfile)
- if err != nil {
- return nil, err
- }
- return &Store{
- geodb: db,
- sysdb: sysdb,
- }, nil
- }
- func (s *Store) ResolveCountryCodeFromIP(ipstring string) (*CountryInfo, error) {
- // If you are using strings that may be invalid, check that ip is not nil
- ip := net.ParseIP(ipstring)
- record, err := s.geodb.City(ip)
- if err != nil {
- return nil, err
- }
- return &CountryInfo{
- record.Country.IsoCode,
- record.Continent.Code,
- }, nil
- }
- func (s *Store) Close() {
- s.geodb.Close()
- }
|