From b5c7ca4c1a43fbafc053b9f04f9a657c2b90266b Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Tue, 22 Dec 2020 16:59:32 +0800 Subject: [PATCH] Refactor NFController SetServer --- Netch/Controllers/MainController.cs | 15 ++++++++++++ Netch/Controllers/NFController.cs | 37 ++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index 101ef86a..d0cbe4be 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -18,14 +18,29 @@ namespace Netch.Controllers private set => _serverController = value; } + public static IServerController UdpServerController + { + get => _udpServerController ?? _serverController; + set => _udpServerController = value; + } + /// TCP or Both Server public static Server Server; + + public static Server UdpServer + { + get => _udpServer ?? Server; + set => _udpServer = value; + } + + private static Server _udpServer; public static IModeController ModeController { get; private set; } public static bool NttTested; private static readonly NTTController NTTController = new NTTController(); private static IServerController _serverController; + private static IServerController _udpServerController; /// /// 启动 diff --git a/Netch/Controllers/NFController.cs b/Netch/Controllers/NFController.cs index 0e26aa02..57a37fee 100644 --- a/Netch/Controllers/NFController.cs +++ b/Netch/Controllers/NFController.cs @@ -56,16 +56,16 @@ namespace Netch.Controllers aio_dial((int) NameList.TYPE_FILTERLOOPBACK, "false"); aio_dial((int) NameList.TYPE_FILTERTCP, "true"); - aio_dial((int)NameList.TYPE_TCPLISN, Global.Settings.RedirectorTCPPort.ToString()); + aio_dial((int) NameList.TYPE_TCPLISN, Global.Settings.RedirectorTCPPort.ToString()); if (Global.Settings.ProcessNoProxyForUdp) { - aio_dial((int)NameList.TYPE_FILTERUDP, "false"); - SetServer(MainController.ServerController, PortType.TCP); + aio_dial((int) NameList.TYPE_FILTERUDP, "false"); + SetServer(PortType.TCP); } else { - aio_dial((int)NameList.TYPE_FILTERUDP, "true"); - SetServer(MainController.ServerController, PortType.Both); + aio_dial((int) NameList.TYPE_FILTERUDP, "true"); + SetServer(PortType.Both); } if (!CheckRule(mode.FullRule, out var list)) @@ -184,18 +184,33 @@ namespace Netch.Controllers return InstallDriver(); } - private void SetServer(in IServerController controller, in PortType portType) + private void SetServer(in PortType portType) { if (portType == PortType.Both) { - SetServer(controller, PortType.TCP); - SetServer(controller, PortType.UDP); + SetServer(PortType.TCP); + SetServer(PortType.UDP); return; } - var offset = portType == PortType.UDP ? UdpNameListOffset : 0; + int offset; + Server server; + IServerController controller; - if (MainController.Server is Socks5 socks5) + if (portType == PortType.UDP) + { + offset = UdpNameListOffset; + server = MainController.UdpServer; + controller = MainController.UdpServerController; + } + else + { + offset = 0; + server = MainController.Server; + controller = MainController.ServerController; + } + + if (server is Socks5 socks5) { aio_dial((int) NameList.TYPE_TCPTYPE + offset, "Socks5"); aio_dial((int) NameList.TYPE_TCPHOST + offset, $"{socks5.AutoResolveHostname()}:{socks5.Port}"); @@ -203,7 +218,7 @@ namespace Netch.Controllers aio_dial((int) NameList.TYPE_TCPPASS + offset, socks5.Password ?? string.Empty); aio_dial((int) NameList.TYPE_TCPMETH + offset, string.Empty); } - else if (MainController.Server is Shadowsocks shadowsocks && !shadowsocks.HasPlugin() && Global.Settings.RedirectorSS) + else if (server is Shadowsocks shadowsocks && !shadowsocks.HasPlugin() && Global.Settings.RedirectorSS) { aio_dial((int) NameList.TYPE_TCPTYPE + offset, "Shadowsocks"); aio_dial((int) NameList.TYPE_TCPHOST + offset, $"{shadowsocks.AutoResolveHostname()}:{shadowsocks.Port}");