-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use maxmind instead of ip2location, include latitude/longitude
- Loading branch information
1 parent
28219be
commit 4d25c5a
Showing
13 changed files
with
156 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package geoip | ||
|
||
import ( | ||
_ "embed" // needed for geolocation database | ||
"errors" | ||
"net" | ||
"sync" | ||
|
||
"github.com/oschwald/geoip2-golang" | ||
) | ||
|
||
//go:embed GeoLite2-City.mmdb | ||
Check failure on line 12 in geoip/geoip.go
|
||
var maxMindCityDB []byte | ||
|
||
// A Location represents an ISO 3166-1 A-2 country codes and an approximate | ||
// latitude/longitude. | ||
type Location struct { | ||
CountryCode string `json:"countryCode"` | ||
|
||
Longitude float64 `json:"longitude"` | ||
Latitude float64 `json:"latitude"` | ||
} | ||
|
||
// A Locator maps IP addresses to their location. | ||
// It is assumed that it implementations are thread-safe. | ||
type Locator interface { | ||
// Close closes the Locator. | ||
Close() error | ||
// Locate maps IP addresses to a Location. | ||
Locate(ip *net.IPAddr) (Location, error) | ||
} | ||
|
||
type maxMindLocator struct { | ||
mu sync.Mutex | ||
|
||
db *geoip2.Reader | ||
} | ||
|
||
// Locate implements Locator. | ||
func (m *maxMindLocator) Locate(addr *net.IPAddr) (Location, error) { | ||
if addr == nil { | ||
return Location{}, errors.New("nil IP") | ||
} | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
record, err := m.db.City(addr.IP) | ||
if err != nil { | ||
return Location{}, err | ||
} | ||
return Location{ | ||
CountryCode: record.Country.IsoCode, | ||
Latitude: record.Location.Latitude, | ||
Longitude: record.Location.Longitude, | ||
}, nil | ||
} | ||
|
||
// Close implements Locator. | ||
func (m *maxMindLocator) Close() error { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
return m.db.Close() | ||
} | ||
|
||
// NewMaxMindLocator returns a Locator that uses an underlying MaxMind | ||
// database. If no path is provided, a default embedded GeoLite2-City database | ||
// is used. | ||
func NewMaxMindLocator(path string) (Locator, error) { | ||
var db *geoip2.Reader | ||
var err error | ||
if path == "" { | ||
db, err = geoip2.FromBytes(maxMindCityDB) | ||
} else { | ||
db, err = geoip2.Open(path) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &maxMindLocator{db: db}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.