-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCheckIssues.cs
378 lines (340 loc) · 12.7 KB
/
CheckIssues.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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;
CheckStores();
refresh();
}
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()
{
if (!IsNetFree())
return -1;
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
string CertIssuer = MyCert().Issuer;
foreach (X509Certificate2 Cert in store.Certificates)
{
if (Cert.Issuer == CertIssuer)//check if issuer is same as downloaded issuer
{
try
{
using (var client = new WebClient())
{
using (var stream = client.OpenRead("https://mail.google.com"))
{
return 2;
}
}
}
catch
{
return 1;
}
}
}
return 0;
}
private void CheckStores()
{
Boolean HasOther = false;
List<StoreLocation> StoreLocations = new List<StoreLocation>
{
StoreLocation.CurrentUser,
StoreLocation.LocalMachine
};
List<StoreName> StoreNames = new List<StoreName>
{
StoreName.AddressBook,
StoreName.AuthRoot,
StoreName.CertificateAuthority,
StoreName.Disallowed,
StoreName.My,
StoreName.Root,
StoreName.TrustedPeople,
StoreName.TrustedPublisher
};
X509Store store;
string CertIssuer = MyCert().Issuer;
foreach (StoreLocation storelocation in StoreLocations)
{
foreach (StoreName storename in StoreNames)
{
if (storelocation != StoreLocation.LocalMachine || storename != StoreName.Root)
{
store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
try
{
store.Open(OpenFlags.ReadWrite);
}
catch
{
DialogResult Response = MessageBox.Show("תוכנה זו דורש הרשאות מנהל", "שגיאה", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (Response == System.Windows.Forms.DialogResult.Retry)
CheckStores();
else
Environment.Exit(1);
}
foreach (X509Certificate2 Cert in store.Certificates)
{
if (Cert.Issuer == CertIssuer)
{
HasOther = true;
store.Remove(Cert);
}
}
store.Close();
}
}
}
if (HasOther && CheckCert() == 0)
{
CertInstall();
}
}
private X509Certificate2 MyCert()
{
try
{
WebClient client = new WebClient();
byte[] CertFile = client.DownloadData("http://netfree.link/netfree-ca.crt");
X509Certificate2 cert = new X509Certificate2();
cert.Import(CertFile);
return cert;
}
catch { return null; }
}
private void CertInstall()
{
try
{
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(MyCert());
store.Close();
}
catch { };
}
static uint SwapEndianness(ulong x)
{
return (uint)(((x & 0x000000ff) << 24) +
((x & 0x0000ff00) << 8) +
((x & 0x00ff0000) >> 8) +
((x & 0xff000000) >> 24));
}
private bool IsNetFree()
{
string ISP = "";
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
try
{
ISP = client.DownloadString("http://netfree.link/nf/user/0");
}
catch { };
}
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)
{
int CertStatus = CheckCert();
Cert.Visible = true;
ISP.Visible = true;
TimeCorrect.Visible = true;
Internet.Text = "אינטרנט מחובר";
if (CertStatus == 2)
Cert.Text = "תעודה מותקן";
else if (CertStatus == 1)
{
Cert.Text = "תעודה לא עובד";
fixcert.Visible = true;
}
else if (CertStatus == 0)
{
Cert.Text = "תעודה לא מותקן";
fixcert.Visible = true;
}
else
Cert.Text = "אין ספק נטפרי";
if (IsNetFree())
ISP.Text = "מחובר לספק נטפרי";
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();
int CertStatus = CheckCert();
refresh();
if (CertStatus == 1 || CertStatus == 2)
{
MessageBox.Show("התעודה הותקן בהצלחה", "התקנת תעודת אבטחה", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
DialogResult answer = MessageBox.Show("התעודה לא הותקן", "התקנת תעודת אבטחה", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (answer == DialogResult.Retry)
fixcert_Click(sender, e);
}
}
}
}