App.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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;
  23. using TechnitiumLibrary.Net.Dns;
  24. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  25. namespace FilterAaaa
  26. {
  27. public sealed class App : IDnsApplication, IDnsPostProcessor
  28. {
  29. #region variables
  30. IDnsServer _dnsServer;
  31. bool _enableFilterAaaa;
  32. bool _bypassLocalZones;
  33. NetworkAddress[] _bypassNetworks;
  34. string[] _bypassDomains;
  35. #endregion
  36. #region IDisposable
  37. public void Dispose()
  38. {
  39. //do nothing
  40. }
  41. #endregion
  42. #region public
  43. public Task InitializeAsync(IDnsServer dnsServer, string config)
  44. {
  45. _dnsServer = dnsServer;
  46. using JsonDocument jsonDocument = JsonDocument.Parse(config);
  47. JsonElement jsonConfig = jsonDocument.RootElement;
  48. _enableFilterAaaa = jsonConfig.GetPropertyValue("enableFilterAaaa", false);
  49. _bypassLocalZones = jsonConfig.GetPropertyValue("bypassLocalZones", false);
  50. if (jsonConfig.TryReadArray("bypassNetworks", NetworkAddress.Parse, out NetworkAddress[] bypassNetworks))
  51. _bypassNetworks = bypassNetworks;
  52. else
  53. _bypassNetworks = [];
  54. if (jsonConfig.TryReadArray("bypassDomains", out string[] bypassDomains))
  55. _bypassDomains = bypassDomains;
  56. else
  57. _bypassDomains = [];
  58. return Task.CompletedTask;
  59. }
  60. public async Task<DnsDatagram> PostProcessAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response)
  61. {
  62. if (!_enableFilterAaaa)
  63. return response;
  64. if (_bypassLocalZones && response.AuthoritativeAnswer)
  65. return response;
  66. if (request.DnssecOk)
  67. return response;
  68. if (response.RCODE != DnsResponseCode.NoError)
  69. return response;
  70. DnsQuestionRecord question = request.Question[0];
  71. if (question.Type != DnsResourceRecordType.AAAA)
  72. return response;
  73. bool hasAAAA = false;
  74. foreach (DnsResourceRecord record in response.Answer)
  75. {
  76. if (record.Type == DnsResourceRecordType.AAAA)
  77. {
  78. hasAAAA = true;
  79. break;
  80. }
  81. }
  82. if (!hasAAAA)
  83. return response;
  84. IPAddress remoteIP = remoteEP.Address;
  85. foreach (NetworkAddress network in _bypassNetworks)
  86. {
  87. if (network.Contains(remoteIP))
  88. return response;
  89. }
  90. string qname = question.Name;
  91. foreach (string allowedDomain in _bypassDomains)
  92. {
  93. if (qname.Equals(allowedDomain, StringComparison.OrdinalIgnoreCase) || qname.EndsWith("." + allowedDomain, StringComparison.OrdinalIgnoreCase))
  94. return response;
  95. }
  96. DnsDatagram aResponse = await _dnsServer.DirectQueryAsync(new DnsQuestionRecord(qname, DnsResourceRecordType.A, DnsClass.IN), 2000);
  97. if (aResponse.RCODE != DnsResponseCode.NoError)
  98. return response;
  99. foreach (DnsResourceRecord record in aResponse.Answer)
  100. {
  101. if (record.Type == DnsResourceRecordType.A)
  102. {
  103. //domain has an A record; filter current AAAA response
  104. List<DnsResourceRecord> answer = new List<DnsResourceRecord>();
  105. foreach (DnsResourceRecord record2 in response.Answer)
  106. {
  107. if (record2.Type == DnsResourceRecordType.CNAME)
  108. {
  109. answer.Add(record2);
  110. qname = (record2.RDATA as DnsCNAMERecordData).Domain;
  111. }
  112. }
  113. DnsResourceRecord[] authority = [new DnsResourceRecord(qname, DnsResourceRecordType.SOA, DnsClass.IN, 30, new DnsSOARecordData(_dnsServer.ServerDomain, _dnsServer.ResponsiblePerson.Address, 1, 3600, 900, 86400, 30))];
  114. return new DnsDatagram(response.Identifier, true, response.OPCODE, false, false, response.RecursionDesired, response.RecursionAvailable, false, false, DnsResponseCode.NoError, response.Question, answer, authority);
  115. }
  116. }
  117. //domain does not have an A record; return current response
  118. return response;
  119. }
  120. #endregion
  121. #region properties
  122. public string Description
  123. { get { return "Filters AAAA records by returning NO DATA response when A records for the same domain name are available."; } }
  124. #endregion
  125. }
  126. }