123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /*
- Technitium DNS Server
- Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- using DnsServerCore.Dns.Zones;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Threading;
- using TechnitiumLibrary.IO;
- using TechnitiumLibrary.Net.Dns;
- using TechnitiumLibrary.Net.Dns.ResourceRecords;
- namespace DnsServerCore.Dns.ZoneManagers
- {
- public sealed class BlockedZoneManager
- {
- #region variables
- readonly DnsServer _dnsServer;
- readonly AuthZoneManager _zoneManager;
- DnsSOARecordData _soaRecord;
- DnsNSRecordData _nsRecord;
- readonly object _saveLock = new object();
- bool _pendingSave;
- readonly Timer _saveTimer;
- const int SAVE_TIMER_INITIAL_INTERVAL = 10000;
- #endregion
- #region constructor
- public BlockedZoneManager(DnsServer dnsServer)
- {
- _dnsServer = dnsServer;
- _zoneManager = new AuthZoneManager(_dnsServer);
- UpdateServerDomain();
- _saveTimer = new Timer(delegate (object state)
- {
- lock (_saveLock)
- {
- if (_pendingSave)
- {
- try
- {
- SaveZoneFileInternal();
- _pendingSave = false;
- }
- catch (Exception ex)
- {
- _dnsServer.LogManager.Write(ex);
- //set timer to retry again
- _saveTimer.Change(SAVE_TIMER_INITIAL_INTERVAL, Timeout.Infinite);
- }
- }
- }
- });
- }
- #endregion
- #region IDisposable
- bool _disposed;
- public void Dispose()
- {
- if (_disposed)
- return;
- lock (_saveLock)
- {
- _saveTimer?.Dispose();
- if (_pendingSave)
- {
- try
- {
- SaveZoneFileInternal();
- }
- catch (Exception ex)
- {
- _dnsServer.LogManager.Write(ex);
- }
- finally
- {
- _pendingSave = false;
- }
- }
- }
- _disposed = true;
- }
- #endregion
- #region private
- internal void UpdateServerDomain()
- {
- _soaRecord = new DnsSOARecordData(_dnsServer.ServerDomain, _dnsServer.ResponsiblePerson.Address, 1, 14400, 3600, 604800, _dnsServer.BlockingAnswerTtl);
- _nsRecord = new DnsNSRecordData(_dnsServer.ServerDomain);
- _zoneManager.UpdateServerDomain(true);
- }
- private void SaveZoneFileInternal()
- {
- IReadOnlyList<AuthZoneInfo> blockedZones = _dnsServer.BlockedZoneManager.GetAllZones();
- string blockedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "blocked.config");
- using (FileStream fS = new FileStream(blockedZoneFile, FileMode.Create, FileAccess.Write))
- {
- BinaryWriter bW = new BinaryWriter(fS);
- bW.Write(Encoding.ASCII.GetBytes("BZ")); //format
- bW.Write((byte)1); //version
- bW.Write(blockedZones.Count);
- foreach (AuthZoneInfo zone in blockedZones)
- bW.WriteShortString(zone.Name);
- }
- _dnsServer.LogManager?.Write("DNS Server blocked zone file was saved: " + blockedZoneFile);
- }
- #endregion
- #region public
- public void LoadBlockedZoneFile()
- {
- _zoneManager.Flush();
- string blockedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "blocked.config");
- try
- {
- string oldCustomBlockedZoneFile = Path.Combine(_dnsServer.ConfigFolder, "custom-blocked.config");
- if (File.Exists(oldCustomBlockedZoneFile))
- {
- if (File.Exists(blockedZoneFile))
- File.Delete(blockedZoneFile);
- File.Move(oldCustomBlockedZoneFile, blockedZoneFile);
- }
- }
- catch (Exception ex)
- {
- _dnsServer.LogManager?.Write(ex);
- }
- try
- {
- _dnsServer.LogManager?.Write("DNS Server is loading blocked zone file: " + blockedZoneFile);
- using (FileStream fS = new FileStream(blockedZoneFile, FileMode.Open, FileAccess.Read))
- {
- BinaryReader bR = new BinaryReader(fS);
- if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "BZ") //format
- throw new InvalidDataException("DnsServer blocked zone file format is invalid.");
- byte version = bR.ReadByte();
- switch (version)
- {
- case 1:
- int length = bR.ReadInt32();
- int i = 0;
- _zoneManager.LoadSpecialPrimaryZones(delegate ()
- {
- if (i++ < length)
- return bR.ReadShortString();
- return null;
- }, _soaRecord, _nsRecord);
- break;
- default:
- throw new InvalidDataException("DnsServer blocked zone file version not supported.");
- }
- }
- _dnsServer.LogManager?.Write("DNS Server blocked zone file was loaded: " + blockedZoneFile);
- }
- catch (FileNotFoundException)
- { }
- catch (Exception ex)
- {
- _dnsServer.LogManager?.Write("DNS Server encountered an error while loading blocked zone file: " + blockedZoneFile + "\r\n" + ex.ToString());
- }
- }
- public void ImportZones(string[] domains)
- {
- _zoneManager.LoadSpecialPrimaryZones(domains, _soaRecord, _nsRecord);
- }
- public bool BlockZone(string domain)
- {
- if (_zoneManager.CreateSpecialPrimaryZone(domain, _soaRecord, _nsRecord) != null)
- return true;
- return false;
- }
- public bool DeleteZone(string domain)
- {
- if (_zoneManager.DeleteZone(domain))
- return true;
- return false;
- }
- public void Flush()
- {
- _zoneManager.Flush();
- }
- public IReadOnlyList<AuthZoneInfo> GetAllZones()
- {
- return _zoneManager.GetAllZones();
- }
- public void ListAllRecords(string domain, List<DnsResourceRecord> records)
- {
- _zoneManager.ListAllRecords(domain, domain, records);
- }
- public void ListSubDomains(string domain, List<string> subDomains)
- {
- _zoneManager.ListSubDomains(domain, subDomains);
- }
- public void SaveZoneFile()
- {
- lock (_saveLock)
- {
- if (_pendingSave)
- return;
- _pendingSave = true;
- _saveTimer.Change(SAVE_TIMER_INITIAL_INTERVAL, Timeout.Infinite);
- }
- }
- public DnsDatagram Query(DnsDatagram request)
- {
- if (_zoneManager.TotalZones < 1)
- return null;
- return _zoneManager.Query(request, false);
- }
- #endregion
- #region properties
- internal DnsSOARecordData DnsSOARecord
- { get { return _soaRecord; } }
- public int TotalZonesBlocked
- { get { return _zoneManager.TotalZones; } }
- #endregion
- }
- }
|