-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainPage.xaml.cs
276 lines (240 loc) · 8.33 KB
/
MainPage.xaml.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace network_info
{
public partial class MainPage : ContentPage
{
public bool ipv4avaible = false;
public bool ipv6avaible = false;
public MainPage()
{
InitializeComponent();
versionInfo.Text = $"Version: {AppInfo.VersionString} ({AppInfo.BuildString})";
RefreshInfo();
}
private void RefreshButton_Clicked(object sender, EventArgs e)
{
RefreshInfo();
}
public async void RefreshInfo()
{
Ipv4.Text = "getting data";
Ipv6.Text = "getting data";
Country4.Text = "getting data";
Country6.Text = "getting data";
Isp4.Text = "getting data";
Isp6.Text = "getting data";
Vpn4.Text = "getting data";
Vpn6.Text = "getting data";
string versionString = "";
string Ipv4LocalString = "";
string Ipv6LocalString = "";
string Ipv4String = "";
string Ipv6String = "";
string Country4String = "";
string Country6String = "";
string Isp4String = "";
string Isp6String = "";
string Vpn4String = "";
string Vpn6String = "";
// Díky Rakočević za návod na await Task.Run
await Task.Run(async () =>
{
Ipv4LocalString = await GetLocalIPv4();
Ipv6LocalString = await GetLocalIPv6();
});
Ipv4Local.Text = Ipv4LocalString;
Ipv6Local.Text = Ipv6LocalString;
await Task.Run(async() =>
{
versionString = await UpdaterFunction();
Ipv4String = await GetIPv4();
});
versionStatus.Text = versionString;
Ipv4.Text = Ipv4String;
if (ipv4avaible)
{
await Task.Run(async () =>
{
Country4String = await GetCountry(Ipv4String);
Isp4String = await GetIsp(Ipv4String);
Vpn4String = await GetVpn(Ipv4String);
});
}
else
{
Country4String = "unknown";
Isp4String = "unknown";
Vpn4String = "unknown";
}
Country4.Text = Country4String;
Isp4.Text = Isp4String;
Vpn4.Text = Vpn4String;
await Task.Run(async () =>
{
Ipv6String = await GetIPv6();
});
Ipv6.Text = Ipv6String;
if (ipv6avaible)
{
await Task.Run(async () =>
{
Country6String = await GetCountry(Ipv6String);
Isp6String = await GetIsp(Ipv6String);
Vpn6String = await GetVpn(Ipv6String);
});
}
else
{
Country6String = "unknown";
Isp6String = "unknown";
Vpn6String = "unknown";
}
Country6.Text = Country6String;
Isp6.Text = Isp6String;
Vpn6.Text = Vpn6String;
Debug.WriteLine("Refreshing done");
}
public async Task<string> UpdaterFunction()
{
Debug.WriteLine("Checking version from server");
string yourVersion = AppInfo.BuildString;
string latestVersion = yourVersion;
latestVersion = await MakeWebRequest("https://raw.githubusercontent.com/filip2cz/network-info/main/ver");
if (yourVersion == latestVersion)
{
return "You have latest version.";
}
else if (latestVersion == "unknown")
{
return "Failed to check for updates.";
}
else
{
return "You do not have latest version of app, consider update.";
}
}
// thanks chatgpt for GetLocalIPv* functions
public async Task<string> GetLocalIPv4()
{
string ipAddress = null;
foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (var addressInfo in netInterface.GetIPProperties().UnicastAddresses)
{
if (addressInfo.Address.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = addressInfo.Address.ToString();
break;
}
}
if (ipAddress != null)
break;
}
return ipAddress;
}
public async Task<string> GetLocalIPv6()
{
string ipAddress = null;
foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (var addressInfo in netInterface.GetIPProperties().UnicastAddresses)
{
if (addressInfo.Address.AddressFamily == AddressFamily.InterNetworkV6)
{
ipAddress = addressInfo.Address.ToString();
break;
}
}
if (ipAddress != null)
break;
}
return ipAddress;
}
public async Task<string> GetIPv4()
{
string Ipv4Url = "https://v4.ipv6-test.com/api/myip.php";
string responseipv4 = await MakeWebRequest(Ipv4Url);
if (responseipv4 == "unknown")
{
ipv4avaible = false;
}
else
{
ipv4avaible = true;
}
Debug.WriteLine("getting IPv4 done");
Debug.WriteLine($"Ipv4 = {responseipv4}");
return responseipv4;
}
public async Task<string> GetIPv6()
{
string Ipv6Url = "https://v6.ipv6-test.com/api/myip.php";
string responseipv6 = await MakeWebRequest(Ipv6Url);
if (responseipv6 == "unknown")
{
ipv6avaible = false;
}
else
{
ipv6avaible = true;
}
Debug.WriteLine("getting IPv6 done");
Debug.WriteLine($"Ipv6 = {responseipv6}");
return responseipv6;
}
public async Task<string> GetCountry(string ip)
{
string url = $"http://ip-api.com/line/{ip}?fields=country";
string response = await MakeWebRequest(url);
return response;
}
public async Task<string> GetIsp(string ip)
{
string url = $"http://ip-api.com/line/{ip}?fields=isp";
string response = await MakeWebRequest(url);
return response;
}
public async Task<string> GetVpn(string ip)
{
string url = $"http://ip-api.com/line/{ip}?fields=proxy";
string response = await MakeWebRequest(url);
return response;
}
public async Task<string> MakeWebRequest(string url)
{
string response = string.Empty;
int i = 0;
while (i < 3)
{
using (WebClient client = new WebClient())
{
try
{
// to do https://stackoverflow.com/questions/1789627/how-to-change-the-timeout-on-a-net-webclient-object
response = client.DownloadString(url);
i = 3;
}
catch (Exception ex)
{
Debug.WriteLine("HTTPS request failed");
Debug.WriteLine(ex);
i++;
if (i < 3)
{
Debug.WriteLine("Trying HTTPS request again");
}
else
{
response = "unknown";
}
}
}
}
response = response.Replace("\n", "").Replace("\r", "");
return response;
}
}
}