From 934840ad714c621df513d6e8b68b3ddced063471 Mon Sep 17 00:00:00 2001 From: M Sasan MH Date: Sat, 27 May 2023 17:45:02 +0430 Subject: [PATCH] v2.1.2 --- SecureDNSClient/Forms/FormMain.Designer.cs | 35 ++++---- SecureDNSClient/Forms/FormMain.cs | 67 +++++++++++++--- SecureDNSClient/MsmhTools/Network.cs | 80 ++++++++++++++++++- .../PublishProfiles/FolderProfile.pubxml.user | 2 +- SecureDNSClient/SecureDNSClient.csproj | 4 +- .../PublishProfiles/FolderProfile.pubxml.user | 2 +- .../SecureDNSClientPortable.csproj | 2 +- 7 files changed, 158 insertions(+), 34 deletions(-) diff --git a/SecureDNSClient/Forms/FormMain.Designer.cs b/SecureDNSClient/Forms/FormMain.Designer.cs index 3f08c67..58297c4 100644 --- a/SecureDNSClient/Forms/FormMain.Designer.cs +++ b/SecureDNSClient/Forms/FormMain.Designer.cs @@ -181,8 +181,9 @@ private void InitializeComponent() this.PictureBoxAbout = new System.Windows.Forms.PictureBox(); this.CustomStatusStrip1 = new CustomControls.CustomStatusStrip(); this.ToolStripStatusLabelDNS = new System.Windows.Forms.ToolStripStatusLabel(); - this.ToolStripStatusLabelDoH = new System.Windows.Forms.ToolStripStatusLabel(); this.ToolStripStatusLabelDnsLatency = new System.Windows.Forms.ToolStripStatusLabel(); + this.ToolStripStatusLabelDoH = new System.Windows.Forms.ToolStripStatusLabel(); + this.ToolStripStatusLabelDohLatency = new System.Windows.Forms.ToolStripStatusLabel(); this.ToolStripStatusLabelSpace = new System.Windows.Forms.ToolStripStatusLabel(); this.ToolStripStatusLabelPercent = new System.Windows.Forms.ToolStripStatusLabel(); this.NotifyIconMain = new System.Windows.Forms.NotifyIcon(this.components); @@ -194,7 +195,6 @@ private void InitializeComponent() this.CustomRichTextBoxStatusIsDPIActive = new CustomControls.CustomRichTextBox(); this.CustomRichTextBoxStatusIsConnected = new CustomControls.CustomRichTextBox(); this.CustomRichTextBoxStatusWorkingServers = new CustomControls.CustomRichTextBox(); - this.ToolStripStatusLabelDohLatency = new System.Windows.Forms.ToolStripStatusLabel(); this.CustomGroupBoxLog.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.CustomNumericUpDownSSLFragmentSize)).BeginInit(); this.CustomTabControlMain.SuspendLayout(); @@ -638,6 +638,7 @@ private void InitializeComponent() this.CustomButtonConnectAll.TabIndex = 9; this.CustomButtonConnectAll.Text = "Connect all/Disconnect all"; this.CustomButtonConnectAll.UseVisualStyleBackColor = true; + this.CustomButtonConnectAll.Visible = false; this.CustomButtonConnectAll.Click += new System.EventHandler(this.CustomButtonConnectAll_Click); // // CustomButtonViewWorkingServers @@ -2807,6 +2808,14 @@ private void InitializeComponent() this.ToolStripStatusLabelDNS.Text = "DNS status: Offline."; this.ToolStripStatusLabelDNS.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // + // ToolStripStatusLabelDnsLatency + // + this.ToolStripStatusLabelDnsLatency.AutoSize = false; + this.ToolStripStatusLabelDnsLatency.Name = "ToolStripStatusLabelDnsLatency"; + this.ToolStripStatusLabelDnsLatency.Size = new System.Drawing.Size(100, 17); + this.ToolStripStatusLabelDnsLatency.Text = "Latency: -1"; + this.ToolStripStatusLabelDnsLatency.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + // // ToolStripStatusLabelDoH // this.ToolStripStatusLabelDoH.AutoSize = false; @@ -2815,18 +2824,18 @@ private void InitializeComponent() this.ToolStripStatusLabelDoH.Text = "DoH status: Offline."; this.ToolStripStatusLabelDoH.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // - // ToolStripStatusLabelDnsLatency + // ToolStripStatusLabelDohLatency // - this.ToolStripStatusLabelDnsLatency.AutoSize = false; - this.ToolStripStatusLabelDnsLatency.Name = "ToolStripStatusLabelDnsLatency"; - this.ToolStripStatusLabelDnsLatency.Size = new System.Drawing.Size(100, 17); - this.ToolStripStatusLabelDnsLatency.Text = "Latency: -1"; - this.ToolStripStatusLabelDnsLatency.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + this.ToolStripStatusLabelDohLatency.AutoSize = false; + this.ToolStripStatusLabelDohLatency.Name = "ToolStripStatusLabelDohLatency"; + this.ToolStripStatusLabelDohLatency.Size = new System.Drawing.Size(100, 17); + this.ToolStripStatusLabelDohLatency.Text = "Latency: -1"; + this.ToolStripStatusLabelDohLatency.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // ToolStripStatusLabelSpace // this.ToolStripStatusLabelSpace.Name = "ToolStripStatusLabelSpace"; - this.ToolStripStatusLabelSpace.Size = new System.Drawing.Size(163, 17); + this.ToolStripStatusLabelSpace.Size = new System.Drawing.Size(194, 17); this.ToolStripStatusLabelSpace.Spring = true; // // ToolStripStatusLabelPercent @@ -3082,14 +3091,6 @@ private void InitializeComponent() this.CustomRichTextBoxStatusWorkingServers.WordWrap = false; this.CustomRichTextBoxStatusWorkingServers.ZoomFactor = 1F; // - // ToolStripStatusLabelDohLatency - // - this.ToolStripStatusLabelDohLatency.AutoSize = false; - this.ToolStripStatusLabelDohLatency.Name = "ToolStripStatusLabelDohLatency"; - this.ToolStripStatusLabelDohLatency.Size = new System.Drawing.Size(100, 17); - this.ToolStripStatusLabelDohLatency.Text = "Latency: -1"; - this.ToolStripStatusLabelDohLatency.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; - // // FormMain // this.AcceptButton = this.CustomButtonCheck; diff --git a/SecureDNSClient/Forms/FormMain.cs b/SecureDNSClient/Forms/FormMain.cs index 715f492..216abda 100644 --- a/SecureDNSClient/Forms/FormMain.cs +++ b/SecureDNSClient/Forms/FormMain.cs @@ -367,6 +367,9 @@ private void UpdateBools() if (IsDNSSet) UnsetSavedDNS(); } + // Update bool IsDnsSet + IsDNSSet = UpdateBoolIsDnsSet(out bool _); + // Update bool IsHTTPProxyRunning if (HTTPProxy != null) IsSharing = HTTPProxy.IsRunning; @@ -452,6 +455,35 @@ private void UpdateBoolDohOnce(int timeout) } } + private bool UpdateBoolIsDnsSet(out bool isAnotherDnsSet) + { + isAnotherDnsSet = false; + if (File.Exists(SecureDNS.NicNamePath)) + { + string nicName = File.ReadAllText(SecureDNS.NicNamePath).Replace(NL, string.Empty); + if (nicName.Length > 0) + { + NetworkInterface? nic = Network.GetNICByName(nicName); + if (nic != null) + { + bool isDnsSet = Network.IsDnsSet(nic, out string dnsServer1, out string dnsServer2); + if (!isDnsSet) return false; // DNS is set to DHCP + else + { + if (dnsServer1 == IPAddress.Loopback.ToString()) + return true; + else + { + isAnotherDnsSet = true; + return false; + } + } + } + } + } + return false; + } + private bool UpdateBoolIsProxySet() { if (IsSharing) @@ -1935,7 +1967,7 @@ private async void SetDNS() // Set DNS Network.UnsetDNS(nic); // Unset first - Task.Delay(100).Wait(); // Wait a moment + Task.Delay(200).Wait(); // Wait a moment Network.SetDNS(nic, dnss); // Set DNS IsDNSSet = true; @@ -2628,8 +2660,6 @@ private async Task CheckDPIWorks(string host, int timeoutSec = 30) //Default tim string url = $"https://{host}/"; Uri uri = new(url, UriKind.Absolute); - HttpResponseMessage checkingResponse; - if (HTTPProxy != null && HTTPProxy.IsRunning && HTTPProxy.IsDpiActive && IsSharing) { string proxyScheme = $"http://{IPAddress.Loopback}:{LastProxyPort}"; @@ -2638,19 +2668,35 @@ private async Task CheckDPIWorks(string host, int timeoutSec = 30) //Default tim using HttpClient httpClientWithProxy = new(socketsHttpHandler); httpClientWithProxy.Timeout = new TimeSpan(0, 0, timeoutSec); - checkingResponse = await httpClientWithProxy.GetAsync(uri); + HttpResponseMessage r = await httpClientWithProxy.GetAsync(uri); Task.Delay(500).Wait(); + + if (r.IsSuccessStatusCode || r.StatusCode.ToString() == "NotFound") + { + msgSuccess(); + r.Dispose(); + } + else + msgFailed(r); } else { using HttpClient httpClient = new(); httpClient.Timeout = new TimeSpan(0, 0, timeoutSec); - checkingResponse = await httpClient.GetAsync(uri); + HttpResponseMessage r = await httpClient.GetAsync(uri); Task.Delay(500).Wait(); + + if (r.IsSuccessStatusCode || r.StatusCode.ToString() == "NotFound") + { + msgSuccess(); + r.Dispose(); + } + else + msgFailed(r); } - - if (checkingResponse.IsSuccessStatusCode) + + void msgSuccess() { // Write Success to log var elapsedTime = Math.Round((double)CheckDPIWorksStopWatch.ElapsedMilliseconds / 1000); @@ -2659,13 +2705,16 @@ private async Task CheckDPIWorks(string host, int timeoutSec = 30) //Default tim this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI1, Color.LightGray)); this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI2, Color.MediumSeaGreen)); } - else + + void msgFailed(HttpResponseMessage r) { // Write Status to log string msgDPI1 = $"DPI Check: "; - string msgDPI2 = $"Status {checkingResponse.StatusCode}: {checkingResponse.ReasonPhrase}.{NL}"; + string msgDPI2 = $"Status {r.StatusCode}: {r.ReasonPhrase}.{NL}"; this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI1, Color.LightGray)); this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI2, Color.DodgerBlue)); + + r.Dispose(); } CheckDPIWorksStopWatch.Stop(); diff --git a/SecureDNSClient/MsmhTools/Network.cs b/SecureDNSClient/MsmhTools/Network.cs index cd90d74..dd7b2c2 100644 --- a/SecureDNSClient/MsmhTools/Network.cs +++ b/SecureDNSClient/MsmhTools/Network.cs @@ -551,7 +551,34 @@ public static List GetNetworkInterfacesIPv4(bool upAndRunning public static void SetDNS(NetworkInterface nic, string dnsServers) { // Requires Elevation + // Only netsh can set DNS on Windows 7 if (nic == null) return; + + try + { + string dnsServer1 = dnsServers; + string dnsServer2 = string.Empty; + if (dnsServers.Contains(',')) + { + string[] split = dnsServers.Split(','); + dnsServer1 = split[0]; + dnsServer2 = split[1]; + } + + string processName = "netsh"; + string processArgs1 = $"interface ipv4 delete dnsservers {nic.Name} all"; + string processArgs2 = $"interface ipv4 set dnsservers {nic.Name} static {dnsServer1} primary"; + string processArgs3 = $"interface ipv4 add dnsservers {nic.Name} {dnsServer2} index=2"; + ProcessManager.Execute(processName, processArgs1, true, true); + ProcessManager.Execute(processName, processArgs2, true, true); + if (!string.IsNullOrEmpty(dnsServer2)) + ProcessManager.Execute(processName, processArgs3, true, true); + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message); + } + try { using ManagementClass managementClass = new("Win32_NetworkAdapterConfiguration"); @@ -581,9 +608,18 @@ public static void UnsetDNS(NetworkInterface nic) // NetSh Command: netsh interface ip set dns "nic.Name" source=dhcp if (nic == null) return; - string processName = "netsh"; - string processArgs = "interface ip set dns \"" + nic.Name + "\" source=dhcp"; - ProcessManager.ExecuteOnly(processName, processArgs, true, true); + try + { + string processName = "netsh"; + string processArgs1 = $"interface ipv4 delete dnsservers {nic.Name} all"; + string processArgs2 = $"interface ipv4 set dnsservers {nic.Name} source=dhcp"; + ProcessManager.Execute(processName, processArgs1, true, true); + ProcessManager.Execute(processName, processArgs2, true, true); + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message); + } try { @@ -608,6 +644,44 @@ public static void UnsetDNS(NetworkInterface nic) } } + /// + /// Check if DNS is set to Static or DHCP + /// + /// Network Interface + /// Primary DNS Server + /// Secondary DNS Server + /// True = Static, False = DHCP + public static bool IsDnsSet(NetworkInterface nic, out string dnsServer1, out string dnsServer2) + { + dnsServer1 = dnsServer2 = string.Empty; + if (nic == null) return false; + + string processName = "netsh"; + string processArgs = $"interface ipv4 show dnsservers {nic.Name}"; + string stdout = ProcessManager.Execute(processName, processArgs, true, false); + + List lines = stdout.SplitToLines(); + for (int n = 0; n < lines.Count; n++) + { + string line = lines[n]; + if (n == 2 && line.Contains(':')) + { + string[] split1 = line.Split(':'); + if (split1.Length > 1) + { + dnsServer1 = split1[1].Trim(); + } + } + else if (n == 3) + { + dnsServer2 = line.Trim(); + } + Debug.WriteLine(line); + } + //Debug.WriteLine(stdout); + return !stdout.Contains("DHCP"); + } + public static bool IsInternetAlive(string? url = null, int timeoutMs = 5000) { // Attempt 1 diff --git a/SecureDNSClient/Properties/PublishProfiles/FolderProfile.pubxml.user b/SecureDNSClient/Properties/PublishProfiles/FolderProfile.pubxml.user index b5c097c..558984e 100644 --- a/SecureDNSClient/Properties/PublishProfiles/FolderProfile.pubxml.user +++ b/SecureDNSClient/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -4,6 +4,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - True|2023-05-25T20:03:02.8133052Z;True|2023-05-26T00:22:55.5762345+04:30;True|2023-05-19T21:55:55.2053137+04:30;True|2023-05-04T21:14:13.1959990+04:30;True|2023-05-03T18:38:41.0925495+04:30;True|2023-05-03T18:22:56.1278337+04:30;True|2023-04-07T19:23:03.7575436+04:30;True|2023-04-07T19:01:53.8374192+04:30;True|2023-04-07T18:56:48.3465035+04:30;True|2023-02-23T19:42:36.6931238+03:30;True|2023-02-21T23:02:29.4417103+03:30;True|2023-02-21T22:58:29.4403662+03:30;True|2023-02-21T22:48:24.4128535+03:30;True|2023-02-21T22:27:17.9388815+03:30;True|2023-02-20T17:48:08.6532315+03:30;True|2023-02-16T01:42:22.8837631+03:30;True|2023-02-16T01:39:16.7954793+03:30;True|2023-02-15T21:24:18.8085514+03:30;True|2023-02-15T21:18:04.1969211+03:30;True|2023-02-15T21:15:01.3739223+03:30;True|2023-02-08T06:22:33.0414550+03:30;True|2023-02-08T05:20:18.9270105+03:30;True|2023-02-08T05:15:17.5374116+03:30;True|2023-02-08T05:03:44.0882459+03:30;True|2023-02-08T04:53:40.5516012+03:30;True|2023-02-08T02:23:19.0633758+03:30; + True|2023-05-27T12:55:56.0498058Z;True|2023-05-27T14:50:19.0862471+04:30;True|2023-05-26T00:33:02.8133052+04:30;True|2023-05-26T00:22:55.5762345+04:30;True|2023-05-19T21:55:55.2053137+04:30;True|2023-05-04T21:14:13.1959990+04:30;True|2023-05-03T18:38:41.0925495+04:30;True|2023-05-03T18:22:56.1278337+04:30;True|2023-04-07T19:23:03.7575436+04:30;True|2023-04-07T19:01:53.8374192+04:30;True|2023-04-07T18:56:48.3465035+04:30;True|2023-02-23T19:42:36.6931238+03:30;True|2023-02-21T23:02:29.4417103+03:30;True|2023-02-21T22:58:29.4403662+03:30;True|2023-02-21T22:48:24.4128535+03:30;True|2023-02-21T22:27:17.9388815+03:30;True|2023-02-20T17:48:08.6532315+03:30;True|2023-02-16T01:42:22.8837631+03:30;True|2023-02-16T01:39:16.7954793+03:30;True|2023-02-15T21:24:18.8085514+03:30;True|2023-02-15T21:18:04.1969211+03:30;True|2023-02-15T21:15:01.3739223+03:30;True|2023-02-08T06:22:33.0414550+03:30;True|2023-02-08T05:20:18.9270105+03:30;True|2023-02-08T05:15:17.5374116+03:30;True|2023-02-08T05:03:44.0882459+03:30;True|2023-02-08T04:53:40.5516012+03:30;True|2023-02-08T02:23:19.0633758+03:30; \ No newline at end of file diff --git a/SecureDNSClient/SecureDNSClient.csproj b/SecureDNSClient/SecureDNSClient.csproj index c1d9fcf..c414f16 100644 --- a/SecureDNSClient/SecureDNSClient.csproj +++ b/SecureDNSClient/SecureDNSClient.csproj @@ -9,7 +9,7 @@ app.manifest MSasanMH SDC - Secure DNS Client - $(VersionPrefix)2.1.1 + $(VersionPrefix)2.1.2 SecureDNSClient.png SecureDNSClientMulti.ico @@ -67,7 +67,7 @@ - + diff --git a/SecureDNSClientPortable/Properties/PublishProfiles/FolderProfile.pubxml.user b/SecureDNSClientPortable/Properties/PublishProfiles/FolderProfile.pubxml.user index 3e38da5..9f3f446 100644 --- a/SecureDNSClientPortable/Properties/PublishProfiles/FolderProfile.pubxml.user +++ b/SecureDNSClientPortable/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -4,6 +4,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - True|2023-05-25T19:52:04.4226759Z;True|2023-05-19T21:55:03.6988953+04:30;True|2023-05-04T21:13:44.0531282+04:30;True|2023-05-03T18:20:58.6158351+04:30;True|2023-04-07T19:22:18.1443242+04:30;True|2023-04-07T19:01:23.1687551+04:30;True|2023-04-07T18:56:25.8534493+04:30;True|2023-02-23T19:42:03.7111255+03:30;True|2023-02-21T22:26:35.4429594+03:30;True|2023-02-20T17:47:25.0792312+03:30;True|2023-02-16T01:42:37.6467629+03:30;True|2023-02-15T22:16:38.0474815+03:30;True|2023-02-15T21:50:25.4952184+03:30;True|2023-02-15T21:49:01.8562159+03:30; + True|2023-05-27T12:55:27.9631407Z;True|2023-05-27T14:50:40.9938592+04:30;True|2023-05-26T00:22:04.4226759+04:30;True|2023-05-19T21:55:03.6988953+04:30;True|2023-05-04T21:13:44.0531282+04:30;True|2023-05-03T18:20:58.6158351+04:30;True|2023-04-07T19:22:18.1443242+04:30;True|2023-04-07T19:01:23.1687551+04:30;True|2023-04-07T18:56:25.8534493+04:30;True|2023-02-23T19:42:03.7111255+03:30;True|2023-02-21T22:26:35.4429594+03:30;True|2023-02-20T17:47:25.0792312+03:30;True|2023-02-16T01:42:37.6467629+03:30;True|2023-02-15T22:16:38.0474815+03:30;True|2023-02-15T21:50:25.4952184+03:30;True|2023-02-15T21:49:01.8562159+03:30; \ No newline at end of file diff --git a/SecureDNSClientPortable/SecureDNSClientPortable.csproj b/SecureDNSClientPortable/SecureDNSClientPortable.csproj index b5caa01..a9a7b8e 100644 --- a/SecureDNSClientPortable/SecureDNSClientPortable.csproj +++ b/SecureDNSClientPortable/SecureDNSClientPortable.csproj @@ -7,7 +7,7 @@ true enable SecureDNSClientMulti.ico - $(VersionPrefix)2.1.1 + $(VersionPrefix)2.1.2 MSasanMH app.manifest