App.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 DnsServerCore.ApplicationCommon;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Net;
  19. using System.Text.Json;
  20. using System.Threading.Tasks;
  21. using TechnitiumLibrary;
  22. using TechnitiumLibrary.Net.Dns;
  23. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  24. namespace NxDomain
  25. {
  26. public sealed class App : IDnsApplication, IDnsAuthoritativeRequestHandler
  27. {
  28. #region variables
  29. DnsSOARecordData _soaRecord;
  30. bool _enableBlocking;
  31. bool _allowTxtBlockingReport;
  32. Dictionary<string, object> _blockListZone;
  33. #endregion
  34. #region IDisposable
  35. public void Dispose()
  36. {
  37. //do nothing
  38. }
  39. #endregion
  40. #region private
  41. private static string GetParentZone(string domain)
  42. {
  43. int i = domain.IndexOf('.');
  44. if (i > -1)
  45. return domain.Substring(i + 1);
  46. //dont return root zone
  47. return null;
  48. }
  49. private bool IsZoneBlocked(string domain, out string blockedDomain)
  50. {
  51. domain = domain.ToLower();
  52. do
  53. {
  54. if (_blockListZone.TryGetValue(domain, out _))
  55. {
  56. //found zone blocked
  57. blockedDomain = domain;
  58. return true;
  59. }
  60. domain = GetParentZone(domain);
  61. }
  62. while (domain is not null);
  63. blockedDomain = null;
  64. return false;
  65. }
  66. #endregion
  67. #region public
  68. public Task InitializeAsync(IDnsServer dnsServer, string config)
  69. {
  70. _soaRecord = new DnsSOARecordData(dnsServer.ServerDomain, dnsServer.ResponsiblePerson.Address, 1, 14400, 3600, 604800, 60);
  71. using JsonDocument jsonDocument = JsonDocument.Parse(config);
  72. JsonElement jsonConfig = jsonDocument.RootElement;
  73. _enableBlocking = jsonConfig.GetProperty("enableBlocking").GetBoolean();
  74. _allowTxtBlockingReport = jsonConfig.GetProperty("allowTxtBlockingReport").GetBoolean();
  75. _blockListZone = jsonConfig.ReadArrayAsMap("blocked", delegate (JsonElement jsonDomainName) { return new Tuple<string, object>(jsonDomainName.GetString(), null); });
  76. return Task.CompletedTask;
  77. }
  78. public Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed)
  79. {
  80. if (!_enableBlocking)
  81. return Task.FromResult<DnsDatagram>(null);
  82. DnsQuestionRecord question = request.Question[0];
  83. if (!IsZoneBlocked(question.Name, out string blockedDomain))
  84. return Task.FromResult<DnsDatagram>(null);
  85. if (_allowTxtBlockingReport && (question.Type == DnsResourceRecordType.TXT))
  86. {
  87. //return meta data
  88. DnsResourceRecord[] answer = new DnsResourceRecord[] { new DnsResourceRecord(question.Name, DnsResourceRecordType.TXT, question.Class, 60, new DnsTXTRecordData("source=nx-domain-app; domain=" + blockedDomain)) };
  89. return Task.FromResult(new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NoError, request.Question, answer) { Tag = DnsServerResponseType.Blocked });
  90. }
  91. else
  92. {
  93. string parentDomain = GetParentZone(blockedDomain);
  94. if (parentDomain is null)
  95. parentDomain = string.Empty;
  96. IReadOnlyList<DnsResourceRecord> authority = new DnsResourceRecord[] { new DnsResourceRecord(parentDomain, DnsResourceRecordType.SOA, question.Class, 60, _soaRecord) };
  97. return Task.FromResult(new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NxDomain, request.Question, null, authority) { Tag = DnsServerResponseType.Blocked });
  98. }
  99. }
  100. #endregion
  101. #region properties
  102. public string Description
  103. { get { return "Blocks configured domain names with a NX Domain response."; } }
  104. #endregion
  105. }
  106. }