-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyip.go
112 lines (107 loc) · 3.27 KB
/
myip.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"encoding/json"
"fmt"
"github.com/alexeyco/simpletable"
"github.com/swaggo/cli"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
var app = cli.NewApp()
type AutoGenerated struct {
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Region string `json:"region"`
City string `json:"city"`
Isp string `json:"isp"`
As string `json:"as"`
Query string `json:"query"`
}
func getIPInfo(c *cli.Context) error {
var httpClient = &http.Client{
Timeout: 5 * time.Second,
}
tab := simpletable.New()
var url string
if len(c.Args()) != 0 {
url = "http://ip-api.com/json/" + c.Args()[0] + "?fields=country,countryCode,region,city,lat,lon,timezone,isp,as,query"
} else {
url = "http://ip-api.com/json/?fields=country,countryCode,region,city,lat,lon,timezone,isp,as,query"
}
resp, err := httpClient.Get(url)
defer resp.Body.Close()
if err == nil {
body, _ := ioutil.ReadAll(resp.Body)
var ipInfo *AutoGenerated
if err = json.Unmarshal(body, &ipInfo); err == nil {
if c.Bool("short") {
tab.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: "Query"},
{Align: simpletable.AlignCenter, Text: "City"},
},
}
r := []*simpletable.Cell{
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", ipInfo.Query)},
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", ipInfo.City)},
}
tab.Body.Cells = append(tab.Body.Cells, r)
} else {
tab.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: "Query"},
{Align: simpletable.AlignCenter, Text: "Country"},
{Align: simpletable.AlignCenter, Text: "Country Code"},
{Align: simpletable.AlignCenter, Text: "Region"},
{Align: simpletable.AlignCenter, Text: "City"},
{Align: simpletable.AlignCenter, Text: "Isp"},
{Align: simpletable.AlignCenter, Text: "As"},
},
}
r := []*simpletable.Cell{
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", ipInfo.Query)},
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", ipInfo.Country)},
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", ipInfo.CountryCode)},
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", ipInfo.Region)},
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", ipInfo.City)},
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", ipInfo.Isp)},
{Align: simpletable.AlignLeft, Text: fmt.Sprintf("%s", ipInfo.As)},
}
tab.Body.Cells = append(tab.Body.Cells, r)
}
} else {
return err
}
} else {
return err
}
tab.SetStyle(simpletable.StyleUnicode)
fmt.Println(tab.String())
return nil
}
func main() {
app.Name = "IP info"
app.Version = "1.0"
app.Author = "Dmitriy Vlassov"
app.Email = "[email protected]"
app.Usage = "Using ip-api.com for getting info about your ip or some another.\r\n"
app.UsageText = "myip - info about yourself ip\r\n" +
"\t myip {{some_ip}} - info about some another ip"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "short, s",
Usage: "short output",
},
}
app.Action = func(c *cli.Context) {
if err := getIPInfo(c); err != nil {
log.Fatal(err)
}
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}