diff --git a/Netch/Controllers/DNSController.cs b/Netch/Controllers/DNSController.cs index f73cdeb0..c704cdbb 100644 --- a/Netch/Controllers/DNSController.cs +++ b/Netch/Controllers/DNSController.cs @@ -1,7 +1,6 @@ using System; using System.IO; -using System.Runtime.InteropServices; -using System.Text; +using static Netch.Interops.AioDNSInterops; namespace Netch.Controllers { @@ -11,7 +10,7 @@ namespace Netch.Controllers public void Stop() { - aiodns_free(); + Free(); } /// @@ -20,36 +19,14 @@ namespace Netch.Controllers /// public void Start() { - aiodns_dial((int) NameList.TYPE_REST, null); - aiodns_dial((int) NameList.TYPE_ADDR, Encoding.UTF8.GetBytes($"{Global.Settings.LocalAddress}:{Global.Settings.AioDNS.ListenPort}")); - aiodns_dial((int) NameList.TYPE_LIST, Encoding.UTF8.GetBytes(Path.GetFullPath(Global.Settings.AioDNS.RulePath))); - aiodns_dial((int) NameList.TYPE_CDNS, Encoding.UTF8.GetBytes($"{Global.Settings.AioDNS.ChinaDNS}")); - aiodns_dial((int) NameList.TYPE_ODNS, Encoding.UTF8.GetBytes($"{Global.Settings.AioDNS.OtherDNS}")); + Dial(NameList.TYPE_REST, ""); + Dial(NameList.TYPE_ADDR, $"{Global.Settings.LocalAddress}:{Global.Settings.AioDNS.ListenPort}"); + Dial(NameList.TYPE_LIST, Path.GetFullPath(Global.Settings.AioDNS.RulePath)); + Dial(NameList.TYPE_CDNS, $"{Global.Settings.AioDNS.ChinaDNS}"); + Dial(NameList.TYPE_ODNS, $"{Global.Settings.AioDNS.OtherDNS}"); - if (!aiodns_init()) + if (!Init()) throw new Exception("AioDNS start failed"); } - - #region NativeMethods - - [DllImport("aiodns.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern bool aiodns_dial(int name, byte[]? 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 enum NameList - { - TYPE_REST, - TYPE_ADDR, - TYPE_LIST, - TYPE_CDNS, - TYPE_ODNS - } - - #endregion } } \ No newline at end of file diff --git a/Netch/Controllers/NFController.cs b/Netch/Controllers/NFController.cs index 75441838..b871b490 100644 --- a/Netch/Controllers/NFController.cs +++ b/Netch/Controllers/NFController.cs @@ -8,7 +8,7 @@ using Netch.Servers.Shadowsocks; using Netch.Servers.Socks5; using Netch.Utils; using nfapinet; -using static Netch.Controllers.RedirectorInterop; +using static Netch.Interops.RedirectorInterop; namespace Netch.Controllers { diff --git a/Netch/Controllers/TUNController.cs b/Netch/Controllers/TUNController.cs index c370d1dc..d5c3faf0 100644 --- a/Netch/Controllers/TUNController.cs +++ b/Netch/Controllers/TUNController.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; using Netch.Models; using Netch.Servers.Socks5; using Netch.Utils; -using static Netch.Controllers.TUNInterop; +using static Netch.Interops.TUNInterop; namespace Netch.Controllers { diff --git a/Netch/Interops/AioDNSInterops.cs b/Netch/Interops/AioDNSInterops.cs new file mode 100644 index 00000000..f2a8cc5e --- /dev/null +++ b/Netch/Interops/AioDNSInterops.cs @@ -0,0 +1,33 @@ +using System.Runtime.InteropServices; +using System.Text; + +namespace Netch.Interops +{ + public static class AioDNSInterops + { + private const string aiodns_bin = "aiodns.bin"; + + public static bool Dial(NameList name, string value) + { + return aiodns_dial(name, Encoding.UTF8.GetBytes(value)); + } + + [DllImport(aiodns_bin, CallingConvention = CallingConvention.Cdecl)] + private static extern bool aiodns_dial(NameList name, byte[] value); + + [DllImport(aiodns_bin, EntryPoint = "aiodns_init", CallingConvention = CallingConvention.Cdecl)] + public static extern bool Init(); + + [DllImport(aiodns_bin, EntryPoint = "aiodns_free", CallingConvention = CallingConvention.Cdecl)] + public static extern void Free(); + + public enum NameList + { + TYPE_REST, + TYPE_ADDR, + TYPE_LIST, + TYPE_CDNS, + TYPE_ODNS + } + } +} \ No newline at end of file diff --git a/Netch/Controllers/RedirectorInterop.cs b/Netch/Interops/RedirectorInterop.cs similarity index 98% rename from Netch/Controllers/RedirectorInterop.cs rename to Netch/Interops/RedirectorInterop.cs index f1353ffd..6051ca2c 100644 --- a/Netch/Controllers/RedirectorInterop.cs +++ b/Netch/Interops/RedirectorInterop.cs @@ -1,7 +1,7 @@ using System.Runtime.InteropServices; using Netch.Utils; -namespace Netch.Controllers +namespace Netch.Interops { public static class RedirectorInterop { diff --git a/Netch/Controllers/TUNInterop.cs b/Netch/Interops/TUNInterop.cs similarity index 97% rename from Netch/Controllers/TUNInterop.cs rename to Netch/Interops/TUNInterop.cs index 540aa280..376f1658 100644 --- a/Netch/Controllers/TUNInterop.cs +++ b/Netch/Interops/TUNInterop.cs @@ -2,9 +2,9 @@ using System.Runtime.InteropServices; using System.Text; using Netch.Utils; -namespace Netch.Controllers +namespace Netch.Interops { - public class TUNInterop + public static class TUNInterop { public enum NameList { diff --git a/Netch/Models/TunAdapter.cs b/Netch/Models/TunAdapter.cs index eb72f45c..1ae12158 100644 --- a/Netch/Models/TunAdapter.cs +++ b/Netch/Models/TunAdapter.cs @@ -2,6 +2,8 @@ using System.Linq; using System.Net; using System.Net.NetworkInformation; +using Netch.Controllers; +using Netch.Interops; using Netch.Utils; namespace Netch.Models diff --git a/Netch/NativeMethods.cs b/Netch/NativeMethods.cs index f43dc2e0..a437fa2d 100644 --- a/Netch/NativeMethods.cs +++ b/Netch/NativeMethods.cs @@ -43,11 +43,5 @@ namespace Netch [DllImport("dnsapi", EntryPoint = "DnsFlushResolverCache")] public static extern uint FlushDNSResolverCache(); - - [DllImport("kernel32.dll")] - public static extern bool AllocConsole(); - - [DllImport("kernel32.dll")] - public static extern bool AttachConsole(int dwProcessId); } } \ No newline at end of file diff --git a/Netch/Netch.cs b/Netch/Netch.cs index 27cf0f2d..883d838c 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -23,10 +23,10 @@ namespace Netch public static void Main(string[] args) { #if DEBUG - AttachConsole(); + AttachAllocConsole(); #else if (args.Contains(Constants.Parameter.Console)) - AttachConsole(); + AttachAllocConsole(); #endif if (args.Contains(Constants.Parameter.ForceUpdate)) @@ -93,10 +93,10 @@ namespace Netch Application.Run(Global.MainForm); } - private static void AttachConsole() + private static void AttachAllocConsole() { - if (!NativeMethods.AttachConsole(-1)) - NativeMethods.AllocConsole(); + if (!AttachConsole(ATTACH_PARENT_PROCESS)) + AllocConsole(); } public static void Application_OnException(object sender, ThreadExceptionEventArgs e)