frmManageDnsProviders.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Net;
  18. using System.Net.Sockets;
  19. using System.Windows.Forms;
  20. namespace DnsServerSystemTrayApp
  21. {
  22. public partial class frmManageDnsProviders : Form
  23. {
  24. #region variables
  25. static readonly char[] commaSeparator = new char[] { ',' };
  26. readonly List<DnsProvider> _dnsProviders = new List<DnsProvider>();
  27. #endregion
  28. #region constructor
  29. public frmManageDnsProviders(ICollection<DnsProvider> dnsProviders)
  30. {
  31. InitializeComponent();
  32. _dnsProviders.AddRange(dnsProviders);
  33. }
  34. #endregion
  35. #region private
  36. private void RefreshDnsProvidersList()
  37. {
  38. listView1.SuspendLayout();
  39. listView1.Items.Clear();
  40. foreach (DnsProvider dnsProvider in _dnsProviders)
  41. {
  42. ListViewItem item = listView1.Items.Add(dnsProvider.Name);
  43. item.SubItems.Add(dnsProvider.GetIpv4Addresses());
  44. item.SubItems.Add(dnsProvider.GetIpv6Addresses());
  45. item.Tag = dnsProvider;
  46. }
  47. listView1.ResumeLayout();
  48. }
  49. private void ClearForm()
  50. {
  51. txtDnsProviderName.Text = "";
  52. txtIpv4Addresses.Text = "";
  53. txtIpv6Addresses.Text = "";
  54. btnAddUpdate.Text = "Add";
  55. btnDelete.Enabled = false;
  56. }
  57. private void frmManageDnsProviders_Load(object sender, EventArgs e)
  58. {
  59. RefreshDnsProvidersList();
  60. }
  61. private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  62. {
  63. if (listView1.SelectedItems.Count > 0)
  64. {
  65. ListViewItem selectedItem = listView1.SelectedItems[0];
  66. txtDnsProviderName.Text = selectedItem.Text;
  67. txtIpv4Addresses.Text = selectedItem.SubItems[1].Text;
  68. txtIpv6Addresses.Text = selectedItem.SubItems[2].Text;
  69. btnAddUpdate.Text = "&Update";
  70. btnDelete.Enabled = true;
  71. }
  72. else
  73. {
  74. ClearForm();
  75. }
  76. }
  77. private void btnAddUpdate_Click(object sender, EventArgs e)
  78. {
  79. if (string.IsNullOrWhiteSpace(txtDnsProviderName.Text))
  80. {
  81. MessageBox.Show("Please enter a valid DNS Provider name.", "Missing DNS Provider!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  82. return;
  83. }
  84. List<IPAddress> addresses = new List<IPAddress>();
  85. foreach (string item in txtIpv4Addresses.Text.Split(commaSeparator, StringSplitOptions.RemoveEmptyEntries))
  86. {
  87. if (IPAddress.TryParse(item.Trim(), out IPAddress address) && (address.AddressFamily == AddressFamily.InterNetwork))
  88. {
  89. addresses.Add(address);
  90. }
  91. else
  92. {
  93. MessageBox.Show("Please enter a valid IPv4 address.", "Invalid IPv4 Address!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  94. return;
  95. }
  96. }
  97. foreach (string item in txtIpv6Addresses.Text.Split(commaSeparator, StringSplitOptions.RemoveEmptyEntries))
  98. {
  99. if (IPAddress.TryParse(item.Trim(), out IPAddress address) && (address.AddressFamily == AddressFamily.InterNetworkV6))
  100. {
  101. addresses.Add(address);
  102. }
  103. else
  104. {
  105. MessageBox.Show("Please enter a valid IPv6 address.", "Invalid IPv6 Address!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  106. return;
  107. }
  108. }
  109. if (addresses.Count == 0)
  110. {
  111. MessageBox.Show("Please enter at least one valid DNS provider IP address.", "Missing DNS Provider IP Address!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  112. return;
  113. }
  114. if ((btnAddUpdate.Text != "Add") && (listView1.SelectedItems.Count > 0))
  115. {
  116. ListViewItem selectedItem = listView1.SelectedItems[0];
  117. DnsProvider dnsProvider = selectedItem.Tag as DnsProvider;
  118. dnsProvider.Name = txtDnsProviderName.Text.Trim();
  119. dnsProvider.Addresses = addresses;
  120. }
  121. else
  122. {
  123. _dnsProviders.Add(new DnsProvider(txtDnsProviderName.Text.Trim(), addresses));
  124. }
  125. RefreshDnsProvidersList();
  126. ClearForm();
  127. }
  128. private void btnDelete_Click(object sender, EventArgs e)
  129. {
  130. if (listView1.SelectedItems.Count > 0)
  131. {
  132. ListViewItem selectedItem = listView1.SelectedItems[0];
  133. DnsProvider dnsProvider = selectedItem.Tag as DnsProvider;
  134. _dnsProviders.Remove(dnsProvider);
  135. listView1.Items.Remove(selectedItem);
  136. }
  137. RefreshDnsProvidersList();
  138. ClearForm();
  139. }
  140. private void btnClear_Click(object sender, EventArgs e)
  141. {
  142. ClearForm();
  143. }
  144. private void btnRestoreDefaults_Click(object sender, EventArgs e)
  145. {
  146. _dnsProviders.Clear();
  147. _dnsProviders.AddRange(DnsProvider.GetDefaultProviders());
  148. RefreshDnsProvidersList();
  149. ClearForm();
  150. }
  151. #endregion
  152. #region properties
  153. public List<DnsProvider> DnsProviders
  154. { get { return _dnsProviders; } }
  155. #endregion
  156. }
  157. }