diff --git a/Netch/Controllers/Mode/NFController.cs b/Netch/Controllers/Mode/NFController.cs index 9d5f8106..3341deb1 100644 --- a/Netch/Controllers/Mode/NFController.cs +++ b/Netch/Controllers/Mode/NFController.cs @@ -41,14 +41,6 @@ namespace Netch.Controllers BinDriver = "bin\\" + BinDriver; } - public NFController() - { - Name = "Redirector"; - MainFile = "Redirector.exe"; - StartedKeywords.Add("Started"); - StoppedKeywords.AddRange(new[] {"Failed", "Unable"}); - } - public override bool Start(Server server, Mode mode) { Logging.Info("内置驱动版本: " + DriverVersion(BinDriver)); @@ -65,86 +57,30 @@ namespace Netch.Controllers return false; } - var processList = ""; - foreach (var proc in mode.Rule) - processList += proc + ","; - processList += "NTT.exe"; - - var argument = new StringBuilder(); - if (server.Type != "Socks5") + NativeMethods.aio_dial((int)NameList.TYPE_CLRNAME, ""); + foreach (var rule in mode.Rule) { - argument.Append($"-r 127.0.0.1:{Global.Settings.Socks5LocalPort} -p \"{processList}\""); + NativeMethods.aio_dial((int)NameList.TYPE_ADDNAME, rule); } - else + var result = DNS.Lookup(server.Hostname); + if (result == null) { - var result = DNS.Lookup(server.Hostname); - if (result == null) - { - Logging.Info("无法解析服务器 IP 地址"); - return false; - } + Logging.Info("无法解析服务器 IP 地址"); + return false; + } + NativeMethods.aio_dial((int)NameList.TYPE_TCPHOST, $"{result}:{server.Port}"); + NativeMethods.aio_dial((int)NameList.TYPE_UDPHOST, $"{result}:{server.Port}"); - argument.Append($"-r {result}:{server.Port} -p \"{processList}\""); - if (!string.IsNullOrWhiteSpace(server.Username) && !string.IsNullOrWhiteSpace(server.Password)) - argument.Append($" -username \"{server.Username}\" -password \"{server.Password}\""); + if (Global.Settings.ModifySystemDNS) + { + // 备份并替换系统 DNS + _sysDns = DNS.getSystemDns(); + string[] dns = { "1.1.1.1", "8.8.8.8" }; + DNS.SetDNS(dns); } - argument.Append($" -t {Global.Settings.RedirectorTCPPort}"); - - for (var i = 0; i < 2; i++) - { - State = State.Starting; - if (!StartInstanceAuto(argument.ToString())) continue; - if (Global.Settings.ModifySystemDNS) - { - //备份并替换系统DNS - _sysDns = DNS.getSystemDns(); - string[] dns = {"1.1.1.1", "8.8.8.8"}; - DNS.SetDNS(dns); - } - - return true; - } - - return false; - } - - private bool RestartService() - { - try - { - switch (NFService.Status) - { - // 启动驱动服务 - case ServiceControllerStatus.Running: - // 防止其他程序占用 重置 NF 百万连接数限制 - NFService.Stop(); - NFService.WaitForStatus(ServiceControllerStatus.Stopped); - Global.MainForm.StatusText(i18N.Translate("Starting netfilter2 Service")); - NFService.Start(); - break; - case ServiceControllerStatus.Stopped: - Global.MainForm.StatusText(i18N.Translate("Starting netfilter2 Service")); - NFService.Start(); - break; - } - } - catch (Exception e) - { - Logging.Error("启动驱动服务失败:\n" + e); - - var result = NFAPI.nf_registerDriver("netfilter2"); - if (result != NF_STATUS.NF_STATUS_SUCCESS) - { - Logging.Error($"注册驱动失败,返回值:{result}"); - return false; - } - - Logging.Info("注册驱动成功"); - } - - return true; + return NativeMethods.aio_init(); } public static string DriverVersion(string file) @@ -221,7 +157,8 @@ namespace Netch.Controllers //恢复系统DNS DNS.SetDNS(_sysDns); }); - StopInstance(); + + NativeMethods.aio_free(); } } } \ No newline at end of file diff --git a/Netch/NativeMethods.cs b/Netch/NativeMethods.cs index 8b53ac72..fd4847dc 100644 --- a/Netch/NativeMethods.cs +++ b/Netch/NativeMethods.cs @@ -2,6 +2,18 @@ namespace Netch { + public enum NameList : int + { + TYPE_FILTERLOOPBACK, + TYPE_FILTERTCP, + TYPE_FILTERUDP, + TYPE_TCPHOST, + TYPE_UDPHOST, + TYPE_ADDNAME, + TYPE_BYPNAME, + TYPE_CLRNAME + } + public static class NativeMethods { /// @@ -66,5 +78,20 @@ namespace Netch [DllImport("dnsapi", EntryPoint = "DnsFlushResolverCache")] public static extern uint FlushDNSResolverCache(); + + [DllImport("bin\\Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool aio_dial(int name, [MarshalAs(UnmanagedType.LPWStr)] string value); + + [DllImport("bin\\Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool aio_init(); + + [DllImport("bin\\Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool aio_free(); + + [DllImport("bin\\Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern ulong aio_getUP(); + + [DllImport("bin\\Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern ulong aio_getDL(); } } \ No newline at end of file diff --git a/Netch/PostBuild.bat b/Netch/PostBuild.bat index d97aaa87..c58db01d 100644 --- a/Netch/PostBuild.bat +++ b/Netch/PostBuild.bat @@ -35,6 +35,8 @@ XCOPY /s /Y %SolutionDir%binaries %TargetDir%bin\ >NUL XCOPY /s /Y %SolutionDir%translations\i18n %TargetDir%i18n\ >NUL XCOPY /s /Y %SolutionDir%modes\mode %TargetDir%mode\ >NUL +MOVE /Y %TargetDir%bin\Core.bin %TargetDir% >NUL + DEL /f %TargetDir%*.config >NUL 2>&1 DEL /f %TargetDir%*.pdb >NUL 2>&1 RD /s /Q %TargetDir%x86 >NUL 2>&1 diff --git a/binaries b/binaries index c9fd269f..79341341 160000 --- a/binaries +++ b/binaries @@ -1 +1 @@ -Subproject commit c9fd269fb88cd3301e742e8b4d4740ee234455e2 +Subproject commit 79341341c16dc65a3322ee7bc2efcd558d8ce575 diff --git a/modes b/modes index f0327e93..20239a17 160000 --- a/modes +++ b/modes @@ -1 +1 @@ -Subproject commit f0327e93991856680b4b5f84f02a5401467ceaf5 +Subproject commit 20239a17aae4ce1de13a44f5b48156133cb9f879