Version 1.2: Add error handling
This commit is contained in:
46
API.go
46
API.go
@@ -1,13 +1,23 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getLocalIP() string {
|
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)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
return string(body[:])
|
return string(body[:])
|
||||||
}
|
}
|
||||||
@@ -24,3 +34,37 @@ func resolveDNSHostname(hostname string) string {
|
|||||||
address, _ := net.LookupHost(hostname)
|
address, _ := net.LookupHost(hostname)
|
||||||
return address[0]
|
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("| By Luke Tainton |")
|
||||||
fmt.Println("| @luketainton |")
|
fmt.Println("| @luketainton |")
|
||||||
fmt.Println("------------------------------")
|
fmt.Println("------------------------------")
|
||||||
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
|
|||||||
32
IPInfo.go
32
IPInfo.go
@@ -1,11 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"os"
|
||||||
"net/http"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,33 +24,10 @@ type IPAddressInfo struct {
|
|||||||
IPAddress string `json:"query"`
|
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) {
|
func printIPInfo(input string, wantPrefixes bool) {
|
||||||
var IPInfo IPAddressInfo = getIPInfo(input)
|
var IPInfo IPAddressInfo = getIPInfo(input)
|
||||||
|
fmt.Printf("%+v\n", IPInfo)
|
||||||
|
os.Exit(200)
|
||||||
var location string = IPInfo.Country + "/" + IPInfo.RegionName + "/" + IPInfo.City
|
var location string = IPInfo.Country + "/" + IPInfo.RegionName + "/" + IPInfo.City
|
||||||
var bgpAS string = strings.Fields(IPInfo.AS)[0]
|
var bgpAS string = strings.Fields(IPInfo.AS)[0]
|
||||||
fmt.Println("IP Address: ", IPInfo.IPAddress)
|
fmt.Println("IP Address: ", IPInfo.IPAddress)
|
||||||
|
|||||||
30
Main.go
30
Main.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -10,20 +11,29 @@ func main() {
|
|||||||
|
|
||||||
var input string
|
var input string
|
||||||
var wantPrefixes bool
|
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.BoolVar(&wantPrefixes, "p", false, "Enable printing of advertised BGP prefixes.")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
var isIPCorrect bool = checkIPSyntax(input)
|
|
||||||
if isIPCorrect == true {
|
if input == "" {
|
||||||
printIPInfo(input, wantPrefixes)
|
fmt.Println("FATAL: No IP address or domain name was specified.")
|
||||||
|
os.Exit(1)
|
||||||
} else {
|
} else {
|
||||||
ipaddress := resolveDNSHostname(input)
|
if input == "me" {
|
||||||
if checkIPSyntax(ipaddress) == true {
|
input = getLocalIP()
|
||||||
fmt.Println("Domain Name: ", input)
|
}
|
||||||
printIPInfo(ipaddress, wantPrefixes)
|
var isIPCorrect bool = checkIPSyntax(input)
|
||||||
|
if isIPCorrect == true {
|
||||||
|
printIPInfo(input, wantPrefixes)
|
||||||
} else {
|
} 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