/
api-calls.go
63 lines (58 loc) · 1.28 KB
/
api-calls.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
package utils
import (
"io/ioutil"
"net/http"
"strings"
)
type GeoIpInfo struct {
Country string
State string
City string
Latitude string
Longitude string
}
//GetIPGeoLocation will use api.hackertarget.com to find the location of an IP
func GetIPGeoLocation(ip string) (GeoIpInfo, error) {
var info GeoIpInfo
//crafting the api url in the form of
//https://api.hackertarget.com/geoip/?q=1.1.1.1
//TODO running into api limit on hackertarget. need to see if http://ip-api.com is better
//http://ip-api.com/#67.185.0.88
url := "https://api.hackertarget.com/geoip/?q=" + ip
resp, err := http.Get(url)
if err != nil {
return info, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
splitBody := strings.Split(string(body), "\n")
for _, line := range splitBody {
splitLine := strings.Split(line, ": ")
var index, value string
if len(splitLine) >= 2 {
index = splitLine[0]
value = splitLine[1]
}
if index == "Country" {
info.Country = value
continue
}
if index == "State" {
info.State = value
continue
}
if index == "City" {
info.City = value
continue
}
if index == "Latitude" {
info.Latitude = value
continue
}
if index == "Longitude" {
info.Longitude = value
continue
}
}
return info, nil
}