SimpleCNAME.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.Net;
  22. using TechnitiumLibrary.Net.Dns;
  23. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  24. namespace SplitHorizon
  25. {
  26. public sealed class SimpleCNAME : IDnsApplication, IDnsAppRecordRequestHandler
  27. {
  28. #region IDisposable
  29. public void Dispose()
  30. {
  31. //do nothing
  32. }
  33. #endregion
  34. #region public
  35. public Task InitializeAsync(IDnsServer dnsServer, string config)
  36. {
  37. //SimpleAddress loads the shared config
  38. return Task.CompletedTask;
  39. }
  40. public Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, string zoneName, string appRecordName, uint appRecordTtl, string appRecordData)
  41. {
  42. DnsQuestionRecord question = request.Question[0];
  43. if (!question.Name.Equals(appRecordName, StringComparison.OrdinalIgnoreCase) && !appRecordName.StartsWith('*'))
  44. return Task.FromResult<DnsDatagram>(null);
  45. using JsonDocument jsonDocument = JsonDocument.Parse(appRecordData);
  46. JsonElement jsonAppRecordData = jsonDocument.RootElement;
  47. JsonElement jsonCname = default;
  48. NetworkAddress selectedNetwork = null;
  49. foreach (JsonProperty jsonProperty in jsonAppRecordData.EnumerateObject())
  50. {
  51. string name = jsonProperty.Name;
  52. if ((name == "public") || (name == "private"))
  53. continue;
  54. if (SimpleAddress.Networks.TryGetValue(name, out List<NetworkAddress> networkAddresses))
  55. {
  56. foreach (NetworkAddress networkAddress in networkAddresses)
  57. {
  58. if (networkAddress.Contains(remoteEP.Address))
  59. {
  60. jsonCname = jsonProperty.Value;
  61. break;
  62. }
  63. }
  64. if (jsonCname.ValueKind != JsonValueKind.Undefined)
  65. break;
  66. }
  67. else if (NetworkAddress.TryParse(name, out NetworkAddress networkAddress))
  68. {
  69. if (networkAddress.Contains(remoteEP.Address) && ((selectedNetwork is null) || (networkAddress.PrefixLength > selectedNetwork.PrefixLength)))
  70. {
  71. selectedNetwork = networkAddress;
  72. jsonCname = jsonProperty.Value;
  73. }
  74. }
  75. }
  76. if (jsonCname.ValueKind == JsonValueKind.Undefined)
  77. {
  78. if (NetUtilities.IsPrivateIP(remoteEP.Address))
  79. {
  80. if (!jsonAppRecordData.TryGetProperty("private", out jsonCname))
  81. return Task.FromResult<DnsDatagram>(null);
  82. }
  83. else
  84. {
  85. if (!jsonAppRecordData.TryGetProperty("public", out jsonCname))
  86. return Task.FromResult<DnsDatagram>(null);
  87. }
  88. }
  89. string cname = jsonCname.GetString();
  90. if (string.IsNullOrEmpty(cname))
  91. return Task.FromResult<DnsDatagram>(null);
  92. IReadOnlyList<DnsResourceRecord> answers;
  93. if (question.Name.Equals(zoneName, StringComparison.OrdinalIgnoreCase)) //check for zone apex
  94. answers = new DnsResourceRecord[] { new DnsResourceRecord(question.Name, DnsResourceRecordType.ANAME, DnsClass.IN, appRecordTtl, new DnsANAMERecordData(cname)) }; //use ANAME
  95. else
  96. answers = new DnsResourceRecord[] { new DnsResourceRecord(question.Name, DnsResourceRecordType.CNAME, DnsClass.IN, appRecordTtl, new DnsCNAMERecordData(cname)) };
  97. return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answers));
  98. }
  99. #endregion
  100. #region properties
  101. public string Description
  102. { get { return "Returns different CNAME record for clients querying over public, private, or other specified networks. Note that the app will return ANAME record for an APP record at zone apex."; } }
  103. public string ApplicationRecordDataTemplate
  104. {
  105. get
  106. {
  107. return @"{
  108. ""public"": ""api.example.com"",
  109. ""private"": ""api.example.corp"",
  110. ""custom-networks"": ""custom.example.corp"",
  111. ""10.0.0.0/8"": ""api.intranet.example.corp""
  112. }";
  113. }
  114. }
  115. #endregion
  116. }
  117. }