This commit is contained in:
EonaCat 2023-03-02 15:27:51 +01:00
parent 4afe9153ed
commit d3aef63feb
3 changed files with 102 additions and 48 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace EonaCat.DnsTester.Helpers namespace EonaCat.DnsTester.Helpers
{ {
@ -12,7 +13,7 @@ namespace EonaCat.DnsTester.Helpers
private static readonly RandomNumberGenerator _randomNumberGenerator = RandomNumberGenerator.Create(); private static readonly RandomNumberGenerator _randomNumberGenerator = RandomNumberGenerator.Create();
public static event EventHandler<string> Log; public static event EventHandler<string> Log;
public static void GetRandomUrls(ref List<string> urlList, int totalUrls) private static async Task<List<string>> GetRandomUrls(int totalUrls)
{ {
var letters = GetRandomLetters(); var letters = GetRandomLetters();
@ -30,8 +31,7 @@ namespace EonaCat.DnsTester.Helpers
Random rand = new Random(); Random rand = new Random();
List<string> urls = urlList; List<string> urls = new List<string>();
while (urls.Count < totalUrls) while (urls.Count < totalUrls)
{ {
int index = rand.Next(searchEngineUrls.Count); int index = rand.Next(searchEngineUrls.Count);
@ -73,6 +73,8 @@ namespace EonaCat.DnsTester.Helpers
} }
} }
} }
await Task.Delay(100);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -85,9 +87,27 @@ namespace EonaCat.DnsTester.Helpers
} }
} }
urlList = urls; var urlText = "url" + (urls.Count > 1 ? "'s" : string.Empty);
var urlText = "url" + (urlList.Count > 1 ? "'s" : string.Empty); SetStatus($"{urls.Count} random {urlText} found");
SetStatus($"{urlList.Count} random {urlText} found"); return urls;
}
public static async Task<List<string>> RetrieveUrls(int numThreads, int numUrlsPerThread)
{
Task[] tasks = new Task[numThreads];
List<string> urlList = new List<string>();
// start each thread to retrieve a subset of unique URLs
for (int i = 0; i < numThreads; i++)
{
tasks[i] = Task.Run(async () => urlList.AddRange(await GetRandomUrls(numUrlsPerThread)));
}
// wait for all threads to complete
await Task.WhenAll(tasks);
return urlList;
} }
private static string GetRandomLetters() private static string GetRandomLetters()

View File

@ -60,10 +60,13 @@
txtResolveIP = new System.Windows.Forms.TextBox(); txtResolveIP = new System.Windows.Forms.TextBox();
label5 = new System.Windows.Forms.Label(); label5 = new System.Windows.Forms.Label();
label1 = new System.Windows.Forms.Label(); label1 = new System.Windows.Forms.Label();
label2 = new System.Windows.Forms.Label();
numericUpDown2 = new System.Windows.Forms.NumericUpDown();
panel1.SuspendLayout(); panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)numericUpDown1).BeginInit(); ((System.ComponentModel.ISupportInitialize)numericUpDown1).BeginInit();
panel2.SuspendLayout(); panel2.SuspendLayout();
panel3.SuspendLayout(); panel3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)numericUpDown2).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// RunTest // RunTest
@ -81,6 +84,8 @@
// panel1 // panel1
// //
panel1.BackColor = System.Drawing.SystemColors.ControlLight; panel1.BackColor = System.Drawing.SystemColors.ControlLight;
panel1.Controls.Add(numericUpDown2);
panel1.Controls.Add(label2);
panel1.Controls.Add(comboBox1); panel1.Controls.Add(comboBox1);
panel1.Controls.Add(numericUpDown1); panel1.Controls.Add(numericUpDown1);
panel1.Controls.Add(label3); panel1.Controls.Add(label3);
@ -380,6 +385,25 @@
label1.TabIndex = 66; label1.TabIndex = 66;
label1.Text = "Resolve ip address:"; label1.Text = "Resolve ip address:";
// //
// label2
//
label2.AutoSize = true;
label2.Location = new System.Drawing.Point(566, 228);
label2.Name = "label2";
label2.Size = new System.Drawing.Size(201, 41);
label2.TabIndex = 60;
label2.Text = "Total Threads:";
//
// numericUpDown2
//
numericUpDown2.Location = new System.Drawing.Point(804, 228);
numericUpDown2.Maximum = new decimal(new int[] { 1000000000, 0, 0, 0 });
numericUpDown2.Minimum = new decimal(new int[] { 1, 0, 0, 0 });
numericUpDown2.Name = "numericUpDown2";
numericUpDown2.Size = new System.Drawing.Size(120, 47);
numericUpDown2.TabIndex = 61;
numericUpDown2.Value = new decimal(new int[] { 5, 0, 0, 0 });
//
// MainForm // MainForm
// //
AutoScaleDimensions = new System.Drawing.SizeF(240F, 240F); AutoScaleDimensions = new System.Drawing.SizeF(240F, 240F);
@ -404,6 +428,7 @@
panel2.ResumeLayout(false); panel2.ResumeLayout(false);
panel3.ResumeLayout(false); panel3.ResumeLayout(false);
panel3.PerformLayout(); panel3.PerformLayout();
((System.ComponentModel.ISupportInitialize)numericUpDown2).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -439,6 +464,8 @@
private System.Windows.Forms.TextBox txtResolveIP; private System.Windows.Forms.TextBox txtResolveIP;
private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label1;
private System.Windows.Forms.NumericUpDown numericUpDown2;
private System.Windows.Forms.Label label2;
} }
} }

