forked from NetFree-Community/NetFree_Check-Issues
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckIssues.cs
More file actions
321 lines (285 loc) · 10.7 KB
/
CheckIssues.cs
File metadata and controls
321 lines (285 loc) · 10.7 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NetFree_Check_Issues
{
public partial class CheckIssues : Form
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetSystemTime(ref SYSTEMTIME st);
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
public CheckIssues()
{
InitializeComponent();
loader.Size = layout.Size;
loader.Location = layout.Location;
refresh();
}
private void CheckIssues_Load(object sender, EventArgs e)
{
}
public DateTime InternetTime()
{
DateTime datetime = DateTime.MinValue;
for (int tries = 1; tries < 3; tries++)
{
try
{
const string ntpServer = "time.windows.com";
// NTP message size - 16 bytes of the digest (RFC 2030)
var ntpData = new byte[48];
//Setting the Leap Indicator, Version Number and Mode values
ntpData[0] = 0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
var addresses = Dns.GetHostEntry(ntpServer).AddressList;
//The UDP port number assigned to NTP is 123
var ipEndPoint = new IPEndPoint(addresses[0], 123);
//NTP uses UDP
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Connect(ipEndPoint);
//Stops code hang if NTP is blocked
socket.ReceiveTimeout = 3000;
socket.Send(ntpData);
socket.Receive(ntpData);
socket.Close();
//Offset to get to the "Transmit Timestamp" field (time at which the reply
//departed the server for the client, in 64-bit timestamp format."
const byte serverReplyTime = 40;
//Get the seconds part
ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);
//Get the seconds fraction
ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);
//Convert From big-endian to little-endian
intPart = SwapEndianness(intPart);
fractPart = SwapEndianness(fractPart);
var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
//**UTC** time
var networkDateTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
datetime = networkDateTime;
return datetime;
}
catch { };
}
return datetime;
}
private bool TimeRight()
{
return InternetTime().ToLocalTime().Date == DateTime.Now.Date;
}
private int CheckCert(string isp)
{
if (isp == "")
return -1;
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 Cert in store.Certificates)
{
if (Cert.Issuer.Contains("NetFree Sign") && Cert.Issuer.Contains(isp))
{
try
{
using (var client = new WebClient())
{
using (var stream = client.OpenRead("https://mail.google.com"))
{
return 2;
}
}
}
catch
{
return 1;
}
}
}
return 0;
}
private void CertInstall()
{
try
{
WebClient client = new WebClient();
byte[] CertFile = client.DownloadData("http://netfree.link/netfree-ca.crt"); ;
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
X509Certificate2 cert = new X509Certificate2();
cert.Import(CertFile);
store.Open(OpenFlags.ReadWrite);
store.Add(cert); //where cert is an X509Certificate object
store.Close();
}
catch { };
}
static uint SwapEndianness(ulong x)
{
return (uint)(((x & 0x000000ff) << 24) +
((x & 0x0000ff00) << 8) +
((x & 0x00ff0000) >> 8) +
((x & 0xff000000) >> 24));
}
private string MyIsp()
{
string ISP = "";
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
try
{
ISP = client.DownloadString("http://netfree.link/nf/user/0");
}
catch { };
if (ISP.Contains("@rl-internet"))
ISP = "RL";
else if (ISP.Contains("@019"))
ISP = "019";
else if (ISP.Contains("@URI"))
ISP = "NISIM-VPN";
else if (ISP.Contains("@netfree-anywhere"))
ISP = "NetFreeAnywhere";
else
ISP = "";
}
return ISP;
}
private int Connected()
{
Ping pinger = new Ping();
for (int tries = 1; tries < 3; tries++)
{
try
{
PingReply netfree = pinger.Send("NetFree.Link");
PingReply gmail = pinger.Send("mail.google.com");
if (netfree.Status == IPStatus.Success)
return 2;
else if (gmail.Status == IPStatus.Success)
return 1;
}
catch { };
}
return 0;
}
private void SetTime(DateTime datetime)
{
SYSTEMTIME st = new SYSTEMTIME();
st.wYear = short.Parse(datetime.Year.ToString());
st.wMonth = short.Parse(datetime.Month.ToString());
st.wDay = short.Parse(datetime.Day.ToString());
st.wHour = short.Parse(datetime.Hour.ToString());
st.wMinute = short.Parse(datetime.Minute.ToString());
st.wSecond = short.Parse(datetime.Second.ToString());
SetSystemTime(ref st);
}
private void button1_Click(object sender, EventArgs e)
{
refresh();
}
private void refresh()
{
fixcert.Visible = false;
fixtime.Visible = false;
loader.Visible = true;
if (Connected() == 2)
{
string isp = MyIsp();
Cert.Visible = true;
ISP.Visible = true;
TimeCorrect.Visible = true;
Internet.Text = "אינטרנט מחובר";
if (CheckCert(isp) == 2)
Cert.Text = "תעודה מותקן";
else if (CheckCert(isp) == 1)
{
Cert.Text = "תעודה לא עובד";
fixcert.Visible = true;
}
else if (CheckCert(isp) == 0)
{
Cert.Text = "תעודה לא מותקן";
fixcert.Visible = true;
}
else
Cert.Text = "אין ספק נטפרי";
if (isp != "")
ISP.Text = isp;
else
ISP.Text = "לא מחובר לספק נטפרי";
if (TimeRight())
TimeCorrect.Text = "תאריך נכון";
else
{
TimeCorrect.Text = "תאריך לא נכון";
fixtime.Visible = true;
}
}
else if (Connected() == 1)
{
TimeCorrect.Visible = true;
Internet.Text = "אין גישה לנטפרי";
Cert.Visible = false;
ISP.Visible = false;
if (TimeRight())
TimeCorrect.Text = "תאריך נכון";
else
{
TimeCorrect.Text = "תאריך נכון";
fixtime.Visible = true;
}
}
else
{
Internet.Text = "אינטרנט לא מחובר";
Cert.Visible = false;
ISP.Visible = false;
TimeCorrect.Visible = false;
}
loader.Visible = false;
}
private void button3_Click(object sender, EventArgs e)
{
SetTime(InternetTime());
refresh();
Process.Start("timedate.cpl").WaitForExit();
refresh();
}
private void fixcert_Click(object sender, EventArgs e)
{
CertInstall();
refresh();
if (CheckCert(MyIsp()) == 1 || CheckCert(MyIsp()) == 2)
{
MessageBox.Show("התעודה הותקן בהצלחה", "התקנת תעודת אבטחה", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
DialogResult answer = MessageBox.Show("התעודה לא הותקן", "התקנת תעודת אבטחה", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (answer == DialogResult.Retry)
fixcert_Click(sender, e);
}
}
}
}