-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (54 loc) · 1.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
func main() {
ip := os.Args[len(os.Args)-1]
if os.Args[0] == ip {
reader := bufio.NewReader(os.Stdin)
fmt.Print("IP: ")
ip, _ = reader.ReadString('\n')
} else if len(os.Args) > 1 {
ips := os.Args[1:]
for x, i := range ips {
getLoc(i)
if x != len(ips)-1 {
fmt.Println()
}
}
os.Exit(0)
}
getLoc(ip)
}
func getLoc(ip string) {
type geolocation struct {
CountryName string `json:"country_name"`
State string `json:"state_prov"`
IP string `json:"ip"`
ISP string `json:"isp"`
}
req, _ := http.NewRequest("GET", "https://api.ipgeolocation.io/ipgeo", nil)
req.Header.Set("Referer", "https://ipgeolocation.io/")
q := req.URL.Query()
q.Add("include", "hostname")
q.Add("ip", ip)
req.URL.RawQuery = q.Encode()
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
geoInfo := &geolocation{}
decoder := json.NewDecoder(resp.Body)
decoder.Decode(&geoInfo)
fmt.Println("Country Name:\t", geoInfo.CountryName)
fmt.Println("State:\t\t", geoInfo.State)
fmt.Println("IP:\t\t", geoInfo.IP)
fmt.Println("ISP:\t\t", geoInfo.ISP)
}