-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.go
67 lines (61 loc) · 1.45 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"sort"
"strings"
)
func getLocalIP() string {
resp, err := http.Get("https://api.ipify.org")
if err != nil {
fmt.Println("FATAL: Cannot get local IP.")
os.Exit(2)
return ""
}
body, _ := ioutil.ReadAll(resp.Body)
return string(body)
}
func isIPAddress(ipaddress string) bool {
addr := net.ParseIP(ipaddress)
return addr != nil
}
func resolveDNSHostname(hostname string) string {
address, _ := net.LookupHost(hostname)
return address[0]
}
func getIPInfo(ipaddress string) IPAddressInfo {
apiEndpoint := "http://ip-api.com/json/" + ipaddress
resp, err := http.Get(apiEndpoint)
if err != nil {
fmt.Println("FATAL: Cannot contact IP address information API.")
os.Exit(3)
}
body, _ := ioutil.ReadAll(resp.Body)
infoString := string(body)
var info IPAddressInfo
err = json.Unmarshal([]byte(infoString), &info)
if err != nil {
fmt.Println("FATAL: Cannot serialize recieved IP address data.")
os.Exit(4)
}
return info
}
func printBGPPrefixes(as string) {
apiEndpoint := "https://api.hackertarget.com/aslookup/?q=" + as
resp, err := http.Get(apiEndpoint)
if err != nil {
fmt.Println("FATAL: Cannot contact BGP Prefixes API.")
os.Exit(5)
}
body, _ := ioutil.ReadAll(resp.Body)
prefixesString := string(body)
var prefixes = strings.Split(prefixesString, "\n")[1:]
sort.Strings(prefixes)
for index := range prefixes {
fmt.Println(prefixes[index])
}
}