View File

@ -46,10 +46,13 @@ namespace EonaCat.DnsTester
List<string> urls = new List<string>(); List<string> urls = new List<string>();
SetupView(); SetupView();
await Task.Run(() =>
{ int numThreads = (int)numericUpDown2.Value; // number of concurrent threads to use
UrlHelper.GetRandomUrls(ref urls, (int)numericUpDown1.Value); int maxUrls = (int)numericUpDown1.Value; // maximum number of unique URLs to retrieve
}); int numUrlsPerThread = maxUrls / numThreads;
urls = await UrlHelper.RetrieveUrls(numThreads, numUrlsPerThread);
AddUrlToView(urls); AddUrlToView(urls);
@ -196,12 +199,14 @@ namespace EonaCat.DnsTester
for (int i = 0; i < urlsTotal; i++) for (int i = 0; i < urlsTotal; i++)
{ {
var currentUrl = urls[i]; await Task.Run(async () =>
{
await ExecuteDns1(recordType, dnsAddress1, currentUrl, dnsId1, i); var currentUrl = urls[i];
if (!chkDns2.Checked) continue;
await ExecuteDns2(recordType, dnsAddress2, currentUrl, dnsId2, i);
await ExecuteDns1(recordType, dnsAddress1, currentUrl, dnsId1, i);
if (!chkDns2.Checked) return;
await ExecuteDns2(recordType, dnsAddress2, currentUrl, dnsId2, i);
});
await Task.Delay(100); await Task.Delay(100);
} }
} }
@ -279,45 +284,47 @@ namespace EonaCat.DnsTester
$"ResourceRecord: Name: {answer.Name} : Type : {answer.Type} : Data : {answer.Data}"); $"ResourceRecord: Name: {answer.Name} : Type : {answer.Type} : Data : {answer.Data}");
} }
ResultView.Invoke(() =>
for (int i = 0; i < ResultView.Items.Count; i++)
{ {
foreach (var answer in dnsResponse.Answers) for (int i = 0; i < ResultView.Items.Count; i++)
{ {
if (ResultView.Items[i].Text != $"{answer.Name.TrimEnd('.')}") foreach (var answer in dnsResponse.Answers)
continue;
string sDeltaTime;
switch (dnsResponse.DnsId)
{ {
case "Dns1": if (ResultView.Items[i].Text != $"{answer.Name.TrimEnd('.')}")
ResultView.Items[i].SubItems[1].Text = continue;
Convert.ToString(answer.Data);
sDeltaTime = Convert.ToString(deltaTime);
ResultView.Items[i].SubItems[2].Text =
sDeltaTime.Length > 5 ? sDeltaTime.Substring(0, 5) : sDeltaTime;
ResultView.Items[i].ForeColor = System.Drawing.Color.Red;
ResultView.EnsureVisible(i);
ResultView.Update();
Application.DoEvents();
ResultView.Items[i].ForeColor = System.Drawing.Color.Black;
break;
case "Dns2": string sDeltaTime;
ResultView.Items[i].SubItems[3].Text = switch (dnsResponse.DnsId)
Convert.ToString(answer.Data); {
sDeltaTime = Convert.ToString(deltaTime); case "Dns1":
ResultView.Items[i].SubItems[4].Text = ResultView.Items[i].SubItems[1].Text =
sDeltaTime.Length > 5 ? sDeltaTime.Substring(0, 5) : sDeltaTime; Convert.ToString(answer.Data);
ResultView.Items[i].ForeColor = System.Drawing.Color.Red; sDeltaTime = Convert.ToString(deltaTime);
ResultView.EnsureVisible(i); ResultView.Items[i].SubItems[2].Text =
ResultView.Update(); sDeltaTime.Length > 5 ? sDeltaTime.Substring(0, 5) : sDeltaTime;
Application.DoEvents(); ResultView.Items[i].ForeColor = System.Drawing.Color.Red;
ResultView.Items[i].ForeColor = System.Drawing.Color.Black; ResultView.EnsureVisible(i);
break; ResultView.Update();
Application.DoEvents();
ResultView.Items[i].ForeColor = System.Drawing.Color.Black;
break;
case "Dns2":
ResultView.Items[i].SubItems[3].Text =
Convert.ToString(answer.Data);
sDeltaTime = Convert.ToString(deltaTime);
ResultView.Items[i].SubItems[4].Text =
sDeltaTime.Length > 5 ? sDeltaTime.Substring(0, 5) : sDeltaTime;
ResultView.Items[i].ForeColor = System.Drawing.Color.Red;
ResultView.EnsureVisible(i);
ResultView.Update();
Application.DoEvents();
ResultView.Items[i].ForeColor = System.Drawing.Color.Black;
break;
}
} }
} }
} });
} }
private async void btnResolveIP_Click(object sender, EventArgs e) private async void btnResolveIP_Click(object sender, EventArgs e)