diff --git a/Netch/Controllers/DNSController.cs b/Netch/Controllers/DNSController.cs index 38de1625..0c4ab3f0 100644 --- a/Netch/Controllers/DNSController.cs +++ b/Netch/Controllers/DNSController.cs @@ -19,7 +19,7 @@ namespace Netch.Controllers /// public bool Start() { - MainForm.Instance.StatusText($"{i18N.Translate("Starting dns Service")}"); + MainForm.Instance.StatusText(i18N.Translate("Starting dns Service")); try { if (!File.Exists("bin\\unbound.exe") && !File.Exists("bin\\unbound-service.conf") && !File.Exists("bin\\forward-zone.conf")) diff --git a/Netch/Controllers/HTTPController.cs b/Netch/Controllers/HTTPController.cs index 4a3e8e34..004644c5 100644 --- a/Netch/Controllers/HTTPController.cs +++ b/Netch/Controllers/HTTPController.cs @@ -25,6 +25,7 @@ namespace Netch.Controllers /// 是否启动成功 public bool Start(Server server, Mode mode) { + RecordPrevious(); try { if (server.Type == "Socks5") @@ -43,21 +44,7 @@ namespace Netch.Controllers if (mode.Type != 5) { - RecordPrevious(); - NativeMethods.SetGlobal($"127.0.0.1:{Global.Settings.HTTPLocalPort}", ""); - - // HTTP 系统代理模式,启动系统代理 - /* - using (var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true)) - { - registry.SetValue("ProxyEnable", 1); - registry.SetValue("ProxyServer", $"127.0.0.1:{Global.Settings.HTTPLocalPort}"); - - Win32Native.InternetSetOption(IntPtr.Zero, 39, IntPtr.Zero, 0); - Win32Native.InternetSetOption(IntPtr.Zero, 37, IntPtr.Zero, 0); - } - */ } } catch (Exception e) @@ -114,18 +101,6 @@ namespace Netch.Controllers if (!prevEnabled) NativeMethods.SetDIRECT(); prevEnabled = false; - - - /* - using (var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true)) - { - registry.SetValue("ProxyEnable", 0); - registry.DeleteValue("ProxyServer", false); - - Win32Native.InternetSetOption(IntPtr.Zero, 39, IntPtr.Zero, 0); - Win32Native.InternetSetOption(IntPtr.Zero, 37, IntPtr.Zero, 0); - } - */ } catch (Exception e) { diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index fb023f63..fcfb6303 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -141,7 +141,7 @@ namespace Netch.Controllers result = pNFController.Start(server, mode, false); if (!result) { - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Restarting Redirector")}"); + MainForm.Instance.StatusText(i18N.Translate("Restarting Redirector")); Logging.Info("正常启动失败后尝试停止驱动服务再重新启动"); //正常启动失败后尝试停止驱动服务再重新启动 result = pNFController.Start(server, mode, true); diff --git a/Netch/Controllers/NFController.cs b/Netch/Controllers/NFController.cs index 6b2e42d2..ad9071b6 100644 --- a/Netch/Controllers/NFController.cs +++ b/Netch/Controllers/NFController.cs @@ -48,7 +48,7 @@ namespace Netch.Controllers public bool Start(Server server, Mode mode, bool StopServiceAndRestart) { if (!StopServiceAndRestart) - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting Redirector")}"); + MainForm.Instance.StatusText(i18N.Translate("Starting Redirector")); if (!File.Exists("bin\\Redirector.exe")) { @@ -130,12 +130,12 @@ namespace Netch.Controllers // 防止其他程序占用 重置 NF 百万连接数限制 service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting netfilter2 Service")}"); + MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service")); service.Start(); } else if (service.Status == ServiceControllerStatus.Stopped) { - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting netfilter2 Service")}"); + MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service")); service.Start(); } } @@ -267,7 +267,7 @@ namespace Netch.Controllers Logging.Info(e.ToString()); return false; } - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Register driver")}"); + MainForm.Instance.StatusText(i18N.Translate("Register driver")); // 注册驱动文件 var result = NFAPI.nf_registerDriver("netfilter2"); if (result != NF_STATUS.NF_STATUS_SUCCESS) diff --git a/Netch/Controllers/NTTController.cs b/Netch/Controllers/NTTController.cs index 637c4c42..eb9b19c7 100644 --- a/Netch/Controllers/NTTController.cs +++ b/Netch/Controllers/NTTController.cs @@ -27,7 +27,7 @@ namespace Netch.Controllers public (bool, string, string, string) Start() { Thread.Sleep(1000); - MainForm.Instance.NatTypeStatusText($"{i18N.Translate("Starting NatTester")}"); + MainForm.Instance.NatTypeStatusText(i18N.Translate("Starting NatTester")); try { if (!File.Exists("bin\\NTT.exe")) diff --git a/Netch/Controllers/SSController.cs b/Netch/Controllers/SSController.cs index 674bfbae..b3bbb4c0 100644 --- a/Netch/Controllers/SSController.cs +++ b/Netch/Controllers/SSController.cs @@ -29,7 +29,7 @@ namespace Netch.Controllers /// 是否启动成功 public bool Start(Server server, Mode mode) { - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting Shadowsocks")}"); + MainForm.Instance.StatusText(i18N.Translate("Starting Shadowsocks")); File.Delete("logging\\shadowsocks.log"); //从DLL启动Shaowsocks diff --git a/Netch/Controllers/SSRController.cs b/Netch/Controllers/SSRController.cs index d2475b46..ba243802 100644 --- a/Netch/Controllers/SSRController.cs +++ b/Netch/Controllers/SSRController.cs @@ -28,7 +28,7 @@ namespace Netch.Controllers /// 是否启动成功 public bool Start(Server server, Mode mode) { - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting ShadowsocksR")}"); + MainForm.Instance.StatusText(i18N.Translate("Starting ShadowsocksR")); File.Delete("logging\\shadowsocksr.log"); diff --git a/Netch/Controllers/TUNTAPController.cs b/Netch/Controllers/TUNTAPController.cs index f9cead05..ce54400d 100644 --- a/Netch/Controllers/TUNTAPController.cs +++ b/Netch/Controllers/TUNTAPController.cs @@ -69,7 +69,7 @@ namespace Netch.Controllers /// public bool SetupBypass() { - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("SetupBypass")}"); + MainForm.Instance.StatusText(i18N.Translate("SetupBypass")); Logging.Info("设置绕行规则 → 设置让服务器 IP 走直连"); // 让服务器 IP 走直连 foreach (var address in ServerAddresses) @@ -375,7 +375,7 @@ namespace Netch.Controllers /// 是否成功 public bool Start(Server server, Mode mode) { - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting Tap")}"); + MainForm.Instance.StatusText(i18N.Translate("Starting Tap")); foreach (var proc in Process.GetProcessesByName("tun2socks")) { try diff --git a/Netch/Controllers/TrojanController.cs b/Netch/Controllers/TrojanController.cs index 96a191fb..c49420d0 100644 --- a/Netch/Controllers/TrojanController.cs +++ b/Netch/Controllers/TrojanController.cs @@ -30,7 +30,7 @@ namespace Netch.Controllers /// 是否启动成功 public bool Start(Server server, Mode mode) { - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting Trojan")}"); + MainForm.Instance.StatusText(i18N.Translate("Starting Trojan")); File.Delete("logging\\trojan.log"); if (!File.Exists("bin\\Trojan.exe")) diff --git a/Netch/Controllers/VMessController.cs b/Netch/Controllers/VMessController.cs index 68e555b6..3689aebf 100644 --- a/Netch/Controllers/VMessController.cs +++ b/Netch/Controllers/VMessController.cs @@ -31,7 +31,7 @@ namespace Netch.Controllers /// 是否启动成功 public bool Start(Server server, Mode mode) { - MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting V2ray")}"); + MainForm.Instance.StatusText(i18N.Translate("Starting V2ray")); if (!File.Exists("bin\\v2ray.exe") || !File.Exists("bin\\v2ctl.exe")) { return false; diff --git a/Netch/Forms/MainForm.Control.cs b/Netch/Forms/MainForm.Control.cs new file mode 100644 index 00000000..e70d477e --- /dev/null +++ b/Netch/Forms/MainForm.Control.cs @@ -0,0 +1,227 @@ +using System; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Forms; +using Netch.Controllers; +using Netch.Models; +using Netch.Utils; + +namespace Netch.Forms +{ + public partial class Dummy + { + } + + partial class MainForm + { + public void ControlFun() + { + SaveConfigs(); + if (State == State.Waiting || State == State.Stopped) + { + // 服务器、模式 需选择 + if (ServerComboBox.SelectedIndex == -1) + { + MessageBox.Show(i18N.Translate("Please select a server first"), i18N.Translate("Information"), + MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + if (ModeComboBox.SelectedIndex == -1) + { + MessageBox.Show(i18N.Translate("Please select an mode first"), i18N.Translate("Information"), + MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + //MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false; + + UpdateStatus(State.Starting); + + Task.Run(() => + { + var server = ServerComboBox.SelectedItem as Models.Server; + var mode = ModeComboBox.SelectedItem as Models.Mode; + + MainController = new MainController(); + + var startResult = MainController.Start(server, mode); + + if (startResult) + { + Task.Run(() => + { + LastUploadBandwidth = 0; + //LastDownloadBandwidth = 0; + //UploadSpeedLabel.Text = "↑: 0 KB/s"; + DownloadSpeedLabel.Text = "↑↓: 0 KB/s"; + UsedBandwidthLabel.Text = $"{i18N.Translate("Used")}{i18N.Translate(": ")}0 KB"; + UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = true; + + + UploadSpeedLabel.Visible = false; + Bandwidth.NetTraffic(server, mode, MainController); + }); + //MainController.pNFController.OnBandwidthUpdated += OnBandwidthUpdated; + + // 如果勾选启动后最小化 + if (Global.Settings.MinimizeWhenStarted) + { + WindowState = FormWindowState.Minimized; + NotifyIcon.Visible = true; + + if (IsFirstOpened) + { + // 显示提示语 + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, + i18N.Translate( + "Netch is now minimized to the notification bar, double click this icon to restore."), + ToolTipIcon.Info); + + IsFirstOpened = false; + } + + Hide(); + } + + // TODO 是否需要移到一个函数中 + var text = new StringBuilder(" ("); + text.Append(Global.Settings.LocalAddress == "0.0.0.0" + ? i18N.Translate("Allow other Devices to connect") + " " + : ""); + if (server.Type == "Socks5") + { + // 不可控Socks5 + if (mode.Type == 3 && mode.Type == 5) + { + // 可控HTTP + text.Append( + $"HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}"); + } + else + { + // 不可控HTTP + text.Clear(); + } + } + else + { + // 可控Socks5 + text.Append( + $"Socks5 {i18N.Translate("Local Port", ": ")}{Global.Settings.Socks5LocalPort}"); + if (mode.Type == 3 || mode.Type == 5) + { + //有HTTP + text.Append( + $" | HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}"); + } + } + if (text.Length > 0) + { + text.Append(")"); + } + UpdateStatus(State.Started); + StatusText(i18N.Translate(StateExtension.GetStatusString(State)) + text); + + if (Global.Settings.StartedTcping) + { + // 自动检测延迟 + Task.Run(() => + { + while (true) + { + if (State == State.Started) + { + server.Test(); + // 重载服务器列表 + InitServer(); + + Thread.Sleep(Global.Settings.StartedTcping_Interval * 1000); + } + else + { + break; + } + } + }); + } + } + else + { + UpdateStatus(State.Stopped); + StatusText(i18N.Translate("Start Failed")); + } + }); + } + else + { + // 停止 + UpdateStatus(State.Stopping); + + Task.Run(() => + { + var server = ServerComboBox.SelectedItem as Models.Server; + var mode = ModeComboBox.SelectedItem as Models.Mode; + + MainController.Stop(); + + UpdateStatus(State.Stopped); + + TestServer(); + }); + } + } + + public void OnBandwidthUpdated(long download) + { + try + { + UsedBandwidthLabel.Text = $"{i18N.Translate("Used", ": ")}{Bandwidth.Compute(download)}"; + //UploadSpeedLabel.Text = $"↑: {Utils.Bandwidth.Compute(upload - LastUploadBandwidth)}/s"; + DownloadSpeedLabel.Text = $"↑↓: {Bandwidth.Compute(download - LastDownloadBandwidth)}/s"; + + //LastUploadBandwidth = upload; + LastDownloadBandwidth = download; + Refresh(); + } + catch (Exception) + { + } + } + + public void OnBandwidthUpdated(long upload, long download) + { + try + { + if (upload < 1 || download < 1) + { + return; + } + + UsedBandwidthLabel.Text = + $"{i18N.Translate("Used")}{i18N.Translate(": ")}{Bandwidth.Compute(upload + download)}"; + UploadSpeedLabel.Text = $"↑: {Bandwidth.Compute(upload - LastUploadBandwidth)}/s"; + DownloadSpeedLabel.Text = $"↓: {Bandwidth.Compute(download - LastDownloadBandwidth)}/s"; + + LastUploadBandwidth = upload; + LastDownloadBandwidth = download; + Refresh(); + } + catch (Exception) + { + } + } + + /// + /// 上一次上传的流量 + /// + public long LastUploadBandwidth; + + /// + /// 上一次下载的流量 + /// + public long LastDownloadBandwidth; + } +} \ No newline at end of file diff --git a/Netch/Forms/MainForm.Designer.cs b/Netch/Forms/MainForm.Designer.cs index d572df09..eda41b50 100644 --- a/Netch/Forms/MainForm.Designer.cs +++ b/Netch/Forms/MainForm.Designer.cs @@ -42,18 +42,17 @@ namespace Netch.Forms this.AddTrojanServerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.ModeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.CreateProcessModeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ReloadModesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.SubscribeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.ManageSubscribeLinksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.UpdateServersFromSubscribeLinksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.OptionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.ReloadModesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.RestartServiceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.UninstallServiceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.OpenDirectoryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.CleanDNSCacheToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.UpdateACLToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.updateACLWithProxyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.UninstallServiceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.reinstallTapDriverToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.OpenDirectoryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.AboutToolStripButton = new System.Windows.Forms.ToolStripButton(); this.VersionLabel = new System.Windows.Forms.ToolStripLabel(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -67,9 +66,9 @@ namespace Netch.Forms this.ModeComboBox = new System.Windows.Forms.SearchComboBox(); this.ServerComboBox = new System.Windows.Forms.ComboBox(); this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); - this.EditPictureBox = new System.Windows.Forms.PictureBox(); + this.EditServerPictureBox = new System.Windows.Forms.PictureBox(); this.CopyLinkPictureBox = new System.Windows.Forms.PictureBox(); - this.DeletePictureBox = new System.Windows.Forms.PictureBox(); + this.DeleteServerPictureBox = new System.Windows.Forms.PictureBox(); this.SpeedPictureBox = new System.Windows.Forms.PictureBox(); this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel(); this.EditModePictureBox = new System.Windows.Forms.PictureBox(); @@ -92,9 +91,9 @@ namespace Netch.Forms this.ConfigurationGroupBox.SuspendLayout(); this.configLayoutPanel.SuspendLayout(); this.tableLayoutPanel2.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.EditPictureBox)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.EditServerPictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.CopyLinkPictureBox)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.DeletePictureBox)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.DeleteServerPictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.SpeedPictureBox)).BeginInit(); this.tableLayoutPanel3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.EditModePictureBox)).BeginInit(); @@ -182,7 +181,8 @@ namespace Netch.Forms // ModeToolStripMenuItem // this.ModeToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.CreateProcessModeToolStripMenuItem}); + this.CreateProcessModeToolStripMenuItem, + this.ReloadModesToolStripMenuItem}); this.ModeToolStripMenuItem.Margin = new System.Windows.Forms.Padding(0, 0, 0, 1); this.ModeToolStripMenuItem.Name = "ModeToolStripMenuItem"; this.ModeToolStripMenuItem.Size = new System.Drawing.Size(55, 21); @@ -195,6 +195,13 @@ namespace Netch.Forms this.CreateProcessModeToolStripMenuItem.Text = "Create Process Mode"; this.CreateProcessModeToolStripMenuItem.Click += new System.EventHandler(this.CreateProcessModeToolStripButton_Click); // + // ReloadModesToolStripMenuItem + // + this.ReloadModesToolStripMenuItem.Name = "ReloadModesToolStripMenuItem"; + this.ReloadModesToolStripMenuItem.Size = new System.Drawing.Size(202, 22); + this.ReloadModesToolStripMenuItem.Text = "Reload Modes"; + this.ReloadModesToolStripMenuItem.Click += new System.EventHandler(this.ReloadModesToolStripMenuItem_Click); + // // SubscribeToolStripMenuItem // this.SubscribeToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -222,39 +229,23 @@ namespace Netch.Forms // OptionsToolStripMenuItem // this.OptionsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.ReloadModesToolStripMenuItem, - this.RestartServiceToolStripMenuItem, - this.UninstallServiceToolStripMenuItem, + this.OpenDirectoryToolStripMenuItem, this.CleanDNSCacheToolStripMenuItem, this.UpdateACLToolStripMenuItem, this.updateACLWithProxyToolStripMenuItem, - this.reinstallTapDriverToolStripMenuItem, - this.OpenDirectoryToolStripMenuItem}); + this.UninstallServiceToolStripMenuItem, + this.reinstallTapDriverToolStripMenuItem}); this.OptionsToolStripMenuItem.Margin = new System.Windows.Forms.Padding(0, 0, 0, 1); this.OptionsToolStripMenuItem.Name = "OptionsToolStripMenuItem"; this.OptionsToolStripMenuItem.Size = new System.Drawing.Size(66, 21); this.OptionsToolStripMenuItem.Text = "Options"; // - // ReloadModesToolStripMenuItem + // OpenDirectoryToolStripMenuItem // - this.ReloadModesToolStripMenuItem.Name = "ReloadModesToolStripMenuItem"; - this.ReloadModesToolStripMenuItem.Size = new System.Drawing.Size(219, 22); - this.ReloadModesToolStripMenuItem.Text = "Reload Modes"; - this.ReloadModesToolStripMenuItem.Click += new System.EventHandler(this.ReloadModesToolStripMenuItem_Click); - // - // RestartServiceToolStripMenuItem - // - this.RestartServiceToolStripMenuItem.Name = "RestartServiceToolStripMenuItem"; - this.RestartServiceToolStripMenuItem.Size = new System.Drawing.Size(219, 22); - this.RestartServiceToolStripMenuItem.Text = "Restart Service"; - this.RestartServiceToolStripMenuItem.Click += new System.EventHandler(this.RestartServiceToolStripMenuItem_Click); - // - // UninstallServiceToolStripMenuItem - // - this.UninstallServiceToolStripMenuItem.Name = "UninstallServiceToolStripMenuItem"; - this.UninstallServiceToolStripMenuItem.Size = new System.Drawing.Size(219, 22); - this.UninstallServiceToolStripMenuItem.Text = "Uninstall Service"; - this.UninstallServiceToolStripMenuItem.Click += new System.EventHandler(this.UninstallServiceToolStripMenuItem_Click); + this.OpenDirectoryToolStripMenuItem.Name = "OpenDirectoryToolStripMenuItem"; + this.OpenDirectoryToolStripMenuItem.Size = new System.Drawing.Size(219, 22); + this.OpenDirectoryToolStripMenuItem.Text = "Open Directory"; + this.OpenDirectoryToolStripMenuItem.Click += new System.EventHandler(this.OpenDirectoryToolStripMenuItem_Click); // // CleanDNSCacheToolStripMenuItem // @@ -277,6 +268,13 @@ namespace Netch.Forms this.updateACLWithProxyToolStripMenuItem.Text = "Update ACL with proxy"; this.updateACLWithProxyToolStripMenuItem.Click += new System.EventHandler(this.updateACLWithProxyToolStripMenuItem_Click); // + // UninstallServiceToolStripMenuItem + // + this.UninstallServiceToolStripMenuItem.Name = "UninstallServiceToolStripMenuItem"; + this.UninstallServiceToolStripMenuItem.Size = new System.Drawing.Size(219, 22); + this.UninstallServiceToolStripMenuItem.Text = "Uninstall Service"; + this.UninstallServiceToolStripMenuItem.Click += new System.EventHandler(this.UninstallServiceToolStripMenuItem_Click); + // // reinstallTapDriverToolStripMenuItem // this.reinstallTapDriverToolStripMenuItem.Name = "reinstallTapDriverToolStripMenuItem"; @@ -284,13 +282,6 @@ namespace Netch.Forms this.reinstallTapDriverToolStripMenuItem.Text = "Reinstall TUN/TAP driver"; this.reinstallTapDriverToolStripMenuItem.Click += new System.EventHandler(this.reinstallTapDriverToolStripMenuItem_Click); // - // OpenDirectoryToolStripMenuItem - // - this.OpenDirectoryToolStripMenuItem.Name = "OpenDirectoryToolStripMenuItem"; - this.OpenDirectoryToolStripMenuItem.Size = new System.Drawing.Size(219, 22); - this.OpenDirectoryToolStripMenuItem.Text = "Open Directory"; - this.OpenDirectoryToolStripMenuItem.Click += new System.EventHandler(this.OpenDirectoryToolStripMenuItem_Click); - // // AboutToolStripButton // this.AboutToolStripButton.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right; @@ -318,7 +309,7 @@ namespace Netch.Forms this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; this.exitToolStripMenuItem.Size = new System.Drawing.Size(40, 22); this.exitToolStripMenuItem.Text = "Exit"; - this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); + this.exitToolStripMenuItem.Click += new System.EventHandler(this.ExitToolStripButton_Click); // // RelyToolStripMenuItem // @@ -435,9 +426,9 @@ namespace Netch.Forms this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); - this.tableLayoutPanel2.Controls.Add(this.EditPictureBox, 0, 0); + this.tableLayoutPanel2.Controls.Add(this.EditServerPictureBox, 0, 0); this.tableLayoutPanel2.Controls.Add(this.CopyLinkPictureBox, 3, 0); - this.tableLayoutPanel2.Controls.Add(this.DeletePictureBox, 1, 0); + this.tableLayoutPanel2.Controls.Add(this.DeleteServerPictureBox, 1, 0); this.tableLayoutPanel2.Controls.Add(this.SpeedPictureBox, 2, 0); this.tableLayoutPanel2.Location = new System.Drawing.Point(606, 3); this.tableLayoutPanel2.Name = "tableLayoutPanel2"; @@ -446,16 +437,17 @@ namespace Netch.Forms this.tableLayoutPanel2.Size = new System.Drawing.Size(94, 24); this.tableLayoutPanel2.TabIndex = 12; // - // EditPictureBox + // EditServerPictureBox // - this.EditPictureBox.Cursor = System.Windows.Forms.Cursors.Hand; - this.EditPictureBox.Image = ((System.Drawing.Image)(resources.GetObject("EditPictureBox.Image"))); - this.EditPictureBox.Location = new System.Drawing.Point(3, 3); - this.EditPictureBox.Name = "EditPictureBox"; - this.EditPictureBox.Size = new System.Drawing.Size(16, 16); - this.EditPictureBox.TabIndex = 7; - this.EditPictureBox.TabStop = false; - this.EditPictureBox.Click += new System.EventHandler(this.EditPictureBox_Click); + this.EditServerPictureBox.BackColor = System.Drawing.SystemColors.Control; + this.EditServerPictureBox.Cursor = System.Windows.Forms.Cursors.Hand; + this.EditServerPictureBox.Image = global::Netch.Properties.Resources.edit; + this.EditServerPictureBox.Location = new System.Drawing.Point(3, 3); + this.EditServerPictureBox.Name = "EditServerPictureBox"; + this.EditServerPictureBox.Size = new System.Drawing.Size(16, 16); + this.EditServerPictureBox.TabIndex = 7; + this.EditServerPictureBox.TabStop = false; + this.EditServerPictureBox.Click += new System.EventHandler(this.EditServerPictureBox_Click); // // CopyLinkPictureBox // @@ -468,21 +460,21 @@ namespace Netch.Forms this.CopyLinkPictureBox.TabStop = false; this.CopyLinkPictureBox.Click += new System.EventHandler(this.CopyLinkPictureBox_Click); // - // DeletePictureBox + // DeleteServerPictureBox // - this.DeletePictureBox.Cursor = System.Windows.Forms.Cursors.Hand; - this.DeletePictureBox.Image = ((System.Drawing.Image)(resources.GetObject("DeletePictureBox.Image"))); - this.DeletePictureBox.Location = new System.Drawing.Point(26, 3); - this.DeletePictureBox.Name = "DeletePictureBox"; - this.DeletePictureBox.Size = new System.Drawing.Size(16, 16); - this.DeletePictureBox.TabIndex = 8; - this.DeletePictureBox.TabStop = false; - this.DeletePictureBox.Click += new System.EventHandler(this.DeletePictureBox_Click); + this.DeleteServerPictureBox.Cursor = System.Windows.Forms.Cursors.Hand; + this.DeleteServerPictureBox.Image = global::Netch.Properties.Resources.delete; + this.DeleteServerPictureBox.Location = new System.Drawing.Point(26, 3); + this.DeleteServerPictureBox.Name = "DeleteServerPictureBox"; + this.DeleteServerPictureBox.Size = new System.Drawing.Size(16, 16); + this.DeleteServerPictureBox.TabIndex = 8; + this.DeleteServerPictureBox.TabStop = false; + this.DeleteServerPictureBox.Click += new System.EventHandler(this.DeleteServerPictureBox_Click); // // SpeedPictureBox // this.SpeedPictureBox.Cursor = System.Windows.Forms.Cursors.Hand; - this.SpeedPictureBox.Image = ((System.Drawing.Image)(resources.GetObject("SpeedPictureBox.Image"))); + this.SpeedPictureBox.Image = global::Netch.Properties.Resources.speed; this.SpeedPictureBox.Location = new System.Drawing.Point(49, 3); this.SpeedPictureBox.Name = "SpeedPictureBox"; this.SpeedPictureBox.Size = new System.Drawing.Size(16, 16); @@ -509,9 +501,7 @@ namespace Netch.Forms // EditModePictureBox // this.EditModePictureBox.Cursor = System.Windows.Forms.Cursors.Hand; - this.EditModePictureBox.ErrorImage = global::Netch.Properties.Resources.edit; this.EditModePictureBox.Image = global::Netch.Properties.Resources.edit; - this.EditModePictureBox.InitialImage = global::Netch.Properties.Resources.edit; this.EditModePictureBox.Location = new System.Drawing.Point(3, 3); this.EditModePictureBox.Name = "EditModePictureBox"; this.EditModePictureBox.Size = new System.Drawing.Size(16, 16); @@ -576,7 +566,8 @@ namespace Netch.Forms // NatTypeStatusLabel // this.NatTypeStatusLabel.Name = "NatTypeStatusLabel"; - this.NatTypeStatusLabel.Size = new System.Drawing.Size(0, 17); + this.NatTypeStatusLabel.Size = new System.Drawing.Size(36, 17); + this.NatTypeStatusLabel.Text = "NAT:"; // // ControlButton // @@ -686,9 +677,9 @@ namespace Netch.Forms this.configLayoutPanel.ResumeLayout(false); this.configLayoutPanel.PerformLayout(); this.tableLayoutPanel2.ResumeLayout(false); - ((System.ComponentModel.ISupportInitialize)(this.EditPictureBox)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.EditServerPictureBox)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.CopyLinkPictureBox)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.DeletePictureBox)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.DeleteServerPictureBox)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.SpeedPictureBox)).EndInit(); this.tableLayoutPanel3.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.EditModePictureBox)).EndInit(); @@ -703,63 +694,62 @@ namespace Netch.Forms } - #endregion - - private System.Windows.Forms.MenuStrip MenuStrip; - private System.Windows.Forms.ToolStripMenuItem ServerToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem SubscribeToolStripMenuItem; - private System.Windows.Forms.ToolStripLabel VersionLabel; - private System.Windows.Forms.GroupBox ConfigurationGroupBox; - private System.Windows.Forms.Label ServerLabel; - private System.Windows.Forms.Label ModeLabel; - private System.Windows.Forms.SearchComboBox ModeComboBox; - private System.Windows.Forms.ComboBox ServerComboBox; - private System.Windows.Forms.StatusStrip StatusStrip; - private System.Windows.Forms.ToolStripStatusLabel StatusLabel; - private System.Windows.Forms.Button ControlButton; - private System.Windows.Forms.ToolStripMenuItem AddSocks5ServerToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem AddShadowsocksServerToolStripMenuItem; + private System.Windows.Forms.ToolStripButton AboutToolStripButton; private System.Windows.Forms.ToolStripMenuItem AddShadowsocksRServerToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem AddShadowsocksServerToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem AddSocks5ServerToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem AddTrojanServerToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem AddVMessServerToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem CleanDNSCacheToolStripMenuItem; + private System.Windows.Forms.TableLayoutPanel configLayoutPanel; + private System.Windows.Forms.GroupBox ConfigurationGroupBox; + private System.Windows.Forms.Button ControlButton; + private System.Windows.Forms.PictureBox CopyLinkPictureBox; + private System.Windows.Forms.ToolStripMenuItem CreateProcessModeToolStripMenuItem; + private System.Windows.Forms.PictureBox DeleteModePictureBox; + private System.Windows.Forms.PictureBox DeleteServerPictureBox; + private System.Windows.Forms.ToolStripStatusLabel DownloadSpeedLabel; + private System.Windows.Forms.PictureBox EditModePictureBox; + private System.Windows.Forms.PictureBox EditServerPictureBox; + private System.Windows.Forms.ToolStripMenuItem ExitToolStripButton; + private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem ImportServersFromClipboardToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem ManageSubscribeLinksToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem UpdateServersFromSubscribeLinksToolStripMenuItem; - private System.Windows.Forms.PictureBox SpeedPictureBox; - private System.Windows.Forms.PictureBox DeletePictureBox; - private System.Windows.Forms.PictureBox EditPictureBox; - private System.Windows.Forms.ToolStripMenuItem RestartServiceToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem UninstallServiceToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem OptionsToolStripMenuItem; + private System.Windows.Forms.MenuStrip MenuStrip; + private System.Windows.Forms.SearchComboBox ModeComboBox; + private System.Windows.Forms.Label ModeLabel; private System.Windows.Forms.ToolStripMenuItem ModeToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem CreateProcessModeToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem ReloadModesToolStripMenuItem; + private System.Windows.Forms.ToolStripStatusLabel NatTypeStatusLabel; private System.Windows.Forms.NotifyIcon NotifyIcon; private System.Windows.Forms.ContextMenuStrip NotifyMenu; - private System.Windows.Forms.ToolStripMenuItem ShowMainFormToolStripButton; - private System.Windows.Forms.ToolStripMenuItem ExitToolStripButton; - private System.Windows.Forms.Button SettingsButton; - private System.Windows.Forms.ToolStripButton AboutToolStripButton; - private System.Windows.Forms.ToolStripStatusLabel UsedBandwidthLabel; - private System.Windows.Forms.ToolStripStatusLabel UploadSpeedLabel; - private System.Windows.Forms.ToolStripStatusLabel DownloadSpeedLabel; - private System.Windows.Forms.ToolStripMenuItem CleanDNSCacheToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem OpenDirectoryToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem OptionsToolStripMenuItem; + private System.Windows.Forms.GroupBox ProfileGroupBox; private System.Windows.Forms.Label ProfileLabel; private System.Windows.Forms.TextBox ProfileNameText; - private System.Windows.Forms.GroupBox ProfileGroupBox; private System.Windows.Forms.TableLayoutPanel ProfileTable; - private System.Windows.Forms.PictureBox EditModePictureBox; - private System.Windows.Forms.PictureBox DeleteModePictureBox; - private System.Windows.Forms.PictureBox CopyLinkPictureBox; - private System.Windows.Forms.ToolStripStatusLabel NatTypeStatusLabel; - private System.Windows.Forms.TableLayoutPanel configLayoutPanel; + private System.Windows.Forms.ToolStripMenuItem reinstallTapDriverToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem ReloadModesToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem RelyToolStripMenuItem; + private System.Windows.Forms.ComboBox ServerComboBox; + private System.Windows.Forms.Label ServerLabel; + private System.Windows.Forms.ToolStripMenuItem ServerToolStripMenuItem; + private System.Windows.Forms.Button SettingsButton; + private System.Windows.Forms.ToolStripMenuItem ShowMainFormToolStripButton; + private System.Windows.Forms.PictureBox SpeedPictureBox; + private System.Windows.Forms.ToolStripStatusLabel StatusLabel; + private System.Windows.Forms.StatusStrip StatusStrip; + private System.Windows.Forms.ToolStripMenuItem SubscribeToolStripMenuItem; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3; + private System.Windows.Forms.ToolStripMenuItem UninstallServiceToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem UpdateACLToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem updateACLWithProxyToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem reinstallTapDriverToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem AddTrojanServerToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem RelyToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem OpenDirectoryToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem UpdateServersFromSubscribeLinksToolStripMenuItem; + private System.Windows.Forms.ToolStripStatusLabel UploadSpeedLabel; + private System.Windows.Forms.ToolStripStatusLabel UsedBandwidthLabel; + private System.Windows.Forms.ToolStripLabel VersionLabel; + + #endregion } } \ No newline at end of file diff --git a/Netch/Forms/MainForm.MenuStrip.cs b/Netch/Forms/MainForm.MenuStrip.cs new file mode 100644 index 00000000..6451dcf0 --- /dev/null +++ b/Netch/Forms/MainForm.MenuStrip.cs @@ -0,0 +1,495 @@ +using System; +using System.IO; +using System.Linq; +using System.Net; +using System.ServiceProcess; +using System.Threading.Tasks; +using System.Windows.Forms; +using Netch.Controllers; +using Netch.Forms.Mode; +using Netch.Forms.Server; +using Netch.Models; +using Netch.Utils; +using nfapinet; +using Trojan = Netch.Forms.Server.Trojan; +using VMess = Netch.Forms.Server.VMess; +using WebClient = Netch.Override.WebClient; + +namespace Netch.Forms +{ + partial class Dummy + {} + partial class MainForm + { + #region MenuStrip + + #region 服务器 + + private void ImportServersFromClipboardToolStripMenuItem_Click(object sender, EventArgs e) + { + var texts = Clipboard.GetText(); + if (!string.IsNullOrWhiteSpace(texts)) + { + var result = ShareLink.Parse(texts); + + if (result != null) + { + Global.Settings.Server.AddRange(result); + } + else + { + MessageBox.Show(i18N.Translate("Import servers error!"), i18N.Translate("Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + } + + InitServer(); + Configuration.Save(); + } + } + + private void AddSocks5ServerToolStripMenuItem_Click(object sender, EventArgs e) + { + new Socks5().Show(); + Hide(); + } + + private void AddShadowsocksServerToolStripMenuItem_Click(object sender, EventArgs e) + { + new Shadowsocks().Show(); + Hide(); + } + + private void AddShadowsocksRServerToolStripMenuItem_Click(object sender, EventArgs e) + { + new ShadowsocksR().Show(); + Hide(); + } + + private void AddVMessServerToolStripMenuItem_Click(object sender, EventArgs e) + { + new VMess().Show(); + Hide(); + } + + private void AddTrojanServerToolStripMenuItem_Click(object sender, EventArgs e) + { + new Trojan().Show(); + Hide(); + } + + #endregion + + #region 模式 + + private void CreateProcessModeToolStripButton_Click(object sender, EventArgs e) + { + new Process().Show(); + Hide(); + } + + #endregion + + #region 订阅 + + private void ManageSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e) + { + new SubscribeForm().Show(); + Hide(); + } + + private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e) + { + if (Global.Settings.UseProxyToUpdateSubscription && ServerComboBox.SelectedIndex == -1) + Global.Settings.UseProxyToUpdateSubscription = false; + + if (Global.Settings.UseProxyToUpdateSubscription) + { + // 当前 ServerComboBox 中至少有一项 + if (ServerComboBox.SelectedIndex == -1) + { + MessageBox.Show(i18N.Translate("Please select a server first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false; + ControlButton.Text = "..."; + } + + if (Global.Settings.SubscribeLink.Count > 0) + { + StatusText(i18N.Translate("Starting update subscription")); + DeleteServerPictureBox.Enabled = false; + + UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false; + Task.Run(() => + { + if (Global.Settings.UseProxyToUpdateSubscription) + { + var mode = new Models.Mode + { + Remark = "ProxyUpdate", + Type = 5 + }; + MainController = new MainController(); + MainController.Start(ServerComboBox.SelectedItem as Models.Server, mode); + } + + foreach (var item in Global.Settings.SubscribeLink) + { + using var client = new WebClient(); + try + { + if (!string.IsNullOrEmpty(item.UserAgent)) + { + client.Headers.Add("User-Agent", item.UserAgent); + } + else + { + client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"); + } + + if (Global.Settings.UseProxyToUpdateSubscription) + { + client.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}"); + } + + var response = client.DownloadString(item.Link); + + try + { + response = ShareLink.URLSafeBase64Decode(response); + } + catch (Exception) + { + // 跳过 + } + + Global.Settings.Server = Global.Settings.Server.Where(server => server.Group != item.Remark).ToList(); + var result = ShareLink.Parse(response); + + if (result != null) + { + foreach (var x in result) + { + x.Group = item.Remark; + } + + Global.Settings.Server.AddRange(result); + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, + string.Format(i18N.Translate("Update {1} server(s) from {0}"), item.Remark, result.Count), + ToolTipIcon.Info); + } + else + { + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, + string.Format(i18N.Translate("Update servers error from {0}"), item.Remark), + ToolTipIcon.Error); + } + } + catch (Exception) + { + } + } + + InitServer(); + DeleteServerPictureBox.Enabled = true; + if (Global.Settings.UseProxyToUpdateSubscription) + { + MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true; + ControlButton.Text = i18N.Translate("Start"); + MainController.Stop(); + NatTypeStatusLabel.Text = ""; + } + + Configuration.Save(); + StatusText(i18N.Translate("Subscription updated")); + }).ContinueWith(task => { BeginInvoke(new Action(() => { UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; })); }); + + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, + i18N.Translate("Updating in the background"), + ToolTipIcon.Info); + } + else + { + MessageBox.Show(i18N.Translate("No subscription link"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + + #endregion + + #region 选项 + + private void RestartServiceToolStripMenuItem_Click(object sender, EventArgs e) + { + Enabled = false; + StatusText(i18N.Translate("Restarting service")); + + Task.Run(() => + { + try + { + var service = new ServiceController("netfilter2"); + if (service.Status == ServiceControllerStatus.Stopped) + { + service.Start(); + service.WaitForStatus(ServiceControllerStatus.Running); + } + else if (service.Status == ServiceControllerStatus.Running) + { + service.Stop(); + service.WaitForStatus(ServiceControllerStatus.Stopped); + service.Start(); + service.WaitForStatus(ServiceControllerStatus.Running); + } + } + catch (Exception) + { + NFAPI.nf_registerDriver("netfilter2"); + } + + MessageBox.Show(this, i18N.Translate("Service has been restarted"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + Enabled = true; + }); + } + + private void UninstallServiceToolStripMenuItem_Click(object sender, EventArgs e) + { + Enabled = false; + StatusText(i18N.Translate("Uninstalling Service")); + + Task.Run(() => + { + var driver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys"; + if (File.Exists(driver)) + { + try + { + var service = new ServiceController("netfilter2"); + if (service.Status == ServiceControllerStatus.Running) + { + service.Stop(); + service.WaitForStatus(ServiceControllerStatus.Stopped); + } + } + catch (Exception) + { + // 跳过 + } + + try + { + NFAPI.nf_unRegisterDriver("netfilter2"); + + File.Delete(driver); + + MessageBox.Show(this, i18N.Translate("Service has been uninstalled"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(this, i18N.Translate("Error") + i18N.Translate(": ") + ex, i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + else + { + MessageBox.Show(this, i18N.Translate("Service has been uninstalled"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + } + + Enabled = true; + }); + } + + private void ReloadModesToolStripMenuItem_Click(object sender, EventArgs e) + { + Enabled = false; + SaveConfigs(); + Task.Run(() => + { + InitMode(); + + MessageBox.Show(this, i18N.Translate("Modes have been reload"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + Enabled = true; + }); + } + + private void CleanDNSCacheToolStripMenuItem_Click(object sender, EventArgs e) + { + Enabled = false; + Task.Run(() => + { + DNS.Cache.Clear(); + + MessageBox.Show(this, i18N.Translate("DNS cache cleanup succeeded"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + StatusText(i18N.Translate("DNS cache cleanup succeeded")); + Enabled = true; + }); + } + + private void OpenDirectoryToolStripMenuItem_Click(object sender, EventArgs e) + { + Utils.Utils.OpenDir(@".\"); + } + + private void reinstallTapDriverToolStripMenuItem_Click(object sender, EventArgs e) + { + Task.Run(() => + { + StatusText(i18N.Translate("Reinstalling TUN/TAP driver")); + Enabled = false; + try + { + Configuration.deltapall(); + Configuration.addtap(); + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, i18N.Translate("Reinstall TUN/TAP driver successfully"), + ToolTipIcon.Info); + } + catch + { + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, i18N.Translate("Reinstall TUN/TAP driver failed"), + ToolTipIcon.Error); + } + finally + { + UpdateStatus(State.Waiting); + Enabled = true; + } + }); + } + + private void updateACLWithProxyToolStripMenuItem_Click(object sender, EventArgs e) + { + updateACLWithProxyToolStripMenuItem.Enabled = false; + + // 当前 ServerComboBox 中至少有一项 + if (ServerComboBox.SelectedIndex == -1) + { + MessageBox.Show(i18N.Translate("Please select a server first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false; + ControlButton.Text = "..."; + + + Task.Run(() => + { + var mode = new Models.Mode + { + Remark = "ProxyUpdate", + Type = 5 + }; + MainController = new MainController(); + MainController.Start(ServerComboBox.SelectedItem as Models.Server, mode); + + using var client = new WebClient(); + + client.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}"); + + StatusText(i18N.Translate("Updating in the background")); + try + { + client.DownloadFile(Global.Settings.ACL, "bin\\default.acl"); + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, i18N.Translate("ACL updated successfully"), + ToolTipIcon.Info); + } + catch (Exception e) + { + Logging.Info("使用代理更新 ACL 失败!" + e.Message); + MessageBox.Show(i18N.Translate("ACL update failed") + "\n" + e.Message); + } + finally + { + + UpdateStatus(State.Waiting); + MainController.Stop(); + } + }); + } + + #endregion + + + private void ExitToolStripButton_Click(object sender, EventArgs e) + { + // 已启动 + if (State != State.Waiting && State != State.Stopped) + { + // 未开启自动停止 + if (!Global.Settings.StopWhenExited) + { + MessageBox.Show(i18N.Translate("Please press Stop button first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + + Visible = true; + ShowInTaskbar = true; // 显示在系统任务栏 + WindowState = FormWindowState.Normal; // 还原窗体 + NotifyIcon.Visible = true; // 托盘图标隐藏 + + return; + } + // 自动停止 + + ControlButton_Click(sender, e); + } + + SaveConfigs(); + + UpdateStatus(State.Terminating); + NotifyIcon.Visible = false; + Close(); + Dispose(); + Environment.Exit(Environment.ExitCode); + } + + private void RelyToolStripMenuItem_Click(object sender, EventArgs e) + { + System.Diagnostics.Process.Start("https://mega.nz/file/9OQ1EazJ#0pjJ3xt57AVLr29vYEEv15GSACtXVQOGlEOPpi_2Ico"); + } + + private void VersionLabel_Click(object sender, EventArgs e) + { + System.Diagnostics.Process.Start($"https://github.com/{UpdateChecker.Owner}/{UpdateChecker.Repo}/releases"); + } + + private void AboutToolStripButton_Click(object sender, EventArgs e) + { + new AboutForm().Show(); + Hide(); + } + + private void updateACLToolStripMenuItem_Click(object sender, EventArgs e) + { + StatusText(i18N.Translate("Starting update ACL")); + using var client = new WebClient(); + + client.DownloadFileTaskAsync(Global.Settings.ACL, "bin\\default.acl"); + client.DownloadFileCompleted += ((sender, args) => + { + try + { + if (args.Error == null) + { + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, i18N.Translate("ACL updated successfully"), + ToolTipIcon.Info); + } + else + { + Logging.Info("ACL 更新失败!" + args.Error); + MessageBox.Show(i18N.Translate("ACL update failed") + "\n" + args.Error); + } + } + finally + { + UpdateStatus(State.Waiting); + } + }); + } + + #endregion + + } +} \ No newline at end of file diff --git a/Netch/Forms/MainForm.Misc.cs b/Netch/Forms/MainForm.Misc.cs new file mode 100644 index 00000000..941df06b --- /dev/null +++ b/Netch/Forms/MainForm.Misc.cs @@ -0,0 +1,32 @@ +using System.ComponentModel; +using System.Windows.Forms; +using Netch.Controllers; +using Netch.Utils; + +namespace Netch.Forms +{ + /// + /// this class is used to disable Designer + /// + /// + /// 此类用于禁用设计器 + /// + [DesignerCategory("")] public partial class Dummy { } + partial class MainForm + { + private void CheckUpdate() + { + var updater = new UpdateChecker(); + updater.NewVersionFound += (o, args) => + { + NotifyIcon.ShowBalloonTip(5, + UpdateChecker.Name, + $"{i18N.Translate(@"New version available")}{i18N.Translate(@": ")}{updater.LatestVersionNumber}", + ToolTipIcon.Info); + }; + updater.Check(false, false); + } + + + } +} \ No newline at end of file diff --git a/Netch/Forms/MainForm.Profile.cs b/Netch/Forms/MainForm.Profile.cs new file mode 100644 index 00000000..528886a6 --- /dev/null +++ b/Netch/Forms/MainForm.Profile.cs @@ -0,0 +1,206 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Forms; +using Netch.Models; +using Netch.Utils; + +namespace Netch.Forms +{ + public partial class Dummy { } + partial class MainForm + { + + // init at MainFrom_Load() + private int _sizeHeight; + private int _controlHeight; + private int _profileBoxHeight; + private int _configurationGroupBoxHeight; + + private void InitProfile() + { + foreach (var button in ProfileButtons) + { + button.Dispose(); + } + + ProfileButtons.Clear(); + ProfileTable.ColumnStyles.Clear(); + ProfileTable.RowStyles.Clear(); + + var numProfile = Global.Settings.ProfileCount; + if (numProfile == 0) + { + configLayoutPanel.RowStyles[2].SizeType = SizeType.Percent; + configLayoutPanel.RowStyles[2].Height = 0; + ProfileGroupBox.Visible = false; + + ConfigurationGroupBox.Size = new Size(ConfigurationGroupBox.Size.Width, _configurationGroupBoxHeight - _controlHeight); + Size = new Size(Size.Width, _sizeHeight - (_controlHeight + _profileBoxHeight)); + + return; + } + + configLayoutPanel.RowStyles[2].SizeType = SizeType.AutoSize; + ProfileGroupBox.Visible = true; + ConfigurationGroupBox.Size = new Size(ConfigurationGroupBox.Size.Width, _configurationGroupBoxHeight); + Size = new Size(Size.Width, _sizeHeight); + + + ProfileTable.ColumnCount = numProfile; + + while (Global.Settings.Profiles.Count < numProfile) + { + Global.Settings.Profiles.Add(new Profile()); + } + + // buttons + for (var i = 0; i < numProfile; ++i) + { + var b = new Button(); + ProfileTable.Controls.Add(b, i, 0); + b.Location = new Point(i * 100, 0); + b.Click += ProfileButton_Click; + b.Dock = DockStyle.Fill; + b.Text = !Global.Settings.Profiles[i].IsDummy ? Global.Settings.Profiles[i].ProfileName : i18N.Translate("None"); + + ProfileButtons.Add(b); + } + + // equal column + for (var i = 1; i <= ProfileTable.RowCount; i++) + { + ProfileTable.RowStyles.Add(new RowStyle(SizeType.Percent, 1)); + } + + for (var i = 1; i <= ProfileTable.ColumnCount; i++) + { + ProfileTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1)); + } + } + + private string LoadProfile(int index) + { + var p = Global.Settings.Profiles[index]; + + if (p.IsDummy) + throw new Exception("Profile not found."); + + var result = false; + + foreach (Models.Server server in ServerComboBox.Items) + { + if (server.Remark.Equals(p.ServerRemark)) + { + ServerComboBox.SelectedItem = server; + result = true; + break; + } + } + + if (!result) + throw new Exception("Server not found."); + + result = false; + foreach (Models.Mode mode in ModeComboBox.Items) + { + if (mode.Remark.Equals(p.ModeRemark)) + { + ModeComboBox.SelectedItem = mode; + result = true; + break; + } + } + + if (!result) + throw new Exception("Mode not found."); + + return p.ProfileName; + } + + private void SaveProfile(int index) + { + var selectedServer = (Models.Server) ServerComboBox.SelectedItem; + var selectedMode = (Models.Mode) ModeComboBox.SelectedItem; + var name = ProfileNameText.Text; + + Global.Settings.Profiles[index] = new Profile(selectedServer, selectedMode, name); + } + + public List