Skip to content

Commit

Permalink
Minimum viable
Browse files Browse the repository at this point in the history
  • Loading branch information
luketainton committed Nov 21, 2020
1 parent d50afa8 commit a32d4bb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 16 deletions.
26 changes: 26 additions & 0 deletions API.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"io/ioutil"
"net"
"net/http"
)

func getLocalIP() string {
resp, _ := http.Get("https://api.ipify.org")
body, _ := ioutil.ReadAll(resp.Body)
return string(body[:])
}

func checkIPSyntax(ipaddress string) bool {
addr := net.ParseIP(ipaddress)
if addr == nil {
return false
}
return true
}

func resolveDNSHostname(hostname string) string {
address, _ := net.LookupHost(hostname)
return address[0]
}
14 changes: 14 additions & 0 deletions Header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "fmt"

func printHeader() {
fmt.Println("------------------------------")
fmt.Println("| IP Address Information |")
fmt.Println("| Lookup Tool |")
fmt.Println("| (iPilot) |")
fmt.Println("| |")
fmt.Println("| By Luke Tainton |")
fmt.Println("| @luketainton |")
fmt.Println("------------------------------")
}
51 changes: 51 additions & 0 deletions IPInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

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

// IPAddressInfo is the IP Address information from API
type IPAddressInfo struct {
Status string `json:"status"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Region string `json:"region"`
RegionName string `json:"regionName"`
City string `json:"city"`
ZIP string `json:"zip"`
Latitude float32 `json:"lat"`
Longitude float32 `json:"lon"`
Timezone string `json:"timezone"`
ISP string `json:"isp"`
Organisation string `json:"org"`
AS string `json:"as"`
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)
// fmt.Print(string(body))
infoString := string(body)
var info IPAddressInfo
err := json.Unmarshal([]byte(infoString), &info)
if err != nil {
fmt.Println(err)
}
return info
}

func printIPInfo(input string) {
var IPInfo IPAddressInfo = getIPInfo(input)
var location string = IPInfo.Country + "/" + IPInfo.RegionName + "/" + IPInfo.City
fmt.Println("IP Address: ", IPInfo.IPAddress)
fmt.Println("Location: ", location)
fmt.Println("Timezone: ", IPInfo.Timezone)
fmt.Println("ISP: ", IPInfo.ISP)
fmt.Println("BGP AS: ", strings.Fields(IPInfo.AS)[0])
}
30 changes: 14 additions & 16 deletions Main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
)

func getLocalIP() string {
resp, _ := http.Get("https://api.ipify.org")
// defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return string(body[:])
}

func main() {
var ipaddress string
printHeader()

var input string
localIPAddress := getLocalIP()
flag.StringVar(&ipaddress, "i", localIPAddress, "Specify IP address. Defaults to current public IP address.")
flag.StringVar(&input, "i", localIPAddress, "Specify IP address or domain name.")
flag.Parse()
apiEndpoint := "http://ip-api.com/json/" + ipaddress
resp, _ := http.Get(apiEndpoint)
// defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Print(string(body))
var isIPCorrect bool = checkIPSyntax(input)
if isIPCorrect == true {
printIPInfo(input)
} else {
// fmt.Println(ipaddress, "is not a valid IP address.")
fmt.Println("Domain Name: ", input)
ipaddress := resolveDNSHostname(input)
printIPInfo(ipaddress)
}

}

0 comments on commit a32d4bb

Please sign in to comment.