From 141db8ad63d434d0e5a018ead34da511396ef67f Mon Sep 17 00:00:00 2001 From: Amazing_DM Date: Sat, 25 Apr 2020 18:50:41 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=E7=9B=91=E5=90=AC=E7=94=B5=E6=BA=90?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=EF=BC=8C=E8=87=AA=E5=8A=A8=E9=87=8D=E5=90=AF?= =?UTF-8?q?Netch=E6=9C=8D=E5=8A=A1=20:art:=E4=BC=98=E5=8C=96=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Netch/Controllers/HTTPController.cs | 11 ++++++-- Netch/Forms/MainForm.cs | 39 +++++++++++++++++++++++++++-- Netch/Netch.cs | 3 ++- Netch/Resources/zh-CN | 1 + 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Netch/Controllers/HTTPController.cs b/Netch/Controllers/HTTPController.cs index 8404312d..08850908 100644 --- a/Netch/Controllers/HTTPController.cs +++ b/Netch/Controllers/HTTPController.cs @@ -1,4 +1,7 @@ -using System; +using Netch.Utils; +using System; +using System.Windows; +using System.Windows.Forms; namespace Netch.Controllers { @@ -52,7 +55,11 @@ namespace Netch.Controllers } catch (Exception e) { - Utils.Logging.Info(e.ToString()); + if (System.Windows.Forms.MessageBox.Show(i18N.Translate("Failed to set the system proxy, it may be caused by the lack of dependent programs. Do you want to jump to Netch's official website to download dependent programs?"), i18N.Translate("Information"), MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) + { + System.Diagnostics.Process.Start("https://netch.org/#/?id=%e4%be%9d%e8%b5%96"); + } + Utils.Logging.Info("设置系统代理失败"+e.ToString()); return false; } diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 0d33fe2e..f9e4877e 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1,3 +1,4 @@ +using Microsoft.Win32; using Netch.Controllers; using Netch.Utils; using System; @@ -50,13 +51,35 @@ namespace Netch.Forms public MainForm() { InitializeComponent(); + + // 监听电源事件 + SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged); + VersionLabel.Text = UpdateChecker.Version; CheckForIllegalCrossThreadCalls = false; // MenuStrip.Renderer = new Override.ToolStripProfessionalRender(); Instance = this; } - + /// + /// 监听电源事件,自动重启Netch服务 + /// + /// + /// + void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) + { + switch (e.Mode) + { + case PowerModes.Suspend://操作系统即将挂起 + Logging.Info("操作系统即将挂起,自动停止===>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); + ControlFun(); + break; + case PowerModes.Resume://操作系统即将从挂起状态继续 + Logging.Info("操作系统即将从挂起状态继续,自动重启===>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); + ControlFun(); + break; + } + } private void CheckUpdate() { var updater = new UpdateChecker(); @@ -779,6 +802,12 @@ namespace Netch.Forms private void ControlButton_Click(object sender, EventArgs e) { + ControlFun(); + } + public void ControlFun() + { + //聚焦到启动按钮,防止模式选择框变成蓝色:D + ControlButton.Focus(); SaveConfigs(); if (State == Models.State.Waiting || State == Models.State.Stopped) { @@ -807,6 +836,8 @@ namespace Netch.Forms updateACLWithProxyToolStripMenuItem.Enabled = false; UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false; reinstallTapDriverToolStripMenuItem.Enabled = false; + ServerComboBox.Enabled = false; + ModeComboBox.Enabled = false; ControlButton.Text = "..."; StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting")}"; @@ -923,6 +954,8 @@ namespace Netch.Forms updateACLWithProxyToolStripMenuItem.Enabled = true; UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; reinstallTapDriverToolStripMenuItem.Enabled = true; + ServerComboBox.Enabled = true; + ModeComboBox.Enabled = true; ControlButton.Text = Utils.i18N.Translate("Start"); StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Start failed")}"; @@ -965,6 +998,8 @@ namespace Netch.Forms updateACLWithProxyToolStripMenuItem.Enabled = true; UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; reinstallTapDriverToolStripMenuItem.Enabled = true; + ServerComboBox.Enabled = true; + ModeComboBox.Enabled = true; ControlButton.Text = Utils.i18N.Translate("Start"); StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Stopped")}"; @@ -973,8 +1008,8 @@ namespace Netch.Forms TestServer(); }); } - } + } private void ShowMainFormToolStripButton_Click(object sender, EventArgs e) { if (WindowState == FormWindowState.Minimized) diff --git a/Netch/Netch.cs b/Netch/Netch.cs index 57c56820..4fe5ea72 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Win32; +using System; using System.Globalization; using System.IO; using System.Text; diff --git a/Netch/Resources/zh-CN b/Netch/Resources/zh-CN index 39d5a2a6..df59fdb4 100644 --- a/Netch/Resources/zh-CN +++ b/Netch/Resources/zh-CN @@ -164,6 +164,7 @@ "STUN_ServerPort value illegal. Try again.": "STUN 端口数值非法。请重试。", "Detection interval value illegal. Try again.": "检测间隔值非法。请重试。", "TUN/TAP driver is not detected. Is it installed now?": "未检测到 TUN/TAP 驱动,是否现在安装?", + "Failed to set the system proxy, it may be caused by the lack of dependent programs. Do you want to jump to Netch's official website to download dependent programs?": "设置系统代理失败,可能是缺少依赖导致,是否跳转Netch官网下载依赖程序?", "Experimental function": "实验性功能", "Delay test after startup": "启动后延迟测试", "Enable": "启用",