Skip to content

Commit

Permalink
Added DPI bypass auto test.
Browse files Browse the repository at this point in the history
  • Loading branch information
msasanmh committed Feb 23, 2023
1 parent 54dbdbf commit 39bc858
Show file tree
Hide file tree
Showing 47 changed files with 222 additions and 53 deletions.
76 changes: 62 additions & 14 deletions SecureDNSClient/Forms/FormMain.Designer.cs

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

111 changes: 105 additions & 6 deletions SecureDNSClient/Forms/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ public partial class FormMain : Form
private HTTPProxyServer? HTTPProxy;
private bool AudioAlertOnline = true;
private bool AudioAlertOffline = false;
private readonly Stopwatch CheckDPIWorksStopWatch = new();

public FormMain()
{
InitializeComponent();
CustomStatusStrip1.SizingGrip = false;

// Rightclick on NotifyIcon
ToolStripMenuItemIcon.Text = "Exit";
ToolStripMenuItemIcon.Click += ToolStripMenuItemIcon_Click;
Expand Down Expand Up @@ -205,6 +206,7 @@ private void DefaultSettings()
CustomTextBoxBootstrapDNS.Text = "8.8.8.8";
CustomCheckBoxDontAskCertificate.Checked = false;
CustomCheckBoxSettingDisableAudioAlert.Checked = false;
CustomTextBoxCheckDPIHost.Text = "youtube.com";
}

//============================== Methods
Expand Down Expand Up @@ -629,7 +631,7 @@ private async Task CheckServers()
if (StopChecking) return;

// Write status to log
string status = dnsOK ? "OK" : "Faild";
string status = dnsOK ? "OK" : "Failed";
Color color = dnsOK ? Color.MediumSeaGreen : Color.IndianRed;
object resultStatus = "[" + status + "]";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(resultStatus.ToString().IsNotNull(), color));
Expand Down Expand Up @@ -1036,7 +1038,7 @@ private async void ConnectDNSCrypt()
}

//============================== DPI
private void DPIBasic()
private async void DPIBasic()
{
// Write Connect first to log
if (!IsConnected)
Expand Down Expand Up @@ -1131,12 +1133,18 @@ private void DPIBasic()
// Update Groupbox Status
UpdateStatus();

// Set DPI Active true
IsDPIActive = true;

// Go to SetDNS Tab if it's not already set
if (ConnectAllClicked && !IsDNSSet)
{
this.InvokeIt(() => CustomTabControlMain.SelectedIndex = 0);
this.InvokeIt(() => CustomTabControlSecureDNS.SelectedIndex = 3);
}

// Check DPI works
await CheckDPIWorks(CustomTextBoxCheckDPIHost.Text);
}
else
{
Expand All @@ -1146,7 +1154,7 @@ private void DPIBasic()
}
}

private void DPIAdvanced()
private async void DPIAdvanced()
{
// Write Connect first to log
if (!IsConnected)
Expand Down Expand Up @@ -1322,12 +1330,18 @@ private void DPIAdvanced()
// Update Groupbox Status
UpdateStatus();

// Set DPI Active true
IsDPIActive = true;

// Go to SetDNS Tab if it's not already set
if (ConnectAllClicked && !IsDNSSet)
{
this.InvokeIt(() => CustomTabControlMain.SelectedIndex = 0);
this.InvokeIt(() => CustomTabControlSecureDNS.SelectedIndex = 3);
}

// Check DPI works
await CheckDPIWorks(CustomTextBoxCheckDPIHost.Text);
}
else
{
Expand All @@ -1352,9 +1366,91 @@ private void DPIDeactive()
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDC, Color.LightGray));
}
}

private async Task CheckDPIWorks(string host, int timeoutSec = 20)
{
if (string.IsNullOrWhiteSpace(host)) return;

// If user changing DPI mode fast, return.
if (CheckDPIWorksStopWatch.IsRunning)
return;

// Start StopWatch
CheckDPIWorksStopWatch.Start();

// Write start DPI checking to log
string msgDPI = $"Checking DPI Bypass ({host})...{Environment.NewLine}";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI, Color.LightGray));

