SimpleCNAME.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2021 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 Newtonsoft.Json;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Net;
  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 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. //no config needed
  38. return Task.CompletedTask;
  39. }
  40. public Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, string zoneName, uint appRecordTtl, string appRecordData)
  41. {
  42. dynamic jsonAppRecordData = JsonConvert.DeserializeObject(appRecordData);
  43. dynamic jsonCname = null;
  44. foreach (dynamic jsonProperty in jsonAppRecordData)
  45. {
  46. string name = jsonProperty.Name;
  47. if ((name == "public") || (name == "private"))
  48. continue;
  49. NetworkAddress networkAddress = NetworkAddress.Parse(name);
  50. if (networkAddress.Contains(remoteEP.Address))
  51. {
  52. jsonCname = jsonProperty.Value;
  53. break;
  54. }
  55. }
  56. if (jsonCname is null)
  57. {
  58. if (NetUtilities.IsPrivateIP(remoteEP.Address))
  59. jsonCname = jsonAppRecordData.@private;
  60. else
  61. jsonCname = jsonAppRecordData.@public;
  62. if (jsonCname is null)
  63. return Task.FromResult<DnsDatagram>(null);
  64. }
  65. string cname = jsonCname.Value;
  66. if (string.IsNullOrEmpty(cname))
  67. return Task.FromResult<DnsDatagram>(null);
  68. DnsQuestionRecord question = request.Question[0];
  69. IReadOnlyList<DnsResourceRecord> answers;
  70. if (question.Name.Equals(zoneName, StringComparison.OrdinalIgnoreCase)) //check for zone apex
  71. answers = new DnsResourceRecord[] { new DnsResourceRecord(question.Name, DnsResourceRecordType.ANAME, DnsClass.IN, appRecordTtl, new DnsANAMERecordData(cname)) }; //use ANAME
  72. else
  73. answers = new DnsResourceRecord[] { new DnsResourceRecord(question.Name, DnsResourceRecordType.CNAME, DnsClass.IN, appRecordTtl, new DnsCNAMERecordData(cname)) };
  74. return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answers));
  75. }
  76. #endregion
  77. #region properties
  78. public string Description
  79. { 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."; } }
  80. public string ApplicationRecordDataTemplate
  81. {
  82. get
  83. {
  84. return @"{
  85. ""public"": ""api.example.com"",
  86. ""private"": ""api.example.corp"",
  87. ""10.0.0.0/8"": ""api.intranet.example.corp""
  88. }";
  89. }
  90. }
  91. #endregion
  92. }
  93. }