SimpleCNAME.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 DnsApplicationCommon;
  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 : IDnsApplicationRequestHandler
  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, string zoneName, uint appRecordTtl, string appRecordData, bool isRecursionAllowed, IDnsServer dnsServer)
  41. {
  42. dynamic jsonAppRecordData = JsonConvert.DeserializeObject(appRecordData);
  43. dynamic jsonCname;
  44. if (NetUtilities.IsPrivateIP(remoteEP.Address))
  45. jsonCname = jsonAppRecordData.@private;
  46. else
  47. jsonCname = jsonAppRecordData.@public;
  48. if (jsonCname == null)
  49. return Task.FromResult<DnsDatagram>(null);
  50. string cname = jsonCname.Value;
  51. if (string.IsNullOrEmpty(cname))
  52. return Task.FromResult<DnsDatagram>(null);
  53. IReadOnlyList<DnsResourceRecord> answers;
  54. if (request.Question[0].Name.Equals(zoneName, StringComparison.OrdinalIgnoreCase)) //check for zone apex
  55. answers = new DnsResourceRecord[] { new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.ANAME, DnsClass.IN, appRecordTtl, new DnsANAMERecord(cname)) }; //use ANAME
  56. else
  57. answers = new DnsResourceRecord[] { new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.CNAME, DnsClass.IN, appRecordTtl, new DnsCNAMERecord(cname)) };
  58. return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answers));
  59. }
  60. #endregion
  61. #region properties
  62. public string Description
  63. { get { return "Returns different CNAME record for clients querying over public and private networks. Note that the app will return ANAME record for an APP record at zone apex."; } }
  64. public string ApplicationRecordDataTemplate
  65. {
  66. get
  67. {
  68. return @"{
  69. ""public"": ""api.example.com"",
  70. ""private"": ""api.example.corp""
  71. }";
  72. }
  73. }
  74. #endregion
  75. }
  76. }