-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMain.cs
495 lines (456 loc) · 19.4 KB
/
FormMain.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
using Microsoft.VisualBasic;
using Microsoft.Win32;
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using static ITClassHelper.Process;
using static ITClassHelper.SharedConst;
using static ITClassHelper.Text;
using static ITClassHelper.Window;
namespace ITClassHelper
{
public partial class FormMain : Form
{
private readonly FormCastControl castControl = new FormCastControl();
private readonly FormDeviceManage deviceManage = new FormDeviceManage();
private static bool firstTimeHide = true;
public FormMain(string[] args)
{
if (GetProcsByName("ITClassHelper").Length > 1)
{
foreach (System.Diagnostics.Process programProc in GetProcsByName("ITClassHelper"))
{
int procId = programProc.Id;
if (procId != System.Diagnostics.Process.GetCurrentProcess().Id)
{
MessageBox.Show("机房助手已在运行!点击[确认]退出当前进程!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
}
}
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
ProgramAboutLabel.Text = ProgramAboutLabel.Text.Replace("X.Y.Z", programVersion);
HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Alt, Keys.H);
castControl.Show();
castControl.Hide();
if (programVersion.Contains("-d"))
{
MessageBox.Show("这是一个实验性版本,尚不稳定,请小心操作!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void FormMain_Load(object sender, EventArgs e)
{
Directory.CreateDirectory(appDataPath);
if (File.Exists(disableAttackFilePath))
{
DisableAttackButton.Enabled = false;
DeviceManageButton.Enabled = false;
}
if (!File.Exists(ntsdPath))
{
byte[] RescNtsd = Properties.Resources.Ntsd;
using (FileStream ntsdFsObj = new FileStream(ntsdPath, FileMode.CreateNew))
{
ntsdFsObj.Write(RescNtsd, 0, RescNtsd.Length);
}
}
byte[] RescLocalUpdateInfo = Properties.Resources.UpdateInfo;
using (FileStream localUpdateInfoFsObj = new FileStream(localUpdateConfigPath, FileMode.Create))
{
localUpdateInfoFsObj.Write(RescLocalUpdateInfo, 0, RescLocalUpdateInfo.Length);
}
byte[] RescRemoveCtrl = Properties.Resources.RemoveCtrl;
using (FileStream removeCtrlFsObj = new FileStream(removeCtrlPath, FileMode.Create))
{
removeCtrlFsObj.Write(RescRemoveCtrl, 0, RescRemoveCtrl.Length);
}
if (Network.GetPortInUse(6666) != true)
{
try
{
if (Network.socketBound == false)
{
Network.socket.Bind(new IPEndPoint(IPAddress.Parse(Network.GetIPAddress()), 6666));
}
Network.socketBound = true;
}
catch (SocketException ex)
{
MessageBox.Show($"本机 IP 地址绑定失败!将无法使用[简易内网聊天]功能!\n错因:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
ChatButton.Enabled = false;
}
}
else
{
MessageBox.Show($"本机聊天端口(6666)被占用!将无法使用[简易内网聊天]功能!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
ChatButton.Enabled = false;
}
File.Delete(cloudUpdateConfigPath);
bool checkStatus = true;
using (WebClient wc = new WebClient())
{
try { wc.DownloadFile($@"{programSite}/Resources/UpdateConfig.csv", cloudUpdateConfigPath); }
catch (WebException) { UpdateProgramButton.Text = "更新检查失败"; checkStatus = false; }
}
if (checkStatus == true)
{
using (StreamReader localConfigSr = File.OpenText(localUpdateConfigPath))
{
using (StreamReader cloudConfigSr = File.OpenText(cloudUpdateConfigPath))
{
string cloudVersion = ConvertTextToList(cloudConfigSr.ReadToEnd(), ',')[0];
string localVersion = ConvertTextToList(localConfigSr.ReadToEnd(), ',')[0];
string cloudStatus = ConvertTextToList(cloudConfigSr.ReadToEnd(), ',')[1];
string localStatus = ConvertTextToList(localConfigSr.ReadToEnd(), ',')[1];
if (cloudVersion != localVersion)
{
UpdateProgramButton.Text = cloudStatus != "stable" ? $"发现实验性新版本 {cloudVersion}" : $"发现新版本 {cloudVersion}";
}
else
{
UpdateProgramButton.Text = "暂无新版本";
}
}
}
}
new Thread(BackgroundThread) { IsBackground = true }.Start();
new Thread(ReceiveMessage) { IsBackground = true }.Start();
}
private void BackgroundThread()
{
while (true)
{
SetRoomPath(true);
IntPtr castWindow = GetCastWindow();
if (castWindow != IntPtr.Zero)
{
int[] castWndInfo = GetWindowInfo(castWindow);
//MoveWindow(castWindow, castWndInfo[2], castWndInfo[3], castWndInfo[0], castWndInfo[1], true);
SetWindowPos(castWindow, WndPos.NoTopMost, castWndInfo[2], castWndInfo[3], castWndInfo[0], castWndInfo[1], (uint)SWP.SWP_SHOWWINDOW);
RemoveHooks();
if (Visible == true)
{
TopMost = true;
}
}
else
{
if (TopMost == true)
{
TopMost = false;
}
}
if (MousePosition == new Point(0, 0))
{
//MoveWindow(castWindow, castControl.Size.Width, castControl.Size.Height, 1000, 500, true);
SetWindowPos(castWindow, WndPos.NoTopMost, castControl.Size.Width, castControl.Size.Height, 1000, 500, (uint)SWP.SWP_SHOWWINDOW);
castControl.Show();
}
if (GetProcsByName("StudentMain").Length > 0 || GetProcsByName("REDAgent").Length > 0)
{
RoomStatusLabel.Text = "正在运行";
RoomStatusLabel.ForeColor = Color.Red;
}
else
{
RoomStatusLabel.Text = "未在运行";
RoomStatusLabel.ForeColor = Color.Green;
}
Thread.Sleep(1000);
}
}
private void SuspendRoomButton_Click(object sender, EventArgs e)
{
SuspendRoom();
}
private void SuspendRoom()
{
if (GetProcsByName("StudentMain").Length > 0)
{
NtSuspendProcess(GetProcsByName("StudentMain")[0].Id);
}
if (GetProcsByName("REDAgent").Length > 0)
{
NtSuspendProcess(GetProcsByName("REDAgent")[0].Id);
}
}
private void KillRoomButton_Click(object sender, EventArgs e)
{
KillRoom();
}
private void KillRoom()
{
if (roomType == RoomType.Mythware)
{
KillProcs("StudentMain");
}
else
{
KillProcs("REDAgent");
try { File.Move(roomPath, redSpiderBackupPath); }
catch (IOException) { }
catch (UnauthorizedAccessException) { }
catch (ArgumentNullException) { }
}
}
private void ResumeRoomButton_Click(object sender, EventArgs e)
{
ResumeRoom();
}
private void ResumeRoom()
{
if (roomType == RoomType.Mythware)
{
if (GetProcsByName("StudentMain").Length > 0)
{
NtResumeProcess(GetProcsByName("StudentMain")[0].Id);
}
else
{
if (GetProcsByName("REDAgent").Length > 0)
{
NtResumeProcess(GetProcsByName("REDAgent")[0].Id);
}
else
{
if (File.Exists(roomPath))
{
Run(roomPath, "", true);
}
else
{
try { File.Move(redSpiderBackupPath, roomPath); }
catch (IOException) { }
catch (UnauthorizedAccessException) { }
}
}
}
}
}
private void DisableAttackButton_Click(object sender, EventArgs e)
{
DisableAttackButton.Enabled = false;
DeviceManageButton.Enabled = false;
File.Create(disableAttackFilePath);
}
private void UpdateProgramButton_Click(object sender, EventArgs e)
{
if (!File.Exists(@".\ITCHLauncher.exe"))
{
using (WebClient wc = new WebClient())
{
wc.DownloadFile($@"{programSite}bin/Release/ITCHLauncher.exe", @".\ITCHLauncher.exe");
}
}
Run(@".\ITCHLauncher.exe", "", true);
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
private void SetRoomPathButton_Click(object sender, EventArgs e)
{
SetRoomPath(false);
}
private void SetRoomPath(bool passive = true)
{
bool gotRoomPath = false;
if (roomPath != null)
{
if (passive == false)
{
MessageBox.Show("已获取过了教室程序路径!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
gotRoomPath = true;
}
else
{
if (GetProcsByName("StudentMain").Length > 0 || GetProcsByName("REDAgent").Length > 0)
{
roomPath = GetProcsByName("StudentMain").Length > 0
? GetProcsByName("StudentMain")[0].MainModule.FileName
: GetProcsByName("REDAgent")[0].MainModule.FileName;
if (passive == false)
{
MessageBox.Show("已自动获取到教室程序路径!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
gotRoomPath = true;
}
else
{
string defRoomPath = @"C:\Program Files\Mythware\e-Learning Class\StudentMain.exe";
if (File.Exists(defRoomPath))
{
roomPath = defRoomPath;
if (passive == false)
{
MessageBox.Show("已自动获取到教室程序路径!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
gotRoomPath = true;
}
else
{
if (passive == false)
{
MessageBox.Show("无法自动找到教室程序路径!\n按[确定]在下一界面手动选择;\n按[取消]放弃。", "错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
OpenFileDialog fileDialog = new OpenFileDialog
{
Multiselect = false,
Title = "选择教室程序",
Filter = "教室程序(*.exe)|*.exe"
};
if (fileDialog.ShowDialog() == DialogResult.OK)
{
roomPath = fileDialog.FileName;
gotRoomPath = true;
}
}
}
}
}
if (gotRoomPath == true)
{
string roomName = roomPath.Substring(roomPath.LastIndexOf(@"\") + 1);
if (roomName == "StudentMain.exe")
{
roomType = RoomType.Mythware;
}
else if (roomName == "REDAgent.exe")
{
roomType = RoomType.RedSpider;
}
else
if (passive == false)
{
MessageBox.Show("获取到了一个无效的教室路径,教室类型已重置!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
roomType = RoomType.None;
}
}
}
private void ReceiveMessage()
{
while (true)
{
EndPoint serverPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] buffer = new byte[10240];
int length = Network.socket.ReceiveFrom(buffer, ref serverPoint);
string message = $"收到来自 {serverPoint} 的消息:" + Encoding.UTF8.GetString(buffer, 0, length);
new Thread(x => MessageBox.Show(message, "消息", MessageBoxButtons.OK, MessageBoxIcon.None)).Start();
}
}
private void GetPswdButton_Click(object sender, EventArgs e)
{
if (MessageBox.Show("友情提示:可在任何时候使用超级密码 mythware_super_password!\n按[确定]继续获取;\n按[取消]将其复制到剪贴板并放弃获取。", "信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.Cancel)
{
Clipboard.SetDataObject("mythware_super_password");
return;
}
RegistryKey pswdKey;
try
{
pswdKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\TopDomain\e-Learning Class Standard\1.00");
string fullPswd = (string)pswdKey.GetValue("UninstallPasswd");
MessageBox.Show($"密码为:{fullPswd.Substring(6)}", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("无法获取到教室密码!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void SetPswdButton_Click(object sender, EventArgs e)
{
if (MessageBox.Show("设置密码完成后将自动重启教室!\n按[确定]继续设置;\n按[取消]放弃设置。", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
{
return;
}
if (MessageBox.Show("友情提示:可在任何时候使用超级密码 mythware_super_password!\n按[确定]继续设置;\n按[取消]将其复制到剪贴板并放弃设置。", "信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.Cancel)
{
Clipboard.SetDataObject("mythware_super_password");
return;
}
string keyDir = @"HKEY_LOCAL_MACHINE\SOFTWARE\TopDomain\e-Learning Class Standard\1.00";
string pswdText = Interaction.InputBox("请输入要设置的密码:", "信息");
Registry.SetValue(keyDir, "UninstallPasswd", $@"Passwd{pswdText}");
new Thread(x =>
{
KillProcs("StudentMain");
Thread.Sleep(1000);
ResumeRoom();
})
{ IsBackground = true }.Start();
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
if (firstTimeHide == true)
{
MessageBox.Show("机房助手已隐藏到后台,按下 Alt+H 即可显示!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
firstTimeHide = false;
}
}
private void ExitProgramButton_Click(object sender, EventArgs e)
{
Network.socket.Close();
HotKey.UnregisterHotKey(Handle, 100);
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
private void DeviceManageButton_Click(object sender, EventArgs e)
{
deviceManage.Show();
}
private void ChatButton_Click(object sender, EventArgs e)
{
string clientIP = Interaction.InputBox("请输入接收者的 IP 地址:", "信息");
EndPoint clientPoint = new IPEndPoint(IPAddress.Parse(clientIP), 6666);
byte[] bytes = Encoding.UTF8.GetBytes(Interaction.InputBox("请输入要发送的消息:", "信息"));
try
{ Network.socket.SendTo(bytes, clientPoint); }
catch (SocketException ex)
{
MessageBox.Show($"发送消息失败!\n错因:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
MessageBox.Show("发送消息成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void ProgramSettingsCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (ProgramSettingsCheckBox.Checked == true)
{
UpdateProgramButton.Visible = false;
DisableAttackButton.Visible = false;
SetRoomPathButton.Visible = false;
DeviceManageButton.Visible = false;
ChatButton.Visible = false;
}
else
{
UpdateProgramButton.Visible = true;
DisableAttackButton.Visible = true;
SetRoomPathButton.Visible = true;
DeviceManageButton.Visible = true;
ChatButton.Visible = true;
}
}
protected override void WndProc(ref Message msg)
{
const int WM_HOTKEY = 0x0312;
switch (msg.Msg)
{
case WM_HOTKEY:
switch (msg.WParam.ToInt32())
{
case 100:
Visible = !Visible;
break;
}
break;
}
base.WndProc(ref msg);
}
}
}