-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
86 lines (72 loc) · 1.73 KB
/
api.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
type InternetDBResponse struct {
IP string `json:"ip"`
Hostnames []string `json:"hostnames"`
Ports []int `json:"ports"`
Tags []string `json:"tags"`
Vulns []string `json:"vulns"`
CPEs []string `json:"cpes"`
}
type IpinfoResponse struct {
IP string `json:"ip"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
Loc string `json:"loc"`
Org string `json:"org"`
Postal string `json:"postal"`
Timezone string `json:"timezone"`
}
func queryInternetDB(ipAddress string) (*InternetDBResponse, error) {
resp, err := http.Get(fmt.Sprintf("https://internetdb.shodan.io/%s", ipAddress))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result InternetDBResponse
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func queryIpinfo(ipAddress string) (*IpinfoResponse, error) {
apiKey := os.Getenv("IPINFO_API_KEY")
if apiKey == "" {
return nil, fmt.Errorf("IPINFO_API_KEY environment variable is not set")
}
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("https://ipinfo.io/%s", ipAddress), nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("token", apiKey)
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result IpinfoResponse
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return &result, nil
}