diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index 3a66eee1..dba9f92d 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -21,31 +21,23 @@ namespace Netch.Controllers case Models.Server.ServerType.Socks: break; case Models.Server.ServerType.Shadowsocks: - { - if (m.Type == Models.Mode.ModeType.ProcessMode) - { - var node = s as Models.Server.Shadowsocks.Shadowsocks; - if (String.IsNullOrEmpty(node.OBFS)) - { - break; - } - } - - this.NodeController = new Server.ShadowsocksController(); - } + this.NodeController = new Server.ShadowsocksController(); break; case Models.Server.ServerType.ShadowsocksR: this.NodeController = new Server.ShadowsocksRController(); break; + case Models.Server.ServerType.WireGuard: + this.NodeController = new Server.WireGuardController(); + break; case Models.Server.ServerType.Trojan: this.NodeController = new Server.TrojanController(); break; - case Models.Server.ServerType.VLess: - this.NodeController = new Server.VLessController(); - break; case Models.Server.ServerType.VMess: this.NodeController = new Server.VMessController(); break; + case Models.Server.ServerType.VLess: + this.NodeController = new Server.VLessController(); + break; default: Global.Logger.Error($"未知的节点类型:{s.Type}"); @@ -63,23 +55,17 @@ namespace Netch.Controllers switch (m.Type) { case Models.Mode.ModeType.ProcessMode: - this.ModeController = new Mode.RedirectorController(); + this.ModeController = new Mode.ProcessController(); break; case Models.Mode.ModeType.ShareMode: this.ModeController = new Mode.ShareController(); break; - case Models.Mode.ModeType.TapMode: - this.ModeController = new Mode.TapController(); - break; case Models.Mode.ModeType.TunMode: this.ModeController = new Mode.TunController(); break; case Models.Mode.ModeType.WebMode: this.ModeController = new Mode.WebController(); break; - case Models.Mode.ModeType.WmpMode: - this.ModeController = new Mode.WmpController(); - break; default: Global.Logger.Error($"未知的模式类型:{s.Type}"); diff --git a/Netch/Controllers/Mode/LSPController.cs b/Netch/Controllers/Mode/LSPController.cs deleted file mode 100644 index ada6865f..00000000 --- a/Netch/Controllers/Mode/LSPController.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace Netch.Controllers.Mode -{ - public class LSPController : Interface.IController - { - public bool Create(Models.Server.Server s, Models.Mode.Mode m) - { - throw new NotImplementedException(); - } - - public bool Delete() - { - throw new NotImplementedException(); - } - } -} diff --git a/Netch/Controllers/Mode/ProcessController.cs b/Netch/Controllers/Mode/ProcessController.cs new file mode 100644 index 00000000..096bde59 --- /dev/null +++ b/Netch/Controllers/Mode/ProcessController.cs @@ -0,0 +1,96 @@ +using System; +using System.Runtime.InteropServices; + +namespace Netch.Controllers.Mode +{ + public class ProcessController : Interface.IController + { + private enum NameList : int + { + AIO_FILTERLOOPBACK, + AIO_FILTERINTRANET, + AIO_FILTERICMP, + AIO_FILTERTCP, + AIO_FILTERUDP, + + AIO_TGTHOST, + AIO_TGTPORT, + AIO_TGTUSER, + AIO_TGTPASS, + + AIO_CLRNAME, + AIO_ADDNAME, + AIO_BYPNAME + } + + private static class Methods + { + [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool aio_dial(NameList name, [MarshalAs(UnmanagedType.LPWStr)] string value); + + [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool aio_init(); + + [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern void aio_free(); + + [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern ulong aio_getUP(); + + [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern ulong aio_getDL(); + } + + public bool Create(Models.Server.Server s, Models.Mode.Mode m) + { + Global.Logger.Info(String.Format("{0:x} Redirector.bin", Utils.FileHelper.Checksum("bin\\Redirector.bin"))); + + var mode = m as Models.Mode.ProcessMode.ProcessMode; + Methods.aio_dial(NameList.AIO_FILTERLOOPBACK, mode.Loopback ? "true" : "false"); + Methods.aio_dial(NameList.AIO_FILTERINTRANET, mode.Intranet ? "true" : "false"); + Methods.aio_dial(NameList.AIO_FILTERTCP, mode.TCP ? "true" : "false"); + Methods.aio_dial(NameList.AIO_FILTERUDP, mode.UDP ? "true" : "false"); + + Methods.aio_dial(NameList.AIO_CLRNAME, ""); + Methods.aio_dial(NameList.AIO_BYPNAME, AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "\\\\")); + for (int i = 0; i < mode.BypassList.Count; i++) if (!Methods.aio_dial(NameList.AIO_BYPNAME, mode.BypassList[i])) return false; + for (int i = 0; i < mode.HandleList.Count; i++) if (!Methods.aio_dial(NameList.AIO_ADDNAME, mode.HandleList[i])) return false; + + switch (s.Type) + { + case Models.Server.ServerType.Socks: + { + var node = s as Models.Server.Socks.Socks; + Methods.aio_dial(NameList.AIO_TGTHOST, node.Resolve()); + Methods.aio_dial(NameList.AIO_TGTPORT, node.Port.ToString()); + + if (!String.IsNullOrEmpty(node.Username)) + { + Methods.aio_dial(NameList.AIO_TGTUSER, node.Username); + } + + if (!String.IsNullOrEmpty(node.Password)) + { + Methods.aio_dial(NameList.AIO_TGTPASS, node.Password); + } + } + break; + default: + { + Methods.aio_dial(NameList.AIO_TGTHOST, "127.0.0.1"); + Methods.aio_dial(NameList.AIO_TGTPORT, Global.Config.Ports.Socks.ToString()); + } + break; + } + + return Methods.aio_init(); + } + + public bool Delete() + { + Methods.aio_free(); + + return true; + } + } +} diff --git a/Netch/Controllers/Mode/RedirectorController.cs b/Netch/Controllers/Mode/RedirectorController.cs deleted file mode 100644 index 5bc22a4f..00000000 --- a/Netch/Controllers/Mode/RedirectorController.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -namespace Netch.Controllers.Mode -{ - public class RedirectorController : Interface.IController - { - private enum NameList : int - { - TYPE_FILTERLOOPBACK, - TYPE_FILTERTCP, - TYPE_FILTERUDP, - TYPE_CLRNAME, - TYPE_ADDNAME, - TYPE_BYPNAME, - TYPE_DNSHOST, - TYPE_TCPLISN, - TYPE_TCPTYPE, - TYPE_TCPHOST, - TYPE_TCPUSER, - TYPE_TCPPASS, - TYPE_TCPMETH, - TYPE_TCPPROT, - TYPE_TCPPRPA, - TYPE_TCPOBFS, - TYPE_TCPOBPA, - TYPE_UDPLISN, - TYPE_UDPTYPE, - TYPE_UDPHOST, - TYPE_UDPUSER, - TYPE_UDPPASS, - TYPE_UDPMETH, - TYPE_UDPPROT, - TYPE_UDPPRPA, - TYPE_UDPOBFS, - TYPE_UDPOBPA - } - - private static class Methods - { - [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool aio_dial(NameList name, [MarshalAs(UnmanagedType.LPWStr)] string value); - - [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool aio_init(); - - [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool aio_free(); - - [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern ulong aio_getUP(); - - [DllImport("Redirector.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern ulong aio_getDL(); - } - - public bool Create(Models.Server.Server s, Models.Mode.Mode m) - { - Global.Logger.Info(String.Format("{0:x} Redirector.bin", Utils.FileHelper.Checksum("bin\\Redirector.bin"))); - - var mode = m as Models.Mode.ProcessMode.ProcessMode; - Methods.aio_dial(NameList.TYPE_FILTERLOOPBACK, mode.Loopback ? "true" : "false"); - Methods.aio_dial(NameList.TYPE_FILTERTCP, mode.TCP ? "true" : "false"); - Methods.aio_dial(NameList.TYPE_FILTERUDP, mode.UDP ? "true" : "false"); - - Methods.aio_dial(NameList.TYPE_CLRNAME, ""); - Methods.aio_dial(NameList.TYPE_BYPNAME, AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "\\\\")); - for (int i = 0; i < mode.HandleList.Count; i++) if (!Methods.aio_dial(NameList.TYPE_ADDNAME, mode.HandleList[i])) return false; - for (int i = 0; i < mode.BypassList.Count; i++) if (!Methods.aio_dial(NameList.TYPE_BYPNAME, mode.BypassList[i])) return false; - - Methods.aio_dial(NameList.TYPE_TCPLISN, Global.Config.Ports.Redir.ToString()); - Methods.aio_dial(NameList.TYPE_UDPLISN, Global.Config.Ports.Redir.ToString()); - - switch (s.Type) - { - case Models.Server.ServerType.Socks: - { - var node = s as Models.Server.Socks.Socks; - Methods.aio_dial(NameList.TYPE_TCPTYPE, "Socks"); - Methods.aio_dial(NameList.TYPE_UDPTYPE, "Socks"); - Methods.aio_dial(NameList.TYPE_TCPHOST, $"{node.Resolve()}:{node.Port}"); - Methods.aio_dial(NameList.TYPE_UDPHOST, $"{node.Resolve()}:{node.Port}"); - - if (!String.IsNullOrEmpty(node.Username)) - { - Methods.aio_dial(NameList.TYPE_TCPUSER, node.Username); - Methods.aio_dial(NameList.TYPE_UDPUSER, node.Username); - } - - if (!String.IsNullOrEmpty(node.Password)) - { - Methods.aio_dial(NameList.TYPE_TCPPASS, node.Password); - Methods.aio_dial(NameList.TYPE_UDPPASS, node.Password); - } - } - break; - case Models.Server.ServerType.Shadowsocks: - { - var node = s as Models.Server.Shadowsocks.Shadowsocks; - if (String.IsNullOrEmpty(node.OBFS)) - { - Methods.aio_dial(NameList.TYPE_TCPTYPE, "Shadowsocks"); - Methods.aio_dial(NameList.TYPE_UDPTYPE, "Shadowsocks"); - Methods.aio_dial(NameList.TYPE_TCPHOST, $"{node.Resolve()}:{node.Port}"); - Methods.aio_dial(NameList.TYPE_UDPHOST, $"{node.Resolve()}:{node.Port}"); - Methods.aio_dial(NameList.TYPE_TCPPASS, node.Passwd); - Methods.aio_dial(NameList.TYPE_UDPPASS, node.Passwd); - Methods.aio_dial(NameList.TYPE_TCPMETH, node.Method); - Methods.aio_dial(NameList.TYPE_UDPMETH, node.Method); - } - else - { - Methods.aio_dial(NameList.TYPE_TCPTYPE, "Socks"); - Methods.aio_dial(NameList.TYPE_UDPTYPE, "Socks"); - Methods.aio_dial(NameList.TYPE_TCPHOST, $"127.0.0.1:{Global.Config.Ports.Socks}"); - Methods.aio_dial(NameList.TYPE_UDPHOST, $"127.0.0.1:{Global.Config.Ports.Socks}"); - } - } - break; - default: - { - Methods.aio_dial(NameList.TYPE_TCPTYPE, "Socks"); - Methods.aio_dial(NameList.TYPE_UDPTYPE, "Socks"); - Methods.aio_dial(NameList.TYPE_TCPHOST, $"127.0.0.1:{Global.Config.Ports.Socks}"); - Methods.aio_dial(NameList.TYPE_UDPHOST, $"127.0.0.1:{Global.Config.Ports.Socks}"); - } - break; - } - - return Methods.aio_init(); - } - - public bool Delete() - { - return Methods.aio_free(); - } - } -} diff --git a/Netch/Controllers/Mode/TapController.cs b/Netch/Controllers/Mode/TapController.cs deleted file mode 100644 index 7ae6a4e2..00000000 --- a/Netch/Controllers/Mode/TapController.cs +++ /dev/null @@ -1,239 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using System.Management; -using System.Net; -using System.Net.NetworkInformation; -using System.Net.Sockets; -using System.Runtime.InteropServices; - -namespace Netch.Controllers.Mode -{ - public class TapController : Interface.IController - { - private enum NameList : int - { - TYPE_BYPBIND, - TYPE_BYPLIST, - TYPE_DNSADDR, - TYPE_ADAPMTU, - TYPE_TCPREST, - TYPE_TCPTYPE, - TYPE_TCPHOST, - TYPE_TCPUSER, - TYPE_TCPPASS, - TYPE_TCPMETH, - TYPE_TCPPROT, - TYPE_TCPPRPA, - TYPE_TCPOBFS, - TYPE_TCPOBPA, - TYPE_UDPREST, - TYPE_UDPTYPE, - TYPE_UDPHOST, - TYPE_UDPUSER, - TYPE_UDPPASS, - TYPE_UDPMETH, - TYPE_UDPPROT, - TYPE_UDPPRPA, - TYPE_UDPOBFS, - TYPE_UDPOBPA - } - - private static class Methods - { - [DllImport("tap2socks.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool tap_dial(NameList name, string value); - - [DllImport("tap2socks.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool tap_init(); - - [DllImport("tap2socks.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool tap_free(); - - [DllImport("tap2socks.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern string tap_name(); - - [DllImport("tap2socks.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern ulong tap_getUP(); - - [DllImport("tap2socks.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern ulong tap_getDL(); - } - - private Tools.TunTap.Outbound Outbound = new(); - private Interface.IController DNSController; - - private bool AssignInterface() - { - var index = Utils.RouteHelper.GetInterfaceIndexByDescription(Methods.tap_name()); - - var address = Global.Config.TunMode.Network.Split('/')[0]; - var netmask = byte.Parse(Global.Config.TunMode.Network.Split('/')[1]); - if (!Utils.RouteHelper.CreateUnicastIP(AddressFamily.InterNetwork, address, netmask, index)) - { - return false; - } - - NetworkInterface adapter = Utils.RouteHelper.GetInterfaceByIndex(index); - if (adapter == null) - { - return false; - } - - using (var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration")) - { - using var ins = wmi.GetInstances(); - var ada = ins.Cast().First(m => m["Description"].ToString() == adapter.Description); - - var dns = new[] { "127.0.0.1" }; - if (Global.Config.TunMode.DNS != "aiodns") - { - dns[0] = Global.Config.TunMode.DNS; - } - - using var ord = wmi.GetMethodParameters("SetDNSServerSearchOrder"); - ord["DNSServerSearchOrder"] = dns; - - ada.InvokeMethod("SetDNSServerSearchOrder", ord, null); - } - - return true; - } - - private bool CreateServerRoute(Models.Server.Server s) - { - var addr = Utils.DNS.Fetch(s.Host); - if (addr == IPAddress.Any) - { - return false; - } - - if (addr.AddressFamily == AddressFamily.InterNetworkV6) - { - return true; - } - - return Utils.RouteHelper.CreateRoute(AddressFamily.InterNetwork, addr.ToString(), 32, this.Outbound.Gateway.ToString(), this.Outbound.Index); - } - - private bool CreateHandleRoute(Models.Mode.TunMode.TunMode mode) - { - var index = Utils.RouteHelper.GetInterfaceIndexByDescription(Methods.tap_name()); - - for (int i = 0; i < mode.HandleList.Count; i++) - { - var address = mode.HandleList[i].Split('/')[0]; - var netmask = byte.Parse(mode.HandleList[i].Split('/')[1]); - - Utils.RouteHelper.CreateRoute(AddressFamily.InterNetwork, address, netmask, Global.Config.TunMode.Gateway, index); - } - - return true; - } - - public bool Create(Models.Server.Server s, Models.Mode.Mode m) - { - Global.Logger.Info(String.Format("{0:x} tap2socks.bin", Utils.FileHelper.Checksum("bin\\tap2socks.bin"))); - - if (!this.Outbound.Get()) - { - Global.Logger.Error(String.Format("Failed to fetch outbound")); - - return false; - } - - Methods.tap_dial(NameList.TYPE_BYPBIND, this.Outbound.Address.ToString()); - - var mode = m as Models.Mode.TunMode.TunMode; - if (mode.BypassList.Count > 0) - { - if (File.Exists("ipcidr.txt")) - { - File.Delete("ipcidr.txt"); - } - File.WriteAllLines("ipcidr.txt", mode.BypassList); - - Methods.tap_dial(NameList.TYPE_BYPLIST, "ipcidr.txt"); - } - else - { - Methods.tap_dial(NameList.TYPE_BYPLIST, "disabled"); - } - - Methods.tap_dial(NameList.TYPE_DNSADDR, (Global.Config.TunMode.DNS == "aiodns") ? "127.0.0.1" : Global.Config.TunMode.DNS); - Methods.tap_dial(NameList.TYPE_TCPREST, ""); - Methods.tap_dial(NameList.TYPE_UDPREST, ""); - - switch (s.Type) - { - case Models.Server.ServerType.Socks: - { - var node = s as Models.Server.Socks.Socks; - - Methods.tap_dial(NameList.TYPE_TCPTYPE, "Socks"); - Methods.tap_dial(NameList.TYPE_UDPTYPE, "Socks"); - Methods.tap_dial(NameList.TYPE_TCPHOST, $"{node.Resolve()}:{node.Port}"); - Methods.tap_dial(NameList.TYPE_UDPHOST, $"{node.Resolve()}:{node.Port}"); - - if (!String.IsNullOrEmpty(node.Username)) - { - Methods.tap_dial(NameList.TYPE_TCPUSER, node.Username); - Methods.tap_dial(NameList.TYPE_UDPUSER, node.Username); - } - - if (!String.IsNullOrEmpty(node.Password)) - { - Methods.tap_dial(NameList.TYPE_TCPPASS, node.Password); - Methods.tap_dial(NameList.TYPE_UDPPASS, node.Password); - } - } - break; - default: - Methods.tap_dial(NameList.TYPE_TCPTYPE, "Socks"); - Methods.tap_dial(NameList.TYPE_TCPHOST, $"127.0.0.1:{Global.Config.Ports.Socks}"); - Methods.tap_dial(NameList.TYPE_UDPTYPE, "Socks"); - Methods.tap_dial(NameList.TYPE_UDPHOST, $"127.0.0.1:{Global.Config.Ports.Socks}"); - break; - } - - if (!Methods.tap_init()) - { - return false; - } - - this.DNSController = new Other.DNS.AioDNSController(); - if (!this.DNSController.Create(s, m)) - { - return false; - } - - if (!this.AssignInterface()) - { - return false; - } - - if (!this.CreateServerRoute(s)) - { - return false; - } - - if (!this.CreateHandleRoute(mode)) - { - return false; - } - - if (File.Exists("ipcidr.txt")) - { - File.Delete("ipcidr.txt"); - } - return true; - } - - public bool Delete() - { - this.DNSController?.Delete(); - - return Methods.tap_free(); - } - } -} diff --git a/Netch/Controllers/Mode/TunController.cs b/Netch/Controllers/Mode/TunController.cs index e6096088..86571283 100644 --- a/Netch/Controllers/Mode/TunController.cs +++ b/Netch/Controllers/Mode/TunController.cs @@ -137,7 +137,7 @@ namespace Netch.Controllers.Mode if (!this.Outbound.Get()) { - Global.Logger.Error(String.Format("Failed to fetch outbound")); + Global.Logger.Error("Fetch outbound adapter failed"); return false; } @@ -201,7 +201,15 @@ namespace Netch.Controllers.Mode return false; } - this.DNSController = new Other.DNS.AioDNSController(); + if (Global.Config.Generic.AioDNS) + { + this.DNSController = new Other.DNS.AioDNSController(); + } + else + { + this.DNSController = new Other.DNS.DNSProxyController(); + } + if (!this.DNSController.Create(s, m)) { return false; diff --git a/Netch/Controllers/Mode/WinDivertController.cs b/Netch/Controllers/Mode/WinDivertController.cs deleted file mode 100644 index a7d8bfc1..00000000 --- a/Netch/Controllers/Mode/WinDivertController.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace Netch.Controllers.Mode -{ - public class WinDivertController : Interface.IController - { - public bool Create(Models.Server.Server s, Models.Mode.Mode m) - { - throw new NotImplementedException(); - } - - public bool Delete() - { - throw new NotImplementedException(); - } - } -} diff --git a/Netch/Controllers/Mode/WmpController.cs b/Netch/Controllers/Mode/WmpController.cs deleted file mode 100644 index c534a1c1..00000000 --- a/Netch/Controllers/Mode/WmpController.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Netch.Controllers.Mode -{ - public class WmpController : Interface.IController - { - public bool Create(Models.Server.Server s, Models.Mode.Mode m) - { - throw new System.NotImplementedException(); - } - - public bool Delete() - { - throw new System.NotImplementedException(); - } - } -} diff --git a/Netch/Controllers/Other/DNS/AioDNSController.cs b/Netch/Controllers/Other/DNS/AioDNSController.cs index cc506b16..9241dc1d 100644 --- a/Netch/Controllers/Other/DNS/AioDNSController.cs +++ b/Netch/Controllers/Other/DNS/AioDNSController.cs @@ -2,43 +2,49 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Runtime.InteropServices; namespace Netch.Controllers.Other.DNS { public class AioDNSController : Interface.IController { - private Tools.Guard Guard = new() + private enum NameList : int { - StartInfo = new ProcessStartInfo() - { - FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\aiodns.exe"), - WorkingDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"), - CreateNoWindow = true, - UseShellExecute = false, - WindowStyle = ProcessWindowStyle.Hidden - }, - JudgmentStarted = new List() - { - "Started" - }, - JudgmentStopped = new List() - { - "[aiodns][main]" - }, - AutoRestart = true - }; + TYPE_REST, + TYPE_LIST, + TYPE_LISN, + TYPE_CDNS, + TYPE_ODNS + } + + private static class Methods + { + [DllImport("aiodns.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool aiodns_dial(NameList name, string value); + + [DllImport("aiodns.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool aiodns_init(); + + [DllImport("aiodns.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern void aiodns_free(); + } public bool Create(Models.Server.Server s, Models.Mode.Mode m) { - Global.Logger.Info(String.Format("{0:x} aiodns.exe", Utils.FileHelper.Checksum("bin\\aiodns.exe"))); + Global.Logger.Info(String.Format("{0:x} aiodns.bin", Utils.FileHelper.Checksum("bin\\aiodns.bin"))); - this.Guard.StartInfo.Arguments = String.Format("-c {} -l {} -cdns {} -odns {}", ".\\aiodns.conf", ":53", Global.Config.AioDNS.ChinaDNS, Global.Config.AioDNS.OtherDNS); - return this.Guard.Create(); + Methods.aiodns_dial(NameList.TYPE_REST, ""); + Methods.aiodns_dial(NameList.TYPE_LIST, "chnsite.txt"); + Methods.aiodns_dial(NameList.TYPE_LISN, Global.Config.AioDNS.ListenPort.ToString()); + Methods.aiodns_dial(NameList.TYPE_CDNS, Global.Config.AioDNS.ChinaDNS); + Methods.aiodns_dial(NameList.TYPE_ODNS, Global.Config.AioDNS.OtherDNS); + + return Methods.aiodns_init(); } public bool Delete() { - this.Guard.Delete(); + Methods.aiodns_free(); return true; } diff --git a/Netch/Controllers/Other/DNS/CoreDNSController.cs b/Netch/Controllers/Other/DNS/CoreDNSController.cs deleted file mode 100644 index fbb3e140..00000000 --- a/Netch/Controllers/Other/DNS/CoreDNSController.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; - -namespace Netch.Controllers.Other.DNS -{ - public class CoreDNSController : Interface.IController - { - private Tools.Guard Guard = new() - { - StartInfo = new ProcessStartInfo() - { - FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\CoreDNS.exe"), - WorkingDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"), - CreateNoWindow = true, - UseShellExecute = false, - WindowStyle = ProcessWindowStyle.Hidden - }, - JudgmentStarted = new List() - { - }, - JudgmentStopped = new List() - { - }, - AutoRestart = true - }; - - public bool Create(Models.Server.Server s, Models.Mode.Mode m) - { - throw new NotImplementedException(); - } - - public bool Delete() - { - throw new NotImplementedException(); - } - } -} diff --git a/Netch/Controllers/Other/DNS/DNSController.cs b/Netch/Controllers/Other/DNS/DNSProxyController.cs similarity index 91% rename from Netch/Controllers/Other/DNS/DNSController.cs rename to Netch/Controllers/Other/DNS/DNSProxyController.cs index 2e8d8eeb..c00bfd71 100644 --- a/Netch/Controllers/Other/DNS/DNSController.cs +++ b/Netch/Controllers/Other/DNS/DNSProxyController.cs @@ -5,7 +5,7 @@ using System.IO; namespace Netch.Controllers.Other.DNS { - public class DNSController : Interface.IController + public class DNSProxyController : Interface.IController { private Tools.Guard Guard = new() { diff --git a/Netch/Controllers/Other/DNS/UnboundController.cs b/Netch/Controllers/Other/DNS/UnboundController.cs deleted file mode 100644 index d72d79d8..00000000 --- a/Netch/Controllers/Other/DNS/UnboundController.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; - -namespace Netch.Controllers.Other.DNS -{ - public class UnboundController : Interface.IController - { - private Tools.Guard Guard = new() - { - StartInfo = new ProcessStartInfo() - { - FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\Unbound.exe"), - WorkingDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"), - CreateNoWindow = true, - UseShellExecute = false, - WindowStyle = ProcessWindowStyle.Hidden - }, - JudgmentStarted = new List() - { - }, - JudgmentStopped = new List() - { - }, - AutoRestart = true - }; - - public bool Create(Models.Server.Server s, Models.Mode.Mode m) - { - throw new NotImplementedException(); - } - - public bool Delete() - { - throw new NotImplementedException(); - } - } -} diff --git a/Netch/Controllers/Other/Web/PrivoxyController.cs b/Netch/Controllers/Other/Web/PrivoxyController.cs deleted file mode 100644 index 4c00e9be..00000000 --- a/Netch/Controllers/Other/Web/PrivoxyController.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace Netch.Controllers.Other.Web -{ - public class PrivoxyController : Interface.IController - { - public bool Create(Models.Server.Server s, Models.Mode.Mode m) - { - throw new NotImplementedException(); - } - - public bool Delete() - { - throw new NotImplementedException(); - } - } -} diff --git a/Netch/Controllers/Server/ClashController.cs b/Netch/Controllers/Server/ClashController.cs deleted file mode 100644 index 76043ee1..00000000 --- a/Netch/Controllers/Server/ClashController.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace Netch.Controllers.Server -{ - public class ClashController : Interface.IController - { - public bool Create(Models.Server.Server s, Models.Mode.Mode m) - { - throw new NotImplementedException(); - } - - public bool Delete() - { - throw new NotImplementedException(); - } - } -} diff --git a/Netch/Controllers/Server/HTTPController.cs b/Netch/Controllers/Server/WireGuardController.cs similarity index 62% rename from Netch/Controllers/Server/HTTPController.cs rename to Netch/Controllers/Server/WireGuardController.cs index 02dadc06..40083e58 100644 --- a/Netch/Controllers/Server/HTTPController.cs +++ b/Netch/Controllers/Server/WireGuardController.cs @@ -1,8 +1,12 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace Netch.Controllers.Server { - public class HTTPController : Interface.IController + public class WireGuardController : Interface.IController { public bool Create(Models.Server.Server s, Models.Mode.Mode m) { diff --git a/Netch/Models/Config/AioDNS.cs b/Netch/Models/Config/AioDNS.cs index 6e6ed104..fa30879c 100644 --- a/Netch/Models/Config/AioDNS.cs +++ b/Netch/Models/Config/AioDNS.cs @@ -2,14 +2,22 @@ { public class AioDNS { + /// + /// 监听地址 + /// + [Newtonsoft.Json.JsonProperty("listenport")] + public ushort ListenPort = 53; + /// /// 国内 DNS 地址 /// + [Newtonsoft.Json.JsonProperty("chinadns")] public string ChinaDNS = "tcp://119.29.29.29:53"; /// /// 国外 DNS 地址 /// + [Newtonsoft.Json.JsonProperty("otherdns")] public string OtherDNS = "tls://1.1.1.1:853"; } } diff --git a/Netch/Models/Config/Config.cs b/Netch/Models/Config/Config.cs index fbf0e65b..4dba8154 100644 --- a/Netch/Models/Config/Config.cs +++ b/Netch/Models/Config/Config.cs @@ -34,12 +34,6 @@ namespace Netch.Models.Config [Newtonsoft.Json.JsonProperty("sharemode")] public ShareMode ShareMode = new(); - /// - /// TapMode 配置 - /// - [Newtonsoft.Json.JsonProperty("tapmode")] - public TapMode TapMode = new(); - /// /// TunMode 配置 /// @@ -52,12 +46,24 @@ namespace Netch.Models.Config [Newtonsoft.Json.JsonProperty("aiodns")] public AioDNS AioDNS = new(); + /// + /// DNSProxy 配置 + /// + [Newtonsoft.Json.JsonProperty("dnsproxy")] + public DNSProxy DNSProxy = new(); + /// /// V2Ray 配置 /// [Newtonsoft.Json.JsonProperty("v2ray")] public V2Ray V2Ray = new(); + /// + /// V2Ray 配置 + /// + [Newtonsoft.Json.JsonProperty("xray")] + public XRay XRay = new(); + /// /// STUN 配置 /// diff --git a/Netch/Models/Config/DNSProxy.cs b/Netch/Models/Config/DNSProxy.cs new file mode 100644 index 00000000..9f768d54 --- /dev/null +++ b/Netch/Models/Config/DNSProxy.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Netch.Models.Config +{ + public class DNSProxy + { + [Newtonsoft.Json.JsonProperty("otherdns")] + public string OtherDNS = "tls://8.8.8.8:853"; + + [Newtonsoft.Json.JsonProperty("args")] + public string Args = ""; + } +} diff --git a/Netch/Models/Config/Generic.cs b/Netch/Models/Config/Generic.cs index 0ec3ac1f..be424eeb 100644 --- a/Netch/Models/Config/Generic.cs +++ b/Netch/Models/Config/Generic.cs @@ -5,11 +5,25 @@ /// /// 检查 Unstable 更新 /// + [Newtonsoft.Json.JsonProperty("unstable")] public bool Unstable = false; /// /// 使用 ICMP 测试延迟 /// + [Newtonsoft.Json.JsonProperty("icmping")] public bool ICMPing = true; + + /// + /// 使用 AioDNS 解析器 + /// + [Newtonsoft.Json.JsonProperty("aiodns")] + public bool AioDNS = false; + + /// + /// 使用 XRay 后端 + /// + [Newtonsoft.Json.JsonProperty("xray")] + public bool XRay = false; } } diff --git a/Netch/Models/Config/Ports.cs b/Netch/Models/Config/Ports.cs index 6fbe6c41..538562e7 100644 --- a/Netch/Models/Config/Ports.cs +++ b/Netch/Models/Config/Ports.cs @@ -13,11 +13,5 @@ /// [Newtonsoft.Json.JsonProperty("mixed")] public int Mixed = 2082; - - /// - /// Redir 端口 - /// - [Newtonsoft.Json.JsonProperty("redir")] - public int Redir = 2083; } } diff --git a/Netch/Models/Config/TapMode.cs b/Netch/Models/Config/TapMode.cs deleted file mode 100644 index 0e52df8b..00000000 --- a/Netch/Models/Config/TapMode.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Collections.Generic; - -namespace Netch.Models.Config -{ - public class TapMode - { - /// - /// 地址 - /// - [Newtonsoft.Json.JsonProperty("network")] - public string Network = "100.64.0.100/24"; - - /// - /// 网关 - /// - [Newtonsoft.Json.JsonProperty("gateway")] - public string Gateway = "100.64.0.1"; - - /// - /// DNS - /// - [Newtonsoft.Json.JsonProperty("dns")] - public string DNS = "aiodns"; - - /// - /// 绕过 IP 地址 - /// - [Newtonsoft.Json.JsonProperty("bypass")] - public List BypassIPs = new(); - } -} diff --git a/Netch/Models/Config/V2Ray.cs b/Netch/Models/Config/V2Ray.cs index 68853b7d..6c5e28ac 100644 --- a/Netch/Models/Config/V2Ray.cs +++ b/Netch/Models/Config/V2Ray.cs @@ -2,11 +2,6 @@ { public class V2Ray { - /// - /// FullCone 支持(需要 xray-core 服务端版本 v1.3.0+) - /// - public bool FullCone = false; - /// /// 跳过证书认证 /// diff --git a/Netch/Models/Config/XRay.cs b/Netch/Models/Config/XRay.cs new file mode 100644 index 00000000..83482391 --- /dev/null +++ b/Netch/Models/Config/XRay.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Netch.Models.Config +{ + public class XRay + { + /// + /// FullCone 支持(需要 xray-core 服务端版本 v1.3.0+) + /// + public bool FullCone = false; + } +} diff --git a/Netch/Models/GitHub/Asset.cs b/Netch/Models/GitHub/Asset.cs deleted file mode 100644 index 96e2dd01..00000000 --- a/Netch/Models/GitHub/Asset.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace Netch.Models.GitHub -{ - public class Asset - { - /// - /// name - /// - [Newtonsoft.Json.JsonProperty("name")] - public string Name; - - /// - /// browser_download_url - /// - [Newtonsoft.Json.JsonProperty("browser_download_url")] - public string URL; - - /// - /// size - /// - [Newtonsoft.Json.JsonProperty("size")] - public ulong Size; - } -} diff --git a/Netch/Models/GitHub/Release.cs b/Netch/Models/GitHub/Release.cs deleted file mode 100644 index 5c0e5e0a..00000000 --- a/Netch/Models/GitHub/Release.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections.Generic; - -namespace Netch.Models.GitHub -{ - /// - /// https://api.github.com/repos/{owner}/{repo}/releases - /// - public class Release - { - /// - /// id - /// - [Newtonsoft.Json.JsonProperty("id")] - public int ID; - - /// - /// html_url - /// - [Newtonsoft.Json.JsonProperty("html_url")] - public string URL; - - /// - /// tag_name - /// - [Newtonsoft.Json.JsonProperty("tag_name")] - public string VerCode; - - /// - /// draft - /// - [Newtonsoft.Json.JsonProperty("draft")] - public bool Draft; - - /// - /// prerelease - /// - [Newtonsoft.Json.JsonProperty("prerelease")] - public bool Unstable; - - /// - /// assets - /// - [Newtonsoft.Json.JsonProperty("assets")] - public List Files; - } -} diff --git a/Netch/Models/Mode/ModeType.cs b/Netch/Models/Mode/ModeType.cs index 3fcb583a..fe3ca33f 100644 --- a/Netch/Models/Mode/ModeType.cs +++ b/Netch/Models/Mode/ModeType.cs @@ -12,11 +12,6 @@ /// ShareMode, - /// - /// 网卡代理 - /// - TapMode, - /// /// 网卡代理 /// @@ -26,10 +21,5 @@ /// 网页代理 /// WebMode, - - /// - /// 代理转发 - /// - WmpMode } } diff --git a/Netch/Models/Mode/ProcessMode/ProcessMode.cs b/Netch/Models/Mode/ProcessMode/ProcessMode.cs index fbb0fa47..297f8a72 100644 --- a/Netch/Models/Mode/ProcessMode/ProcessMode.cs +++ b/Netch/Models/Mode/ProcessMode/ProcessMode.cs @@ -15,6 +15,12 @@ namespace Netch.Models.Mode.ProcessMode [Newtonsoft.Json.JsonProperty("filterLoopback")] public bool Loopback = false; + /// + /// 过滤 内网 流量 + /// + [Newtonsoft.Json.JsonProperty("filterIntranet")] + public bool Intranet = false; + /// /// 过滤 ICMP 流量(伪造 ICMP 回复) /// diff --git a/Netch/Models/Mode/TapMode/TapMode.cs b/Netch/Models/Mode/TapMode/TapMode.cs deleted file mode 100644 index b754ab5e..00000000 --- a/Netch/Models/Mode/TapMode/TapMode.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Collections.Generic; - -namespace Netch.Models.Mode.TapMode -{ - public class TapMode : Mode - { - public TapMode() - { - this.Type = ModeType.TapMode; - } - - /// - /// 绕过列表(IP CIDR) - /// - [Newtonsoft.Json.JsonProperty("bypass")] - public List BypassList; - - /// - /// 代理列表(IP CIDR) - /// - [Newtonsoft.Json.JsonProperty("handle")] - public List HandleList; - } -} diff --git a/Netch/Models/Mode/WmpMode/WmpMode.cs b/Netch/Models/Mode/WmpMode/WmpMode.cs deleted file mode 100644 index b1e7cf45..00000000 --- a/Netch/Models/Mode/WmpMode/WmpMode.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace Netch.Models.Mode.WmpMode -{ - public class WmpMode : Mode - { - public WmpMode() - { - this.Type = ModeType.WmpMode; - } - - /// - /// 监听地址(为空则监听所有 IPv4 + IPv6 地址) - /// - public string ListenAddr; - - /// - /// 监听端口 - /// - public ushort ListenPort; - - /// - /// 远端地址 - /// - public string RemoteAddr; - - /// - /// 远端端口 - /// - public ushort RemotePort; - } -} diff --git a/Netch/Models/Server/Clash/Clash.cs b/Netch/Models/Server/Clash/Clash.cs deleted file mode 100644 index e2fa2f83..00000000 --- a/Netch/Models/Server/Clash/Clash.cs +++ /dev/null @@ -1,32 +0,0 @@ -namespace Netch.Models.Server.Clash -{ - public class Clash : Server - { - public Clash() - { - this.Type = ServerType.Clash; - } - - /// - /// 自定义配置 - /// - [Newtonsoft.Json.JsonProperty("custom")] - public bool Custom = true; - - /// - /// 自定义配置文件路径 - /// - [Newtonsoft.Json.JsonProperty("filepath")] - public string FilePath; - - /// - /// 解析链接 - /// - /// 链接 - /// 是否成功 - public bool ParseLink(string link) - { - return false; - } - } -} diff --git a/Netch/Models/Server/Server.cs b/Netch/Models/Server/Server.cs index 8d99f4fa..4e5d07ac 100644 --- a/Netch/Models/Server/Server.cs +++ b/Netch/Models/Server/Server.cs @@ -58,9 +58,10 @@ namespace Netch.Models.Server ServerType.Socks => "S5", ServerType.Shadowsocks => "SS", ServerType.ShadowsocksR => "SR", + ServerType.WireGuard => "WG", ServerType.Trojan => "TR", - ServerType.VLess => "VL", ServerType.VMess => "VM", + ServerType.VLess => "VL", _ => "UN", }; diff --git a/Netch/Models/Server/ServerType.cs b/Netch/Models/Server/ServerType.cs index 0ab552bb..23010321 100644 --- a/Netch/Models/Server/ServerType.cs +++ b/Netch/Models/Server/ServerType.cs @@ -2,16 +2,6 @@ { public enum ServerType : int { - /// - /// HTTP - /// - HTTP, - - /// - /// Clash - /// - Clash, - /// /// Socks5 /// @@ -27,19 +17,24 @@ /// ShadowsocksR, + /// + /// WireGuard + /// + WireGuard, + /// /// Trojan /// Trojan, - /// - /// VLess - /// - VLess, - /// /// VMess /// - VMess + VMess, + + /// + /// VLess + /// + VLess } } diff --git a/Netch/Utils/DNS.cs b/Netch/Utils/DNS.cs index bf18abea..8f6f9797 100644 --- a/Netch/Utils/DNS.cs +++ b/Netch/Utils/DNS.cs @@ -9,7 +9,7 @@ namespace Netch.Utils /// /// 缓存表 /// - private static readonly Hashtable Cache = new Hashtable(); + private static Hashtable Cache = new Hashtable(); /// /// 获取 IP 地址 diff --git a/Netch/Utils/GitHub.cs b/Netch/Utils/GitHub.cs deleted file mode 100644 index 769a9b75..00000000 --- a/Netch/Utils/GitHub.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System.Collections.Generic; - -namespace Netch.Utils -{ - public static class GitHub - { - /// - /// 地址 - /// - public static readonly string URL = "https://api.github.com/repos/NetchX/Netch/releases"; - - /// - /// 检查是否有更新 - /// - /// - public static int HasUpdate() - { - var list = GetReleaseList(); - if (list.Count < 1) - { - return 0; - } - - for (int i = 0; i < list.Count; i++) - { - if (list[i].Draft) - { - continue; - } - - if (Global.Config.Generic.Unstable) - { - if (list[i].Unstable) - { - continue; - } - } - - if (list[i].VerCode.Equals(Global.VerCode)) - { - return 0; - } - - return list[i].ID; - } - - return 0; - } - - /// - /// 获取单个发布 - /// - /// - /// - public static Models.GitHub.Release GetRelease(int id) - { - var data = HTTP.GetString($"{URL}/{id}"); - - return Newtonsoft.Json.JsonConvert.DeserializeObject(data); - } - - /// - /// 获取发布列表 - /// - /// - public static List GetReleaseList() - { - var data = HTTP.GetString(URL); - - return Newtonsoft.Json.JsonConvert.DeserializeObject>(data); - } - } -} diff --git a/Netch/Utils/RouteHelper.cs b/Netch/Utils/RouteHelper.cs index 22c1ecbe..6d27a589 100644 --- a/Netch/Utils/RouteHelper.cs +++ b/Netch/Utils/RouteHelper.cs @@ -14,7 +14,7 @@ namespace Netch.Utils /// /// [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern ulong ConvertLuidToIndex(ulong id); + public static extern uint ConvertLuidToIndex(ulong id); /// /// 绑定 IP 地址(Windows XP) @@ -24,7 +24,7 @@ namespace Netch.Utils /// /// [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool CreateIPv4(string address, string netmask, ulong index); + public static extern bool CreateIPv4(string address, string netmask, uint index); /// /// 绑定 IP 地址 @@ -35,7 +35,7 @@ namespace Netch.Utils /// /// [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool CreateUnicastIP(AddressFamily inet, string address, byte cidr, ulong index); + public static extern bool CreateUnicastIP(AddressFamily inet, string address, byte cidr, uint index); /// /// 创建路由 @@ -47,7 +47,7 @@ namespace Netch.Utils /// /// [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool CreateRoute(AddressFamily inet, string address, byte cidr, string gateway, ulong index); + public static extern bool CreateRoute(AddressFamily inet, string address, byte cidr, string gateway, uint index, uint metric = 1); /// /// 删除路由 @@ -59,7 +59,7 @@ namespace Netch.Utils /// /// [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool DeleteRoute(AddressFamily inet, string address, byte cidr, string gateway, ulong index); + public static extern bool DeleteRoute(AddressFamily inet, string address, byte cidr, string gateway, uint index); /// /// 使用索引获取适配器