From dcb90ccdcdc004acdc4bfbd228d92546c65d9a4e Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Thu, 25 Mar 2021 12:21:42 +0800 Subject: [PATCH] Refactor: split Global.cs --- Netch/Constants.cs | 10 +++++++ Netch/Controllers/TUNTAPController.cs | 2 +- Netch/Flags.cs | 14 +++++++++ Netch/Forms/MainForm.cs | 8 ++--- Netch/Forms/SettingForm.cs | 2 +- Netch/Global.cs | 30 ++----------------- Netch/Models/LogStopwatch.cs | 27 ----------------- Netch/Models/Mode.cs | 2 +- Netch/Netch.cs | 20 +++++-------- Netch/Servers/Shadowsocks/SSController.cs | 2 +- Netch/Servers/ShadowsocksR/SSRController.cs | 2 +- Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs | 2 +- Netch/Updater/Updater.cs | 2 +- Netch/Utils/Bandwidth.cs | 2 +- Netch/Utils/Logging.cs | 2 +- 15 files changed, 45 insertions(+), 82 deletions(-) create mode 100644 Netch/Constants.cs create mode 100644 Netch/Flags.cs delete mode 100644 Netch/Models/LogStopwatch.cs diff --git a/Netch/Constants.cs b/Netch/Constants.cs new file mode 100644 index 00000000..4e4e8754 --- /dev/null +++ b/Netch/Constants.cs @@ -0,0 +1,10 @@ +namespace Netch +{ + public static class Constants + { + public const string EOF = "\r\n"; + public const string UserACL = "data\\user.acl"; + public const string BuiltinACL = "bin\\default.acl"; + public const string ParameterShow = "-show"; + } +} \ No newline at end of file diff --git a/Netch/Controllers/TUNTAPController.cs b/Netch/Controllers/TUNTAPController.cs index 78f0c74f..43e723d1 100644 --- a/Netch/Controllers/TUNTAPController.cs +++ b/Netch/Controllers/TUNTAPController.cs @@ -74,7 +74,7 @@ namespace Netch.Controllers argument.Append( $"-tunAddr {Global.Settings.TUNTAP.Address} -tunMask {Global.Settings.TUNTAP.Netmask} -tunGw {Global.Settings.TUNTAP.Gateway} -tunDns {DnsUtils.Join(dns)} -tunName \"{TUNTAP.GetName(_tap.ComponentID)}\" "); - if (Global.Settings.TUNTAP.UseFakeDNS && Global.Flags.SupportFakeDns) + if (Global.Settings.TUNTAP.UseFakeDNS && Flags.SupportFakeDns) argument.Append("-fakeDns "); StartInstanceAuto(argument.ToString(), ProcessPriorityClass.RealTime); diff --git a/Netch/Flags.cs b/Netch/Flags.cs new file mode 100644 index 00000000..656d55b6 --- /dev/null +++ b/Netch/Flags.cs @@ -0,0 +1,14 @@ +using System; +using Netch.Controllers; + +namespace Netch +{ + public static class Flags + { + public static readonly bool IsWindows10Upper = Environment.OSVersion.Version.Major >= 10; + + private static readonly Lazy LazySupportFakeDns = new(() => new TUNTAPController().TestFakeDNS()); + + public static bool SupportFakeDns => LazySupportFakeDns.Value; + } +} \ No newline at end of file diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 13b7caa2..740445cd 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -84,8 +84,6 @@ namespace Netch.Forms private void MainForm_Load(object sender, EventArgs e) { - Global.LogStopwatch.Log("MainForm ctor (Pre MainForm Load)"); - // 计算 ComboBox绘制 目标宽度 RecordSize(); @@ -121,8 +119,6 @@ namespace Netch.Forms if (Global.Settings.StartWhenOpened) ControlButton_Click(null, null); }); - - Global.LogStopwatch.Log("Post Form Load", true); } private void RecordSize() @@ -448,7 +444,7 @@ namespace Netch.Forms if (useProxy) req.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}"); - await WebUtil.DownloadFileAsync(req, Path.Combine(Global.NetchDir, Global.UserACL)); + await WebUtil.DownloadFileAsync(req, Path.Combine(Global.NetchDir, Constants.UserACL)); NotifyTip(i18N.Translate("ACL updated successfully")); } catch (Exception e) @@ -1238,7 +1234,7 @@ namespace Netch.Forms { if (natType > 0 && natType < 5) { - NatTypeStatusLightLabel.Visible = Global.Flags.IsWindows10Upper; + NatTypeStatusLightLabel.Visible = Flags.IsWindows10Upper; var c = natType switch { 1 => Color.LimeGreen, diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 8ba79aff..20192e1d 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -235,7 +235,7 @@ namespace Netch.Forms private void SettingForm_Load(object sender, EventArgs e) { TUNTAPUseCustomDNSCheckBox_CheckedChanged(null, null); - Task.Run(() => BeginInvoke(new Action(() => UseFakeDNSCheckBox.Visible = Global.Flags.SupportFakeDns))); + Task.Run(() => BeginInvoke(new Action(() => UseFakeDNSCheckBox.Visible = Flags.SupportFakeDns))); } private void TUNTAPUseCustomDNSCheckBox_CheckedChanged(object? sender, EventArgs? e) diff --git a/Netch/Global.cs b/Netch/Global.cs index 5959f2eb..44913acc 100644 --- a/Netch/Global.cs +++ b/Netch/Global.cs @@ -2,9 +2,7 @@ using System; using System.Collections.Generic; using System.Text.Encodings.Web; using System.Text.Json; -using System.Threading; using System.Windows.Forms; -using Netch.Controllers; using Netch.Forms; using Netch.Models; @@ -12,27 +10,11 @@ namespace Netch { public static class Global { - /// - /// 换行 - /// - public const string EOF = "\r\n"; - - public const string UserACL = "data\\user.acl"; - public const string BuiltinACL = "bin\\default.acl"; - - public static readonly string NetchDir = Application.StartupPath; - - public static readonly string NetchExecutable = Application.ExecutablePath; - /// /// 主窗体的静态实例 /// private static readonly Lazy LazyMainForm = new(() => new MainForm()); - public static SingleInstance.SingleInstance SingleInstance = new($"Global\\{nameof(Netch)}"); - - public static LogStopwatch LogStopwatch = null!; - /// /// 用于读取和写入的配置 /// @@ -43,15 +25,6 @@ namespace Netch /// public static readonly List Modes = new(); - public static class Flags - { - public static readonly bool IsWindows10Upper = Environment.OSVersion.Version.Major >= 10; - - private static readonly Lazy LazySupportFakeDns = new(() => new TUNTAPController().TestFakeDNS()); - - public static bool SupportFakeDns => LazySupportFakeDns.Value; - } - /// /// 主窗体的静态实例 /// @@ -64,6 +37,7 @@ namespace Netch Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; - public const string ParameterShow = "-show"; + public static readonly string NetchDir = Application.StartupPath; + public static readonly string NetchExecutable = Application.ExecutablePath; } } \ No newline at end of file diff --git a/Netch/Models/LogStopwatch.cs b/Netch/Models/LogStopwatch.cs deleted file mode 100644 index 15ae0d33..00000000 --- a/Netch/Models/LogStopwatch.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Diagnostics; - -namespace Netch.Models -{ - public class LogStopwatch - { - public LogStopwatch(string message) - { - _stopwatch = Stopwatch.StartNew(); - Console.WriteLine($"Start {message} LogStopwatch"); - } - - private readonly Stopwatch _stopwatch; - - public void Log(string name,bool stop = false) - { - if (!_stopwatch.IsRunning) - throw new Exception(); - - _stopwatch.Stop(); - Console.WriteLine($"{name} LogStopwatch: {_stopwatch.ElapsedMilliseconds}"); - if(!stop) - _stopwatch.Start(); - } - } -} \ No newline at end of file diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs index 8c509e69..de27acf5 100644 --- a/Netch/Models/Mode.cs +++ b/Netch/Models/Mode.cs @@ -161,7 +161,7 @@ namespace Netch.Models /// 模式文件字符串 public string ToFileString() { - return $"# {Remark}, {Type}, {(BypassChina ? 1 : 0)}{Global.EOF}{string.Join(Global.EOF, Rule)}"; + return $"# {Remark}, {Type}, {(BypassChina ? 1 : 0)}{Constants.EOF}{string.Join(Constants.EOF, Rule)}"; } } diff --git a/Netch/Netch.cs b/Netch/Netch.cs index ff97f7d3..1779db31 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -7,13 +7,14 @@ using System.Threading.Tasks; using System.Windows.Forms; using Netch.Controllers; using Netch.Forms; -using Netch.Models; using Netch.Utils; namespace Netch { public static class Netch { + public static readonly SingleInstance.SingleInstance SingleInstance = new($"Global\\{nameof(Netch)}"); + /// /// 应用程序的主入口点 /// @@ -26,7 +27,7 @@ namespace Netch if (args.Contains("-console")) AttachConsole(); #endif - Global.LogStopwatch = new LogStopwatch("Netch"); + // 设置当前目录 Directory.SetCurrentDirectory(Global.NetchDir); @@ -42,21 +43,18 @@ namespace Netch if (!Directory.Exists(item)) Directory.CreateDirectory(item); - Global.LogStopwatch.Log("Clean Old, Create Directory"); // 加载配置 Configuration.Load(); - Global.LogStopwatch.Log("Load Configuration"); - - if (!Global.SingleInstance.IsFirstInstance) + if (!SingleInstance.IsFirstInstance) { - Global.SingleInstance.PassArgumentsToFirstInstance(args.Append(Global.ParameterShow)); + SingleInstance.PassArgumentsToFirstInstance(args.Append(Constants.ParameterShow)); Environment.Exit(0); return; } - Global.SingleInstance.ArgumentsReceived.Subscribe(SingleInstance_ArgumentsReceived); - Global.SingleInstance.ListenForArgumentsFromSuccessiveInstances(); + SingleInstance.ArgumentsReceived.Subscribe(SingleInstance_ArgumentsReceived); + SingleInstance.ListenForArgumentsFromSuccessiveInstances(); // 清理上一次的日志文件,防止淤积占用磁盘空间 if (Directory.Exists("logging")) @@ -82,8 +80,6 @@ namespace Netch Logging.Info($"版本: {UpdateChecker.Owner}/{UpdateChecker.Repo}@{UpdateChecker.Version}"); Task.Run(() => { Logging.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Global.NetchExecutable)}"); }); - Global.LogStopwatch.Log("Get Info, Pre-Form"); - // 绑定错误捕获 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += Application_OnException; @@ -107,7 +103,7 @@ namespace Netch private static void SingleInstance_ArgumentsReceived(IEnumerable args) { - if (args.Contains(Global.ParameterShow)) + if (args.Contains(Constants.ParameterShow)) { Global.MainForm.ShowMainFormToolStripButton_Click(null!, null!); } diff --git a/Netch/Servers/Shadowsocks/SSController.cs b/Netch/Servers/Shadowsocks/SSController.cs index 9ade86bf..493bb5ec 100644 --- a/Netch/Servers/Shadowsocks/SSController.cs +++ b/Netch/Servers/Shadowsocks/SSController.cs @@ -38,7 +38,7 @@ namespace Netch.Servers.Shadowsocks }; if (mode.BypassChina) - command.acl = $"{Path.GetFullPath(File.Exists(Global.UserACL) ? Global.UserACL : Global.BuiltinACL)}"; + command.acl = $"{Path.GetFullPath(File.Exists(Constants.UserACL) ? Constants.UserACL : Constants.BuiltinACL)}"; StartInstanceAuto(command.ToString()); } diff --git a/Netch/Servers/ShadowsocksR/SSRController.cs b/Netch/Servers/ShadowsocksR/SSRController.cs index 97772ffa..976f51c3 100644 --- a/Netch/Servers/ShadowsocksR/SSRController.cs +++ b/Netch/Servers/ShadowsocksR/SSRController.cs @@ -41,7 +41,7 @@ namespace Netch.Servers.ShadowsocksR }; if (mode.BypassChina) - command.acl = $"{Path.GetFullPath(File.Exists(Global.UserACL) ? Global.UserACL : Global.BuiltinACL)}"; + command.acl = $"{Path.GetFullPath(File.Exists(Constants.UserACL) ? Constants.UserACL : Constants.BuiltinACL)}"; StartInstanceAuto(command.ToString()); } diff --git a/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs b/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs index 015dde6a..5dfb7a7d 100644 --- a/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs +++ b/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs @@ -73,7 +73,7 @@ namespace Netch.Servers.V2ray.Utils break; case 1: case 2: - if (Global.Flags.SupportFakeDns && Global.Settings.TUNTAP.UseFakeDNS) + if (Flags.SupportFakeDns && Global.Settings.TUNTAP.UseFakeDNS) directRuleObject.domain.Add("geosite:cn"); else directRuleObject.ip.Add("geoip:cn"); diff --git a/Netch/Updater/Updater.cs b/Netch/Updater/Updater.cs index 48fe71f2..764abd92 100644 --- a/Netch/Updater/Updater.cs +++ b/Netch/Updater/Updater.cs @@ -120,7 +120,7 @@ namespace Netch.Updater // save, release mutex, then exit Configuration.Save(); - Global.MainForm.Invoke(new Action(() => { Global.SingleInstance.Dispose(); })); + Global.MainForm.Invoke(new Action(() => { Netch.SingleInstance.Dispose(); })); Process.Start(Global.NetchExecutable); Global.MainForm.Exit(true, false); } diff --git a/Netch/Utils/Bandwidth.cs b/Netch/Utils/Bandwidth.cs index 3c86c728..c5e61462 100644 --- a/Netch/Utils/Bandwidth.cs +++ b/Netch/Utils/Bandwidth.cs @@ -45,7 +45,7 @@ namespace Netch.Utils /// public static void NetTraffic() { - if (!Global.Flags.IsWindows10Upper) + if (!Flags.IsWindows10Upper) return; var counterLock = new object(); diff --git a/Netch/Utils/Logging.cs b/Netch/Utils/Logging.cs index 5e733af9..d62b77c3 100644 --- a/Netch/Utils/Logging.cs +++ b/Netch/Utils/Logging.cs @@ -39,7 +39,7 @@ namespace Netch.Utils private static void Write(string text, LogLevel logLevel) { - var contents = $@"[{DateTime.Now}][{logLevel.ToString()}] {text}{Global.EOF}"; + var contents = $@"[{DateTime.Now}][{logLevel.ToString()}] {text}{Constants.EOF}"; #if DEBUG switch (logLevel) {