refactor returns and function case
This commit is contained in:
73
ip_api.go
73
ip_api.go
@@ -2,17 +2,51 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// IP-API data struct
|
||||||
|
// https://ip-api.com/docs/api:json
|
||||||
|
type IPAPI struct {
|
||||||
|
Continent string `json:"continent"`
|
||||||
|
ContinentCode string `json:"continentCode"`
|
||||||
|
Country string `json:"country"`
|
||||||
|
CountryCode string `json:"countryCode"`
|
||||||
|
Region string `json:"region"`
|
||||||
|
RegionName string `json:"regionName"`
|
||||||
|
City string `json:"city"`
|
||||||
|
Zip string `json:"zip"`
|
||||||
|
Lat float32 `json:"lat"`
|
||||||
|
Lon float32 `json:"lon"`
|
||||||
|
Timezone string `json:"timezone"`
|
||||||
|
Offset int `json:"offset"`
|
||||||
|
Currency string `json:"currency"`
|
||||||
|
ISP string `json:"isp"`
|
||||||
|
Org string `json:"org"`
|
||||||
|
AS string `json:"as"`
|
||||||
|
ASName string `json:"asname"`
|
||||||
|
Reverse string `json:"reverse"`
|
||||||
|
Hosting bool `json:"hosting"`
|
||||||
|
Query string `json:"query"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
// Build URL to query IP-API
|
// Build URL to query IP-API
|
||||||
// https://ip-api.com/docs/api:json
|
// https://ip-api.com/docs/api:json
|
||||||
func buildURL(ip string) string {
|
func buildURL(ip string) string {
|
||||||
return "http://ip-api.com/json/" + ip
|
return "http://ip-api.com/json/" + ip
|
||||||
}
|
}
|
||||||
|
|
||||||
func Lookup(ip string, properties []string) map[string]interface{} {
|
func lookup(ip string, properties []string) (*IPAPI, error) {
|
||||||
CheckValidIP(ip)
|
var data *IPAPI
|
||||||
|
|
||||||
|
if !checkValidIP(ip) {
|
||||||
|
return data, errors.New("please enter a valid IP address")
|
||||||
|
}
|
||||||
|
|
||||||
url := buildURL(ip)
|
url := buildURL(ip)
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
|
|
||||||
@@ -21,36 +55,35 @@ func Lookup(ip string, properties []string) map[string]interface{} {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
var data map[string]interface{}
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(&data)
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
debugOut(Error, err.Error())
|
debugOut(Error, err.Error())
|
||||||
}
|
}
|
||||||
if data["status"] == "fail" {
|
if data.Status == "fail" {
|
||||||
debugOut(Error, data["message"].(string))
|
debugOut(Error, data.Message)
|
||||||
}
|
}
|
||||||
return data
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetProperties(data map[string]interface{}) string {
|
func getProperties(data *IPAPI) string {
|
||||||
|
var result string
|
||||||
var output string = ""
|
var output string = ""
|
||||||
|
|
||||||
for _, property := range properties {
|
for _, property := range properties {
|
||||||
found := false
|
datafield := reflect.Indirect(reflect.ValueOf(data)).FieldByName(property).String()
|
||||||
for key, value := range data {
|
if datafield != "" {
|
||||||
if key == property {
|
result = datafield
|
||||||
if output != "" {
|
} else {
|
||||||
output += ","
|
result = "Not found"
|
||||||
}
|
|
||||||
output += value.(string)
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if !found {
|
if detail {
|
||||||
debugOut(Error, "Property '"+property+"' not found")
|
output += property + ": " + result + "\n"
|
||||||
|
} else {
|
||||||
|
if output != "" {
|
||||||
|
output += ","
|
||||||
|
}
|
||||||
|
output += result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -3,13 +3,23 @@ package main
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
var verbose bool = false
|
var verbose bool = false
|
||||||
|
var detail bool
|
||||||
var ip string
|
var ip string
|
||||||
var properties []string
|
var properties []string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
parseFlags()
|
parseFlags()
|
||||||
|
|
||||||
result := GetProperties(Lookup(ip, properties))
|
if verbose {
|
||||||
|
fmt.Println("Verbose mode on")
|
||||||
|
}
|
||||||
|
data, err := lookup(ip, properties)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := getProperties(data)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
}
|
}
|
||||||
|
|||||||
9
utils.go
9
utils.go
@@ -39,16 +39,15 @@ func debugOut(level Verbosity, text string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckValidIP(ip string) {
|
func checkValidIP(ip string) bool {
|
||||||
re := regexp.MustCompile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
|
re := regexp.MustCompile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
|
||||||
if !re.MatchString(ip) {
|
return re.MatchString(ip)
|
||||||
debugOut(Error, "Please enter a valid IP address")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFlags() {
|
func parseFlags() {
|
||||||
_ip := flag.String("ip", "", "IP address to lookup")
|
_ip := flag.String("ip", "", "IP address to lookup")
|
||||||
_props := flag.String("p", "country", "Properties to retrieve")
|
_props := flag.String("p", "Country", "Properties to retrieve")
|
||||||
|
flag.BoolVar(&detail, "d", false, "Show Detail")
|
||||||
_verbose := flag.Bool("v", false, "Verbose output")
|
_verbose := flag.Bool("v", false, "Verbose output")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
_loose := flag.Args()
|
_loose := flag.Args()
|
||||||
|
|||||||
Reference in New Issue
Block a user