Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·179 lines (164 loc) · 6.03 KB
/
Copy pathProgram.cs
File metadata and controls
executable file
·179 lines (164 loc) · 6.03 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
using Shadowsocks.Controller;
using Shadowsocks.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
using Shadowsocks.Model;
#if !_CONSOLE
using Shadowsocks.View;
#endif
namespace Shadowsocks
{
static class Program
{
static ShadowsocksController _controller;
#if !_CONSOLE
static MenuViewController _viewController;
#endif
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
#if !_CONSOLE
foreach (string arg in args)
{
if (arg == "--setautorun")
{
if (!Controller.AutoStartup.Switch())
{
Environment.ExitCode = 1;
}
return;
}
}
using (Mutex mutex = new Mutex(false, "Global\\ShadowsocksR_" + Application.StartupPath.GetHashCode()))
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.EnableVisualStyles();
Application.ApplicationExit += Application_ApplicationExit;
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
Application.SetCompatibleTextRenderingDefault(false);
if (!mutex.WaitOne(0, false))
{
MessageBox.Show(I18N.GetString("Find Shadowsocks icon in your notify tray.") + "\n" +
I18N.GetString("If you want to start multiple Shadowsocks, make a copy in another directory."),
I18N.GetString("ShadowsocksR is already running."));
return;
}
#endif
Directory.SetCurrentDirectory(Application.StartupPath);
#if !_CONSOLE
int try_times = 0;
while (Configuration.Load() == null)
{
if (try_times >= 5)
return;
using (InputPassword dlg = new InputPassword())
{
if (dlg.ShowDialog() == DialogResult.OK)
Configuration.SetPassword(dlg.password);
else
return;
}
try_times += 1;
}
if (try_times > 0)
Logging.save_to_file = false;
#endif
_controller = new ShadowsocksController();
HostMap.Instance().LoadHostFile();
// Logging
Configuration cfg = _controller.GetConfiguration();
Logging.save_to_file = cfg.logEnable;
//#if !DEBUG
Logging.OpenLogFile();
//#endif
#if _DOTNET_4_0
// Enable Modern TLS when .NET 4.5+ installed.
if (Util.EnvCheck.CheckDotNet45())
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
#endif
#if !_CONSOLE
_viewController = new MenuViewController(_controller);
SystemEvents.SessionEnding += _viewController.Quit_Click;
#endif
_controller.Start();
#if !_CONSOLE
//Util.Utils.ReleaseMemory();
Application.Run();
}
#else
Console.ReadLine();
_controller.Stop();
#endif
}
private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Resume:
if (_controller != null)
{
System.Timers.Timer timer = new System.Timers.Timer(5 * 1000);
timer.Elapsed += Timer_Elapsed;
timer.AutoReset = false;
timer.Enabled = true;
timer.Start();
}
break;
case PowerModes.Suspend:
if (_controller != null) _controller.Stop();
break;
}
}
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
if (_controller != null) _controller.Start();
}
catch (Exception ex)
{
Logging.LogUsefulException(ex);
}
finally
{
try
{
System.Timers.Timer timer = (System.Timers.Timer)sender;
timer.Enabled = false;
timer.Stop();
timer.Dispose();
}
catch (Exception ex)
{
Logging.LogUsefulException(ex);
}
}
}
private static void Application_ApplicationExit(object sender, EventArgs e)
{
if (_controller != null) _controller.Stop();
_controller = null;
}
private static int exited = 0;
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (Interlocked.Increment(ref exited) == 1)
{
Logging.Log(LogLevel.Error, e.ExceptionObject != null ? e.ExceptionObject.ToString() : "");
MessageBox.Show(I18N.GetString("Unexpected error, ShadowsocksR will exit.") +
Environment.NewLine + (e.ExceptionObject != null ? e.ExceptionObject.ToString() : ""),
"Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
}
}
}