SimpleAddress.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.Collections.Generic;
  18. using System.Net;
  19. using System.Net.Sockets;
  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 SimpleAddress : 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. switch (request.Question[0].Type)
  43. {
  44. case DnsResourceRecordType.A:
  45. case DnsResourceRecordType.AAAA:
  46. dynamic jsonAppRecordData = JsonConvert.DeserializeObject(appRecordData);
  47. dynamic jsonAddresses;
  48. if (NetUtilities.IsPrivateIP(remoteEP.Address))
  49. jsonAddresses = jsonAppRecordData.@private;
  50. else
  51. jsonAddresses = jsonAppRecordData.@public;
  52. if (jsonAddresses == null)
  53. return Task.FromResult<DnsDatagram>(null);
  54. List<DnsResourceRecord> answers = new List<DnsResourceRecord>();
  55. foreach (dynamic jsonAddress in jsonAddresses)
  56. {
  57. IPAddress address = IPAddress.Parse(jsonAddress.Value);
  58. switch (request.Question[0].Type)
  59. {
  60. case DnsResourceRecordType.A:
  61. if (address.AddressFamily == AddressFamily.InterNetwork)
  62. answers.Add(new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.A, DnsClass.IN, appRecordTtl, new DnsARecord(address)));
  63. break;
  64. case DnsResourceRecordType.AAAA:
  65. if (address.AddressFamily == AddressFamily.InterNetworkV6)
  66. answers.Add(new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.AAAA, DnsClass.IN, appRecordTtl, new DnsAAAARecord(address)));
  67. break;
  68. }
  69. }
  70. if (answers.Count == 0)
  71. return Task.FromResult<DnsDatagram>(null);
  72. return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answers));
  73. default:
  74. return Task.FromResult<DnsDatagram>(null);
  75. }
  76. }
  77. #endregion
  78. #region properties
  79. public string Description
  80. { get { return "Returns A or AAAA records with different set of IP addresses for clients querying over public and private networks."; } }
  81. public string ApplicationRecordDataTemplate
  82. {
  83. get
  84. {
  85. return @"{
  86. ""public"": [
  87. ""1.1.1.1"",
  88. ""2.2.2.2""
  89. ],
  90. ""private"": [
  91. ""192.168.1.1"",
  92. ""::1""
  93. ]
  94. }";
  95. }
  96. }
  97. #endregion
  98. }
  99. }