-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathProgram.cs
184 lines (164 loc) · 6.39 KB
/
Program.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
using MsmhToolsClass;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text;
namespace SecureDNSClient;
internal static partial class Program
{
private static readonly ReaderWriterLockSlim Locker = new();
internal static bool IsPortable = false;
internal static bool IsStartup = false;
internal static int StartupDelaySec = 0; // Default
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
// Setting Culture Is Necessary To Read Args In Any Windows Display Language Other Than English
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
}
catch (Exception) { }
// Exit If It's Root Directory
if (FileDirectory.IsRootDirectory())
{
try
{
string isRoot = "Application cannot run on root directory.";
MessageBox.Show(isRoot, "Not Supported", MessageBoxButtons.OK, MessageBoxIcon.Information);
Environment.Exit(0);
Application.Exit();
return;
}
catch (Exception) { }
}
try
{
string[] args = Environment.GetCommandLineArgs();
bool oldCli = false;
if (args.Any())
{
// Support Old Cli
if (args.Length >= 2)
{
string su = args[1].Trim().ToLower();
if (su.Equals("startup"))
{
IsPortable = true;
IsStartup = true;
oldCli = true;
}
}
if (oldCli && args.Length >= 3)
{
string d = args[2].Trim().ToLower();
bool isInt = int.TryParse(d, out int value);
if (isInt) StartupDelaySec = value;
}
// New CLI
if (!oldCli)
{
for (int n = 0; n < args.Length; n++)
{
string arg = args[n].ToLower().Trim(); // e.g. -IsStartup=True
KeyValue kv = GetValue(arg);
if (string.IsNullOrEmpty(kv.Key)) continue;
if (kv.Key.Equals(Key.IsPortable, StringComparison.InvariantCultureIgnoreCase) && kv.Type == typeof(bool)) IsPortable = kv.ValueBool;
if (kv.Key.Equals(Key.IsStartup, StringComparison.InvariantCultureIgnoreCase) && kv.Type == typeof(bool)) IsStartup = kv.ValueBool;
if (kv.Key.Equals(Key.StartupDelaySec, StringComparison.InvariantCultureIgnoreCase) && kv.Type == typeof(int)) StartupDelaySec = kv.ValueInt;
}
}
}
}
catch (Exception) { }
#if DEBUG
Debug.WriteLine("=========");
Debug.WriteLine("Is Portable: " + IsPortable);
Debug.WriteLine("Is Startup: " + IsStartup);
Debug.WriteLine("Startup Delay: " + StartupDelaySec);
Debug.WriteLine("=========");
#endif
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
// Prevent multiple instances
string productName = Info.GetAppInfo(Assembly.GetExecutingAssembly()).ProductName ?? "SDC - Secure DNS Client";
Mutex mutex = new(false, productName);
if (!mutex.WaitOne(0, true))
{
MessageBox.Show($"{productName} is already running.");
Environment.Exit(0);
Application.Exit();
return;
}
GC.KeepAlive(mutex);
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
Application.ThreadException += Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.Run(new FormMain());
}
private static async void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
try
{
string err = e.Exception.GetInnerExceptions() + Environment.NewLine;
Debug.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-= UnobservedTaskException:" + Environment.NewLine + err);
try
{
Locker.EnterWriteLock();
await FileDirectory.AppendTextLineAsync(SecureDNS.ErrorLogPath, err, new UTF8Encoding(false));
}
catch (Exception) { }
finally
{
Locker.ExitWriteLock();
}
}
catch (Exception) { }
}
private static async void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
try
{
string err = e.Exception.GetInnerExceptions() + Environment.NewLine;
Debug.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-= ThreadException:" + Environment.NewLine + err);
try
{
Locker.EnterWriteLock();
await FileDirectory.AppendTextLineAsync(SecureDNS.ErrorLogPath, err, new UTF8Encoding(false));
}
catch (Exception) { }
finally
{
Locker.ExitWriteLock();
}
}
catch (Exception) { }
}
private static async void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
string? err = e.ExceptionObject.ToString() + Environment.NewLine;
if (string.IsNullOrEmpty(err)) return;
Debug.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-= UnhandledException:" + Environment.NewLine + err);
try
{
Locker.EnterWriteLock();
await FileDirectory.AppendTextLineAsync(SecureDNS.ErrorLogPath, err, new UTF8Encoding(false));
}
catch (Exception) { }
finally
{
Locker.ExitWriteLock();
}
}
catch (Exception) { }
}
}