|
17 | 17 | using System.IO; |
18 | 18 | using System.Reflection; |
19 | 19 | using System.Threading; |
| 20 | +using System.Net.Sockets; |
| 21 | +using System.Net; |
20 | 22 |
|
21 | 23 | namespace ADReplStatus |
22 | 24 | { |
@@ -538,37 +540,142 @@ private void diagnosticMenuSelector(object sender, ToolStripItemClickedEventArgs |
538 | 540 | private void diagnosticPing(object sender, ToolStripItemClickedEventArgs e) |
539 | 541 | { |
540 | 542 | string destination = this.treeListView1.SelectedItem.Text; |
541 | | - |
542 | | - if(destination != "") |
| 543 | + |
| 544 | + if (destination != "") |
543 | 545 | { |
544 | | - try |
| 546 | + using (var dialog = new Form()) |
545 | 547 | { |
546 | | - Ping p = new Ping(); |
547 | | - PingReply r; |
548 | | - r = p.Send(destination); |
549 | | - if (gLoggingEnabled) |
| 548 | + //Set up the ping test window |
| 549 | + dialog.Text = "Ping Test"; |
| 550 | + dialog.StartPosition = FormStartPosition.CenterParent; |
| 551 | + dialog.MaximizeBox = false; |
| 552 | + dialog.MinimizeBox = false; |
| 553 | + dialog.FormBorderStyle = FormBorderStyle.FixedDialog; |
| 554 | + dialog.ShowInTaskbar = false; |
| 555 | + dialog.Width = 290; |
| 556 | + dialog.Height = 150; |
| 557 | + |
| 558 | + var ipv4Button = new Button(); |
| 559 | + ipv4Button.Text = "IPv4"; |
| 560 | + ipv4Button.Location = new Point(10, 20); |
| 561 | + ipv4Button.Click += (s, ev) => RunPing(destination, AddressFamily.InterNetwork, dialog); |
| 562 | + |
| 563 | + var ipv6Button = new Button(); |
| 564 | + ipv6Button.Text = "IPv6"; |
| 565 | + ipv6Button.Location = new Point(180, 20); |
| 566 | + ipv6Button.Click += (s, ev) => RunPing(destination, AddressFamily.InterNetworkV6, dialog); |
| 567 | + |
| 568 | + var statusTextBox = new TextBox(); |
| 569 | + statusTextBox.Multiline = true; |
| 570 | + statusTextBox.ReadOnly = true; |
| 571 | + statusTextBox.Location = new Point(10, 60); |
| 572 | + statusTextBox.Width = dialog.Width - 45; |
| 573 | + statusTextBox.Height = dialog.Height - 110; |
| 574 | + statusTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right; |
| 575 | + |
| 576 | + dialog.Controls.Add(ipv4Button); |
| 577 | + dialog.Controls.Add(ipv6Button); |
| 578 | + dialog.Controls.Add(statusTextBox); |
| 579 | + |
| 580 | + //Add support for dark mode |
| 581 | + if (ADReplStatusForm.gDarkMode == true) |
550 | 582 | { |
551 | | - System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] Ping to {this.treeListView1.SelectedItem.Text}\nIPAddress:{r.Address.ToString()} is successful!\n"); |
| 583 | + dialog.BackColor = Color.FromArgb(32, 32, 32); |
| 584 | + foreach (var control in dialog.Controls) |
| 585 | + { |
| 586 | + if (control is Label) |
| 587 | + { |
| 588 | + ((Label)control).BackColor = Color.FromArgb(32, 32, 32); |
| 589 | + ((Label)control).ForeColor = Color.White; |
| 590 | + } |
| 591 | + else if (control is TextBox) |
| 592 | + { |
| 593 | + ((TextBox)control).BackColor = Color.FromArgb(32, 32, 32); |
| 594 | + ((TextBox)control).ForeColor = Color.White; |
| 595 | + } |
| 596 | + else if (control is Button) |
| 597 | + { |
| 598 | + ((Button)control).BackColor = Color.FromArgb(32, 32, 32); |
| 599 | + ((Button)control).ForeColor = Color.White; |
| 600 | + } |
| 601 | + else if (control is CheckBox) |
| 602 | + { |
| 603 | + ((CheckBox)control).BackColor = Color.FromArgb(32, 32, 32); |
| 604 | + ((CheckBox)control).ForeColor = Color.White; |
| 605 | + } |
| 606 | + else if (control is RadioButton) |
| 607 | + { |
| 608 | + ((RadioButton)control).BackColor = Color.FromArgb(32, 32, 32); |
| 609 | + ((RadioButton)control).ForeColor = Color.White; |
| 610 | + } |
| 611 | + else if (control is ListBox) |
| 612 | + { |
| 613 | + ((ListBox)control).BackColor = Color.FromArgb(32, 32, 32); |
| 614 | + ((ListBox)control).ForeColor = Color.White; |
| 615 | + } |
| 616 | + } |
552 | 617 | } |
553 | 618 |
|
554 | | - string successMessage = $"Success:\nDCName: {this.treeListView1.SelectedItem.Text}\nIPAddress: {r.Address.ToString()}\nProtocol: ICMP"; |
555 | | - |
556 | | - new Thread(() => System.Windows.Forms.MessageBox.Show(successMessage, "Ping Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)).Start(); |
| 619 | + dialog.ShowDialog(this); |
557 | 620 | } |
558 | | - catch (Exception ex) |
| 621 | + } |
| 622 | + } |
| 623 | + |
| 624 | + |
| 625 | + private async void RunPing(string destination, AddressFamily addressFamily, Form dialog) |
| 626 | + { |
| 627 | + try |
| 628 | + { |
| 629 | + IPAddress address; |
| 630 | + if (!IPAddress.TryParse(destination, out address)) |
559 | 631 | { |
560 | | - string errorMessage = $"ERROR: Ping to {this.treeListView1.SelectedItem.Text} failed!\n{ex.Message}\n"; |
| 632 | + var entry = await Dns.GetHostEntryAsync(destination); |
| 633 | + address = entry.AddressList.FirstOrDefault(a => a.AddressFamily == addressFamily); |
| 634 | + if (address == null) |
| 635 | + { |
| 636 | + throw new Exception($"No {addressFamily} address found for {destination}"); |
| 637 | + } |
| 638 | + } |
561 | 639 |
|
562 | | - new Thread(() => System.Windows.Forms.MessageBox.Show(errorMessage, "Ping Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)).Start(); |
| 640 | + using (var p = new Ping()) |
| 641 | + { |
| 642 | + var reply = await p.SendPingAsync(address, 5000, new byte[1], new PingOptions(64, true)); |
| 643 | + if (reply.Status == IPStatus.Success) |
| 644 | + { |
| 645 | + string protocol = addressFamily == AddressFamily.InterNetwork ? "IPv4" : "IPv6"; |
| 646 | + string successMessage = $"Success:\nDCName: {destination} ({reply.Address.ToString()})\nProtocol: {protocol}"; |
| 647 | + var statusTextBox = (TextBox)dialog.Controls[2]; |
| 648 | + statusTextBox.Clear(); |
| 649 | + statusTextBox.AppendText($"Ping to {destination} using {protocol} ({reply.Address.ToString()}) successful.\n"); |
563 | 650 |
|
564 | | - if (gLoggingEnabled) |
| 651 | + if (gLoggingEnabled) |
| 652 | + { |
| 653 | + System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {statusTextBox.Text}"); |
| 654 | + } |
| 655 | + } |
| 656 | + else |
565 | 657 | { |
566 | | - System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {errorMessage}\n"); |
| 658 | + throw new Exception(reply.Status.ToString()); |
567 | 659 | } |
568 | 660 | } |
569 | 661 | } |
| 662 | + catch (Exception ex) |
| 663 | + { |
| 664 | + dialog.Invoke(new Action(() => |
| 665 | + { |
| 666 | + string errorMessage = $"Ping failed!\n{ex.Message}\n"; |
| 667 | + var statusTextBox = (TextBox)dialog.Controls[2]; |
| 668 | + statusTextBox.Clear(); |
| 669 | + statusTextBox.AppendText($"{errorMessage}\n"); |
| 670 | + if (gLoggingEnabled) |
| 671 | + { |
| 672 | + System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {errorMessage}"); |
| 673 | + } |
| 674 | + })); |
| 675 | + } |
570 | 676 | } |
571 | 677 |
|
| 678 | + |
572 | 679 | private void diagnosticRdp(object sender, ToolStripItemClickedEventArgs e) |
573 | 680 | { |
574 | 681 | try |
|
0 commit comments