diff --git a/Netch/Controllers/Interface/Controller.cs b/Netch/Controllers/Interface/Controller.cs index ff1f885161..53302a7f36 100644 --- a/Netch/Controllers/Interface/Controller.cs +++ b/Netch/Controllers/Interface/Controller.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Text; @@ -67,10 +68,14 @@ protected void StopInstance() Instance.Kill(); Instance.WaitForExit(); } - catch (Exception e) + catch (Win32Exception e) { Logging.Error($"停止 {MainFile} 错误:\n" + e); } + catch + { + // ignored + } } diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index d4c58a54b7..34a3007461 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -146,9 +146,14 @@ public bool Start(Server server, Mode mode) /// public void Stop() { - Task.Run(() => pEncryptedProxyController?.Stop()); - Task.Run(() => UsingPorts.Clear()); - pModeController?.Stop(); + var tasks = new[] + { + Task.Factory.StartNew(() => pEncryptedProxyController?.Stop()), + Task.Factory.StartNew(() => UsingPorts.Clear()), + Task.Factory.StartNew(() => pModeController?.Stop()), + Task.Factory.StartNew(() => pNTTController.Stop()) + }; + Task.WaitAll(tasks); } public static void KillProcessByName(string name) diff --git a/Netch/Controllers/Mode/HTTPController.cs b/Netch/Controllers/Mode/HTTPController.cs index 5b09d99cdd..4b36e702fa 100644 --- a/Netch/Controllers/Mode/HTTPController.cs +++ b/Netch/Controllers/Mode/HTTPController.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Win32; using Netch.Models; @@ -30,7 +31,8 @@ public HTTPController() /// 是否启动成功 public override bool Start(Server server, Mode mode) { - RecordPrevious(); + Task.Run(RecordPrevious); + try { if (server.Type == "Socks5") @@ -79,21 +81,20 @@ private void RecordPrevious() /// public override void Stop() { - try + var tasks = new[] { - pPrivoxyController.Stop(); - - NativeMethods.SetGlobal(prevHTTP, prevBypass); - if (prevPAC != "") - NativeMethods.SetURL(prevPAC); - if (!prevEnabled) - NativeMethods.SetDIRECT(); - prevEnabled = false; - } - catch (Exception e) - { - Logging.Error("停止HTTP控制器错误:\n" + e); - } + Task.Factory.StartNew(pPrivoxyController.Stop), + Task.Factory.StartNew(() => + { + NativeMethods.SetGlobal(prevHTTP, prevBypass); + if (prevPAC != "") + NativeMethods.SetURL(prevPAC); + if (!prevEnabled) + NativeMethods.SetDIRECT(); + prevEnabled = false; + }) + }; + Task.WaitAll(tasks); } } } \ No newline at end of file diff --git a/Netch/Controllers/Mode/TUNTAPController.cs b/Netch/Controllers/Mode/TUNTAPController.cs index b1c3f49581..7c570463dc 100644 --- a/Netch/Controllers/Mode/TUNTAPController.cs +++ b/Netch/Controllers/Mode/TUNTAPController.cs @@ -7,6 +7,7 @@ using System.Net.Sockets; using System.Text; using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; using Netch.Models; using Netch.Properties; @@ -366,10 +367,11 @@ public override bool Start(Server server, Mode mode) { Thread.Sleep(10); - if (State == State.Started) return true; - - if (State == State.Stopped) + switch (State) { + case State.Started: + return true; + case State.Stopped: Stop(); return false; } @@ -383,9 +385,13 @@ public override bool Start(Server server, Mode mode) /// public override void Stop() { - StopInstance(); - ClearBypass(); - pDNSController.Stop(); + var tasks = new[] + { + Task.Factory.StartNew(StopInstance), + Task.Factory.StartNew(ClearBypass), + Task.Factory.StartNew(pDNSController.Stop) + }; + Task.WaitAll(tasks); } /// diff --git a/Netch/Controllers/NTTController.cs b/Netch/Controllers/NTTController.cs index c2568af254..6fd24209c8 100644 --- a/Netch/Controllers/NTTController.cs +++ b/Netch/Controllers/NTTController.cs @@ -43,9 +43,9 @@ public NTTController() return (true, natType, localEnd, publicEnd); } - catch (Exception) + catch (Exception e) { - Logging.Error("NTT 进程出错"); + Logging.Error("NTT 进程出错\n" + e); Stop(); return (false, null, null, null); } @@ -57,11 +57,9 @@ public NTTController() _lastResult = e.Data; } - /// - /// 无用 - /// public override void Stop() { + StopInstance(); } } } \ No newline at end of file diff --git a/Netch/Forms/MainForm.Control.cs b/Netch/Forms/MainForm.Control.cs index f17f8a0f08..71c5f78d72 100644 --- a/Netch/Forms/MainForm.Control.cs +++ b/Netch/Forms/MainForm.Control.cs @@ -19,9 +19,6 @@ partial class MainForm private void ControlFun() { - //防止模式选择框变成蓝色:D - ModeComboBox.Select(0, 0); - if (State == State.Waiting || State == State.Stopped) { // 服务器、模式 需选择 @@ -37,7 +34,10 @@ private void ControlFun() return; } - UpdateStatus(State.Starting); + State = State.Starting; + + // 清除模式搜索框文本选择 + ModeComboBox.Select(0, 0); Task.Run(() => { @@ -50,15 +50,14 @@ private void ControlFun() { Task.Run(() => { - UpdateStatus(State.Started, - i18N.Translate(StateExtension.GetStatusString(State.Started)) + LocalPortText(server.Type, mode.Type)); + State = State.Started; + StatusTextAppend(LocalPortText(server.Type, mode.Type)); Bandwidth.NetTraffic(server, mode, _mainController); }); // 如果勾选启动后最小化 if (Global.Settings.MinimizeWhenStarted) { WindowState = FormWindowState.Minimized; - NotifyIcon.Visible = true; if (_isFirstCloseWindow) { @@ -100,16 +99,20 @@ private void ControlFun() } else { - UpdateStatus(State.Stopped, i18N.Translate("Start failed")); + State = State.Stopped; + StatusText(i18N.Translate("Start failed")); } }); } else { - // 停止 - UpdateStatus(State.Stopping); - _mainController.Stop(); - UpdateStatus(State.Stopped); + Task.Run(() => + { + // 停止 + State = State.Stopping; + _mainController.Stop(); + State = State.Stopped; + }); Task.Run(TestServer); } } diff --git a/Netch/Forms/MainForm.MenuStrip.cs b/Netch/Forms/MainForm.MenuStrip.cs index f7a11e0280..bb404e661d 100644 --- a/Netch/Forms/MainForm.MenuStrip.cs +++ b/Netch/Forms/MainForm.MenuStrip.cs @@ -210,7 +210,7 @@ private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sende StatusText(i18N.Translate("Subscription updated")); MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true; - UpdateStatus(bak_State); + State = bak_State; StatusLabel.Text = bak_StateText; }).ContinueWith(task => { BeginInvoke(new Action(() => { UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; })); }); @@ -283,7 +283,7 @@ private void CleanDNSCacheToolStripMenuItem_Click(object sender, EventArgs e) } finally { - UpdateStatus(bak_State); + State = bak_State; StatusLabel.Text = bak_StateText; } } @@ -315,7 +315,7 @@ private void reinstallTapDriverToolStripMenuItem_Click(object sender, EventArgs } finally { - UpdateStatus(State.Waiting); + State = State.Waiting; Enabled = true; } }); @@ -365,7 +365,7 @@ private void updateACLWithProxyToolStripMenuItem_Click(object sender, EventArgs } finally { - UpdateStatus(State.Waiting); + State = State.Waiting; _mainController.Stop(); } }); @@ -422,7 +422,7 @@ private void updateACLToolStripMenuItem_Click(object sender, EventArgs e) } finally { - UpdateStatus(bak_State); + State = bak_State; StatusLabel.Text = bak_StateText; } }); diff --git a/Netch/Forms/MainForm.Status.cs b/Netch/Forms/MainForm.Status.cs index 771ae447e6..ae365c9730 100644 --- a/Netch/Forms/MainForm.Status.cs +++ b/Netch/Forms/MainForm.Status.cs @@ -1,5 +1,6 @@ using System; using System.Drawing; +using System.Threading; using Netch.Models; using Netch.Utils; @@ -11,10 +12,74 @@ public partial class Dummy partial class MainForm { + private State _state = State.Waiting; + /// /// 当前状态 /// - public State State { get; private set; } = State.Waiting; + public State State + { + get => _state; + private set + { + _state = value; + if (IsDisposed) + return; + StatusText(i18N.Translate(StateExtension.GetStatusString(value))); + switch (value) + { + case State.Waiting: + ControlButton.Enabled = true; + ControlButton.Text = i18N.Translate("Start"); + + break; + case State.Starting: + ControlButton.Enabled = false; + ControlButton.Text = "..."; + + ConfigurationGroupBox.Enabled = false; + + MenuStripsEnabled(false); + break; + case State.Started: + ControlButton.Enabled = true; + ControlButton.Text = i18N.Translate("Stop"); + + LastUploadBandwidth = 0; + //LastDownloadBandwidth = 0; + //UploadSpeedLabel.Text = "↑: 0 KB/s"; + DownloadSpeedLabel.Text = @"↑↓: 0 KB/s"; + UsedBandwidthLabel.Text = $@"{i18N.Translate("Used", ": ")}0 KB"; + UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = true; + break; + case State.Stopping: + ControlButton.Enabled = false; + ControlButton.Text = "..."; + + ProfileGroupBox.Enabled = false; + + UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = false; + NatTypeStatusText(); + break; + case State.Stopped: + ControlButton.Enabled = true; + ControlButton.Text = i18N.Translate("Start"); + + LastUploadBandwidth = 0; + LastDownloadBandwidth = 0; + + ProfileGroupBox.Enabled = true; + ConfigurationGroupBox.Enabled = true; + + MenuStripsEnabled(true); + break; + case State.Terminating: + Dispose(); + Environment.Exit(Environment.ExitCode); + return; + } + } + } public void NatTypeStatusText(string text = "", string country = "") { @@ -89,85 +154,18 @@ public void StatusText(string text) StatusLabel.Text = i18N.Translate("Status", ": ") + text; } - /// - /// 更新 UI, 状态栏文本, 状态 - /// - /// - /// - private void UpdateStatus(State state, string text = "") + public void StatusTextAppend(string text) { - State = state; - StatusText(text == "" ? i18N.Translate(StateExtension.GetStatusString(state)) : text); - - void MenuStripsEnabled(bool enabled) - { - // 需要禁用的菜单项 - UninstallServiceToolStripMenuItem.Enabled = - updateACLWithProxyToolStripMenuItem.Enabled = - UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = - reinstallTapDriverToolStripMenuItem.Enabled = enabled; - } - - // TODO 补充 - switch (state) - { - case State.Waiting: - ControlButton.Enabled = true; - ControlButton.Text = i18N.Translate("Start"); - - break; - case State.Starting: - ControlButton.Enabled = false; - ControlButton.Text = "..."; - - ConfigurationGroupBox.Enabled = false; - - MenuStripsEnabled(false); - break; - case State.Started: - ControlButton.Enabled = true; - ControlButton.Text = i18N.Translate("Stop"); - - LastUploadBandwidth = 0; - //LastDownloadBandwidth = 0; - //UploadSpeedLabel.Text = "↑: 0 KB/s"; - DownloadSpeedLabel.Text = @"↑↓: 0 KB/s"; - UsedBandwidthLabel.Text = $@"{i18N.Translate("Used", ": ")}0 KB"; - UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = true; - break; - case State.Stopping: - ControlButton.Enabled = false; - ControlButton.Text = "..."; - - ProfileGroupBox.Enabled = false; - - UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = false; - NatTypeStatusText(); - break; - case State.Stopped: - ControlButton.Enabled = true; - ControlButton.Text = i18N.Translate("Start"); - - LastUploadBandwidth = 0; - LastDownloadBandwidth = 0; - - ProfileGroupBox.Enabled = true; - ConfigurationGroupBox.Enabled = true; - - MenuStripsEnabled(true); - break; - case State.Terminating: - - break; - } + StatusLabel.Text += text; } - /// - /// 刷新 UI - /// - private void UpdateStatus() + public void MenuStripsEnabled(bool enabled) { - UpdateStatus(State); + // 需要禁用的菜单项 + UninstallServiceToolStripMenuItem.Enabled = + updateACLWithProxyToolStripMenuItem.Enabled = + UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = + reinstallTapDriverToolStripMenuItem.Enabled = enabled; } } } \ No newline at end of file diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 4c68a86877..3bc63abb5a 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -118,7 +118,6 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { // 使关闭时窗口向右下角缩小的效果 WindowState = FormWindowState.Minimized; - NotifyIcon.Visible = true; if (_isFirstCloseWindow) { @@ -211,39 +210,31 @@ private void InitText() // 加载翻译 UsedBandwidthLabel.Text = $@"{i18N.Translate("Used", ": ")}0 KB"; - UpdateStatus(); + State = State; VersionLabel.Text = UpdateChecker.Version; } private void Exit(bool forceExit = false) { - if (IsDisposed) return; - - // 已启动 - if (State != State.Waiting && State != State.Stopped) + if (State != State.Waiting && State != State.Stopped && !Global.Settings.StopWhenExited && !forceExit) { - if (Global.Settings.StopWhenExited || forceExit) - { - UpdateStatus(State.Stopping); - ControlFun(); - } - else - { - // 未开启自动停止 - MessageBoxX.Show(i18N.Translate("Please press Stop button first")); - - Visible = true; - ShowInTaskbar = true; // 显示在系统任务栏 - WindowState = FormWindowState.Normal; // 还原窗体 - NotifyIcon.Visible = true; // 托盘图标隐藏 + MessageBoxX.Show(i18N.Translate("Please press Stop button first")); - return; - } + Visible = true; + ShowInTaskbar = true; // 显示在系统任务栏 + WindowState = FormWindowState.Normal; // 还原窗体 + NotifyIcon.Visible = true; // 托盘图标隐藏 + return; } - NotifyIcon.Visible = false; Hide(); + NotifyIcon.Visible = false; + if (State != State.Waiting && State != State.Stopped) + { + // 已启动 + ControlFun(); + } Task.Run(() => { @@ -255,9 +246,7 @@ private void Exit(bool forceExit = false) } SaveConfigs(); - UpdateStatus(State.Terminating); - Dispose(); - Environment.Exit(Environment.ExitCode); + State = State.Terminating; }); } @@ -439,7 +428,6 @@ private void ShowMainFormToolStripButton_Click(object sender, EventArgs e) Visible = true; ShowInTaskbar = true; // 显示在系统任务栏 WindowState = FormWindowState.Normal; // 还原窗体 - NotifyIcon.Visible = true; // 托盘图标隐藏 } Activate(); diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 1525e7aeab..e5c20d79e0 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -376,6 +376,11 @@ private bool CheckPortText(string portName, ref TextBox portTextBox, ref int ori throw new FormatException(); } + if (port == originPort) + { + return true; + } + if (PortHelper.PortInUse(port, portType)) { MessageBoxX.Show(i18N.TranslateFormat("The {0} port is in use.", portName)); diff --git a/Netch/Resources/zh-CN b/Netch/Resources/zh-CN index b31bde0c0a..b7e8381596 100644 --- a/Netch/Resources/zh-CN +++ b/Netch/Resources/zh-CN @@ -75,7 +75,7 @@ "Options": "选项", "Uninstall NF Service": "卸载 NF 服务", - "Uninstalling NF Service": "正在 NF 卸载服务中", + "Uninstalling NF Service": "正在卸载 NF 服务中", "Service has been uninstalled": "服务已卸载", "Reload Modes": "重载模式", "Modes have been reload": "模式已重载",