From b3f8b863e29e45f33b4df7a69e2a61e7f7452a35 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Mon, 21 Sep 2020 21:27:54 +0800 Subject: [PATCH] Check whether tun2socks supports fakeDNS --- Netch/Controllers/Mode/TUNTAPController.cs | 36 +++++++++++++++++++++- Netch/Forms/SettingForm.Designer.cs | 1 + Netch/Forms/SettingForm.cs | 1 + Netch/Global.cs | 2 ++ Netch/Netch.cs | 13 ++++++-- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Netch/Controllers/Mode/TUNTAPController.cs b/Netch/Controllers/Mode/TUNTAPController.cs index 7512cf5c..fb423dac 100644 --- a/Netch/Controllers/Mode/TUNTAPController.cs +++ b/Netch/Controllers/Mode/TUNTAPController.cs @@ -237,7 +237,7 @@ namespace Netch.Controllers argument.Append( $"-tunAddr {Global.Settings.TUNTAP.Address} -tunMask {Global.Settings.TUNTAP.Netmask} -tunGw {Global.Settings.TUNTAP.Gateway} -tunDns {dns} -tunName \"{adapterName}\" "); - if (Global.Settings.TUNTAP.UseFakeDNS) + if (Global.Settings.TUNTAP.UseFakeDNS && Global.SupportFakeDns) argument.Append("-fakeDns "); return StartInstanceAuto(argument.ToString(), ProcessPriorityClass.RealTime); @@ -257,6 +257,40 @@ namespace Netch.Controllers Task.WaitAll(tasks); } + public bool TestFakeDNS() + { + var exited = false; + var helpStr = new StringBuilder(); + try + { + void OnOutputDataReceived(object sender,DataReceivedEventArgs e) + { + if (e.Data == null) + { + exited = true; + return; + } + helpStr.Append(e.Data); + } + InitInstance("-h"); + // Instance.OutputDataReceived += OnOutputDataReceived; + Instance.ErrorDataReceived += OnOutputDataReceived; + Instance.Start(); + Instance.BeginOutputReadLine(); + Instance.BeginErrorReadLine(); + while (!exited) + { + Thread.Sleep(200); + } + + return helpStr.ToString().Contains("-fakeDns"); + } + catch + { + return false; + } + } + /// /// 搜索出口和TUNTAP适配器 /// diff --git a/Netch/Forms/SettingForm.Designer.cs b/Netch/Forms/SettingForm.Designer.cs index 01e501c4..18bb10bf 100644 --- a/Netch/Forms/SettingForm.Designer.cs +++ b/Netch/Forms/SettingForm.Designer.cs @@ -184,6 +184,7 @@ this.UseFakeDNSCheckBox.Size = new System.Drawing.Size(110, 21); this.UseFakeDNSCheckBox.TabIndex = 11; this.UseFakeDNSCheckBox.Text = "Use Fake DNS"; + this.UseFakeDNSCheckBox.Visible = false; this.UseFakeDNSCheckBox.UseVisualStyleBackColor = true; // // ProxyDNSCheckBox diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index ca20dfd4..417ff934 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -122,6 +122,7 @@ namespace Netch.Forms private void SettingForm_Load(object sender, EventArgs e) { + UseFakeDNSCheckBox.Visible = Global.SupportFakeDns; InitText(); InitValue(); } diff --git a/Netch/Global.cs b/Netch/Global.cs index d918dbe5..1f10a127 100644 --- a/Netch/Global.cs +++ b/Netch/Global.cs @@ -24,6 +24,8 @@ namespace Netch /// public static MainForm MainForm; + public static bool SupportFakeDns = false; + /// /// SS/SSR 加密方式 /// diff --git a/Netch/Netch.cs b/Netch/Netch.cs index 356ce15d..7ff47fdf 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -67,9 +67,16 @@ namespace Netch } Logging.Info($"版本: {UpdateChecker.Owner}/{UpdateChecker.Repo}@{UpdateChecker.Version}"); - Task.Run(() => { Logging.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Application.ExecutablePath)}"); }); - Logging.Info("启动单实例"); - Task.Run(OnlyInstance.Server); + Task.Run(() => + { + Logging.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Application.ExecutablePath)}"); + }); + Task.Run(() => + { + Logging.Info("启动单实例"); + OnlyInstance.Server(); + }); + Task.Run(() => { Global.SupportFakeDns = new TUNTAPController().TestFakeDNS(); }); // 绑定错误捕获 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);