App.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.IO;
  19. using System.Net;
  20. using System.Text.Json;
  21. using System.Threading.Tasks;
  22. using TechnitiumLibrary;
  23. using TechnitiumLibrary.Net;
  24. using TechnitiumLibrary.Net.Dns;
  25. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  26. namespace FilterAaaa
  27. {
  28. public sealed class App : IDnsApplication, IDnsPostProcessor
  29. {
  30. #region variables
  31. IDnsServer _dnsServer;
  32. bool _enableFilterAaaa;
  33. uint _defaultTtl;
  34. bool _bypassLocalZones;
  35. NetworkAddress[] _bypassNetworks;
  36. string[] _bypassDomains;
  37. string[] _filterDomains;
  38. #endregion
  39. #region IDisposable
  40. public void Dispose()
  41. {
  42. //do nothing
  43. }
  44. #endregion
  45. #region public
  46. public async Task InitializeAsync(IDnsServer dnsServer, string config)
  47. {
  48. _dnsServer = dnsServer;
  49. using JsonDocument jsonDocument = JsonDocument.Parse(config);
  50. JsonElement jsonConfig = jsonDocument.RootElement;
  51. _enableFilterAaaa = jsonConfig.GetPropertyValue("enableFilterAaaa", false);
  52. if (jsonConfig.TryGetProperty("defaultTtl", out JsonElement jsonValue))
  53. {
  54. if (!jsonValue.TryGetUInt32(out _defaultTtl))
  55. _defaultTtl = 30u;
  56. }
  57. else
  58. {
  59. _defaultTtl = 30u;
  60. //update config for new option
  61. config = config.Replace("\"bypassLocalZones\"", "\"defaultTtl\": 30,\r\n \"bypassLocalZones\"");
  62. await File.WriteAllTextAsync(Path.Combine(dnsServer.ApplicationFolder, "dnsApp.config"), config);
  63. }
  64. _bypassLocalZones = jsonConfig.GetPropertyValue("bypassLocalZones", false);
  65. if (jsonConfig.TryReadArray("bypassNetworks", NetworkAddress.Parse, out NetworkAddress[] bypassNetworks))
  66. _bypassNetworks = bypassNetworks;
  67. else
  68. _bypassNetworks = [];
  69. if (jsonConfig.TryReadArray("bypassDomains", out string[] bypassDomains))
  70. _bypassDomains = bypassDomains;
  71. else
  72. _bypassDomains = [];
  73. if (jsonConfig.TryReadArray("filterDomains", out string[] filterDomains))
  74. {
  75. _filterDomains = filterDomains;
  76. }
  77. else
  78. {
  79. _filterDomains = [];
  80. //update config for new feature
  81. config = config.TrimEnd('\r', '\n', ' ', '}');
  82. config += ",\r\n \"filterDomains\": [\r\n ]\r\n}";
  83. await File.WriteAllTextAsync(Path.Combine(dnsServer.ApplicationFolder, "dnsApp.config"), config);
  84. }
  85. }
  86. public async Task<DnsDatagram> PostProcessAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response)
  87. {
  88. if (!_enableFilterAaaa)
  89. return response;
  90. if (_bypassLocalZones && response.AuthoritativeAnswer)
  91. return response;
  92. if (request.DnssecOk)
  93. return response;
  94. if (response.RCODE != DnsResponseCode.NoError)
  95. return response;
  96. DnsQuestionRecord question = request.Question[0];
  97. if (question.Type != DnsResourceRecordType.AAAA)
  98. return response;
  99. bool hasAAAA = false;
  100. foreach (DnsResourceRecord record in response.Answer)
  101. {
  102. if (record.Type == DnsResourceRecordType.AAAA)
  103. {
  104. hasAAAA = true;
  105. break;
  106. }
  107. }
  108. if (!hasAAAA)
  109. return response;
  110. IPAddress remoteIP = remoteEP.Address;
  111. foreach (NetworkAddress network in _bypassNetworks)
  112. {
  113. if (network.Contains(remoteIP))
  114. return response;
  115. }
  116. string qname = question.Name;
  117. foreach (string allowedDomain in _bypassDomains)
  118. {
  119. if (qname.Equals(allowedDomain, StringComparison.OrdinalIgnoreCase) || qname.EndsWith("." + allowedDomain, StringComparison.OrdinalIgnoreCase))
  120. return response;
  121. }
  122. bool filterDomain = _filterDomains.Length == 0;
  123. foreach (string blockedDomain in _filterDomains)
  124. {
  125. if (qname.Equals(blockedDomain, StringComparison.OrdinalIgnoreCase) || qname.EndsWith("." + blockedDomain, StringComparison.OrdinalIgnoreCase))
  126. {
  127. filterDomain = true;
  128. break;
  129. }
  130. }
  131. if (!filterDomain)
  132. return response;
  133. DnsDatagram aResponse = await _dnsServer.DirectQueryAsync(new DnsQuestionRecord(qname, DnsResourceRecordType.A, DnsClass.IN), 2000);
  134. if (aResponse.RCODE != DnsResponseCode.NoError)
  135. return response;
  136. foreach (DnsResourceRecord record in aResponse.Answer)
  137. {
  138. if (record.Type == DnsResourceRecordType.A)
  139. {
  140. //domain has an A record; filter current AAAA response
  141. List<DnsResourceRecord> answer = new List<DnsResourceRecord>();
  142. foreach (DnsResourceRecord record2 in response.Answer)
  143. {
  144. if (record2.Type == DnsResourceRecordType.CNAME)
  145. {
  146. answer.Add(record2);
  147. qname = (record2.RDATA as DnsCNAMERecordData).Domain;
  148. }
  149. }
  150. DnsResourceRecord[] authority = [new DnsResourceRecord(qname, DnsResourceRecordType.SOA, DnsClass.IN, _defaultTtl, new DnsSOARecordData(_dnsServer.ServerDomain, _dnsServer.ResponsiblePerson.Address, 1, 3600, 900, 86400, _defaultTtl))];
  151. return new DnsDatagram(response.Identifier, true, response.OPCODE, false, false, response.RecursionDesired, response.RecursionAvailable, false, false, DnsResponseCode.NoError, response.Question, answer, authority);
  152. }
  153. }
  154. //domain does not have an A record; return current response
  155. return response;
  156. }
  157. #endregion
  158. #region properties
  159. public string Description
  160. { get { return "Filters AAAA records by returning NO DATA response when A records for the same domain name are available."; } }
  161. #endregion
  162. }
  163. }