-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipaddressinfo.go
163 lines (142 loc) · 3.74 KB
/
ipaddressinfo.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"encoding/json"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"image/color"
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
a := app.New()
w := a.NewWindow("Get IP Address Information")
labelIpAddress := canvas.NewText("Your IP Address", color.White)
inputIpAddress := widget.NewEntry()
inputIpAddress.SetPlaceHolder("0.0.0.0")
labelIPType := canvas.NewText("IP Type", color.White)
inputIPType := widget.NewEntry()
inputIPType.SetPlaceHolder("IP Type")
labelOrg := canvas.NewText("Provider", color.White)
inputOrg := widget.NewEntry()
inputOrg.SetPlaceHolder("Provider")
labelCountry := canvas.NewText("Country", color.White)
inputCountry := widget.NewEntry()
inputCountry.SetPlaceHolder("Country")
labelRegion := canvas.NewText("Region", color.White)
inputRegion := widget.NewEntry()
inputRegion.SetPlaceHolder("Region")
labelCity := canvas.NewText("City", color.White)
inputCity := widget.NewEntry()
inputCity.SetPlaceHolder("Your City")
progress := widget.NewProgressBar()
locationGrid := container.New(layout.NewGridLayout(2), labelCountry, inputCountry, labelRegion, inputRegion, labelCity, inputCity)
getInformationButton := widget.NewButtonWithIcon("Get IP Information", theme.SearchIcon(), func() {
ip := getIp()
inputIpAddress.SetText(ip)
progress.SetValue(0.5)
if ip != "" {
ipInfo := getIpInfo(ip)
inputIPType.SetText(ipInfo.Type)
inputOrg.SetText(ipInfo.Connection.Org)
inputCity.SetText(ipInfo.City)
inputCountry.SetText(ipInfo.Country)
inputRegion.SetText(ipInfo.Region)
}
progress.SetValue(1.0)
})
cleanFieldsButton := widget.NewButtonWithIcon("Clean Fields", theme.ContentClearIcon(), func() {
inputIpAddress.SetText("")
inputIPType.SetText("")
inputOrg.SetText("")
inputCity.SetText("")
inputCountry.SetText("")
inputRegion.SetText("")
progress.SetValue(0)
})
copyIpAddressButton := widget.NewButtonWithIcon("Copy IP Address", theme.ContentCopyIcon(), func() {
w.Clipboard().SetContent(inputIpAddress.Text)
})
buttonsGrid := container.New(layout.NewGridLayout(3), getInformationButton, cleanFieldsButton, copyIpAddressButton)
w.SetContent(container.NewVBox(
labelIpAddress,
inputIpAddress,
labelIPType,
inputIPType,
labelOrg,
inputOrg,
locationGrid,
buttonsGrid,
progress,
))
w.Resize(fyne.NewSize(300, 200))
w.ShowAndRun()
}
type Response struct {
Status string
Code string
Total int
}
type IpInfo struct {
Ip string
Type string
Continent string
Country string
Region string
City string
Connection Connection
}
type Connection struct {
Org string
Domain string
}
func red_button() *fyne.Container { // return type
btn := widget.NewButton("Visit", nil) // button widget
// button color
btn_color := canvas.NewRectangle(
color.NRGBA{R: 255, G: 0, B: 0, A: 255})
// container for colored button
container1 := container.New(
// layout of container
layout.NewMaxLayout(),
// first use btn color
btn_color,
// 2nd btn widget
btn,
)
// our button is ready
return container1
}
func getIp() string {
response, err := http.Get("https://api.ipify.org")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
return string(responseData)
}
func getIpInfo(ip string) IpInfo {
res, err := http.Get("https://ipwho.is/" + ip)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
var myStoredVariable IpInfo
json.Unmarshal([]byte(responseData), &myStoredVariable)
return myStoredVariable
}