Skip to content

Commit

Permalink
Version 1.2: Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
luketainton committed Dec 17, 2020
1 parent d2f7dc9 commit e64ede6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 40 deletions.
46 changes: 45 additions & 1 deletion API.go
Original file line number Diff line number Diff line change
@@ -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[:])
}
Expand All @@ -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])
}
}
1 change: 1 addition & 0 deletions Header.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ func printHeader() {
fmt.Println("| By Luke Tainton |")
fmt.Println("| @luketainton |")
fmt.Println("------------------------------")
fmt.Println("")
}
32 changes: 3 additions & 29 deletions IPInfo.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"os"
"strings"
)

Expand All @@ -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)
Expand Down
30 changes: 20 additions & 10 deletions Main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,37 @@ package main
import (
"flag"
"fmt"
"os"
)

func main() {
printHeader()

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.")
}
}
}

Expand Down

0 comments on commit e64ede6

Please sign in to comment.