Version 1.2: Add error handling
This commit is contained in:
46
API.go
46
API.go
@@ -1,13 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func getLocalIP() string {
|
||||
resp, _ := http.Get("https://api.ipify.org")
|
||||
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[:])
|
||||
}
|
||||
@@ -24,3 +34,37 @@ 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 getBGPPrefixes(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])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,4 +11,5 @@ func printHeader() {
|
||||
fmt.Println("| By Luke Tainton |")
|
||||
fmt.Println("| @luketainton |")
|
||||
fmt.Println("------------------------------")
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
32
IPInfo.go
32
IPInfo.go
@@ -1,11 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"sort"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -27,33 +24,10 @@ type IPAddressInfo struct {
|
||||
IPAddress string `json:"query"`
|
||||
}
|
||||
|
||||
func getIPInfo(ipaddress string) IPAddressInfo {
|
||||
apiEndpoint := "http://ip-api.com/json/" + ipaddress
|
||||
resp, _ := http.Get(apiEndpoint)
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
infoString := string(body)
|
||||
var info IPAddressInfo
|
||||
err := json.Unmarshal([]byte(infoString), &info)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
func getBGPPrefixes(as string) {
|
||||
apiEndpoint := "https://api.hackertarget.com/aslookup/?q=" + as
|
||||
resp, _ := http.Get(apiEndpoint)
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
prefixesString := string(body)
|
||||
var prefixes = strings.Split(prefixesString, "\n")[1:]
|
||||
sort.Strings(prefixes)
|
||||
for i := 0; i < len(prefixes); i++ {
|
||||
fmt.Println(prefixes[i])
|
||||
}
|
||||
}
|
||||
|
||||
func printIPInfo(input string, wantPrefixes bool) {
|
||||
var IPInfo IPAddressInfo = getIPInfo(input)
|
||||
fmt.Printf("%+v\n", IPInfo)
|
||||
os.Exit(200)
|
||||
var location string = IPInfo.Country + "/" + IPInfo.RegionName + "/" + IPInfo.City
|
||||
var bgpAS string = strings.Fields(IPInfo.AS)[0]
|
||||
fmt.Println("IP Address: ", IPInfo.IPAddress)
|
||||
|
||||
30
Main.go
30
Main.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -10,20 +11,29 @@ func main() {
|
||||
|
||||
var input string
|
||||
var wantPrefixes bool
|
||||
localIPAddress := getLocalIP()
|
||||
flag.StringVar(&input, "i", localIPAddress, "Specify IP address or domain name.")
|
||||
|
||||
flag.StringVar(&input, "i", "", "Specify IP address or domain name.")
|
||||
flag.BoolVar(&wantPrefixes, "p", false, "Enable printing of advertised BGP prefixes.")
|
||||
flag.Parse()
|
||||
var isIPCorrect bool = checkIPSyntax(input)
|
||||
if isIPCorrect == true {
|
||||
printIPInfo(input, wantPrefixes)
|
||||
|
||||
if input == "" {
|
||||
fmt.Println("FATAL: No IP address or domain name was specified.")
|
||||
os.Exit(1)
|
||||
} else {
|
||||
ipaddress := resolveDNSHostname(input)
|
||||
if checkIPSyntax(ipaddress) == true {
|
||||
fmt.Println("Domain Name: ", input)
|
||||
printIPInfo(ipaddress, wantPrefixes)
|
||||
if input == "me" {
|
||||
input = getLocalIP()
|
||||
}
|
||||
var isIPCorrect bool = checkIPSyntax(input)
|
||||
if isIPCorrect == true {
|
||||
printIPInfo(input, wantPrefixes)
|
||||
} else {
|
||||
fmt.Println("Invalid query.")
|
||||
ipaddress := resolveDNSHostname(input)
|
||||
if checkIPSyntax(ipaddress) == true {
|
||||
fmt.Println("Domain Name: ", input)
|
||||
printIPInfo(ipaddress, wantPrefixes)
|
||||
} else {
|
||||
fmt.Println("Invalid query.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user