From cd376044d57368d1f016f5bcbad1aba210d4543c Mon Sep 17 00:00:00 2001 From: EonaCat Date: Wed, 1 Mar 2023 21:15:40 +0100 Subject: [PATCH] Updated --- EonaCat.DnsTester/Helpers/DnsHelper.cs | 142 ++--- EonaCat.DnsTester/Helpers/DnsNameParser.cs | 33 ++ EonaCat.DnsTester/Helpers/UrlHelper.cs | 17 +- EonaCat.DnsTester/MainForm.Designer.cs | 650 +++++++++++---------- EonaCat.DnsTester/MainForm.cs | 123 ++-- 5 files changed, 516 insertions(+), 449 deletions(-) create mode 100644 EonaCat.DnsTester/Helpers/DnsNameParser.cs diff --git a/EonaCat.DnsTester/Helpers/DnsHelper.cs b/EonaCat.DnsTester/Helpers/DnsHelper.cs index 9a337ed..58fa434 100644 --- a/EonaCat.DnsTester/Helpers/DnsHelper.cs +++ b/EonaCat.DnsTester/Helpers/DnsHelper.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Net; using System.Net.Sockets; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -21,13 +22,26 @@ namespace EonaCat.DnsTester.Helpers client.DontFragment = true; client.EnableBroadcast = false; client.Client.ReceiveTimeout = DnsReceiveTimeout; - await client.SendAsync(queryBytes, queryBytes.Length, endPoint); - var responseResult = await client.ReceiveAsync(); - DnsResponse response = ParseDnsResponsePacket(dnsId, startTime, server, responseResult.Buffer); + byte[] responseBytes = null; + if (FakeResponse) + { + responseBytes = DnsHelper.GetExampleResponse(); + } + else + { + await client.SendAsync(queryBytes, queryBytes.Length, endPoint); + var responseResult = await client.ReceiveAsync(); + responseBytes = responseResult.Buffer; + } + + DnsResponse response = ParseDnsResponsePacket(dnsId, startTime, server, responseBytes); return response; } } + // For testing purposes + public static bool FakeResponse { get; set; } + public static int DnsReceiveTimeout { get; set; } = 5; @@ -84,7 +98,7 @@ namespace EonaCat.DnsTester.Helpers static DnsQuestion ParseDnsQuestionRecord(byte[] queryBytes, ref int offset) { // Parse the DNS name - string name = ParseDnsName(queryBytes, ref offset); + string name = DnsNameParser.ParseName(queryBytes, ref offset); if (name == null) { return null; @@ -103,8 +117,38 @@ namespace EonaCat.DnsTester.Helpers }; } + public static byte[] GetExampleResponse() + { + // Example response bytes for the A record of google.com + byte[] response = { + 0x9d, 0xa9, // Query ID + 0x81, 0x80, // Flags + 0x00, 0x01, // Questions: 1 + 0x00, 0x01, // Answer RRs: 1 + 0x00, 0x00, // Authority RRs: 0 + 0x00, 0x00, // Additional RRs: 0 + 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, // Query: google + 0x03, 0x63, 0x6f, 0x6d, // Query: com + 0x00, + 0x00, 0x01, // Record type: A + 0x00, 0x01, // Record class: IN + 0xc0, 0x0c, // Name pointer to google.com + 0x00, 0x01, // Record type: A + 0x00, 0x01, // Record class: IN + 0x00, 0x00, 0x00, 0x3d, // TTL: 61 seconds + 0x00, 0x04, // Data length: 4 bytes + 0xac, 0xd9, 0x03, 0x3d // Data: 172.217.3.61 + }; + return response; + } + static DnsResponse ParseDnsResponsePacket(string dnsId, long startTime, string server, byte[] responseBytes) { + Console.WriteLine("new byte[] { " + responseBytes + " }"); + + // Set the offset to the start + var offset = 0; + // Parse the DNS header ushort id = (ushort)((responseBytes[0] << 8) | responseBytes[1]); ushort flags = (ushort)((responseBytes[2] << 8) | responseBytes[3]); @@ -112,19 +156,20 @@ namespace EonaCat.DnsTester.Helpers ushort ancount = (ushort)((responseBytes[6] << 8) | responseBytes[7]); ushort nscount = (ushort)((responseBytes[8] << 8) | responseBytes[9]); ushort arcount = (ushort)((responseBytes[10] << 8) | responseBytes[11]); + + // We parsed the header set the offset past the header + offset = 12; List questions = new List(); - //for (int i = 0; i < qdcount; i++) - //{ - // DnsQuestion question = ParseDnsQuestionRecord(responseBytes, ref offset); - // if (question != null) - // { - // questions.Add(question); - // } - //} - - int offset = 12; + for (int i = 0; i < qdcount; i++) + { + DnsQuestion question = ParseDnsQuestionRecord(responseBytes, ref offset); + if (question != null) + { + questions.Add(question); + } + } // Parse the DNS answer records List answers = new List(); @@ -198,17 +243,17 @@ namespace EonaCat.DnsTester.Helpers static ResourceRecord ParseDnsAnswerRecord(byte[] responseBytes, ref int offset) { // Parse the DNS name - string name = ParseDnsName(responseBytes, ref offset); + string name = DnsNameParser.ParseName(responseBytes, ref offset); if (name == null) { return null; } // Parse the DNS type, class, ttl, and data length - DnsRecordType type = (DnsRecordType)((responseBytes[offset++] << 8) | responseBytes[offset++]); - DnsRecordClass klass = (DnsRecordClass)((responseBytes[offset++] << 8) | responseBytes[offset++]); - uint ttl = (uint)((responseBytes[offset++] << 24) | (responseBytes[offset++] << 16) | (responseBytes[offset++] << 8) | responseBytes[offset++]); - ushort rdlen = (ushort)((responseBytes[responseBytes.Length - 5])); + DnsRecordType type = (DnsRecordType)((responseBytes[offset] << 8) | responseBytes[offset++]); + DnsRecordClass klass = (DnsRecordClass)((responseBytes[offset] << 8) | responseBytes[offset++]); + uint ttl = (uint)((responseBytes[offset] << 24) | (responseBytes[offset++] << 16) | (responseBytes[offset++] << 8) | responseBytes[offset++]); + ushort rdlen = (ushort)((responseBytes[offset] << 8) + responseBytes[offset++]); offset += 10; // Parse the DNS data @@ -230,7 +275,7 @@ namespace EonaCat.DnsTester.Helpers break; case DnsRecordType.CNAME: case DnsRecordType.NS: - data = ParseDnsName(responseBytes, ref offset); + data = DnsNameParser.ParseName(responseBytes, ref offset); if (data == null) { return null; @@ -240,7 +285,7 @@ namespace EonaCat.DnsTester.Helpers case DnsRecordType.MX: ushort preference = (ushort)((responseBytes[offset] << 8) | responseBytes[offset + 1]); offset += 2; - string exchange = ParseDnsName(responseBytes, ref offset); + string exchange = DnsNameParser.ParseName(responseBytes, ref offset); if (exchange == null) { return null; @@ -267,60 +312,15 @@ namespace EonaCat.DnsTester.Helpers }; } - static string ParseDnsName(byte[] responseBytes, ref int offset) + static string GetString(byte[] bytes, ref int index, int length) { - StringBuilder name = new StringBuilder(); - int originalOffset = offset; - while (true) + string str = ""; + for (int i = 0; i < length; i++) { - byte len = responseBytes[offset++]; - if (len == 0) - { - break; - } - - if ((len & 0xc0) == 0xc0) - { - ushort pointer = (ushort)(((len & 0x3f) << 8) | responseBytes[offset++]); - int pointerOffset = pointer & 0x3fff; - if (pointerOffset >= responseBytes.Length) - { - return null; - } - - int pointerEnd = offset; - offset = pointerOffset; - if (offset < originalOffset) - { - return null; - } - - name.Append(ParseDnsName(responseBytes, ref offset)); - offset = pointerEnd; - break; - } - - if (len > 63) - { - return null; - } - - name.Append(Encoding.ASCII.GetString(responseBytes, offset, len)); - offset += len; - if (responseBytes[offset - 1] != 0) - { - name.Append("."); - } + str += (char)bytes[index++]; } - - if (originalOffset == offset) - { - return null; - } - - return name.ToString(); + return str; } - } public class DnsQuestion diff --git a/EonaCat.DnsTester/Helpers/DnsNameParser.cs b/EonaCat.DnsTester/Helpers/DnsNameParser.cs new file mode 100644 index 0000000..f41a8c1 --- /dev/null +++ b/EonaCat.DnsTester/Helpers/DnsNameParser.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; + +namespace EonaCat.DnsTester.Helpers +{ + public class DnsNameParser + { + public static string ParseName(byte[] responseBytes, ref int offset) + { + List labels = new List(); + int length; + + while ((length = responseBytes[offset++]) != 0) + { + if ((length & 0xC0) == 0xC0) + { + // The name is compressed + int pointer = ((length & 0x3F) << 8) | responseBytes[offset++]; + int savedOffset = offset; + offset = pointer; + labels.AddRange(ParseName(responseBytes, ref offset).Split('.')); + offset = savedOffset; + break; + } + + // The name is not compressed + labels.Add(System.Text.Encoding.ASCII.GetString(responseBytes, offset, length)); + offset += length; + } + + return string.Join(".", labels); + } + } +} diff --git a/EonaCat.DnsTester/Helpers/UrlHelper.cs b/EonaCat.DnsTester/Helpers/UrlHelper.cs index cc83003..95a5a3c 100644 --- a/EonaCat.DnsTester/Helpers/UrlHelper.cs +++ b/EonaCat.DnsTester/Helpers/UrlHelper.cs @@ -3,9 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Security.Cryptography; -using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; namespace EonaCat.DnsTester.Helpers { @@ -32,9 +30,9 @@ namespace EonaCat.DnsTester.Helpers Random rand = new Random(); - List localCleanNames = urlList; + List urls = urlList; - while (localCleanNames.Count < totalUrls) + while (urls.Count < totalUrls) { int index = rand.Next(searchEngineUrls.Count); KeyValuePair searchEngine = searchEngineUrls.ElementAt(index); @@ -64,14 +62,14 @@ namespace EonaCat.DnsTester.Helpers // Add the names to the list foreach (string name in uniqueNames) { - if (localCleanNames.Count >= totalUrls) + if (urls.Count >= totalUrls) { break; } - if (!localCleanNames.Contains(name)) + if (!urls.Contains(name)) { - localCleanNames.Add(name); + urls.Add(name); } } } @@ -87,8 +85,9 @@ namespace EonaCat.DnsTester.Helpers } } - urlList = localCleanNames; // Update the reference to the list - SetStatus($"{urlList.Count} Random URL's found"); + urlList = urls; + var urlText = "url" + (urlList.Count > 1 ? "'s" : string.Empty); + SetStatus($"{urlList.Count} random {urlText} found"); } private static string GetRandomLetters() diff --git a/EonaCat.DnsTester/MainForm.Designer.cs b/EonaCat.DnsTester/MainForm.Designer.cs index 6f29e22..318cb67 100644 --- a/EonaCat.DnsTester/MainForm.Designer.cs +++ b/EonaCat.DnsTester/MainForm.Designer.cs @@ -29,56 +29,266 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); - ResultView = new System.Windows.Forms.ListView(); - WebName = new System.Windows.Forms.ColumnHeader(); - DNS1IP = new System.Windows.Forms.ColumnHeader(); - DNS1Timing = new System.Windows.Forms.ColumnHeader(); - DNS2IP = new System.Windows.Forms.ColumnHeader(); - DNS2Timing = new System.Windows.Forms.ColumnHeader(); RunTest = new System.Windows.Forms.Button(); - lblDns1 = new System.Windows.Forms.Label(); - lblDns2 = new System.Windows.Forms.Label(); - dnsList1 = new System.Windows.Forms.ComboBox(); - dnsList2 = new System.Windows.Forms.ComboBox(); - UseCustomServers = new System.Windows.Forms.CheckBox(); - lblCustom1 = new System.Windows.Forms.Label(); - lblCustom2 = new System.Windows.Forms.Label(); - CustomDns1 = new System.Windows.Forms.TextBox(); - CustomDns2 = new System.Windows.Forms.TextBox(); - lslStatus = new System.Windows.Forms.Label(); - StatusBox = new System.Windows.Forms.ListBox(); - chkDns1 = new System.Windows.Forms.CheckBox(); - chkDns2 = new System.Windows.Forms.CheckBox(); - label1 = new System.Windows.Forms.Label(); - label2 = new System.Windows.Forms.Label(); - txtResolveIP = new System.Windows.Forms.TextBox(); - txtResolveHost = new System.Windows.Forms.TextBox(); - btnResolveIP = new System.Windows.Forms.Button(); - btnResolveHost = new System.Windows.Forms.Button(); - label3 = new System.Windows.Forms.Label(); - numericUpDown1 = new System.Windows.Forms.NumericUpDown(); + panel1 = new System.Windows.Forms.Panel(); comboBox1 = new System.Windows.Forms.ComboBox(); + numericUpDown1 = new System.Windows.Forms.NumericUpDown(); + label3 = new System.Windows.Forms.Label(); + chkDns2 = new System.Windows.Forms.CheckBox(); + chkDns1 = new System.Windows.Forms.CheckBox(); + CustomDns2 = new System.Windows.Forms.TextBox(); + CustomDns1 = new System.Windows.Forms.TextBox(); + lblCustom2 = new System.Windows.Forms.Label(); + lblCustom1 = new System.Windows.Forms.Label(); + UseCustomServers = new System.Windows.Forms.CheckBox(); + dnsList2 = new System.Windows.Forms.ComboBox(); + dnsList1 = new System.Windows.Forms.ComboBox(); + lblDns2 = new System.Windows.Forms.Label(); + lblDns1 = new System.Windows.Forms.Label(); + panel2 = new System.Windows.Forms.Panel(); + StatusBox = new System.Windows.Forms.ListBox(); + ResultView = new System.Windows.Forms.ListView(); + Url = new System.Windows.Forms.ColumnHeader(); + DNS1IP = new System.Windows.Forms.ColumnHeader(); + DNS1Performance = new System.Windows.Forms.ColumnHeader(); + DNS2IP = new System.Windows.Forms.ColumnHeader(); + DNS2Performance = new System.Windows.Forms.ColumnHeader(); + panel3 = new System.Windows.Forms.Panel(); + btnResolveHost = new System.Windows.Forms.Button(); + btnResolveIP = new System.Windows.Forms.Button(); + txtResolveHost = new System.Windows.Forms.TextBox(); + txtResolveIP = new System.Windows.Forms.TextBox(); + label5 = new System.Windows.Forms.Label(); + label1 = new System.Windows.Forms.Label(); + panel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)numericUpDown1).BeginInit(); + panel2.SuspendLayout(); + panel3.SuspendLayout(); SuspendLayout(); // + // RunTest + // + RunTest.BackColor = System.Drawing.Color.FromArgb(192, 255, 192); + RunTest.Location = new System.Drawing.Point(1068, 1539); + RunTest.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); + RunTest.Name = "RunTest"; + RunTest.Size = new System.Drawing.Size(435, 115); + RunTest.TabIndex = 12; + RunTest.Text = "Run Test"; + RunTest.UseVisualStyleBackColor = false; + RunTest.Click += RunTest_Click; + // + // panel1 + // + panel1.BackColor = System.Drawing.SystemColors.ControlLight; + panel1.Controls.Add(comboBox1); + panel1.Controls.Add(numericUpDown1); + panel1.Controls.Add(label3); + panel1.Controls.Add(chkDns2); + panel1.Controls.Add(chkDns1); + panel1.Controls.Add(CustomDns2); + panel1.Controls.Add(CustomDns1); + panel1.Controls.Add(lblCustom2); + panel1.Controls.Add(lblCustom1); + panel1.Controls.Add(UseCustomServers); + panel1.Controls.Add(dnsList2); + panel1.Controls.Add(dnsList1); + panel1.Controls.Add(lblDns2); + panel1.Controls.Add(lblDns1); + panel1.Location = new System.Drawing.Point(155, 294); + panel1.Name = "panel1"; + panel1.Size = new System.Drawing.Size(2209, 396); + panel1.TabIndex = 24; + // + // comboBox1 + // + comboBox1.FormattingEnabled = true; + comboBox1.Items.AddRange(new object[] { "A", "NS", "CNAME", "MX", "TXT" }); + comboBox1.Location = new System.Drawing.Point(1583, 71); + comboBox1.Name = "comboBox1"; + comboBox1.Size = new System.Drawing.Size(302, 49); + comboBox1.TabIndex = 59; + comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged; + // + // numericUpDown1 + // + numericUpDown1.Location = new System.Drawing.Point(804, 147); + numericUpDown1.Maximum = new decimal(new int[] { 1000000000, 0, 0, 0 }); + numericUpDown1.Minimum = new decimal(new int[] { 1, 0, 0, 0 }); + numericUpDown1.Name = "numericUpDown1"; + numericUpDown1.Size = new System.Drawing.Size(120, 47); + numericUpDown1.TabIndex = 58; + numericUpDown1.Value = new decimal(new int[] { 20, 0, 0, 0 }); + // + // label3 + // + label3.AutoSize = true; + label3.Location = new System.Drawing.Point(566, 147); + label3.Name = "label3"; + label3.Size = new System.Drawing.Size(210, 41); + label3.TabIndex = 57; + label3.Text = "Total domains:"; + // + // chkDns2 + // + chkDns2.AutoSize = true; + chkDns2.Checked = true; + chkDns2.CheckState = System.Windows.Forms.CheckState.Checked; + chkDns2.Location = new System.Drawing.Point(1071, 147); + chkDns2.Name = "chkDns2"; + chkDns2.Size = new System.Drawing.Size(251, 45); + chkDns2.TabIndex = 45; + chkDns2.Text = "Test dns server"; + chkDns2.UseVisualStyleBackColor = true; + chkDns2.CheckedChanged += chkDns2_CheckedChanged; + // + // chkDns1 + // + chkDns1.AutoSize = true; + chkDns1.Checked = true; + chkDns1.CheckState = System.Windows.Forms.CheckState.Checked; + chkDns1.Location = new System.Drawing.Point(46, 147); + chkDns1.Name = "chkDns1"; + chkDns1.Size = new System.Drawing.Size(251, 45); + chkDns1.TabIndex = 44; + chkDns1.Text = "Test dns server"; + chkDns1.UseVisualStyleBackColor = true; + chkDns1.CheckedChanged += chkDns1_CheckedChanged; + // + // CustomDns2 + // + CustomDns2.Enabled = false; + CustomDns2.Location = new System.Drawing.Point(1318, 297); + CustomDns2.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); + CustomDns2.Name = "CustomDns2"; + CustomDns2.Size = new System.Drawing.Size(668, 47); + CustomDns2.TabIndex = 43; + CustomDns2.Visible = false; + // + // CustomDns1 + // + CustomDns1.BackColor = System.Drawing.Color.White; + CustomDns1.Enabled = false; + CustomDns1.Location = new System.Drawing.Point(299, 291); + CustomDns1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); + CustomDns1.Name = "CustomDns1"; + CustomDns1.Size = new System.Drawing.Size(625, 47); + CustomDns1.TabIndex = 42; + CustomDns1.Visible = false; + // + // lblCustom2 + // + lblCustom2.AutoSize = true; + lblCustom2.Location = new System.Drawing.Point(1065, 301); + lblCustom2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); + lblCustom2.Name = "lblCustom2"; + lblCustom2.Size = new System.Drawing.Size(219, 41); + lblCustom2.TabIndex = 41; + lblCustom2.Text = "Custom Dns 2: "; + lblCustom2.Visible = false; + // + // lblCustom1 + // + lblCustom1.AutoSize = true; + lblCustom1.Location = new System.Drawing.Point(46, 295); + lblCustom1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); + lblCustom1.Name = "lblCustom1"; + lblCustom1.Size = new System.Drawing.Size(219, 41); + lblCustom1.TabIndex = 40; + lblCustom1.Text = "Custom Dns 1: "; + lblCustom1.Visible = false; + // + // UseCustomServers + // + UseCustomServers.AutoSize = true; + UseCustomServers.Location = new System.Drawing.Point(1723, 147); + UseCustomServers.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); + UseCustomServers.Name = "UseCustomServers"; + UseCustomServers.Size = new System.Drawing.Size(262, 45); + UseCustomServers.TabIndex = 39; + UseCustomServers.Text = "Custom Servers"; + UseCustomServers.UseVisualStyleBackColor = true; + UseCustomServers.CheckedChanged += UseCustomServers_CheckedChanged; + // + // dnsList2 + // + dnsList2.FormattingEnabled = true; + dnsList2.ItemHeight = 41; + dnsList2.Location = new System.Drawing.Point(1181, 71); + dnsList2.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); + dnsList2.Name = "dnsList2"; + dnsList2.Size = new System.Drawing.Size(320, 49); + dnsList2.TabIndex = 38; + // + // dnsList1 + // + dnsList1.FormattingEnabled = true; + dnsList1.ItemHeight = 41; + dnsList1.Location = new System.Drawing.Point(168, 71); + dnsList1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); + dnsList1.Name = "dnsList1"; + dnsList1.Size = new System.Drawing.Size(451, 49); + dnsList1.TabIndex = 37; + // + // lblDns2 + // + lblDns2.AutoSize = true; + lblDns2.Location = new System.Drawing.Point(1065, 79); + lblDns2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); + lblDns2.Name = "lblDns2"; + lblDns2.Size = new System.Drawing.Size(100, 41); + lblDns2.TabIndex = 36; + lblDns2.Text = "Dns 2:"; + // + // lblDns1 + // + lblDns1.AutoSize = true; + lblDns1.Location = new System.Drawing.Point(46, 79); + lblDns1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); + lblDns1.Name = "lblDns1"; + lblDns1.Size = new System.Drawing.Size(100, 41); + lblDns1.TabIndex = 35; + lblDns1.Text = "Dns 1:"; + // + // panel2 + // + panel2.BackColor = System.Drawing.SystemColors.ControlLight; + panel2.Controls.Add(StatusBox); + panel2.Controls.Add(ResultView); + panel2.Location = new System.Drawing.Point(145, 765); + panel2.Name = "panel2"; + panel2.Size = new System.Drawing.Size(2219, 745); + panel2.TabIndex = 25; + // + // StatusBox + // + StatusBox.FormattingEnabled = true; + StatusBox.HorizontalScrollbar = true; + StatusBox.ItemHeight = 41; + StatusBox.Location = new System.Drawing.Point(56, 571); + StatusBox.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); + StatusBox.Name = "StatusBox"; + StatusBox.Size = new System.Drawing.Size(2126, 127); + StatusBox.TabIndex = 25; + // // ResultView // - ResultView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { WebName, DNS1IP, DNS1Timing, DNS2IP, DNS2Timing }); + ResultView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { Url, DNS1IP, DNS1Performance, DNS2IP, DNS2Performance }); ResultView.GridLines = true; ResultView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable; - ResultView.Location = new System.Drawing.Point(155, 469); + ResultView.Location = new System.Drawing.Point(56, 49); ResultView.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); ResultView.MultiSelect = false; ResultView.Name = "ResultView"; - ResultView.Size = new System.Drawing.Size(2209, 663); - ResultView.TabIndex = 11; + ResultView.Size = new System.Drawing.Size(2126, 487); + ResultView.TabIndex = 24; ResultView.UseCompatibleStateImageBehavior = false; ResultView.View = System.Windows.Forms.View.Details; // - // WebName + // Url // - WebName.Text = "URL"; - WebName.Width = 160; + Url.Text = "URL"; + Url.Width = 160; // // DNS1IP // @@ -86,11 +296,11 @@ DNS1IP.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; DNS1IP.Width = 140; // - // DNS1Timing + // DNS1Performance // - DNS1Timing.Text = "DNS 1 Timing"; - DNS1Timing.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; - DNS1Timing.Width = 120; + DNS1Performance.Text = "DNS 1 Performance"; + DNS1Performance.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + DNS1Performance.Width = 120; // // DNS2IP // @@ -98,242 +308,77 @@ DNS2IP.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; DNS2IP.Width = 140; // - // DNS2Timing + // DNS2Performance // - DNS2Timing.Text = "DNS 2 Timing"; - DNS2Timing.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; - DNS2Timing.Width = 120; + DNS2Performance.Text = "DNS 2 Performance"; + DNS2Performance.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + DNS2Performance.Width = 120; // - // RunTest + // panel3 // - RunTest.Location = new System.Drawing.Point(1131, 1152); - RunTest.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); - RunTest.Name = "RunTest"; - RunTest.Size = new System.Drawing.Size(200, 55); - RunTest.TabIndex = 12; - RunTest.Text = "Run Test"; - RunTest.Click += RunTest_Click; - // - // lblDns1 - // - lblDns1.AutoSize = true; - lblDns1.Location = new System.Drawing.Point(155, 167); - lblDns1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); - lblDns1.Name = "lblDns1"; - lblDns1.Size = new System.Drawing.Size(100, 41); - lblDns1.TabIndex = 13; - lblDns1.Text = "Dns 1:"; - // - // lblDns2 - // - lblDns2.AutoSize = true; - lblDns2.Location = new System.Drawing.Point(1174, 173); - lblDns2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); - lblDns2.Name = "lblDns2"; - lblDns2.Size = new System.Drawing.Size(100, 41); - lblDns2.TabIndex = 14; - lblDns2.Text = "Dns 2:"; - // - // dnsList1 - // - dnsList1.FormattingEnabled = true; - dnsList1.ItemHeight = 41; - dnsList1.Location = new System.Drawing.Point(277, 155); - dnsList1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); - dnsList1.Name = "dnsList1"; - dnsList1.Size = new System.Drawing.Size(756, 49); - dnsList1.TabIndex = 15; - // - // dnsList2 - // - dnsList2.FormattingEnabled = true; - dnsList2.ItemHeight = 41; - dnsList2.Location = new System.Drawing.Point(1307, 161); - dnsList2.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); - dnsList2.Name = "dnsList2"; - dnsList2.Size = new System.Drawing.Size(788, 49); - dnsList2.TabIndex = 16; - // - // UseCustomServers - // - UseCustomServers.AutoSize = true; - UseCustomServers.Location = new System.Drawing.Point(2169, 173); - UseCustomServers.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); - UseCustomServers.Name = "UseCustomServers"; - UseCustomServers.Size = new System.Drawing.Size(262, 45); - UseCustomServers.TabIndex = 17; - UseCustomServers.Text = "Custom Servers"; - UseCustomServers.UseVisualStyleBackColor = true; - UseCustomServers.CheckedChanged += UseCustomServers_CheckedChanged; - // - // lblCustom1 - // - lblCustom1.AutoSize = true; - lblCustom1.Location = new System.Drawing.Point(155, 328); - lblCustom1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); - lblCustom1.Name = "lblCustom1"; - lblCustom1.Size = new System.Drawing.Size(219, 41); - lblCustom1.TabIndex = 18; - lblCustom1.Text = "Custom Dns 1: "; - lblCustom1.Visible = false; - // - // lblCustom2 - // - lblCustom2.AutoSize = true; - lblCustom2.Location = new System.Drawing.Point(1174, 334); - lblCustom2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); - lblCustom2.Name = "lblCustom2"; - lblCustom2.Size = new System.Drawing.Size(219, 41); - lblCustom2.TabIndex = 19; - lblCustom2.Text = "Custom Dns 2: "; - lblCustom2.Visible = false; - // - // CustomDns1 - // - CustomDns1.BackColor = System.Drawing.Color.White; - CustomDns1.Enabled = false; - CustomDns1.Location = new System.Drawing.Point(408, 324); - CustomDns1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); - CustomDns1.Name = "CustomDns1"; - CustomDns1.Size = new System.Drawing.Size(625, 47); - CustomDns1.TabIndex = 20; - CustomDns1.Visible = false; - // - // CustomDns2 - // - CustomDns2.Enabled = false; - CustomDns2.Location = new System.Drawing.Point(1427, 330); - CustomDns2.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); - CustomDns2.Name = "CustomDns2"; - CustomDns2.Size = new System.Drawing.Size(668, 47); - CustomDns2.TabIndex = 21; - CustomDns2.Visible = false; - // - // lslStatus - // - lslStatus.AutoSize = true; - lslStatus.Location = new System.Drawing.Point(155, 1219); - lslStatus.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0); - lslStatus.Name = "lslStatus"; - lslStatus.Size = new System.Drawing.Size(105, 41); - lslStatus.TabIndex = 22; - lslStatus.Text = "Status:"; - // - // StatusBox - // - StatusBox.FormattingEnabled = true; - StatusBox.HorizontalScrollbar = true; - StatusBox.ItemHeight = 41; - StatusBox.Location = new System.Drawing.Point(155, 1271); - StatusBox.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); - StatusBox.Name = "StatusBox"; - StatusBox.Size = new System.Drawing.Size(2209, 373); - StatusBox.TabIndex = 23; - // - // chkDns1 - // - chkDns1.AutoSize = true; - chkDns1.Checked = true; - chkDns1.CheckState = System.Windows.Forms.CheckState.Checked; - chkDns1.Location = new System.Drawing.Point(277, 235); - chkDns1.Name = "chkDns1"; - chkDns1.Size = new System.Drawing.Size(251, 45); - chkDns1.TabIndex = 24; - chkDns1.Text = "Test dns server"; - chkDns1.UseVisualStyleBackColor = true; - // - // chkDns2 - // - chkDns2.AutoSize = true; - chkDns2.Checked = true; - chkDns2.CheckState = System.Windows.Forms.CheckState.Checked; - chkDns2.Location = new System.Drawing.Point(1307, 241); - chkDns2.Name = "chkDns2"; - chkDns2.Size = new System.Drawing.Size(251, 45); - chkDns2.TabIndex = 25; - chkDns2.Text = "Test dns server"; - chkDns2.UseVisualStyleBackColor = true; - // - // label1 - // - label1.AutoSize = true; - label1.Location = new System.Drawing.Point(155, 27); - label1.Name = "label1"; - label1.Size = new System.Drawing.Size(270, 41); - label1.TabIndex = 26; - label1.Text = "Resolve ip address:"; - // - // label2 - // - label2.AutoSize = true; - label2.Location = new System.Drawing.Point(1180, 33); - label2.Name = "label2"; - label2.Size = new System.Drawing.Size(266, 41); - label2.TabIndex = 27; - label2.Text = "Resolve hostname:"; - // - // txtResolveIP - // - txtResolveIP.Location = new System.Drawing.Point(425, 21); - txtResolveIP.Name = "txtResolveIP"; - txtResolveIP.Size = new System.Drawing.Size(608, 47); - txtResolveIP.TabIndex = 28; - // - // txtResolveHost - // - txtResolveHost.Location = new System.Drawing.Point(1443, 27); - txtResolveHost.Name = "txtResolveHost"; - txtResolveHost.Size = new System.Drawing.Size(651, 47); - txtResolveHost.TabIndex = 29; - // - // btnResolveIP - // - btnResolveIP.Location = new System.Drawing.Point(425, 75); - btnResolveIP.Name = "btnResolveIP"; - btnResolveIP.Size = new System.Drawing.Size(205, 51); - btnResolveIP.TabIndex = 30; - btnResolveIP.Text = "Resolve"; - btnResolveIP.UseVisualStyleBackColor = true; - btnResolveIP.Click += btnResolveIP_Click; + panel3.BackColor = System.Drawing.SystemColors.ControlLight; + panel3.Controls.Add(btnResolveHost); + panel3.Controls.Add(btnResolveIP); + panel3.Controls.Add(txtResolveHost); + panel3.Controls.Add(txtResolveIP); + panel3.Controls.Add(label5); + panel3.Controls.Add(label1); + panel3.Location = new System.Drawing.Point(155, 45); + panel3.Name = "panel3"; + panel3.Size = new System.Drawing.Size(2209, 175); + panel3.TabIndex = 26; // // btnResolveHost // - btnResolveHost.Location = new System.Drawing.Point(1443, 81); + btnResolveHost.Location = new System.Drawing.Point(1583, 104); btnResolveHost.Name = "btnResolveHost"; - btnResolveHost.Size = new System.Drawing.Size(167, 45); - btnResolveHost.TabIndex = 31; + btnResolveHost.Size = new System.Drawing.Size(240, 51); + btnResolveHost.TabIndex = 71; btnResolveHost.Text = "Resolve"; btnResolveHost.UseVisualStyleBackColor = true; btnResolveHost.Click += btnResolveHost_Click; // - // label3 + // btnResolveIP // - label3.AutoSize = true; - label3.Location = new System.Drawing.Point(675, 238); - label3.Name = "label3"; - label3.Size = new System.Drawing.Size(210, 41); - label3.TabIndex = 32; - label3.Text = "Total domains:"; + btnResolveIP.Location = new System.Drawing.Point(405, 104); + btnResolveIP.Name = "btnResolveIP"; + btnResolveIP.Size = new System.Drawing.Size(240, 51); + btnResolveIP.TabIndex = 70; + btnResolveIP.Text = "Resolve"; + btnResolveIP.UseVisualStyleBackColor = true; + btnResolveIP.Click += btnResolveIP_Click; // - // numericUpDown1 + // txtResolveHost // - numericUpDown1.Location = new System.Drawing.Point(913, 238); - numericUpDown1.Maximum = new decimal(new int[] { 1000000000, 0, 0, 0 }); - numericUpDown1.Minimum = new decimal(new int[] { 1, 0, 0, 0 }); - numericUpDown1.Name = "numericUpDown1"; - numericUpDown1.Size = new System.Drawing.Size(120, 47); - numericUpDown1.TabIndex = 33; - numericUpDown1.Value = new decimal(new int[] { 20, 0, 0, 0 }); + txtResolveHost.Location = new System.Drawing.Point(1583, 41); + txtResolveHost.Name = "txtResolveHost"; + txtResolveHost.Size = new System.Drawing.Size(551, 47); + txtResolveHost.TabIndex = 69; // - // comboBox1 + // txtResolveIP // - comboBox1.FormattingEnabled = true; - comboBox1.Items.AddRange(new object[] { "A", "NS", "CNAME", "MX", "TXT" }); - comboBox1.Location = new System.Drawing.Point(2129, 251); - comboBox1.Name = "comboBox1"; - comboBox1.Size = new System.Drawing.Size(302, 49); - comboBox1.TabIndex = 34; - comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged; + txtResolveIP.Location = new System.Drawing.Point(405, 41); + txtResolveIP.Name = "txtResolveIP"; + txtResolveIP.Size = new System.Drawing.Size(551, 47); + txtResolveIP.TabIndex = 68; + // + // label5 + // + label5.AutoSize = true; + label5.Location = new System.Drawing.Point(1267, 41); + label5.Name = "label5"; + label5.Size = new System.Drawing.Size(266, 41); + label5.TabIndex = 67; + label5.Text = "Resolve hostname:"; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new System.Drawing.Point(72, 41); + label1.Name = "label1"; + label1.Size = new System.Drawing.Size(270, 41); + label1.TabIndex = 66; + label1.Text = "Resolve ip address:"; // // MainForm // @@ -342,30 +387,10 @@ AutoSize = true; AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; ClientSize = new System.Drawing.Size(2517, 1693); - Controls.Add(comboBox1); - Controls.Add(numericUpDown1); - Controls.Add(label3); - Controls.Add(btnResolveHost); - Controls.Add(btnResolveIP); - Controls.Add(txtResolveHost); - Controls.Add(txtResolveIP); - Controls.Add(label2); - Controls.Add(label1); - Controls.Add(chkDns2); - Controls.Add(chkDns1); - Controls.Add(StatusBox); - Controls.Add(lslStatus); - Controls.Add(CustomDns2); - Controls.Add(CustomDns1); - Controls.Add(lblCustom2); - Controls.Add(lblCustom1); - Controls.Add(UseCustomServers); - Controls.Add(dnsList2); - Controls.Add(dnsList1); - Controls.Add(lblDns2); - Controls.Add(lblDns1); + Controls.Add(panel3); + Controls.Add(panel2); + Controls.Add(panel1); Controls.Add(RunTest); - Controls.Add(ResultView); Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon"); Margin = new System.Windows.Forms.Padding(8, 7, 8, 7); MaximizeBox = false; @@ -373,42 +398,47 @@ StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; Text = "EonaCat.DnsTester"; Load += TesterUI_Load; + panel1.ResumeLayout(false); + panel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)numericUpDown1).EndInit(); + panel2.ResumeLayout(false); + panel3.ResumeLayout(false); + panel3.PerformLayout(); ResumeLayout(false); - PerformLayout(); } #endregion - - private System.Windows.Forms.ListView ResultView; - private System.Windows.Forms.ColumnHeader WebName; - private System.Windows.Forms.ColumnHeader DNS1IP; - private System.Windows.Forms.ColumnHeader DNS1Timing; - private System.Windows.Forms.ColumnHeader DNS2IP; - private System.Windows.Forms.ColumnHeader DNS2Timing; private System.Windows.Forms.Button RunTest; - private System.Windows.Forms.Label lblDns1; - private System.Windows.Forms.Label lblDns2; - private System.Windows.Forms.ComboBox dnsList1; - private System.Windows.Forms.ComboBox dnsList2; - private System.Windows.Forms.CheckBox UseCustomServers; - private System.Windows.Forms.Label lblCustom1; - private System.Windows.Forms.Label lblCustom2; - private System.Windows.Forms.TextBox CustomDns1; - private System.Windows.Forms.TextBox CustomDns2; - private System.Windows.Forms.Label lslStatus; - private System.Windows.Forms.ListBox StatusBox; - private System.Windows.Forms.CheckBox chkDns1; - private System.Windows.Forms.CheckBox chkDns2; - private System.Windows.Forms.Label label1; - private System.Windows.Forms.Label label2; - private System.Windows.Forms.TextBox txtResolveIP; - private System.Windows.Forms.TextBox txtResolveHost; - private System.Windows.Forms.Button btnResolveIP; - private System.Windows.Forms.Button btnResolveHost; - private System.Windows.Forms.Label label3; - private System.Windows.Forms.NumericUpDown numericUpDown1; + private System.Windows.Forms.Panel panel1; private System.Windows.Forms.ComboBox comboBox1; + private System.Windows.Forms.NumericUpDown numericUpDown1; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.CheckBox chkDns2; + private System.Windows.Forms.CheckBox chkDns1; + private System.Windows.Forms.TextBox CustomDns2; + private System.Windows.Forms.TextBox CustomDns1; + private System.Windows.Forms.Label lblCustom2; + private System.Windows.Forms.Label lblCustom1; + private System.Windows.Forms.CheckBox UseCustomServers; + private System.Windows.Forms.ComboBox dnsList2; + private System.Windows.Forms.ComboBox dnsList1; + private System.Windows.Forms.Label lblDns2; + private System.Windows.Forms.Label lblDns1; + private System.Windows.Forms.Panel panel2; + private System.Windows.Forms.ListBox StatusBox; + private System.Windows.Forms.ListView ResultView; + private System.Windows.Forms.ColumnHeader Url; + private System.Windows.Forms.ColumnHeader DNS1IP; + private System.Windows.Forms.ColumnHeader DNS1Performance; + private System.Windows.Forms.ColumnHeader DNS2IP; + private System.Windows.Forms.ColumnHeader DNS2Performance; + private System.Windows.Forms.Panel panel3; + private System.Windows.Forms.Button btnResolveHost; + private System.Windows.Forms.Button btnResolveIP; + private System.Windows.Forms.TextBox txtResolveHost; + private System.Windows.Forms.TextBox txtResolveIP; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.Label label1; } } diff --git a/EonaCat.DnsTester/MainForm.cs b/EonaCat.DnsTester/MainForm.cs index 91719bd..17300ae 100644 --- a/EonaCat.DnsTester/MainForm.cs +++ b/EonaCat.DnsTester/MainForm.cs @@ -1,15 +1,11 @@ using System; using System.Collections.Generic; using System.Data; -using System.DirectoryServices.ActiveDirectory; +using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; -using System.Security.Cryptography; -using System.Security.Policy; -using System.Text; -using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using EonaCat.DnsTester.Helpers; @@ -18,7 +14,7 @@ namespace EonaCat.DnsTester { public partial class MainForm : Form { - private bool useCustomDnsServers; + private bool _useCustomDnsServers; private string _dnsServer1, _dnsServer2; private DnsRecordType _recordType; private int _dnsTotalChecked; @@ -32,6 +28,21 @@ namespace EonaCat.DnsTester private async void RunTest_Click(object sender, EventArgs e) { + if (!_useCustomDnsServers) + { + if (!chkDns1.Checked && !chkDns2.Checked) + { + MessageBox.Show("Please enable DNS 1 or 2 before starting"); + return; + } + } + + if (_useCustomDnsServers && (!chkDns1.Checked || chkDns2.Checked)) + { + MessageBox.Show("Please enable DNS 1 or 2 before using custom Dns"); + return; + } + List urls = new List(); SetupView(); @@ -48,13 +59,13 @@ namespace EonaCat.DnsTester } IsRunning = true; - Process(_recordType, urls.ToArray(), _dnsServer1, _dnsServer2); + await Process(_recordType, urls.ToArray(), _dnsServer1, _dnsServer2); IsRunning = false; } private void SetupView() { - if (useCustomDnsServers) + if (_useCustomDnsServers) { _dnsTotalChecked = 0; if (chkDns1.Checked) @@ -150,39 +161,22 @@ namespace EonaCat.DnsTester private void UseCustomServers_CheckedChanged(object sender, EventArgs e) { - useCustomDnsServers = UseCustomServers.Checked; + _useCustomDnsServers = UseCustomServers.Checked; SetUI(); } private void SetUI() { - if (useCustomDnsServers) - { - dnsList1.Enabled = false; - dnsList2.Enabled = false; + dnsList1.Enabled = !_useCustomDnsServers && chkDns1.Checked; + dnsList2.Enabled = !_useCustomDnsServers && chkDns2.Checked; - CustomDns1.Enabled = true; - CustomDns2.Enabled = true; + CustomDns1.Enabled = _useCustomDnsServers && chkDns1.Checked; + CustomDns2.Enabled = _useCustomDnsServers && chkDns2.Checked; - CustomDns1.Visible = true; - CustomDns2.Visible = true; - lblCustom1.Visible = true; - lblCustom2.Visible = true; - - } - else - { - dnsList1.Enabled = true; - dnsList2.Enabled = true; - CustomDns1.Enabled = false; - CustomDns2.Enabled = false; - - CustomDns1.Visible = false; - CustomDns2.Visible = false; - lblCustom1.Visible = false; - lblCustom2.Visible = false; - - } + CustomDns1.Visible = _useCustomDnsServers && chkDns1.Checked; + CustomDns2.Visible = _useCustomDnsServers && chkDns2.Checked; + lblCustom1.Visible = _useCustomDnsServers && chkDns1.Checked; + lblCustom2.Visible = _useCustomDnsServers && chkDns2.Checked; } @@ -270,14 +264,14 @@ namespace EonaCat.DnsTester private void ProcessResponse(DnsResponse dnsResponse) { - if (dnsResponse == null || dnsResponse.Answers == null || !dnsResponse.Answers.Any()) + if (dnsResponse?.Answers == null || !dnsResponse.Answers.Any()) { return; } // Retrieve stopTime var stopTime = DateTime.Now.Ticks; - var deltaTime = Convert.ToString((double)(stopTime - dnsResponse.StartTime) / 10000000); + var deltaTime = Convert.ToString((double)(stopTime - dnsResponse.StartTime) / 10000000, CultureInfo.InvariantCulture); SetStatus($"ResourceRecord: Name: {dnsResponse.Answers.FirstOrDefault().Name} : Type : {dnsResponse.Answers.FirstOrDefault().Type} : Data : {dnsResponse.Answers.FirstOrDefault().Data}"); @@ -329,47 +323,48 @@ namespace EonaCat.DnsTester return; } - try + await Task.Run(() => { - await Task.Run(() => + try { var dnsEntry = Dns.GetHostEntry(iPAddress); txtResolveHost.Invoke(() => - { - txtResolveHost.Text = dnsEntry.HostName; - }); - }); - } - catch (Exception) - { - MessageBox.Show($"Could not get hostname for IP address '{txtResolveIP.Text}'"); - } + { + txtResolveHost.Text = dnsEntry.HostName; + }); + } + catch (Exception) + { + MessageBox.Show($"Could not get hostname for IP address '{txtResolveIP.Text}'"); + } + }); } private async void btnResolveHost_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtResolveHost.Text)) { - MessageBox.Show("Please enter an hostname to resolve"); + MessageBox.Show("Please enter a hostname to resolve"); return; } - try + + await Task.Run(() => { - await Task.Run(() => + try { var dnsEntry = Dns.GetHostEntry(txtResolveHost.Text); txtResolveHost.Invoke(() => - { - txtResolveIP.Text = dnsEntry.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().Address.ToString(); - }); - }); - } - catch (Exception) - { - MessageBox.Show($"Could not get hostname for IP address '{txtResolveIP.Text}'"); - } + { + txtResolveIP.Text = dnsEntry.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().Address.ToString(); + }); + } + catch (Exception) + { + MessageBox.Show($"Could not get IP address for hostname '{txtResolveHost.Text}'"); + } + }); } private void SetStatus(string text) @@ -396,5 +391,15 @@ namespace EonaCat.DnsTester _recordType = queryType; } } + + private void chkDns2_CheckedChanged(object sender, EventArgs e) + { + SetUI(); + } + + private void chkDns1_CheckedChanged(object sender, EventArgs e) + { + SetUI(); + } } }