if (IsDNSSet)
{
if (IsDPIActive)
{
try
{
string url = $"https://{host}";
Uri uri = new(url);
using HttpClient httpClient = new();
httpClient.Timeout = new TimeSpan(0, 0, timeoutSec);
HttpResponseMessage checkingResponse = await httpClient.GetAsync(uri);
if (checkingResponse.IsSuccessStatusCode)
{
// Write Success to log
var elapsedTime = Math.Round((double)CheckDPIWorksStopWatch.ElapsedMilliseconds / 1000);
string msgDPI1 = $"DPI Check: ";
string msgDPI2 = $"Successfully opened {host} in {elapsedTime} seconds.{Environment.NewLine}";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI1, Color.LightGray));
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI2, Color.MediumSeaGreen));
}
else
{
// Write Failed to log
string msgDPI1 = $"DPI Check: ";
string msgDPI2 = $"Faild to open {host}. Change DPI mode.{Environment.NewLine}";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI1, Color.LightGray));
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI2, Color.IndianRed));
}
CheckDPIWorksStopWatch.Stop();
CheckDPIWorksStopWatch.Reset();
}
catch (Exception ex)
{
// Write Failed to log
string msgDPI1 = $"DPI Check: ";
string msgDPI2 = $"Faild to open {host}. Change DPI mode.{Environment.NewLine}";
string msgDPI3 = $"({ex.Message}){Environment.NewLine}";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI1, Color.LightGray));
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI2, Color.IndianRed));
//this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI3, Color.IndianRed)); // No need to write this message
CheckDPIWorksStopWatch.Stop();
CheckDPIWorksStopWatch.Reset();
}
}
else
{
// Write activate DPI first to log
string msgDPI1 = $"DPI Check: ";
string msgDPI2 = $"Activate DPI first.{Environment.NewLine}";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI1, Color.LightGray));
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI2, Color.IndianRed));
CheckDPIWorksStopWatch.Stop();
CheckDPIWorksStopWatch.Reset();
}
}
else
{
// Write set DNS first to log
string msgDPI1 = $"DPI Check: ";
string msgDPI2 = $"Set DNS first.{Environment.NewLine}";
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI1, Color.LightGray));
this.InvokeIt(() => CustomRichTextBoxLog.AppendText(msgDPI2, Color.IndianRed));
CheckDPIWorksStopWatch.Stop();
CheckDPIWorksStopWatch.Reset();
}
}

//============================== DNS
private void SetDNS()
private async void SetDNS()
{
string? nicName = CustomComboBoxNICs.SelectedItem as string;
// Check if NIC Name is empty
Expand Down Expand Up @@ -1391,6 +1487,7 @@ private void SetDNS()
// Set DNS
Network.SetDNS(nic, dnss);
IsDNSSet = true;

// Save NIC name to file
FileDirectory.CreateEmptyFile(SecureDNS.NicNamePath);
File.WriteAllText(SecureDNS.NicNamePath, nicName);
Expand All @@ -1408,14 +1505,16 @@ private void SetDNS()
CustomRichTextBoxLog.AppendText(msg3, Color.LightGray);
CustomRichTextBoxLog.AppendText(msg4 + Environment.NewLine, Color.DodgerBlue);


// Go to Check Tab
if (ConnectAllClicked && IsConnected)
{
this.InvokeIt(() => CustomTabControlMain.SelectedIndex = 0);
this.InvokeIt(() => CustomTabControlSecureDNS.SelectedIndex = 0);
ConnectAllClicked = false;
}

// Check DPI works
await CheckDPIWorks(CustomTextBoxCheckDPIHost.Text);
}
else
{
Expand Down
8 changes: 8 additions & 0 deletions SecureDNSClient/MsmhTools/Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public static class Info
public static FileVersionInfo InfoCallingAssembly => FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location);
public static FileVersionInfo InfoEntryAssembly => FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);
public static FileVersionInfo InfoExecutingAssembly => FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);

public static string GetAppGUID()
{
Assembly assembly = Assembly.GetExecutingAssembly();
GuidAttribute attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
return attribute.Value;
}

public static bool IsRunningOnWindows
{
get
Expand Down
14 changes: 14 additions & 0 deletions SecureDNSClient/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using MsmhTools;

namespace SecureDNSClient
{
internal static class Program
Expand All @@ -11,6 +13,18 @@ static void Main()
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();

// Prevent multiple instances
using Mutex mutex = new(false, Info.InfoExecutingAssembly.ProductName);
if (!mutex.WaitOne(0, true))
{
MessageBox.Show($"{Info.InfoExecutingAssembly.ProductName} is already running.");
Environment.Exit(0);
Application.Exit();
return;
}
GC.KeepAlive(mutex);

Application.Run(new FormMain());
}
}
Expand Down
Loading

0 comments on commit 39bc858

Please sign in to comment.