From f4759d2f94ad583430f1a37825c0ece6082f4878 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 26 Mar 2021 18:17:58 +0800 Subject: [PATCH] Drop HTTPController(Drop WebProxy, Update server with proxy etc.) --- Netch/Controllers/HTTPController.cs | 93 --------------- Netch/Controllers/MainController.cs | 6 - Netch/Controllers/PrivoxyController.cs | 45 -------- Netch/Forms/MainForm.Designer.cs | 54 ++------- Netch/Forms/MainForm.cs | 106 ++---------------- Netch/Models/Mode.cs | 5 +- Netch/Models/Setting.cs | 5 - Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs | 1 - Netch/Utils/Bandwidth.cs | 3 - Netch/Utils/ModeHelper.cs | 18 +-- 10 files changed, 29 insertions(+), 307 deletions(-) delete mode 100644 Netch/Controllers/HTTPController.cs delete mode 100644 Netch/Controllers/PrivoxyController.cs diff --git a/Netch/Controllers/HTTPController.cs b/Netch/Controllers/HTTPController.cs deleted file mode 100644 index fa08bd2c..00000000 --- a/Netch/Controllers/HTTPController.cs +++ /dev/null @@ -1,93 +0,0 @@ -using System.Threading.Tasks; -using WindowsProxy; -using Netch.Models; -using Netch.Servers.Socks5; -using Netch.Servers.Trojan; -using Netch.Utils; -using Netch.Utils.HttpProxyHandler; - -namespace Netch.Controllers -{ - public class HTTPController : IModeController - { - public readonly PrivoxyController PrivoxyController = new(); - - private ProxyStatus? _oldState; - - public string Name { get; } = "HTTP"; - - /// - /// 启动 - /// - /// 模式 - /// 是否启动成功 - public void Start(in Mode mode) - { - PrivoxyController.Start(MainController.Server!); - string? pacUrl = null; - - if (MainController.Server is Socks5 or Trojan && mode.BypassChina || (Global.Settings.AlwaysStartPACServer ?? false)) - { - try - { - PortHelper.CheckPort(Global.Settings.Pac_Port); - } - catch - { - Global.Settings.Pac_Port = PortHelper.GetAvailablePort(); - } - - pacUrl = PACServerHandle.InitPACServer(); - } - - if (mode.Type is 3) - { - using var service = new ProxyService(); - _oldState = service.Query(); - - if (pacUrl != null) - { - service.AutoConfigUrl = pacUrl; - service.Pac(); - } - else - { - service.Server = $"127.0.0.1:{Global.Settings.HTTPLocalPort}"; - service.Bypass = string.Join(";", ProxyService.LanIp); - - service.Global(); - } - } - } - - /// - /// 停止 - /// - public void Stop() - { - var tasks = new[] - { - Task.Run(PrivoxyController.Stop), - Task.Run(() => - { - PACServerHandle.Stop(); - - if (_oldState != null) - { - using var service = new ProxyService(); - if (_oldState.IsProxy && _oldState.ProxyServer == service.Query().ProxyServer || - _oldState.IsAutoProxyUrl && _oldState.AutoConfigUrl!.StartsWith(PACServerHandle.PacPrefix)) - { - service.Direct(); - return; - } - - service.Set(_oldState); - } - }) - }; - - Task.WaitAll(tasks); - } - } -} \ No newline at end of file diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index a340cd63..ea618040 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -59,9 +59,6 @@ namespace Netch.Controllers Server = server; Mode = mode; - if (server is Socks5 && mode.Type == 4) - throw new MessageException("Already Socks5 Server"); - // 刷新DNS缓存 NativeMethods.FlushDNSResolverCache(); @@ -125,9 +122,6 @@ namespace Netch.Controllers { ModeController = ModeHelper.GetModeControllerByType(mode.Type, out var port, out var portName); - if (ModeController == null) - return; - if (port != null) TryReleaseTcpPort((ushort) port, portName); diff --git a/Netch/Controllers/PrivoxyController.cs b/Netch/Controllers/PrivoxyController.cs deleted file mode 100644 index 296eaac3..00000000 --- a/Netch/Controllers/PrivoxyController.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.IO; -using System.Text; -using Netch.Models; -using Netch.Servers.Socks5; - -namespace Netch.Controllers -{ - public class PrivoxyController : Guard, IController - { - public PrivoxyController() - { - RedirectStd = false; - } - - public override string MainFile { get; protected set; } = "Privoxy.exe"; - - public override string Name { get; } = "Privoxy"; - - public override void Stop() - { - StopInstance(); - } - - public void Start(Server server) - { - var text = new StringBuilder(File.ReadAllText("bin\\default.conf")); - - text.Replace("_BIND_PORT_", Global.Settings.HTTPLocalPort.ToString()); - text.Replace("0.0.0.0", Global.Settings.LocalAddress); /* BIND_HOST */ - - if (server is Socks5 socks5 && !socks5.Auth()) - { - text.Replace("/ 127.0.0.1", $"/ {server.AutoResolveHostname()}"); /* DEST_HOST */ - text.Replace("_DEST_PORT_", socks5.Port.ToString()); - } - - text.Replace("_DEST_PORT_", Global.Settings.Socks5LocalPort.ToString()); - - - File.WriteAllText("data\\privoxy.conf", text.ToString()); - - StartInstanceAuto("..\\data\\privoxy.conf"); - } - } -} \ No newline at end of file diff --git a/Netch/Forms/MainForm.Designer.cs b/Netch/Forms/MainForm.Designer.cs index 094641cc..bfdd9411 100644 --- a/Netch/Forms/MainForm.Designer.cs +++ b/Netch/Forms/MainForm.Designer.cs @@ -38,13 +38,9 @@ this.SubscribeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.ManageSubscribeLinksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.UpdateServersFromSubscribeLinksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.OptionsToolStripMenuItem = 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.updatePACToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.UninstallServiceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.removeNetchFirewallRulesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.HelpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -88,6 +84,7 @@ this.ProfileTable = new System.Windows.Forms.TableLayoutPanel(); this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); this.ButtomControlContainerControl = new System.Windows.Forms.ContainerControl(); + this.UpdateACLToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.MenuStrip.SuspendLayout(); this.ConfigurationGroupBox.SuspendLayout(); this.configLayoutPanel.SuspendLayout(); @@ -170,8 +167,7 @@ // this.SubscribeToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.ManageSubscribeLinksToolStripMenuItem, - this.UpdateServersFromSubscribeLinksToolStripMenuItem, - this.UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem}); + this.UpdateServersFromSubscribeLinksToolStripMenuItem}); this.SubscribeToolStripMenuItem.Margin = new System.Windows.Forms.Padding(0, 0, 0, 1); this.SubscribeToolStripMenuItem.Name = "SubscribeToolStripMenuItem"; this.SubscribeToolStripMenuItem.Size = new System.Drawing.Size(77, 21); @@ -180,32 +176,23 @@ // ManageSubscribeLinksToolStripMenuItem // this.ManageSubscribeLinksToolStripMenuItem.Name = "ManageSubscribeLinksToolStripMenuItem"; - this.ManageSubscribeLinksToolStripMenuItem.Size = new System.Drawing.Size(360, 22); + this.ManageSubscribeLinksToolStripMenuItem.Size = new System.Drawing.Size(294, 22); this.ManageSubscribeLinksToolStripMenuItem.Text = "Manage Subscribe Links"; this.ManageSubscribeLinksToolStripMenuItem.Click += new System.EventHandler(this.ManageSubscribeLinksToolStripMenuItem_Click); // // UpdateServersFromSubscribeLinksToolStripMenuItem // this.UpdateServersFromSubscribeLinksToolStripMenuItem.Name = "UpdateServersFromSubscribeLinksToolStripMenuItem"; - this.UpdateServersFromSubscribeLinksToolStripMenuItem.Size = new System.Drawing.Size(360, 22); + this.UpdateServersFromSubscribeLinksToolStripMenuItem.Size = new System.Drawing.Size(294, 22); this.UpdateServersFromSubscribeLinksToolStripMenuItem.Text = "Update Servers From Subscribe Links"; this.UpdateServersFromSubscribeLinksToolStripMenuItem.Click += new System.EventHandler(this.UpdateServersFromSubscribeLinksToolStripMenuItem_Click); // - // UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem - // - this.UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem.Name = "UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem"; - this.UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem.Size = new System.Drawing.Size(360, 22); - this.UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem.Text = "Update Servers From Subscribe Links With Proxy"; - this.UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem.Click += new System.EventHandler(this.UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem_Click); - // // OptionsToolStripMenuItem // this.OptionsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.OpenDirectoryToolStripMenuItem, this.CleanDNSCacheToolStripMenuItem, this.UpdateACLToolStripMenuItem, - this.updateACLWithProxyToolStripMenuItem, - this.updatePACToolStripMenuItem, this.UninstallServiceToolStripMenuItem, this.removeNetchFirewallRulesToolStripMenuItem}); this.OptionsToolStripMenuItem.Margin = new System.Windows.Forms.Padding(0, 0, 0, 1); @@ -227,27 +214,6 @@ this.CleanDNSCacheToolStripMenuItem.Text = "Clean DNS Cache"; this.CleanDNSCacheToolStripMenuItem.Click += new System.EventHandler(this.CleanDNSCacheToolStripMenuItem_Click); // - // UpdateACLToolStripMenuItem - // - this.UpdateACLToolStripMenuItem.Name = "UpdateACLToolStripMenuItem"; - this.UpdateACLToolStripMenuItem.Size = new System.Drawing.Size(243, 22); - this.UpdateACLToolStripMenuItem.Text = "Update ACL"; - this.UpdateACLToolStripMenuItem.Click += new System.EventHandler(this.updateACLToolStripMenuItem_Click); - // - // updateACLWithProxyToolStripMenuItem - // - this.updateACLWithProxyToolStripMenuItem.Name = "updateACLWithProxyToolStripMenuItem"; - this.updateACLWithProxyToolStripMenuItem.Size = new System.Drawing.Size(243, 22); - this.updateACLWithProxyToolStripMenuItem.Text = "Update ACL with proxy"; - this.updateACLWithProxyToolStripMenuItem.Click += new System.EventHandler(this.updateACLWithProxyToolStripMenuItem_Click); - // - // updatePACToolStripMenuItem - // - this.updatePACToolStripMenuItem.Name = "updatePACToolStripMenuItem"; - this.updatePACToolStripMenuItem.Size = new System.Drawing.Size(243, 22); - this.updatePACToolStripMenuItem.Text = "Update PAC"; - this.updatePACToolStripMenuItem.Click += new System.EventHandler(this.updatePACToolStripMenuItem_Click); - // // UninstallServiceToolStripMenuItem // this.UninstallServiceToolStripMenuItem.Name = "UninstallServiceToolStripMenuItem"; @@ -713,6 +679,13 @@ this.ButtomControlContainerControl.TabStop = false; this.ButtomControlContainerControl.Text = "groupBox1"; // + // UpdateACLToolStripMenuItem + // + this.UpdateACLToolStripMenuItem.Name = "UpdateACLToolStripMenuItem"; + this.UpdateACLToolStripMenuItem.Size = new System.Drawing.Size(243, 22); + this.UpdateACLToolStripMenuItem.Text = "Update ACL"; + this.UpdateACLToolStripMenuItem.Click += new System.EventHandler(this.updateACLToolStripMenuItem_Click); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); @@ -805,10 +778,7 @@ 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 updateACLWithProxyToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem UpdateServersFromSubscribeLinksToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem; private System.Windows.Forms.ToolStripStatusLabel UploadSpeedLabel; private System.Windows.Forms.ToolStripStatusLabel UsedBandwidthLabel; private System.Windows.Forms.ToolStripLabel NewVersionLabel; @@ -821,6 +791,6 @@ private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1; private System.Windows.Forms.ContainerControl ButtomControlContainerControl; - private System.Windows.Forms.ToolStripMenuItem updatePACToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem UpdateACLToolStripMenuItem; } } \ No newline at end of file diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index dc5de0db..4af55046 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -112,7 +112,7 @@ namespace Netch.Forms { // 检查订阅更新 if (Global.Settings.UpdateServersWhenOpened) - UpdateServersFromSubscribe(Global.Settings.UseProxyToUpdateSubscription).Wait(); + UpdateServersFromSubscribe().Wait(); // 打开软件时启动加速,产生开始按钮点击事件 if (Global.Settings.StartWhenOpened) @@ -275,33 +275,16 @@ namespace Netch.Forms private async void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e) { - Global.Settings.UseProxyToUpdateSubscription = false; await UpdateServersFromSubscribe(); } - private async void UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem_Click(object sender, EventArgs e) - { - Global.Settings.UseProxyToUpdateSubscription = true; - await UpdateServersFromSubscribe(true); - } - - private async Task UpdateServersFromSubscribe(bool useProxy = false) + private async Task UpdateServersFromSubscribe() { void DisableItems(bool v) { MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ProfileGroupBox.Enabled = ControlButton.Enabled = v; } - var server = ServerComboBox.SelectedItem as Server; - if (useProxy) - { - if (server == null) - { - MessageBoxX.Show(i18N.Translate("Please select a server first")); - return; - } - } - if (Global.Settings.SubscribeLink.Count <= 0) { MessageBoxX.Show(i18N.Translate("No subscription link")); @@ -310,22 +293,10 @@ namespace Netch.Forms StatusText(i18N.Translate("Starting update subscription")); DisableItems(false); + try { - string? proxyServer = null; - if (useProxy) - { - var mode = new Models.Mode - { - Remark = "ProxyUpdate", - Type = 5 - }; - - await MainController.StartAsync(server!, mode); - proxyServer = $"http://127.0.0.1:{Global.Settings.HTTPLocalPort}"; - } - - await Subscription.UpdateServersAsync(proxyServer); + await Subscription.UpdateServersAsync(); LoadServers(); Configuration.Save(); @@ -338,9 +309,6 @@ namespace Netch.Forms } finally { - if (useProxy) - await MainController.StopAsync(); - DisableItems(true); } } @@ -404,45 +372,18 @@ namespace Netch.Forms } } - private void updateACLWithProxyToolStripMenuItem_Click(object sender, EventArgs e) - { - UpdateACL(true); - } - private void updateACLToolStripMenuItem_Click(object sender, EventArgs e) { - UpdateACL(false); + UpdateACL(); } - private async void UpdateACL(bool useProxy) + private async void UpdateACL() { Enabled = false; StatusText(i18N.TranslateFormat("Updating {0}", "ACL")); try { - if (useProxy) - { - if (!(ServerComboBox.SelectedItem is Server server)) - { - MessageBoxX.Show(i18N.Translate("Please select a server first")); - return; - } - else - { - var mode = new Models.Mode - { - Remark = "ProxyUpdate", - Type = 5 - }; - - await MainController.StartAsync(server, mode); - } - } - var req = WebUtil.CreateRequest(Global.Settings.ACL); - if (useProxy) - req.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}"); - await WebUtil.DownloadFileAsync(req, Path.Combine(Global.NetchDir, Constants.UserACL)); NotifyTip(i18N.Translate("ACL updated successfully")); } @@ -452,36 +393,6 @@ namespace Netch.Forms Logging.Error("更新 ACL 失败!" + e); } finally - { - if (useProxy) - await MainController.StopAsync(); - - StatusText(); - Enabled = true; - } - } - - private async void updatePACToolStripMenuItem_Click(object sender, EventArgs eventArgs) - { - Enabled = false; - - StatusText(i18N.TranslateFormat("Updating {0}", "PAC")); - try - { - var req = WebUtil.CreateRequest(Global.Settings.PAC); - - var pac = Path.Combine(Global.NetchDir, "bin\\pac.txt"); - - await WebUtil.DownloadFileAsync(req, pac); - - NotifyTip(i18N.Translate("PAC updated successfully")); - } - catch (Exception e) - { - NotifyTip(i18N.Translate("PAC update failed") + "\n" + e.Message, info: false); - Logging.Error("更新 PAC 失败!" + e); - } - finally { StatusText(); Enabled = true; @@ -1078,9 +989,8 @@ namespace Netch.Forms EditServerPictureBox.Enabled = DeleteModePictureBox.Enabled = DeleteServerPictureBox.Enabled = enabled; // 启动需要禁用的控件 - UninstallServiceToolStripMenuItem.Enabled = UpdateACLToolStripMenuItem.Enabled = updateACLWithProxyToolStripMenuItem.Enabled = - updatePACToolStripMenuItem.Enabled = UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = - UpdateServersFromSubscribeLinksWithProxyToolStripMenuItem.Enabled = enabled; + UninstallServiceToolStripMenuItem.Enabled = + UpdateACLToolStripMenuItem.Enabled = UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = enabled; } _state = value; diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs index de27acf5..6bc7b70a 100644 --- a/Netch/Models/Mode.cs +++ b/Netch/Models/Mode.cs @@ -14,7 +14,7 @@ namespace Netch.Models public string? FullName { get; private set; } - public Mode(string? fullName = default) + public Mode(string? fullName) { _lazyRule = new Lazy>(ReadRules); if (fullName == null) @@ -35,7 +35,8 @@ namespace Netch.Models var typeResult = int.TryParse(split.ElementAtOrDefault(1), out var type); Type = typeResult ? type : 0; - // TODO throw NotSupportedModeTypeException + if (!ModeHelper.ModeTypes.Contains(Type)) + throw new NotSupportedException($"not support mode \"[{Type}]{Remark}\"."); var bypassChinaResult = int.TryParse(split.ElementAtOrDefault(2), out var bypassChina); BypassChina = this.ClientRouting() && bypassChinaResult && bypassChina == 1; diff --git a/Netch/Models/Setting.cs b/Netch/Models/Setting.cs index 7957dafa..1c1d28da 100644 --- a/Netch/Models/Setting.cs +++ b/Netch/Models/Setting.cs @@ -282,11 +282,6 @@ namespace Netch.Models /// public bool UpdateServersWhenOpened { get; set; } = false; - /// - /// 使用代理更新订阅 - /// - public bool UseProxyToUpdateSubscription { get; set; } = false; - public V2rayConfig V2RayConfig { get; set; } = new(); public bool? AlwaysStartPACServer { get; set; } diff --git a/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs b/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs index cdbb65f4..b9d7f8aa 100644 --- a/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs +++ b/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs @@ -74,7 +74,6 @@ namespace Netch.Servers.V2ray.Utils case 1: case 2: // directRuleObject.ip.Add("geoip:cn"); - break; default: directRuleObject.domain.Add("geosite:cn"); diff --git a/Netch/Utils/Bandwidth.cs b/Netch/Utils/Bandwidth.cs index c5e61462..499b27e4 100644 --- a/Netch/Utils/Bandwidth.cs +++ b/Netch/Utils/Bandwidth.cs @@ -69,9 +69,6 @@ namespace Netch.Utils { case null: break; - case HTTPController httpController: - instances.Add(httpController.PrivoxyController.Instance!); - break; case NFController _: instances.Add(Process.GetCurrentProcess()); break; diff --git a/Netch/Utils/ModeHelper.cs b/Netch/Utils/ModeHelper.cs index 7cd750cd..f4c78045 100644 --- a/Netch/Utils/ModeHelper.cs +++ b/Netch/Utils/ModeHelper.cs @@ -83,9 +83,9 @@ namespace Netch.Utils { Global.Modes.Add(new Mode(file)); } - catch (Exception) + catch (Exception e) { - // ignored + Logging.Warning($"Load mode \"{file}\" failed: {e.Message}"); } } catch @@ -120,13 +120,15 @@ namespace Netch.Utils }; case 1: case 2: - return server is Socks5 ; + return server is Socks5; default: return false; } } - public static IModeController? GetModeControllerByType(int type, out ushort? port, out string portName) + public static readonly int[] ModeTypes = {0, 1, 2, 6}; + + public static IModeController GetModeControllerByType(int type, out ushort? port, out string portName) { port = null; portName = string.Empty; @@ -139,14 +141,6 @@ namespace Netch.Utils case 1: case 2: return new TUNController(); - case 3: - case 5: - port = Global.Settings.HTTPLocalPort; - portName = "HTTP"; - StatusPortInfoText.HttpPort = (ushort) port; - return new HTTPController(); - case 4: - return null; case 6: return new PcapController(); default: