Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (80 loc) · 3.37 KB
/
Copy pathProgram.cs
File metadata and controls
93 lines (80 loc) · 3.37 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
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CfdTcpAccess
{
internal static class Program
{
private static readonly string CrashLog = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"CfdTcpAccess", "crash.log");
[STAThread]
private static int Main(string[] args)
{
InstallCrashLogging();
// 自检模式(不弹界面):
// --selftest 参数拼装 / 端口规划 / 配置读写 / 域名校验
// --selftest-live 额外真实拉起一次 cloudflared,验证日志解析、状态机、本机 TCP 监听
// --uicheck 屏幕外创建主窗口并导出控件尺寸,检查布局是否被压扁或横向溢出
if (args.Any(a => a.Equals("--selftest", StringComparison.OrdinalIgnoreCase) ||
a.Equals("--selftest-live", StringComparison.OrdinalIgnoreCase)))
{
var live = args.Any(a => a.Equals("--selftest-live", StringComparison.OrdinalIgnoreCase));
return SelfTest.Run(live);
}
ApplicationConfiguration.Initialize();
if (args.Any(a => a.Equals("--uicheck", StringComparison.OrdinalIgnoreCase)))
{
return SelfTest.RunUiCheck();
}
Application.Run(new MainForm());
return 0;
}
private static void InstallCrashLogging()
{
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
WriteCrash("AppDomain.UnhandledException", e.ExceptionObject as Exception);
Application.ThreadException += (_, e) => WriteCrash("WinForms ThreadException", e.Exception);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
}
private static void WriteCrash(string source, Exception? ex)
{
var text = new StringBuilder()
.AppendLine("==================== 崩溃 ====================")
.AppendLine($"时间 : {DateTime.Now:yyyy-MM-dd HH:mm:ss}")
.AppendLine($"来源 : {source}")
.AppendLine($"命令行 : {Environment.CommandLine}")
.AppendLine($"异常 : {ex?.GetType().FullName ?? "(非 Exception 对象)"}")
.AppendLine($"消息 : {ex?.Message}")
.AppendLine(ex?.StackTrace)
.ToString();
try
{
var dir = Path.GetDirectoryName(CrashLog);
if (!string.IsNullOrEmpty(dir))
{
Directory.CreateDirectory(dir);
}
File.AppendAllText(CrashLog, text, new UTF8Encoding(false));
}
catch
{
// 连崩溃日志都写不出去时只能放弃。
}
try
{
MessageBox.Show(
"程序发生未处理的异常,详情已记录到:\n" + CrashLog + "\n\n" + ex,
"Cloudflare Tunnel TCP Access 助手",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
catch
{
// 弹不出提示框时忽略。
}
}
}
}