Skip to content

Commit

Permalink
v2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
msasanmh committed May 27, 2023
1 parent d036380 commit 934840a
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 34 deletions.
35 changes: 18 additions & 17 deletions SecureDNSClient/Forms/FormMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 58 additions & 9 deletions SecureDNSClient/Forms/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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}";
Expand All @@ -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);
Expand All @@ -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();
Expand Down
80 changes: 77 additions & 3 deletions SecureDNSClient/MsmhTools/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,34 @@ public static List<NetworkInterface> 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");
Expand Down Expand Up @@ -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
{
Expand All @@ -608,6 +644,44 @@ public static void UnsetDNS(NetworkInterface nic)
}
}

/// <summary>
/// Check if DNS is set to Static or DHCP
/// </summary>
/// <param name="nic">Network Interface</param>
/// <param name="dnsServer1">Primary DNS Server</param>
/// <param name="dnsServer2">Secondary DNS Server</param>
/// <returns>True = Static, False = DHCP</returns>
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<string> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>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;</History>
<History>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;</History>
</PropertyGroup>
</Project>
4 changes: 2 additions & 2 deletions SecureDNSClient/SecureDNSClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<Copyright>MSasanMH</Copyright>
<Product>SDC - Secure DNS Client</Product>
<Version>$(VersionPrefix)2.1.1</Version>
<Version>$(VersionPrefix)2.1.2</Version>
<PackageIcon>SecureDNSClient.png</PackageIcon>
<ApplicationIcon>SecureDNSClientMulti.ico</ApplicationIcon>
</PropertyGroup>
Expand Down Expand Up @@ -67,7 +67,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ARSoft.Tools.Net" Version="3.2.0" />
<PackageReference Include="ARSoft.Tools.Net" Version="3.3.0" />
<PackageReference Include="DnsCrypt.Stamps" Version="0.2.2" />
<PackageReference Include="System.Management" Version="8.0.0-preview.4.23259.5" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>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;</History>
<History>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;</History>
</PropertyGroup>
</Project>
Loading

0 comments on commit 934840a

Please sign in to comment.