App.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.Net;
  18. using System.Text.Json;
  19. using System.Threading.Tasks;
  20. using TechnitiumLibrary.Net.Dns;
  21. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  22. namespace NoData
  23. {
  24. public sealed class App : IDnsApplication, IDnsAppRecordRequestHandler
  25. {
  26. #region IDisposable
  27. public void Dispose()
  28. {
  29. //do nothing
  30. }
  31. #endregion
  32. #region public
  33. public Task InitializeAsync(IDnsServer dnsServer, string config)
  34. {
  35. //do nothing
  36. return Task.CompletedTask;
  37. }
  38. public Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, string zoneName, string appRecordName, uint appRecordTtl, string appRecordData)
  39. {
  40. DnsQuestionRecord question = request.Question[0];
  41. if (!question.Name.Equals(appRecordName, StringComparison.OrdinalIgnoreCase) && !appRecordName.StartsWith('*'))
  42. return Task.FromResult<DnsDatagram>(null);
  43. using JsonDocument jsonDocument = JsonDocument.Parse(appRecordData);
  44. JsonElement jsonAppRecordData = jsonDocument.RootElement;
  45. foreach (JsonElement jsonBlockedType in jsonAppRecordData.GetProperty("blockedTypes").EnumerateArray())
  46. {
  47. DnsResourceRecordType blockedType = Enum.Parse<DnsResourceRecordType>(jsonBlockedType.GetString(), true);
  48. if ((blockedType == question.Type) || (blockedType == DnsResourceRecordType.ANY))
  49. return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question));
  50. }
  51. return Task.FromResult<DnsDatagram>(null);
  52. }
  53. #endregion
  54. #region properties
  55. public string Description
  56. { get { return "Returns a NO DATA response for requests that query for the blocked resource record types in Conditional Forwarder zones."; } }
  57. public string ApplicationRecordDataTemplate
  58. {
  59. get
  60. {
  61. return @"{
  62. ""blockedTypes"": [
  63. ""A"",
  64. ""AAAA"",
  65. ""ANY""
  66. ]
  67. }";
  68. }
  69. }
  70. #endregion
  71. }
  72. }