frmManageDnsProviders.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2019 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. readonly List<DnsProvider> _dnsProviders = new List<DnsProvider>();
  26. #endregion
  27. #region constructor
  28. public frmManageDnsProviders(ICollection<DnsProvider> dnsProviders)
  29. {
  30. InitializeComponent();
  31. _dnsProviders.AddRange(dnsProviders);
  32. }
  33. #endregion
  34. #region private
  35. private void RefreshDnsProvidersList()
  36. {
  37. listView1.SuspendLayout();
  38. listView1.Items.Clear();
  39. foreach (DnsProvider dnsProvider in _dnsProviders)
  40. {
  41. ListViewItem item = listView1.Items.Add(dnsProvider.Name);
  42. item.SubItems.Add(dnsProvider.GetIpv4Addresses());
  43. item.SubItems.Add(dnsProvider.GetIpv6Addresses());
  44. item.Tag = dnsProvider;
  45. }
  46. listView1.ResumeLayout();
  47. }
  48. private void ClearForm()
  49. {
  50. txtDnsProviderName.Text = "";
  51. txtIpv4Addresses.Text = "";
  52. txtIpv6Addresses.Text = "";
  53. btnAddUpdate.Text = "Add";
  54. btnDelete.Enabled = false;
  55. }
  56. private void frmManageDnsProviders_Load(object sender, EventArgs e)
  57. {
  58. RefreshDnsProvidersList();
  59. }
  60. private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  61. {
  62. if (listView1.SelectedItems.Count > 0)
  63. {
  64. ListViewItem selectedItem = listView1.SelectedItems[0];
  65. txtDnsProviderName.Text = selectedItem.Text;
  66. txtIpv4Addresses.Text = selectedItem.SubItems[1].Text;
  67. txtIpv6Addresses.Text = selectedItem.SubItems[2].Text;
  68. btnAddUpdate.Text = "&Update";
  69. btnDelete.Enabled = true;
  70. }
  71. else
  72. {
  73. ClearForm();
  74. }
  75. }
  76. private void btnAddUpdate_Click(object sender, EventArgs e)
  77. {
  78. if (string.IsNullOrWhiteSpace(txtDnsProviderName.Text))
  79. {
  80. MessageBox.Show("Please enter a valid DNS Provider name.", "Missing DNS Provider!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  81. return;
  82. }
  83. List<IPAddress> addresses = new List<IPAddress>();
  84. foreach (string item in txtIpv4Addresses.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
  85. {
  86. if (IPAddress.TryParse(item.Trim(), out IPAddress address) && (address.AddressFamily == AddressFamily.InterNetwork))
  87. {
  88. addresses.Add(address);
  89. }
  90. else
  91. {
  92. MessageBox.Show("Please enter a valid IPv4 address.", "Invalid IPv4 Address!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  93. return;
  94. }
  95. }
  96. foreach (string item in txtIpv6Addresses.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
  97. {
  98. if (IPAddress.TryParse(item.Trim(), out IPAddress address) && (address.AddressFamily == AddressFamily.InterNetworkV6))
  99. {
  100. addresses.Add(address);
  101. }
  102. else
  103. {
  104. MessageBox.Show("Please enter a valid IPv6 address.", "Invalid IPv6 Address!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  105. return;
  106. }
  107. }
  108. if (addresses.Count == 0)
  109. {
  110. MessageBox.Show("Please enter at least one valid DNS provider IP address.", "Missing DNS Provider IP Address!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  111. return;
  112. }
  113. if ((btnAddUpdate.Text != "Add") && (listView1.SelectedItems.Count > 0))
  114. {
  115. ListViewItem selectedItem = listView1.SelectedItems[0];
  116. DnsProvider dnsProvider = selectedItem.Tag as DnsProvider;
  117. dnsProvider.Name = txtDnsProviderName.Text.Trim();
  118. dnsProvider.Addresses = addresses;
  119. }
  120. else
  121. {
  122. _dnsProviders.Add(new DnsProvider(txtDnsProviderName.Text.Trim(), addresses));
  123. }
  124. RefreshDnsProvidersList();
  125. ClearForm();
  126. }
  127. private void btnDelete_Click(object sender, EventArgs e)
  128. {
  129. if (listView1.SelectedItems.Count > 0)
  130. {
  131. ListViewItem selectedItem = listView1.SelectedItems[0];
  132. DnsProvider dnsProvider = selectedItem.Tag as DnsProvider;
  133. _dnsProviders.Remove(dnsProvider);
  134. listView1.Items.Remove(selectedItem);
  135. }
  136. RefreshDnsProvidersList();
  137. ClearForm();
  138. }
  139. private void btnClear_Click(object sender, EventArgs e)
  140. {
  141. ClearForm();
  142. }
  143. private void btnRestoreDefaults_Click(object sender, EventArgs e)
  144. {
  145. _dnsProviders.Clear();
  146. _dnsProviders.AddRange(DnsProvider.GetDefaultProviders());
  147. RefreshDnsProvidersList();
  148. ClearForm();
  149. }
  150. #endregion
  151. #region properties
  152. public List<DnsProvider> DnsProviders
  153. { get { return _dnsProviders; } }
  154. #endregion
  155. }
  156. }