-
Notifications
You must be signed in to change notification settings - Fork 975
Expand file tree
/
Copy pathForm1.cs
More file actions
3205 lines (2820 loc) · 141 KB
/
Form1.cs
File metadata and controls
3205 lines (2820 loc) · 141 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
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace subs_check.win.gui
{
public partial class Form1: Form
{
//string 版本号;
string 标题;
private Process subsCheckProcess = null;
string nodeInfo;//进度
private Icon originalNotifyIcon; // 保存原始图标
private ToolStripMenuItem startMenuItem;
private ToolStripMenuItem stopMenuItem;
string githubProxyURL = "";
int run = 0;
string 当前subsCheck版本号 = "未知版本";
string 当前GUI版本号 = "未知版本";
string 最新GUI版本号 = "未知版本";
private string nextCheckTime = null;// 用于存储下次检查时间
string WebUIapiKey = "CMLiussss";
int downloading = 0;
public Form1()
{
InitializeComponent();
originalNotifyIcon = notifyIcon1.Icon;
toolTip1.SetToolTip(numericUpDown1, "并发线程数:推荐 宽带峰值/50M。");
toolTip1.SetToolTip(numericUpDown2, "检查间隔时间(分钟):放置后台的时候,下次自动测速的间隔时间。\n\n 双击切换 使用「cron表达式」");
toolTip1.SetToolTip(label2, "检查间隔时间(分钟):放置后台的时候,下次自动测速的间隔时间。\n\n 双击切换 使用「cron表达式」");
toolTip1.SetToolTip(numericUpDown3, "超时时间(毫秒):节点的最大延迟。");
toolTip1.SetToolTip(numericUpDown4, "最低测速结果舍弃(KB/s)。");
toolTip1.SetToolTip(numericUpDown5, "下载测试时间(s):与下载链接大小相关,默认最大测试10s。");
toolTip1.SetToolTip(numericUpDown6, "本地监听端口:用于直接返回测速结果的节点信息,方便 Sub-Store 实现订阅转换。");
toolTip1.SetToolTip(numericUpDown7, "Sub-Store监听端口:用于订阅订阅转换。\n注意:除非你知道你在干什么,否则不要将你的 Sub-Store 暴露到公网,否则可能会被滥用");
toolTip1.SetToolTip(textBox1, "节点池订阅地址:支持 Link、Base64、Clash 格式的订阅链接。");
toolTip1.SetToolTip(checkBox1, "以节点IP查询位置重命名节点。\n质量差的节点可能造成IP查询失败,造成整体检查速度稍微变慢。");
toolTip1.SetToolTip(checkBox2, "是否开启流媒体检测,其中IP欺诈依赖'节点地址查询',内核版本需要 v2.0.8 以上\n\n示例:美国1 | ⬇️ 5.6MB/s |0%|Netflix|Disney|Openai\n风控值:0% (使用ping0.cc标准)\n流媒体解锁:Netflix、Disney、Openai");
toolTip1.SetToolTip(comboBox3, "GitHub 代理:代理订阅 GitHub raw 节点池。");
toolTip1.SetToolTip(comboBox2, "测速地址:注意 并发数*节点速度<最大网速 否则测速结果不准确\n尽量不要使用Speedtest,Cloudflare提供的下载链接,因为很多节点屏蔽测速网站。");
toolTip1.SetToolTip(textBox7, "将测速结果推送到Worker的地址。");
toolTip1.SetToolTip(textBox6, "Worker令牌。");
toolTip1.SetToolTip(comboBox1, "测速结果的保存方法。");
toolTip1.SetToolTip(textBox2, "Gist ID:注意!非Github用户名!");
toolTip1.SetToolTip(textBox3, "Github TOKEN");
toolTip1.SetToolTip(comboBox4, "通用订阅:内置了Sub-Store程序,自适应订阅格式。\nClash订阅:带规则的 Mihomo、Clash 订阅格式。");
toolTip1.SetToolTip(comboBox5, "生成带规则的 Clash 订阅所需的覆写规则文件");
toolTip1.SetToolTip(checkBox3, "保存几个成功的节点,不选代表不限制,内核版本需要 v2.1.0 以上\n如果你的并发数量超过这个参数,那么成功的结果可能会大于这个数值");
toolTip1.SetToolTip(numericUpDown8, "保存几个成功的节点,不选代表不限制,内核版本需要 v2.1.0 以上\n如果你的并发数量超过这个参数,那么成功的结果可能会大于这个数值");
toolTip1.SetToolTip(textBox11, "支持标准cron表达式,如:\n 0 */2 * * * 表示每2小时的整点执行\n 0 0 */2 * * 表示每2天的0点执行\n 0 0 1 * * 表示每月1日0点执行\n */30 * * * * 表示每30分钟执行一次\n\n 双击切换 使用「分钟倒计时」");
toolTip1.SetToolTip(checkBox5, "开机启动:勾选后,程序将在Windows启动时自动运行");
// 设置通知图标的上下文菜单
SetupNotifyIconContextMenu();
}
private void SetupNotifyIconContextMenu()
{
// 创建上下文菜单
ContextMenuStrip contextMenu = new ContextMenuStrip();
// 创建"▶️ 启动"菜单项
startMenuItem = new ToolStripMenuItem("启动");
startMenuItem.Click += (sender, e) =>
{
if (button1.Text == "▶️ 启动")
{
button1_Click(sender, e);
}
};
// 创建"⏹️ 停止"菜单项
stopMenuItem = new ToolStripMenuItem("停止");
stopMenuItem.Click += (sender, e) =>
{
if (button1.Text == "⏹️ 停止")
{
button1_Click(sender, e);
}
};
stopMenuItem.Enabled = false; // 初始状态下禁用
// 创建"退出"菜单项
ToolStripMenuItem exitMenuItem = new ToolStripMenuItem("退出");
exitMenuItem.Click += async (sender, e) =>
{
try
{
// 如果程序正在运行,先停止它
if (subsCheckProcess != null && !subsCheckProcess.HasExited)
{
await KillNodeProcessAsync();
StopSubsCheckProcess();
}
// 确保通知图标被移除
notifyIcon1.Visible = false;
// 使用更直接的退出方式
Environment.Exit(0);
}
catch (Exception ex)
{
MessageBox.Show($"退出程序时发生错误: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
// 如果仍然失败,尝试强制退出
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
};
// 将菜单项添加到上下文菜单
contextMenu.Items.Add(startMenuItem);
contextMenu.Items.Add(stopMenuItem);
contextMenu.Items.Add(new ToolStripSeparator()); // 分隔线
contextMenu.Items.Add(exitMenuItem);
// 将上下文菜单分配给通知图标
notifyIcon1.ContextMenuStrip = contextMenu;
// 确保通知图标可见
notifyIcon1.Visible = true;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
// 取消关闭操作
e.Cancel = true;
// 调用隐藏窗口方法
隐藏窗口();
}
}
private async void timer1_Tick(object sender, EventArgs e)//初始化
{
timer1.Enabled = false;
if (button2.Text == "高级设置∧") button2_Click(sender, e);
// 检查并创建config文件夹
string executablePath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
string configFolderPath = System.IO.Path.Combine(executablePath, "config");
if (!System.IO.Directory.Exists(configFolderPath))
{
// 文件不存在,可以给用户反馈
string 免责声明 = "SubsCheck-Win-GUI 项目仅供教育、研究和安全测试目的而设计和开发。本项目旨在为安全研究人员、学术界人士及技术爱好者提供一个探索和实践网络通信技术的工具。\r\n在下载和使用本项目代码时,使用者必须严格遵守其所适用的法律和规定。使用者有责任确保其行为符合所在地区的法律框架、规章制度及其他相关规定。\r\n\r\n使用条款\r\n\r\n教育与研究用途:本软件仅可用于网络技术和编程领域的学习、研究和安全测试。\r\n禁止非法使用:严禁将 SubsCheck-Win-GUI 用于任何非法活动或违反使用者所在地区法律法规的行为。\r\n使用时限:基于学习和研究目的,建议用户在完成研究或学习后,或在安装后的24小时内,删除本软件及所有相关文件。\r\n免责声明:SubsCheck-Win-GUI 的创建者和贡献者不对因使用或滥用本软件而导致的任何损害或法律问题负责。\r\n用户责任:用户对使用本软件的方式以及由此产生的任何后果完全负责。\r\n无技术支持:本软件的创建者不提供任何技术支持或使用协助。\r\n知情同意:使用 SubsCheck-Win-GUI 即表示您已阅读并理解本免责声明,并同意受其条款的约束。\r\n\r\n请记住:本软件的主要目的是促进学习、研究和安全测试。创作者不支持或认可任何其他用途。使用者应当在合法和负责任的前提下使用本工具。\r\n\r\n同意以上条款请点击\"是 / Yes\",否则程序将退出。";
// 显示带有 "同意" 和 "拒绝" 选项的对话框
DialogResult result = MessageBox.Show(免责声明, "SubsCheck-Win-GUI 免责声明", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
// 如果用户点击 "拒绝" (对应于 No 按钮)
if (result == DialogResult.No)
{
// 退出程序
Environment.Exit(0); // 立即退出程序
}
System.IO.Directory.CreateDirectory(configFolderPath);
}
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
当前GUI版本号 = "v" + myFileVersionInfo.FileVersion;
最新GUI版本号 = 当前GUI版本号;
标题 = "SubsCheck Win GUI " + 当前GUI版本号;
this.Text = 标题;// + " TG:CMLiussss BY:CM喂饭 干货满满";
comboBox1.Text = "本地";
comboBox4.Text = "通用订阅";
ReadConfig();
if (CheckCommandLineParameter("-auto"))
{
Log("检测到开机启动,准备执行任务...");
button1_Click(this, EventArgs.Empty);
this.Hide();
notifyIcon1.Visible = true;
} else await CheckGitHubVersionAsync();
}
private async Task CheckGitHubVersionAsync()
{
try
{
// 首先检查是否有网络连接
if (!IsNetworkAvailable())
{
return; // 静默返回,不显示错误
}
var result = await 获取版本号("https://api.github.com/repos/cmliu/SubsCheck-Win-GUI/releases/latest");
if (result.Item1 != "未知版本")
{
string latestVersion = result.Item1;
if (latestVersion != 当前GUI版本号)
{
最新GUI版本号 = latestVersion;
标题 = "SubsCheck Win GUI " + 当前GUI版本号 + $" 发现新版本: {最新GUI版本号} 请及时更新!";
this.Text = 标题;
}
}
}
catch
{
// 静默处理任何其他异常
return;
}
}
// 添加检查网络连接的辅助方法
private bool IsNetworkAvailable()
{
try
{
return NetworkInterface.GetIsNetworkAvailable();
}
catch
{
return false; // 如果无法检查网络状态,假设网络不可用
}
}
private async void ReadConfig()//读取配置文件
{
checkBox5.CheckedChanged -= checkBox5_CheckedChanged;// 临时移除事件处理器,防止触发事件
try
{
string executablePath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
string configFilePath = Path.Combine(executablePath, "config", "config.yaml");
if (File.Exists(configFilePath))
{
// 读取YAML文件内容
string yamlContent = File.ReadAllText(configFilePath);
// 使用YamlDotNet解析YAML
var deserializer = new YamlDotNet.Serialization.Deserializer();
var config = deserializer.Deserialize<Dictionary<string, object>>(yamlContent);
// 使用新函数获取整数值并设置UI控件
int? concurrentValue = 读取config整数(config, "concurrent");
if (concurrentValue.HasValue) numericUpDown1.Value = concurrentValue.Value;
int? checkIntervalValue = 读取config整数(config, "check-interval");
if (checkIntervalValue.HasValue) numericUpDown2.Value = checkIntervalValue.Value;
int? timeoutValue = 读取config整数(config, "timeout");
if (timeoutValue.HasValue) numericUpDown3.Value = timeoutValue.Value;
int? minspeedValue = 读取config整数(config, "min-speed");
if (minspeedValue.HasValue) numericUpDown4.Value = minspeedValue.Value;
int? downloadtimeoutValue = 读取config整数(config, "download-timeout");
if (downloadtimeoutValue.HasValue) numericUpDown5.Value = downloadtimeoutValue.Value;
string speedTestUrl = 读取config字符串(config, "speed-test-url");
if (speedTestUrl != null) comboBox2.Text = speedTestUrl;
string savemethod = 读取config字符串(config, "save-method");
if (savemethod != null)
{
if (savemethod == "local") comboBox1.Text = "本地";
else comboBox1.Text = savemethod;
}
string listenport = 读取config字符串(config, "listen-port");
if (listenport != null)
{
// 查找最后一个冒号的位置
int colonIndex = listenport.LastIndexOf(':');
if (colonIndex >= 0 && colonIndex < listenport.Length - 1)
{
// 提取冒号后面的部分作为端口号
string portStr = listenport.Substring(colonIndex + 1);
if (decimal.TryParse(portStr, out decimal port))
{
numericUpDown6.Value = port;
}
}
}
/*
int? substoreport = 读取config整数(config, "sub-store-port");
if (substoreport.HasValue) numericUpDown7.Value = substoreport.Value;
*/
string substoreport = 读取config字符串(config, "sub-store-port");
if (substoreport != null)
{
// 查找最后一个冒号的位置
int colonIndex = substoreport.LastIndexOf(':');
if (colonIndex >= 0 && colonIndex < substoreport.Length - 1)
{
// 提取冒号后面的部分作为端口号
string portStr = substoreport.Substring(colonIndex + 1);
if (decimal.TryParse(portStr, out decimal port))
{
numericUpDown7.Value = port;
}
}
}
string githubproxy = 读取config字符串(config, "githubproxy");
if (githubproxy != null) comboBox3.Text = githubproxy;
const string githubRawPrefix = "https://raw.githubusercontent.com/";
string mihomoOverwriteUrl = 读取config字符串(config, "mihomo-overwrite-url");
int mihomoOverwriteUrlIndex = mihomoOverwriteUrl.IndexOf(githubRawPrefix);
if (mihomoOverwriteUrl != null)
{
if (mihomoOverwriteUrl.Contains("http://127.0.0"))
{
if (mihomoOverwriteUrl.EndsWith("bdg.yaml", StringComparison.OrdinalIgnoreCase))
{
comboBox5.Text = "[内置]布丁狗的订阅转换";
await ProcessComboBox5Selection();
}
else if (mihomoOverwriteUrl.EndsWith("ACL4SSR_Online_Full.yaml", StringComparison.OrdinalIgnoreCase))
{
comboBox5.Text = "[内置]ACL4SSR_Online_Full";
await ProcessComboBox5Selection();
}
}
else if (mihomoOverwriteUrlIndex > 0) comboBox5.Text = mihomoOverwriteUrl.Substring(mihomoOverwriteUrlIndex);
else comboBox5.Text = mihomoOverwriteUrl;
}
// 处理URLs,检查是否包含GitHub raw链接
List<string> subUrls = 读取config列表(config, "sub-urls");
if (subUrls != null && subUrls.Count > 0)
{
// 创建一个新的过滤后的列表
var filteredUrls = new List<string>();
for (int i = 0; i < subUrls.Count; i++)
{
// 排除本地URL
string localUrlPattern = $"http://127.0.0.1:{numericUpDown6.Value}/all.yaml";
if (!subUrls[i].Equals(localUrlPattern, StringComparison.OrdinalIgnoreCase))
{
// 处理GitHub raw链接
int index = subUrls[i].IndexOf(githubRawPrefix);
if (index > 0) // 如果找到且不在字符串开头
{
// 只保留从githubRawPrefix开始的部分
filteredUrls.Add(subUrls[i].Substring(index));
}
else
{
// 如果不是GitHub链接,直接添加
filteredUrls.Add(subUrls[i]);
}
}
}
// 将过滤后的列表中的每个URL放在单独的行上
textBox1.Text = string.Join(Environment.NewLine, filteredUrls);
}
string renamenode = 读取config字符串(config, "rename-node");
if (renamenode != null && renamenode == "true") checkBox1.Checked = true;
else checkBox1.Checked = false;
string mediacheck = 读取config字符串(config, "media-check");
if (mediacheck != null && mediacheck == "true") checkBox2.Checked = true;
else checkBox2.Checked = false;
string githubgistid = 读取config字符串(config, "github-gist-id");
if (githubgistid != null) textBox2.Text = githubgistid;
string githubtoken = 读取config字符串(config, "github-token");
if (githubtoken != null) textBox3.Text = githubtoken;
string githubapimirror = 读取config字符串(config, "github-api-mirror");
if (githubapimirror != null) textBox4.Text = githubapimirror;
string workerurl = 读取config字符串(config, "worker-url");
if (workerurl != null) textBox7.Text = workerurl;
string workertoken = 读取config字符串(config, "worker-token");
if (workertoken != null) textBox6.Text = workertoken;
string webdavusername = 读取config字符串(config, "webdav-username");
if (webdavusername != null) textBox9.Text = webdavusername;
string webdavpassword = 读取config字符串(config, "webdav-password");
if (webdavpassword != null) textBox8.Text = webdavpassword;
string webdavurl = 读取config字符串(config, "webdav-url");
if (webdavurl != null) textBox5.Text = webdavurl;
string subscheckversion = 读取config字符串(config, "subscheck-version");
if (subscheckversion != null) 当前subsCheck版本号 = subscheckversion;
int? successlimit = 读取config整数(config, "success-limit");
if (successlimit.HasValue)
{
if (successlimit.Value == 0)
{
checkBox3.Checked = false;
numericUpDown8.Enabled = false;
}
else
{
checkBox3.Checked = true;
numericUpDown8.Enabled = true;
numericUpDown8.Value = successlimit.Value;
}
}
string enablewebui = 读取config字符串(config, "enable-web-ui");
if (enablewebui != null && enablewebui == "true") checkBox4.Checked = true;
else checkBox4.Checked = false;
string apikey = 读取config字符串(config, "api-key");
if (apikey != null)
{
if (apikey == GetComputerNameMD5())
{
checkBox4.Checked = false;
string oldapikey = 读取config字符串(config, "old-api-key");
if (oldapikey != null)
{
textBox10.Text = oldapikey;
}
else
{
textBox10.PasswordChar = '\0';
textBox10.Text = "请输入密钥";
textBox10.ForeColor = Color.Gray;
}
}
else
{
textBox10.Text = apikey;
}
}
string cronexpression = 读取config字符串(config, "cron-expression");
if (cronexpression != null)
{
textBox11.Text = cronexpression;
string cronDescription = GetCronExpressionDescription(textBox11.Text);
textBox11.Location = new Point(9, 48);
textBox11.Visible = true;
label2.Visible = false;
numericUpDown2.Visible = false;
}
string guiauto = 读取config字符串(config, "gui-auto");
if (guiauto != null && guiauto == "true") checkBox5.Checked = true;
else checkBox5.Checked = false;
}
else
{
comboBox3.Text = "自动选择";
comboBox5.Text = "[内置]布丁狗的订阅转换";
}
}
catch (Exception ex)
{
MessageBox.Show($"读取配置文件时发生错误: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
checkBox5.CheckedChanged += checkBox5_CheckedChanged;// 重新绑定事件处理器
}
private int? 读取config整数(Dictionary<string, object> config, string fieldName)
{
// 检查是否存在指定字段且不为null
if (config.ContainsKey(fieldName) && config[fieldName] != null)
{
int value;
if (int.TryParse(config[fieldName].ToString(), out value))
return value;
}
return null;
}
private string 读取config字符串(Dictionary<string, object> config, string fieldName)
{
// 检查是否存在指定字段且不为null
if (config.ContainsKey(fieldName) && config[fieldName] != null)
{
return config[fieldName].ToString();
}
return null;
}
private List<string> 读取config列表(Dictionary<string, object> config, string fieldName)
{
// 检查是否存在指定字段且不为null
if (config.ContainsKey(fieldName) && config[fieldName] != null)
{
// 尝试将对象转换为列表
if (config[fieldName] is List<object> listItems)
{
return listItems.Select(item => item?.ToString()).Where(s => !string.IsNullOrEmpty(s)).ToList();
}
}
return null;
}
private async Task SaveConfig(bool githubProxyCheck = true)//保存配置文件
{
try
{
string executablePath = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
string configFilePath = Path.Combine(executablePath, "config", "config.yaml");
// 创建配置字典
var config = new Dictionary<string, object>();
// 从UI控件获取值并添加到字典中
config["concurrent"] = (int)numericUpDown1.Value;
config["check-interval"] = (int)numericUpDown2.Value;
if (textBox11.Visible) config["cron-expression"] = textBox11.Text;
config["timeout"] = (int)numericUpDown3.Value;
config["min-speed"] = (int)numericUpDown4.Value;
config["download-timeout"] = (int)numericUpDown5.Value;
if (!string.IsNullOrEmpty(comboBox2.Text)) config["speed-test-url"] = comboBox2.Text;
// 保存save-method,将"本地"转换为"local"
config["save-method"] = comboBox1.Text == "本地" ? "local" : comboBox1.Text;
// 保存gist参数
config["github-gist-id"] = textBox2.Text;
config["github-token"] = textBox3.Text;
config["github-api-mirror"] = textBox4.Text;
// 保存r2参数
config["worker-url"] = textBox7.Text;
config["worker-token"] = textBox6.Text;
// 保存webdav参数
config["webdav-username"] = textBox9.Text;
config["webdav-password"] = textBox8.Text;
config["webdav-url"] = textBox5.Text;
// 保存enable-web-ui
config["enable-web-ui"] = true;
// 保存listen-port
if (checkBox4.Checked)
{
WebUIapiKey = textBox10.Text;
config["listen-port"] = $@":{numericUpDown6.Value}";
}
else
{
WebUIapiKey = GetComputerNameMD5();
config["listen-port"] = $@"127.0.0.1:{numericUpDown6.Value}";
if (textBox10.Text != "请输入密钥") config["old-api-key"] = textBox10.Text;
}
config["api-key"] = WebUIapiKey;
// 保存sub-store-port
config["sub-store-port"] = $@":{numericUpDown7.Value}";
string githubRawPrefix = "https://raw.githubusercontent.com/";
if (githubProxyCheck)
{
// 检查并处理 GitHub Raw URLs
if (comboBox3.Text == "自动选择")
{
// 创建不包含"自动选择"的代理列表
List<string> proxyItems = new List<string>();
for (int j = 0; j < comboBox3.Items.Count; j++)
{
string proxyItem = comboBox3.Items[j].ToString();
if (proxyItem != "自动选择")
proxyItems.Add(proxyItem);
}
// 随机打乱列表顺序
Random random = new Random();
proxyItems = proxyItems.OrderBy(x => random.Next()).ToList();
// 异步检测可用代理
githubProxyURL = await DetectGitHubProxyAsync(proxyItems);
}
}
if (comboBox3.Text != "自动选择") githubProxyURL = $"https://{comboBox3.Text}/";
config["githubproxy"] = comboBox3.Text;
config["github-proxy"] = githubProxyURL;
// 保存sub-urls列表
List<string> subUrls = new List<string>();
string allyamlFilePath = System.IO.Path.Combine(executablePath, "output", "all.yaml");
if (System.IO.File.Exists(allyamlFilePath))
{
subUrls.Add($"http://127.0.0.1:{numericUpDown6.Value}/all.yaml");
Log("已加载上次测试结果。");
}
if (!string.IsNullOrEmpty(textBox1.Text))
{
subUrls.AddRange(textBox1.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList());
// 处理URLs
for (int i = 0; i < subUrls.Count; i++)
{
if (subUrls[i].StartsWith(githubRawPrefix) && !string.IsNullOrEmpty(githubProxyURL))
{
// 替换为代理 URL 格式
//subUrls[i] = githubProxyURL + githubRawPrefix + subUrls[i].Substring(githubRawPrefix.Length);
// 使用subs-check内置github-proxy参数
subUrls[i] = githubRawPrefix + subUrls[i].Substring(githubRawPrefix.Length);
}
}
}
config["sub-urls"] = subUrls;
// 处理配置文件下载与配置
if (comboBox5.Text.Contains("[内置]"))
{
// 确定文件名和下载URL
string fileName;
string downloadFilePath;
string downloadUrl;
string displayName;
if (comboBox5.Text.Contains("[内置]布丁狗"))
{
fileName = "bdg.yaml";
displayName = "[内置]布丁狗的订阅转换";
downloadUrl = "https://raw.githubusercontent.com/cmliu/ACL4SSR/main/yaml/bdg.yaml";
}
else // [内置]ACL4SSR
{
fileName = "ACL4SSR_Online_Full.yaml";
displayName = "[内置]ACL4SSR_Online_Full";
downloadUrl = "https://raw.githubusercontent.com/beck-8/override-hub/main/yaml/ACL4SSR_Online_Full.yaml";
}
// 确保output文件夹存在
string outputFolderPath = Path.Combine(executablePath, "output");
if (!Directory.Exists(outputFolderPath))
{
Directory.CreateDirectory(outputFolderPath);
}
// 确定文件完整路径
downloadFilePath = Path.Combine(outputFolderPath, fileName);
if (!File.Exists(downloadFilePath)) await ProcessComboBox5Selection();
// 检查文件是否存在
if (!File.Exists(downloadFilePath))
{
Log($"{displayName} 覆写配置文件 未找到,将使用在线版本。");
config["mihomo-overwrite-url"] = githubProxyURL + downloadUrl;
}
else
{
Log($"{displayName} 覆写配置文件 加载成功。");
config["mihomo-overwrite-url"] = $"http://127.0.0.1:{numericUpDown6.Value}/{fileName}";
}
}
else if (comboBox5.Text.StartsWith(githubRawPrefix)) config["mihomo-overwrite-url"] = githubProxyURL + comboBox5.Text;
else config["mihomo-overwrite-url"] = comboBox5.Text != "" ? comboBox5.Text : $"http://127.0.0.1:{numericUpDown6.Value}/ACL4SSR_Online_Full.yaml";
config["rename-node"] = checkBox1.Checked;//以节点IP查询位置重命名节点
config["media-check"] = checkBox2.Checked;//是否开启流媒体检测
config["keep-success-proxies"] = false;
config["print-progress"] = false;//是否显示进度
config["sub-urls-retry"] = 3;//重试次数(获取订阅失败后重试次数)
config["subscheck-version"] = 当前subsCheck版本号;//当前subsCheck版本号
config["gui-auto"] = checkBox5.Checked;//是否开机自启
//保存几个成功的节点,为0代表不限制
if (checkBox3.Checked) config["success-limit"] = (int)numericUpDown8.Value;
else config["success-limit"] = 0;
// 使用YamlDotNet序列化配置
var serializer = new YamlDotNet.Serialization.SerializerBuilder()
.WithIndentedSequences() // 使序列化结果更易读
.Build();
string yamlContent = serializer.Serialize(config);
// 确保配置目录存在
string configDirPath = Path.GetDirectoryName(configFilePath);
if (!Directory.Exists(configDirPath))
Directory.CreateDirectory(configDirPath);
string moreYamlPath = Path.Combine(configDirPath, "more.yaml");
if (File.Exists(moreYamlPath))
{
// 读取more.yaml的内容
string moreYamlContent = File.ReadAllText(moreYamlPath);
// 确保more.yaml内容以换行开始
if (!moreYamlContent.StartsWith("\n") && !moreYamlContent.StartsWith("\r\n"))
{
yamlContent += "\n"; // 添加换行符作为分隔
}
// 将more.yaml的内容追加到要写入的config.yaml内容后
yamlContent += moreYamlContent;
Log($"已将补充参数配置 more.yaml 内容追加到配置文件");
}
// 写入YAML文件
File.WriteAllText(configFilePath, yamlContent);
}
catch (Exception ex)
{
MessageBox.Show($"保存配置文件时发生错误: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "高级设置∧")
{
button2.Text = "高级设置∨";
groupBox3.Visible = false;
}
else
{
button2.Text = "高级设置∧";
groupBox3.Visible = true;
}
判断保存类型();
}
private async void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
if (button1.Text == "▶️ 启动")
{
if (checkBox4.Checked && textBox10.Text == "请输入密钥")
{
MessageBox.Show("您已启用WebUI,请设置WebUI API密钥!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
run = 1;
if (button3.Enabled==false)
{
string executablePath = Path.GetDirectoryName(Application.ExecutablePath);
string allyamlFilePath = Path.Combine(executablePath, "output", "all.yaml");
button3.Enabled = File.Exists(allyamlFilePath);
}
numericUpDown1.Enabled = false;
numericUpDown2.Enabled = false;
textBox11.Enabled = false;
numericUpDown3.Enabled = false;
numericUpDown4.Enabled = false;
numericUpDown5.Enabled = false;
numericUpDown6.Enabled = false;
numericUpDown7.Enabled = false;
comboBox1.Enabled = false;
textBox1.Enabled = false;
groupBox3.Enabled = false;
groupBox4.Enabled = false;
groupBox5.Enabled = false;
groupBox6.Enabled = false;
if (checkBox4.Checked) button6.Enabled = true;
button1.Text = "⏹️ 停止";
//timer3.Enabled = true;
// 清空 richTextBox1
richTextBox1.Clear();
await KillNodeProcessAsync();
await SaveConfig();
if (run == 1)
{
// 更新菜单项的启用状态
startMenuItem.Enabled = false;
stopMenuItem.Enabled = true;
// 清空 richTextBox1
//richTextBox1.Clear();
notifyIcon1.Text = "SubsCheck: 已就绪";
// 启动 subs-check.exe 程序
StartSubsCheckProcess();
}
}
else
{
run = 0;
Log("任务停止");
progressBar1.Value = 0;
groupBox2.Text = "实时日志";
notifyIcon1.Text = "SubsCheck: 未运行";
// 停止 subs-check.exe 程序
StopSubsCheckProcess();
// 结束 Sub-Store
await KillNodeProcessAsync();
if (checkBox4.Checked) ReadConfig();
button3.Enabled = false;
numericUpDown1.Enabled = true;
numericUpDown2.Enabled = true;
textBox11.Enabled = true;
numericUpDown3.Enabled = true;
numericUpDown4.Enabled = true;
numericUpDown5.Enabled = true;
numericUpDown6.Enabled = true;
numericUpDown7.Enabled = true;
comboBox1.Enabled = true;
textBox1.Enabled = true;
groupBox3.Enabled = true;
groupBox4.Enabled = true;
groupBox5.Enabled = true;
groupBox6.Enabled = true;
button6.Enabled = false;
button1.Text = "▶️ 启动";
//timer3.Enabled = false;
// 更新菜单项的启用状态
startMenuItem.Enabled = true;
stopMenuItem.Enabled = false;
}
if (downloading == 0) button1.Enabled = true;
}
private async Task DownloadSubsCheckEXE()
{
button1.Enabled = false;
downloading = 1;
try
{
Log("正在检查网络连接...");
// 首先检查是否有网络连接
if (!IsNetworkAvailable())
{
Log("网络连接不可用,无法下载核心文件。", true);
MessageBox.Show("缺少 subs-check.exe 核心文件。\n\n您可以前往 https://github.com/beck-8/subs-check/releases 自行下载!",
"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
var result = await 获取版本号("https://api.github.com/repos/beck-8/subs-check/releases/latest", true);
if (result.Item1 != "未知版本")
{
// 创建不使用系统代理的 HttpClientHandler
HttpClientHandler handler = new HttpClientHandler
{
UseProxy = false,
Proxy = null
};
// 使用自定义 handler 创建 HttpClient
using (HttpClient client = new HttpClient(handler))
{
try
{
string latestVersion = result.Item1;
JArray assets = result.Item2;
Log($"subs-check.exe 最新版本为: {latestVersion} ");
// 查找Windows i386版本的资源
string downloadUrl = null;
foreach (var asset in assets)
{
if (asset["name"].ToString() == "subs-check_Windows_i386.zip")
{
downloadUrl = asset["browser_download_url"].ToString();
break;
}
}
if (downloadUrl != null)
{
string 代理下载链接 = githubProxyURL + downloadUrl;
string 原生下载链接 = 代理下载链接;
// 计算"https://"在下载链接中出现的次数
int httpsCount = 0;
int lastIndex = -1;
int currentIndex = 0;
// 查找所有"https://"出现的位置
while ((currentIndex = 代理下载链接.IndexOf("https://", currentIndex)) != -1)
{
httpsCount++;
lastIndex = currentIndex;
currentIndex += 8; // "https://".Length = 8
}
// 如果"https://"出现2次或以上,提取最后一个"https://"之后的内容
if (httpsCount >= 2 && lastIndex != -1)
{
原生下载链接 = 代理下载链接.Substring(lastIndex);
}
string executablePath = Path.GetDirectoryName(Application.ExecutablePath);
// 创建下载请求 - 优化的多级尝试下载逻辑
Log("开始下载文件...");
bool downloadSuccess = false;
string zipFilePath = Path.Combine(executablePath, "subs-check_Windows_i386.zip");
string failureReason = "";
// 如果文件已存在,先删除
if (File.Exists(zipFilePath)) File.Delete(zipFilePath);
// 第一次尝试:使用代理下载链接 + 当前HttpClient(不使用系统代理)
try
{
Log($"[尝试1/4] 使用代理下载链接:{代理下载链接}");
downloadSuccess = await DownloadFileAsync(client, 代理下载链接, zipFilePath);
}
catch (Exception ex)
{
Log($"[尝试1/4] 失败: {ex.Message}", true);
failureReason = ex.Message;
}
// 如果第一次尝试失败,且代理链接与原生链接不同,使用原生下载链接尝试
if (!downloadSuccess && 代理下载链接 != 原生下载链接)
{
try
{
Log($"[尝试2/4] 使用原生下载链接:{原生下载链接}");
downloadSuccess = await DownloadFileAsync(client, 原生下载链接, zipFilePath);
}
catch (Exception ex)
{
Log($"[尝试2/4] 失败: {ex.Message}", true);
failureReason = ex.Message;
}
}
// 如果前面的尝试都失败,创建使用系统代理的HttpClient再次尝试
if (!downloadSuccess)
{
try
{
Log("[尝试3/4] 使用系统代理 + 代理下载链接");
using (HttpClient proxyClient = new HttpClient())
{
proxyClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win32; x86) AppleWebKit/537.36 (KHTML, like Gecko) cmliu/SubsCheck-Win-GUI");
proxyClient.Timeout = TimeSpan.FromSeconds(30);
downloadSuccess = await DownloadFileAsync(proxyClient, 代理下载链接, zipFilePath);
}
}
catch (Exception ex)
{
Log($"[尝试3/4] 失败: {ex.Message}", true);
failureReason = ex.Message;
}
// 最后一次尝试:使用系统代理 + 原生链接(如果不同)
if (!downloadSuccess && 代理下载链接 != 原生下载链接)
{
try
{
Log("[尝试4/4] 使用系统代理 + 原生下载链接");
using (HttpClient proxyClient = new HttpClient())
{
proxyClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win32; x86) AppleWebKit/537.36 (KHTML, like Gecko) cmliu/SubsCheck-Win-GUI");
proxyClient.Timeout = TimeSpan.FromSeconds(30);
downloadSuccess = await DownloadFileAsync(proxyClient, 原生下载链接, zipFilePath);
}
}
catch (Exception ex)
{
Log($"[尝试4/4] 失败: {ex.Message}", true);
failureReason = ex.Message;
}
}
}
if (downloadSuccess)
{
Log("下载完成,正在解压文件...");
// 解压文件的代码保持不变
using (System.IO.Compression.ZipArchive archive = System.IO.Compression.ZipFile.OpenRead(zipFilePath))
{
// 查找subs-check.exe
System.IO.Compression.ZipArchiveEntry exeEntry = archive.Entries.FirstOrDefault(
entry => entry.Name.Equals("subs-check.exe", StringComparison.OrdinalIgnoreCase));
if (exeEntry != null)
{
string exeFilePath = Path.Combine(executablePath, "subs-check.exe");
// 如果文件已存在,先删除