Initial commit after restructure
This commit is contained in:
45
network/geoip/lookup.go
Normal file
45
network/geoip/lookup.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package geoip
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
// GetLocation returns Location data of an IP address
|
||||
func GetLocation(ip net.IP) (record *Location, err error) {
|
||||
dbLock.Lock()
|
||||
defer dbLock.Unlock()
|
||||
|
||||
err = prepDatabaseForUse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
record = &Location{}
|
||||
|
||||
// fetch
|
||||
err = dbCity.Lookup(ip, record)
|
||||
if err == nil {
|
||||
err = dbASN.Lookup(ip, record)
|
||||
}
|
||||
|
||||
// retry
|
||||
if err != nil {
|
||||
// reprep
|
||||
handleError(err)
|
||||
err = prepDatabaseForUse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// refetch
|
||||
err = dbCity.Lookup(ip, record)
|
||||
if err == nil {
|
||||
err = dbASN.Lookup(ip, record)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
Reference in New Issue
Block a user