This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Form1.cs
669 lines (620 loc) · 26.2 KB
/
Form1.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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using System.Net;
using System.Text.RegularExpressions;
namespace qsign
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
this.FormClosing += Main_FormClosing;
}
private void label1_Click(object sender, EventArgs e)
{ }
private void write_config(string config_path)
{
string json = File.ReadAllText(config_path);
JObject jo = new JObject();
try
{
jo = (JObject)JsonConvert.DeserializeObject(json);
}
catch
{
MessageBox.Show("读取config.json失败,请检查相关配置", "读取失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
jo["server"]["port"] = Convert.ToInt32(textBox1.Text);
jo["key"] = textBox3.Text;
jo["server"]["host"] = textBox2.Text;
jo["auto_register"] = checkBox1.Checked;
jo["unidbg"]["dynarmic"] = checkBox2.Checked;
jo["unidbg"]["unicorn"] = checkBox3.Checked;
jo["unidbg"]["debug"] = checkBox4.Checked;
string json1 = jo.ToString();
File.WriteAllText(config_path, json1);
}
private string get_target_id(int port)
{
return Tool.PortToPid(port);
}
private void check()
{
while (flag)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:" + port.ToString());
request.Method = "GET";
HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
if (((int)httpWebResponse.StatusCode) == 200)
{
checknum += 1;
this.Invoke((Action)(() =>
{
label5.Text = "正在运行";
DisplayResourceUsage(port);
label5.ForeColor = Color.Green;
}));
if (checknum == 1)
{
Add_log(DateTime.Now.ToString("HH:mm:ss:fff") + " [qsign]在http://127.0.0.1:" + port.ToString() + "上运行");
if (MessageBox.Show("http://127.0.0.1:" + port.ToString() + "/sign?key=" + key + "\n点击确定复制Sign地址", "启动成功", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
Clipboard.SetDataObject("http://127.0.0.1:" + port.ToString() + "/sign?key=" + key);
}
}
}
else
{
label5.Invoke((Action)(() =>
{
label5.Text = "状态异常 " + httpWebResponse.StatusCode.ToString();
label7.Text = "内存使用量: 0 MB CPU使用率: 0%";
label5.ForeColor = Color.Red;
}));
}
httpWebResponse.Close();
}
catch
{
label5.Invoke((Action)(() =>
{
label5.Text = "状态异常";
label7.Text = "内存使用量: 0 MB CPU使用率: 0%";
label5.ForeColor = Color.Red;
string targetPid = get_target_id(port);
if (targetPid != "")
{
System.Diagnostics.Debug.WriteLine(targetPid);
var fileName = "cmd";
var arguments = "/c taskkill /F /PID " + targetPid;
// 创建进程启动信息
var processStartInfo = new ProcessStartInfo()
{
FileName = fileName,
Arguments = arguments,
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false
};
// 执行命令
var process1 = Process.Start(processStartInfo);
process1.WaitForExit();
while (newThread.IsAlive)
{
Thread.Sleep(100);
}
}
flag = true;
checknum = 0;
richTextBox1.Clear();
newThread = new Thread(new ThreadStart(NewThread));
CheckThread = new Thread(new ThreadStart(check));
run_state = true;
button1.Text = "停止";
newThread.IsBackground = true;
newThread.Start();
CheckThread.IsBackground = true;
CheckThread.Start();
comboBox1.Enabled = false;
textBox1.Enabled = false;
textBox3.Enabled = false;
textBox2.Enabled = false;
checkBox1.Enabled = false;
checkBox2.Enabled = false;
checkBox3.Enabled = false;
checkBox4.Enabled = false;
}));
}
try
{
Thread.Sleep(1000);
}
catch
{ }
}
}
public static bool ValidateIPAddress(string ipAddress)
{
string pattern = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
Regex regex = new Regex(pattern);
Match match = regex.Match(ipAddress);
return match.Success;
}
Thread newThread;
Thread CheckThread;
string verison;
int port;
string host;
string key;
bool flag = false;
int checknum = 0;
bool run_state = false;
[Obsolete]
private void button1_Click(object sender, EventArgs e)
{
if (!run_state)
{
string currentDirectory = Environment.CurrentDirectory;
bool exist = File.Exists(currentDirectory + "\\bin\\unidbg-fetch-qsign.bat");
if (exist == false)
{
MessageBox.Show("启动脚本不存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string path = currentDirectory + "\\txlib";
string[] folders = Directory.GetDirectories(path);
List<string> versions = new List<string>();
foreach (string folder in folders)
{
versions.Add(folder.Replace(currentDirectory + "\\txlib\\", ""));
System.Diagnostics.Debug.WriteLine(folder.Replace(currentDirectory + "\\txlib\\", ""));
}
if (!versions.Contains<string>(comboBox1.Text))
{
MessageBox.Show("版本不存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (textBox1.Text == "" || textBox3.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入相应内容", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
port = Convert.ToInt32(textBox1.Text);
}
catch
{
MessageBox.Show("端口错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (port < 0 || port > 65535)
{
MessageBox.Show("端口错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (get_target_id(port) != "")
{
MessageBox.Show("端口被占用,请更换端口号再试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!ValidateIPAddress(textBox2.Text))
{
MessageBox.Show("IP地址不合法", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int[] banned_port = {
1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42, 43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111, 113, 115, 117, 119, 123, 135, 139, 143, 179, 389, 465, 512, 513, 514, 515, 526, 530, 531, 532, 540, 556, 563, 587, 601, 636, 993, 995, 2049, 3659, 4045, 6000, 6665, 6666, 6667, 6668, 6669
};
if (banned_port.Contains<int>(port))
{
MessageBox.Show("非安全端口,请更换端口号再试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
verison = comboBox1.Text;
write_config(currentDirectory + "\\txlib\\" + comboBox1.Text + "\\config.json");
flag = true;
checknum = 0;
richTextBox1.Clear();
newThread = new Thread(new ThreadStart(NewThread));
CheckThread = new Thread(new ThreadStart(check));
run_state = true;
button1.Text = "停止";
newThread.IsBackground = true;
newThread.Start();
CheckThread.IsBackground = true;
CheckThread.Start();
comboBox1.Enabled = false;
textBox1.Enabled = false;
textBox3.Enabled = false;
textBox2.Enabled = false;
checkBox1.Enabled = false;
checkBox2.Enabled = false;
checkBox3.Enabled = false;
checkBox4.Enabled = false;
}
else
{
flag = false;
if (newThread.IsAlive)
{
string targetPid = get_target_id(port);
if (targetPid != "")
{
System.Diagnostics.Debug.WriteLine(targetPid);
var fileName = "cmd";
var arguments = "/c taskkill /F /PID " + targetPid;
var processStartInfo = new ProcessStartInfo()
{
FileName = fileName,
Arguments = arguments,
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false
};
var process1 = Process.Start(processStartInfo);
process1.WaitForExit();
while (newThread.IsAlive)
{
Thread.Sleep(100);
}
}
CheckThread.Interrupt();
button1.Text = "启动";
run_state = false;
label5.Text = "未启动";
label7.Text = "内存使用量: 0 MB CPU使用率: 0%";
label5.ForeColor = Color.Black;
Add_log(DateTime.Now.ToString("HH:mm:ss:fff") + " [qsign]Server已停止");
comboBox1.Enabled = true;
textBox1.Enabled = true;
textBox3.Enabled = true;
textBox2.Enabled = true;
checkBox1.Enabled = true;
checkBox2.Enabled = true;
checkBox3.Enabled = true;
checkBox4.Enabled = true;
}
}
}
private void NewThread()
{
RunCmd();
}
private void RunCmd()
{
string currentDirectory = Environment.CurrentDirectory;
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "cmd.exe";
ThreadPool.SetMaxThreads(1, 1);
process.OutputDataReceived += (sender, args) =>
{
if (!string.IsNullOrEmpty(args.Data))
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Add_log), args.Data + "\n");
}
};
process.ErrorDataReceived += (sender, args) =>
{
if (!string.IsNullOrEmpty(args.Data))
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Add_log), args.Data + "\n");
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.StandardInput.WriteLine("call bin\\unidbg-fetch-qsign.bat --basePath=txlib\\" + verison);
while (flag)
{
Thread.Sleep(10);
}
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
string currentDirectory = Environment.CurrentDirectory;
string config_path = currentDirectory + "\\txlib\\" + comboBox1.Text + "\\config.json";
if (!File.Exists(config_path))
{
MessageBox.Show("...");
return;
}
string json = File.ReadAllText(config_path);
JObject jo = new JObject();
try
{
jo = (JObject)JsonConvert.DeserializeObject(json);
if (jo == new JObject()) { return; }
port = (int)jo["server"]["port"];
key = (string)jo["key"];
host = (string)jo["server"]["host"];
textBox1.Text = port.ToString();
textBox3.Text = key;
textBox2.Text = host;
checkBox1.Checked = (bool)jo["auto_register"];
checkBox2.Checked = (bool)jo["unidbg"]["dynarmic"];
checkBox3.Checked = (bool)jo["unidbg"]["unicorn"];
checkBox4.Checked = (bool)jo["unidbg"]["debug"];
}
catch
{
MessageBox.Show("读取config.json失败,请检查相关配置", "读取失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{ }
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
if (flag)
{
if (MessageBox.Show("Server仍在运行,是否关闭窗口", "是否关闭", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
flag = false;
if (newThread.IsAlive)
{
string targetPid = get_target_id(port);
if (targetPid != "")
{
System.Diagnostics.Debug.WriteLine(targetPid);
var fileName = "cmd";
var arguments = "/c taskkill /F /PID " + targetPid;
// 创建进程启动信息
var processStartInfo = new ProcessStartInfo()
{
FileName = fileName,
Arguments = arguments,
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false
};
// 执行命令
var process1 = Process.Start(processStartInfo);
process1.WaitForExit();
while (newThread.IsAlive)
{
Thread.Sleep(100);
}
}
CheckThread.Interrupt();
run_state = false;
button1.Text = "启动";
label5.Text = "未启动";
label5.ForeColor = Color.Black;
Add_log(DateTime.Now.ToString("HH:mm:ss:fff") + " [qsign]Server已停止");
}
}
else
{
e.Cancel = true;
}
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(new ProcessStartInfo()
{
FileName = "https://github.com/fuqiuluo/unidbg-fetch-qsign",
UseShellExecute = true
});
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(new ProcessStartInfo()
{
FileName = "https://github.com/CikeyQi/unidbg-fetch-qsign-gui",
UseShellExecute = true
});
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(new ProcessStartInfo()
{
FileName = "http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=l6BrF5gd4T7QoUv_OXKTQqlRewtGe-lU&authKey=52bpY2z9OVc0pjVqkWMOt9PHAq20cNhk%2BzL1blVn0S%2B0U6znaVFjseaYiv5QG%2Fxu&noverify=0&group_code=551081559",
UseShellExecute = true
});
}
int lenght;
private void Add_log(object stats)
{
string logs = stats.ToString();
string[] logss = logs.Split(new string[] { "\n" }, StringSplitOptions.None);
if (logs == null || logs == "" || logs.Length == 0) { return; }
richTextBox1.Invoke((Action)(() =>
{
richTextBox1.AppendText(logs + "\n");
foreach (string log in logss)
{
// int currentLength = richTextBox1.Lines.Length;
//int lineFirstCharIndex = richTextBox1.GetFirstCharIndexFromLine(currentLength);
//if (lineFirstCharIndex == -1) { return; }
//richTextBox1.Select(lenght, (log + "\n").Length);
//if (log.Contains("INFO"))
//{
// richTextBox1.SelectionColor = Color.DeepSkyBlue;
// return;
//}
//if (log.Contains("DEBUG"))
//{
// richTextBox1.SelectionColor = Color.White;
// return;
//}
//if (log.Contains("WARNING"))
//{
// richTextBox1.SelectionColor = Color.FromArgb(255, 215, 0);
// return;
//}
//if (log.Contains("ERROR") || log.Contains("Exception"))
//{
// richTextBox1.SelectionColor = Color.Red;
// return;
//}
//if (log.Contains("[qsign]"))
//{
// richTextBox1.SelectionColor = Color.FromArgb(255, 215, 0);
// return;
//}
//else
//{
// richTextBox1.SelectionColor = Color.Black;
// return;
//}
}
lenght = richTextBox1.Text.Length;
richTextBox1.Select(richTextBox1.TextLength, 0);
richTextBox1.ScrollToCaret();
int maxLines = 100;
// 检查行数是否超过最大行数
if (richTextBox1.Lines.Length > maxLines)
{
// 获取超过最大行数的行数
int linesToRemove = richTextBox1.Lines.Length - maxLines;
// 删除多余的行
richTextBox1.Text = string.Join(Environment.NewLine, richTextBox1.Lines.Skip(linesToRemove));
}
}
));
}
private void DrawFont()
{
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
pfc.AddFontFile("PingFang.ttf");
Font myFont = new Font(pfc.Families[0], 12, FontStyle.Regular);
textBox1.Font = myFont;
textBox2.Font = myFont;
textBox3.Font = myFont;
comboBox1.Font = myFont;
checkBox1.Font = myFont;
checkBox2.Font = myFont;
checkBox3.Font = myFont;
checkBox4.Font = myFont;
label1.Font = myFont;
label2.Font = myFont;
label3.Font = myFont;
Font litemyFont = new Font(pfc.Families[0], 9, FontStyle.Regular);
richTextBox1.Font = litemyFont;
linkLabel1.Font = litemyFont;
linkLabel2.Font = litemyFont;
linkLabel3.Font = litemyFont;
label7.Font = litemyFont;
contextMenuStrip1.Font = litemyFont;
Font titlemyFont = new Font(pfc.Families[0], 20, FontStyle.Regular);
label5.Font = titlemyFont;
Font urlmyFont = new Font(pfc.Families[0], 12, FontStyle.Regular);
label6.Font = urlmyFont;
label4.Font = urlmyFont;
}
private void Main_Load_1(object sender, EventArgs e)
{
DrawFont();
notifyIcon1.Icon = Icon;
notifyIcon1.Text = "Windows下免安装环境一键启动qsign签名服务";
string currentDirectory = Environment.CurrentDirectory;
string path = currentDirectory + "\\txlib";
string[] folders = Directory.GetDirectories(path);
System.Diagnostics.Debug.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
Text = Text.Replace("%v", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
foreach (string folder in folders)
{
System.Diagnostics.Debug.WriteLine(folder);
comboBox1.Items.Add(folder.Replace(currentDirectory + "\\txlib\\", ""));
}
}
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
Process.Start(new ProcessStartInfo()
{
FileName = e.LinkText,
UseShellExecute = true
});
}
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
this.ShowInTaskbar = true;
notifyIcon1.Visible = false;
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
this.Close();
}
private void Main_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)//当窗体设置值为最小化时
{
this.Hide();
notifyIcon1.Visible = true;
}
else
{
notifyIcon1.Visible = false;//否则该控件不可见
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
this.ShowInTaskbar = true;
notifyIcon1.Visible = false;
}
private void Main_MinimumSizeChanged(object sender, EventArgs e)
{
this.Hide();
notifyIcon1.Visible = true;
this.ShowInTaskbar = false;
}
TimeSpan prevCpuTime = TimeSpan.Zero;
private void DisplayResourceUsage(int port)
{
int targetPID;
Process targetProcess;
// Get the PID of the target process
string targetPIDString = get_target_id(port);
if (!int.TryParse(targetPIDString, out targetPID))
{
targetPID = -1; // Set to -1 if PID cannot be obtained
}
// Get the process instance based on PID
try
{
targetProcess = targetPID != -1 ? Process.GetProcessById(targetPID) : null;
}
catch (ArgumentException)
{
targetProcess = null; // Set process instance to null if PID is invalid
return;
}
// Get memory and CPU information
double memoryUsageMB = targetProcess != null ? targetProcess.WorkingSet64 / (1024.0 * 1024.0) : 0; // Memory usage in MB
PerformanceCounter curtime = new PerformanceCounter("Process", "% Processor Time", targetProcess.ProcessName);
PerformanceCounter totalcpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
TimeSpan curCpuTime = targetProcess.TotalProcessorTime;
double value = (curCpuTime - prevCpuTime).TotalMilliseconds / 1000 / Environment.ProcessorCount * 100;
prevCpuTime = curCpuTime;
label7.Invoke((Action)(() =>
{
label7.Text = string.Format("内存使用量: {0:F2} MB CPU使用率: {1:F2}%", memoryUsageMB, value);
}));
}
private void richTextBox1_TextChanged_1(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
}
}