This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
lookup.go
71 lines (63 loc) · 2.12 KB
/
lookup.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
66
67
68
69
70
71
package ipdata
import (
"net"
"strings"
"github.com/mmcloughlin/geohash"
)
// IPData is a struct that contains information about a particular IP address
type IPData struct {
IPAddress string `json:"ip_address,omitempty"`
Hostname string `json:"hostname,omitempty"`
ISP string `json:"isp,omitempty"`
CountryCode string `json:"country_code,omitempty"`
CountryName string `json:"country_name,omitempty"`
RegionCode string `json:"region_code,omitempty"`
RegionName string `json:"region_name,omitempty"`
City string `json:"city,omitempty"`
ZipCode string `json:"zip_code,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
GeoHash string `json:"geohash,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
MetroCode uint `json:"metro_code,omitempty"`
}
// Lookup performs the task of retrieving and returning the users IP address
// info
func (c *Client) Lookup(ipStr string) *IPData {
// Reverse lookup the passed IP to retrieve any hostname that exists
var hostname string
if hs, _ := net.LookupAddr(ipStr); len(hs) > 0 {
hostname = strings.TrimSuffix(hs[0], ".")
}
// Parse the IP address string passed
ip := net.ParseIP(ipStr)
// Lock and defer unlock the client to prevent concurrent read/write
c.mu.Lock()
defer c.mu.Unlock()
// Lookup the Geo and ASN info from the stored client dbs
var asn ASN
var city City
c.asn.Lookup(ip, &asn)
c.city.Lookup(ip, &city)
// Populate and return a fully populated ipdata
t := IPData{
IPAddress: ip.String(),
Hostname: hostname,
ISP: asn.Organization,
CountryCode: city.Country.ISOCode,
CountryName: city.Country.Names["en"],
City: city.City.Names["en"],
ZipCode: city.Postal.Code,
TimeZone: city.Location.TimeZone,
Latitude: city.Location.Latitude,
Longitude: city.Location.Longitude,
MetroCode: city.Location.MetroCode,
GeoHash: geohash.Encode(city.Location.Latitude,
city.Location.Longitude),
}
if len(city.Region) > 0 {
t.RegionCode = city.Region[0].ISOCode
t.RegionName = city.Region[0].Names["en"]
}
return &t
}