diff --git a/Netch/Controllers/DNSController.cs b/Netch/Controllers/DNSController.cs index 67643a3d..8c16fb3d 100644 --- a/Netch/Controllers/DNSController.cs +++ b/Netch/Controllers/DNSController.cs @@ -1,12 +1,85 @@ using Netch.Forms; using System; +using System.Diagnostics; +using System.IO; using System.Net; +using System.Threading; namespace Netch.Controllers { public class DNSController { - public static DNS.Server.DnsServer Server = new DNS.Server.DnsServer(new Resolver()); + /// + /// 进程实例 + /// + public Process Instance; + + /// + /// 启动NatTypeTester + /// + /// + public bool Start() + { + MainForm.Instance.StatusText($"{Utils.i18N.Translate("Starting dns2tcp Service")}"); + try + { + if (!File.Exists("bin\\dns2tcp.exe")) + { + return false; + } + + Instance = MainController.GetProcess(); + Instance.StartInfo.FileName = "bin\\dns2tcp.exe"; + + Instance.StartInfo.Arguments = " -L 127.0.0.1:53 -R 1.1.1.1:53"; + + Instance.OutputDataReceived += OnOutputDataReceived; + Instance.ErrorDataReceived += OnOutputDataReceived; + + Instance.Start(); + Instance.BeginOutputReadLine(); + Instance.BeginErrorReadLine(); + return true; + } + catch (Exception) + { + Utils.Logging.Info("dns2tcp 进程出错"); + Stop(); + return false; + } + } + + /// + /// 停止 + /// + public void Stop() + { + try + { + if (Instance != null && !Instance.HasExited) + { + Instance.Kill(); + } + } + catch (Exception e) + { + Utils.Logging.Info(e.ToString()); + } + } + + public void OnOutputDataReceived(object sender, DataReceivedEventArgs e) + { + if (!string.IsNullOrWhiteSpace(e.Data)) + { + if (File.Exists("logging\\dns2tcp.log")) + { + File.Delete("logging\\dns2tcp.log"); + } + File.AppendAllText("logging\\dns2tcp.log", $"{e.Data}\r\n"); + } + } + + /* public static DNS.Server.DnsServer Server = new DNS.Server.DnsServer(new Resolver()); public bool Start() { @@ -34,6 +107,6 @@ namespace Netch.Controllers { Utils.Logging.Info(e.ToString()); } - } + }*/ } } diff --git a/Netch/Controllers/TUNTAPController.cs b/Netch/Controllers/TUNTAPController.cs index 4ca092ba..59969a71 100644 --- a/Netch/Controllers/TUNTAPController.cs +++ b/Netch/Controllers/TUNTAPController.cs @@ -300,7 +300,7 @@ namespace Netch.Controllers else { pDNSController.Start(); - dns = "127.0.0.1"; + dns = "127.0.0.1,127.0.0.1"; //dns = "1.1.1.1,1.0.0.1"; } @@ -362,6 +362,7 @@ namespace Netch.Controllers //pDNSController.Stop(); //修复点击停止按钮后再启动,DNS服务没监听的BUG ClearBypass(); + pDNSController.Stop(); } catch (Exception e) { diff --git a/Netch/Resources/zh-CN b/Netch/Resources/zh-CN index eecc1fc4..1c8e6b63 100644 --- a/Netch/Resources/zh-CN +++ b/Netch/Resources/zh-CN @@ -24,6 +24,7 @@ "Starting LocalDns service": "正在启动本地DNS服务", "Starting Redirector": "正在启动Redirector", "Starting netfilter2 Service": "正在启动netfilter2服务", + "Starting dns2tcp Service": "正在启动dns2tcp服务", "SetupBypass": "设置绕行规则", "Server": "服务器",