-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeoip.go
55 lines (47 loc) · 1.15 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
package main
import (
"crypto/tls"
"encoding/json"
"io/ioutil"
"net"
"net/http"
"time"
)
// GeoIPResult describe a GeoIP entry
type GeoIPResult struct {
IP string `json:"ip"`
CountryCode string `json:"country_code"`
Country string `json:"country"`
ContinentCode string `json:"continent_code"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
ASNumber int `json:"asn"`
Organization string `json:"organization"`
Timezone string `json:"timezone"`
}
func lookupIPAddress(ip net.IP) (result *GeoIPResult, err error) {
apiURL := "https://api.ip.sb/geoip/"
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
}
client := &http.Client{
Transport: tr,
Timeout: 15 * time.Second,
}
req, _ := http.NewRequest("GET", apiURL+ip.String(), nil)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resultdata := new(GeoIPResult)
err = json.Unmarshal(content, resultdata)
if err != nil {
return nil, err
}
return resultdata, nil
}