/
geoip.go
117 lines (91 loc) · 2.08 KB
/
geoip.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package utils
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/Jeffail/gabs"
)
var (
// GeoIPUnknown is returned when the city or country name can't be resolved.
GeoIPUnknown = "<unknown>"
errInvalidGeoip = errors.New("invalid geoip data")
)
// GeoIP contains information about the geolocalization.
type GeoIP struct {
data *gabs.Container
}
// NewGeoIP returns a new instance of the Geoip struct
func NewGeoIP(ip string) (*GeoIP, error) {
var data *gabs.Container
var err error
if ip == "::1" || ip == "127.0.0.1" {
data, err = lookupMyIP()
} else {
data, err = lookupIP(ip)
}
if err != nil {
return nil, err
}
if data == nil {
return nil, errInvalidGeoip
}
geoip := &GeoIP{
data: data,
}
return geoip, nil
}
// City returns the name of the city
func (geoip *GeoIP) City() string {
if geoip.data == nil {
return GeoIPUnknown
}
city, ok := geoip.data.Search("city").Data().(string)
if !ok {
return GeoIPUnknown
}
return city
}
// Country returns the name of the country
func (geoip *GeoIP) Country() string {
if geoip.data == nil {
return GeoIPUnknown
}
country, ok := geoip.data.Search("country", "name").Data().(string)
if !ok {
return GeoIPUnknown
}
return country
}
func lookupMyIP() (*gabs.Container, error) {
response, err := http.Get("https://geoip.nekudo.com/api")
if err != nil {
return nil, err
}
return parseGeoIPResponse(response)
}
func lookupIP(ip string) (*gabs.Container, error) {
response, err := http.Get(fmt.Sprintf("https://geoip.nekudo.com/api/%s", ip))
if err != nil {
return nil, err
}
return parseGeoIPResponse(response)
}
// FormatIPAndLocation returns the ip with geo location for the given ip.
func FormatIPAndLocation(ip string) string {
geoip, err := NewGeoIP(ip)
if err != nil {
return ip
}
return fmt.Sprintf("%s (%s, %s)", ip, geoip.City(), geoip.Country())
}
func parseGeoIPResponse(response *http.Response) (*gabs.Container, error) {
defer func() {
_ = response.Body.Close()
}()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return gabs.ParseJSON(body)
}