-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
720 lines (510 loc) · 14.9 KB
/
MainForm.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// Programming by Eric Chauvin.
// Notes on this source code are at:
// ericbreakingrsa.blogspot.com
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Diagnostics; // For start program process.
using System.Security.Cryptography;
// System.Windows.Controls; ViewPort3D
// System.Windows.Media.Media3D.MeshGeometry3D
// System.Windows.Media
// CryptoGraphic Services:
// https://msdn.microsoft.com/en-us/library/92f9ye3s%28v=vs.110%29.aspx
// using System.Net.Security; // SslStream
// using System.Security;
// ICryptoTransform
// using System.Security.Cryptography.X509Certificates;
namespace ExampleServer
{
public partial class MainForm : Form
{
internal const string VersionDate = "4/2/2016";
internal const int VersionNumber = 09; // 0.9
internal const string MessageBoxTitle = "Eric's Example Server";
private System.Threading.Mutex SingleInstanceMutex = null;
private bool IsSingleInstance = false;
private bool IsClosing = false;
// private bool FormShownOnce = false;
internal GlobalProperties GlobalProps;
private string WebPagesDirectory = "";
private string DataDirectory = "";
internal NetIPStatus NetStats;
// private long CheckTimerCount = 0;
internal WebListenerForm WebListenForm = null;
internal TLSListenerForm TLSListenForm = null;
internal CustomerMessageForm CustomerMsgForm = null;
internal LaPlataData LaPlataData1;
internal WebFilesData WebFData;
internal DomainX509Data X509Data;
private RNGCryptoServiceProvider CryptoRand;
private QuadResSearchForm QuadResForm;
private bool Cancelled = false;
private FactorDictionary FactorDictionary1;
public MainForm()
{
InitializeComponent();
SetLocaleEnglish();
///////////////////////
// Keep this at the top:
SetupDirectories();
GlobalProps = new GlobalProperties( this );
///////////////////////
CryptoRand = new RNGCryptoServiceProvider();
NetStats = new NetIPStatus( this );
NetStats.ReadFromFile();
FactorDictionary1 = new FactorDictionary( this );
LaPlataData1 = new LaPlataData( this );
WebFData = new WebFilesData( this );
X509Data = new DomainX509Data( this, GetDataDirectory() + "Certificates.txt" );
if( !CheckSingleInstance())
return;
IsSingleInstance = true;
WebListenForm = new WebListenerForm( this );
TLSListenForm = new TLSListenerForm( this );
CustomerMsgForm = new CustomerMessageForm( this );
QuadResForm = new QuadResSearchForm( this );
CheckTimer.Interval = 5 * 60 * 1000;
CheckTimer.Start();
StartTimer.Interval = 1000;
StartTimer.Start();
}
internal void ShowStatus( string Status )
{
if( IsClosing )
return;
if( MainTextBox.Text.Length > (80 * 5000))
MainTextBox.Text = "";
MainTextBox.AppendText( Status + "\r\n" );
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
string ShowS = "Programming by Eric Chauvin. Version date: " + VersionDate;
MessageBox.Show( ShowS, MessageBoxTitle, MessageBoxButtons.OK );
}
internal string GetWebPagesDirectory()
{
return WebPagesDirectory;
}
internal string GetDataDirectory()
{
return DataDirectory;
}
private void SetupDirectories()
{
DataDirectory = Application.StartupPath + "\\Data\\";
WebPagesDirectory = Application.StartupPath + "\\WebPages\\";
if( !Directory.Exists( WebPagesDirectory ))
{
try
{
Directory.CreateDirectory( WebPagesDirectory );
}
catch( Exception )
{
MessageBox.Show( "Error: The WebPages directory could not be created.", MessageBoxTitle, MessageBoxButtons.OK );
return;
}
}
if( !Directory.Exists( DataDirectory ))
{
try
{
Directory.CreateDirectory( DataDirectory );
}
catch( Exception )
{
MessageBox.Show( "Error: The data directory could not be created.", MessageBoxTitle, MessageBoxButtons.OK );
return;
}
}
}
internal bool GetIsClosing()
{
return IsClosing;
}
internal bool CheckEvents()
{
if( IsClosing )
return false;
Application.DoEvents();
if( Cancelled )
return false;
return true;
}
// This has to be added in the Program.cs file.
internal static void UIThreadException( object sender, ThreadExceptionEventArgs t )
{
string ErrorString = t.Exception.Message;
try
{
string ShowString = "There was an unexpected error:\r\n\r\n" +
"The program will close now.\r\n\r\n" +
ErrorString;
MessageBox.Show( ShowString, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Stop );
}
finally
{
Application.Exit();
}
}
private bool CheckSingleInstance()
{
bool InitialOwner = false; // Owner for single instance check.
string ShowS = "Another instance of the Example Server is already running." +
" This instance will close.";
try
{
SingleInstanceMutex = new System.Threading.Mutex( true, "Example Server Single Instance", out InitialOwner );
}
catch
{
MessageBox.Show( ShowS, MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop );
// mutex.Close();
// mutex = null;
// Can't do this here:
// Application.Exit();
SingleInstanceTimer.Interval = 50;
SingleInstanceTimer.Start();
return false;
}
if( !InitialOwner )
{
MessageBox.Show( ShowS, MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop );
// Application.Exit();
SingleInstanceTimer.Interval = 50;
SingleInstanceTimer.Start();
return false;
}
return true;
}
private void SetLocaleEnglish()
{
// Make sure you know what you're dealing with when converting money, numbers,
// delimiters, etc.
RegionInfo Region = new RegionInfo( Application.CurrentCulture.Name );
string CName = Application.CurrentCulture.Name;
string CultureName = Application.CurrentCulture.EnglishName;
if( CName != "en-US" )
{
try
{
CultureInfo American = new CultureInfo( "en-US" );
Application.CurrentCulture = American;
}
catch
{
MessageBox.Show( "This program may not work correctly in this culture. Could not set the culture to English (United States). Culture name is: " + CultureName, MessageBoxTitle, MessageBoxButtons.OK );
}
}
}
private void SingleInstanceTimer_Tick(object sender, EventArgs e)
{
SingleInstanceTimer.Stop();
Application.Exit();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if( IsSingleInstance )
{
if( DialogResult.Yes != MessageBox.Show( "Close the program?", MessageBoxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question ))
{
e.Cancel = true;
return;
}
}
IsClosing = true;
CheckTimer.Stop();
X509Data.WriteToTextFile();
NetStats.SaveToFile();
if( WebListenForm != null )
{
if( !WebListenForm.IsDisposed )
{
WebListenForm.Hide();
// This could take a while since it's closing connections:
WebListenForm.FreeEverythingAndStopServer();
WebListenForm.Dispose();
}
WebListenForm = null;
}
if( TLSListenForm != null )
{
if( !TLSListenForm.IsDisposed )
{
TLSListenForm.Hide();
// This could take a while since it's closing connections:
TLSListenForm.FreeEverythingAndStopServer();
TLSListenForm.Dispose();
}
TLSListenForm = null;
}
if( CustomerMsgForm != null )
{
if( !CustomerMsgForm.IsDisposed )
{
CustomerMsgForm.Hide();
CustomerMsgForm.FreeEverything();
CustomerMsgForm.Dispose();
}
CustomerMsgForm = null;
}
/*
if( MakeKeysForm != null )
{
if( !MakeKeysForm.IsDisposed )
{
MakeKeysForm.Hide();
// This could take a while:
MakeKeysForm.FreeEverything();
MakeKeysForm.Dispose();
}
MakeKeysForm = null;
}
*/
if( QuadResForm != null )
{
if( !QuadResForm.IsDisposed )
{
QuadResForm.Hide();
// This could take a while:
QuadResForm.FreeEverything();
QuadResForm.Dispose();
}
QuadResForm = null;
}
}
private void SaveAllMidnightFiles()
{
/*
if( WebListenForm != null )
{
if( !WebListenForm.IsDisposed )
{
WebListenForm.ClearDailyHackCount();
}
}
*/
NetStats.ClearMidnightValues();
}
private void CheckTimer_Tick(object sender, EventArgs e)
{
CheckTimer.Stop();
try
{
// CheckTimerCount++;
// int CountMod = (int)(CheckTimerCount % 5);
// if( CountMod == 0 )
NetStats.SaveToFile();
// ECTime ShowTime = new ECTime();
// ShowTime.SetToNow();
// ShowStatus( "Saved data files at: " + ShowTime.ToLocalTimeString() );
}
finally
{
CheckTimer.Start();
}
}
// Be careful about what you execute from the server.
internal bool StartProgramOrFile( string FileName )
{
if( !File.Exists( FileName ))
return false;
Process ProgProcess = new Process();
try
{
ProgProcess.StartInfo.FileName = FileName;
ProgProcess.StartInfo.Verb = ""; // "Print";
ProgProcess.StartInfo.CreateNoWindow = false;
ProgProcess.StartInfo.ErrorDialog = false;
ProgProcess.Start();
}
catch( Exception Except )
{
MessageBox.Show( "Could not start the file: \r\n" + FileName + "\r\n\r\nThe error was:\r\n" + Except.Message, MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop );
return false;
}
return true;
}
internal void ShowWebListenerFormStatus( string Status )
{
if( IsClosing )
return;
if( WebListenForm == null )
return;
if( WebListenForm.IsDisposed )
return;
WebListenForm.ShowStatus( Status );
}
internal void ShowCustomerMsgFormStatus( string Status )
{
if( IsClosing )
return;
if( CustomerMsgForm == null )
return;
if( CustomerMsgForm.IsDisposed )
return;
CustomerMsgForm.ShowStatus( Status );
}
/*
internal void ShowMakeKeysFormStatus( string Status )
{
if( IsClosing )
return;
if( MakeKeysForm == null )
return;
if( MakeKeysForm.IsDisposed )
return;
MakeKeysForm.ShowStatus( Status );
}
*/
internal void ShowQuadResFormStatus( string Status )
{
if( IsClosing )
return;
if( QuadResForm == null )
return;
if( QuadResForm.IsDisposed )
return;
QuadResForm.ShowStatus( Status );
}
internal void ShowTLSListenerFormStatus( string Status )
{
if( IsClosing )
return;
if( TLSListenForm == null )
return;
if( TLSListenForm.IsDisposed )
return;
TLSListenForm.ShowStatus( Status );
}
private void showBasicWebServerToolStripMenuItem_Click(object sender, EventArgs e)
{
if( WebListenForm == null )
return;
if( WebListenForm.IsDisposed )
return;
WebListenForm.Show();
WebListenForm.WindowState = FormWindowState.Normal;
WebListenForm.BringToFront();
}
private void StartTimer_Tick(object sender, EventArgs e)
{
ShowStatus( "StartTimer was called." );
StartTimer.Stop();
LaPlataData1.ReadFromFile();
ShowStatus( "Finished reading La Plata data." );
ReadWebFileData();
ShowStatus( "Finished reading web files." );
X509Data.ReadFromTextFile();
// X509Data.ImportFromOriginalListFile();
ShowStatus( "Finished reading X509 data list." );
WebListenForm.StartServer();
TLSListenForm.StartServer();
ShowStatus( "After the servers were started." );
}
private void showTLSServerToolStripMenuItem_Click(object sender, EventArgs e)
{
if( TLSListenForm == null )
return;
if( TLSListenForm.IsDisposed )
return;
TLSListenForm.Show();
TLSListenForm.WindowState = FormWindowState.Normal;
TLSListenForm.BringToFront();
}
internal void ReadWebFileData()
{
ShowStatus( "Starting to read web file data." );
WebFData.SearchWebPagesDirectory();
ShowStatus( "Finished reading web file data." );
}
internal int GetRandomNumber()
{
byte[] TheBytes = new byte[4];
CryptoRand.GetBytes( TheBytes );
uint Result = (uint)(TheBytes[0]) & 0x7F;
Result <<= 8;
Result |= TheBytes[1];
Result <<= 8;
Result |= TheBytes[2];
Result <<= 8;
Result |= TheBytes[3];
return (int)Result;
}
private void showTLSCustomerToolStripMenuItem_Click(object sender, EventArgs e)
{
if( CustomerMsgForm == null )
return;
if( CustomerMsgForm.IsDisposed )
return;
CustomerMsgForm.Show();
CustomerMsgForm.WindowState = FormWindowState.Normal;
CustomerMsgForm.BringToFront();
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if( e.KeyCode == Keys.Escape ) // && (e.Alt || e.Control || e.Shift))
{
ShowStatus( "Cancelled." );
Cancelled = true;
}
}
/*
internal void FoundSolutionShutDown( string ProcessName,
string SolutionP,
string SolutionQ )
{
if( IsClosing )
return;
if( MakeKeysForm == null )
return;
if( MakeKeysForm.IsDisposed )
return;
MakeKeysForm.FoundSolutionShutDown( ProcessName, SolutionP, SolutionQ );
}
*/
private void showQuadResToolStripMenuItem_Click( object sender, EventArgs e )
{
if( QuadResForm == null )
return;
if( QuadResForm.IsDisposed )
return;
QuadResForm.Show();
QuadResForm.WindowState = FormWindowState.Normal;
QuadResForm.BringToFront();
}
internal void FoundSolutionShutDownQuadRes( string ProcessName,
string SolutionP,
string SolutionQ )
{
if( IsClosing )
return;
if( QuadResForm == null )
return;
if( QuadResForm.IsDisposed )
return;
QuadResForm.FoundSolutionShutDown( ProcessName, SolutionP, SolutionQ );
}
internal void AddToFactorDictionary( string ToAdd )
{
FactorDictionary1.AddDelimString( ToAdd );
}
internal void FactorDictSetProduct( string ProductS )
{
FactorDictionary1.SetProduct( ProductS );
}
private void factorDictionaryToolStripMenuItem_Click( object sender, EventArgs e )
{
FactorDictionary1.FindFactors();
}
}
}