BlockedZoneManager.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 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 DnsServerCore.Dns.Zones;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Text;
  20. using TechnitiumLibrary.IO;
  21. using TechnitiumLibrary.Net.Dns;
  22. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  23. namespace DnsServerCore.Dns.ZoneManagers
  24. {
  25. public sealed class BlockedZoneManager
  26. {
  27. #region variables
  28. readonly DnsServer _dnsServer;
  29. readonly AuthZoneManager _zoneManager;
  30. DnsSOARecordData _soaRecord;
  31. DnsNSRecordData _nsRecord;
  32. #endregion
  33. #region constructor
  34. public BlockedZoneManager(DnsServer dnsServer)
  35. {
  36. _dnsServer = dnsServer;
  37. _zoneManager = new AuthZoneManager(_dnsServer);
  38. UpdateServerDomain(_dnsServer.ServerDomain);
  39. }
  40. #endregion
  41. #region private
  42. private void UpdateServerDomain(string serverDomain)
  43. {
  44. _soaRecord = new DnsSOARecordData(serverDomain, "hostadmin@" + serverDomain, 1, 900, 300, 604800, 60);
  45. _nsRecord = new DnsNSRecordData(serverDomain);
  46. _zoneManager.ServerDomain = serverDomain;
  47. }
  48. #endregion
  49. #region public
  50. public void LoadBlockedZoneFile()
  51. {
  52. _zoneManager.Flush();
  53. string blockedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "blocked.config");
  54. try
  55. {
  56. string oldCustomBlockedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "custom-blocked.config");
  57. if (File.Exists(oldCustomBlockedZoneFile))
  58. {
  59. if (File.Exists(blockedZoneFile))
  60. File.Delete(blockedZoneFile);
  61. File.Move(oldCustomBlockedZoneFile, blockedZoneFile);
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. _dnsServer.LogManager?.Write(ex);
  67. }
  68. try
  69. {
  70. _dnsServer.LogManager?.Write("DNS Server is loading blocked zone file: " + blockedZoneFile);
  71. using (FileStream fS = new FileStream(blockedZoneFile, FileMode.Open, FileAccess.Read))
  72. {
  73. BinaryReader bR = new BinaryReader(fS);
  74. if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "BZ") //format
  75. throw new InvalidDataException("DnsServer blocked zone file format is invalid.");
  76. byte version = bR.ReadByte();
  77. switch (version)
  78. {
  79. case 1:
  80. int length = bR.ReadInt32();
  81. int i = 0;
  82. _zoneManager.LoadSpecialPrimaryZones(delegate ()
  83. {
  84. if (i++ < length)
  85. return bR.ReadShortString();
  86. return null;
  87. }, _soaRecord, _nsRecord);
  88. break;
  89. default:
  90. throw new InvalidDataException("DnsServer blocked zone file version not supported.");
  91. }
  92. }
  93. _dnsServer.LogManager?.Write("DNS Server blocked zone file was loaded: " + blockedZoneFile);
  94. }
  95. catch (FileNotFoundException)
  96. { }
  97. catch (Exception ex)
  98. {
  99. _dnsServer.LogManager?.Write("DNS Server encountered an error while loading blocked zone file: " + blockedZoneFile + "\r\n" + ex.ToString());
  100. }
  101. }
  102. public void ImportZones(string[] domains)
  103. {
  104. _zoneManager.LoadSpecialPrimaryZones(domains, _soaRecord, _nsRecord);
  105. }
  106. public bool BlockZone(string domain)
  107. {
  108. if (_zoneManager.CreateSpecialPrimaryZone(domain, _soaRecord, _nsRecord) != null)
  109. return true;
  110. return false;
  111. }
  112. public bool DeleteZone(string domain)
  113. {
  114. if (_zoneManager.DeleteZone(domain))
  115. return true;
  116. return false;
  117. }
  118. public void Flush()
  119. {
  120. _zoneManager.Flush();
  121. }
  122. public IReadOnlyList<AuthZoneInfo> GetAllZones()
  123. {
  124. return _zoneManager.GetAllZones();
  125. }
  126. public void ListAllRecords(string domain, List<DnsResourceRecord> records)
  127. {
  128. _zoneManager.ListAllRecords(domain, domain, records);
  129. }
  130. public void ListSubDomains(string domain, List<string> subDomains)
  131. {
  132. _zoneManager.ListSubDomains(domain, subDomains);
  133. }
  134. public void SaveZoneFile()
  135. {
  136. IReadOnlyList<AuthZoneInfo> blockedZones = _dnsServer.BlockedZoneManager.GetAllZones();
  137. string blockedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "blocked.config");
  138. using (FileStream fS = new FileStream(blockedZoneFile, FileMode.Create, FileAccess.Write))
  139. {
  140. BinaryWriter bW = new BinaryWriter(fS);
  141. bW.Write(Encoding.ASCII.GetBytes("BZ")); //format
  142. bW.Write((byte)1); //version
  143. bW.Write(blockedZones.Count);
  144. foreach (AuthZoneInfo zone in blockedZones)
  145. bW.WriteShortString(zone.Name);
  146. }
  147. _dnsServer.LogManager?.Write("DNS Server blocked zone file was saved: " + blockedZoneFile);
  148. }
  149. public DnsDatagram Query(DnsDatagram request)
  150. {
  151. if (_zoneManager.TotalZones < 1)
  152. return null;
  153. return _zoneManager.Query(request, false);
  154. }
  155. #endregion
  156. #region properties
  157. internal DnsSOARecordData DnsSOARecord
  158. { get { return _soaRecord; } }
  159. public string ServerDomain
  160. {
  161. get { return _soaRecord.PrimaryNameServer; }
  162. set { UpdateServerDomain(value); }
  163. }
  164. public int TotalZonesBlocked
  165. { get { return _zoneManager.TotalZones; } }
  166. #endregion
  167. }
  168. }