SimpleAddress.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2022 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.Collections.Generic;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using System.Threading.Tasks;
  21. using TechnitiumLibrary;
  22. using TechnitiumLibrary.Net;
  23. using TechnitiumLibrary.Net.Dns;
  24. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  25. namespace SplitHorizon
  26. {
  27. public class SimpleAddress : IDnsApplication, IDnsAppRecordRequestHandler
  28. {
  29. #region IDisposable
  30. public void Dispose()
  31. {
  32. //do nothing
  33. }
  34. #endregion
  35. #region public
  36. public Task InitializeAsync(IDnsServer dnsServer, string config)
  37. {
  38. //no config needed
  39. return Task.CompletedTask;
  40. }
  41. public Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, string zoneName, uint appRecordTtl, string appRecordData)
  42. {
  43. DnsQuestionRecord question = request.Question[0];
  44. switch (question.Type)
  45. {
  46. case DnsResourceRecordType.A:
  47. case DnsResourceRecordType.AAAA:
  48. dynamic jsonAppRecordData = JsonConvert.DeserializeObject(appRecordData);
  49. dynamic jsonAddresses = null;
  50. foreach (dynamic jsonProperty in jsonAppRecordData)
  51. {
  52. string name = jsonProperty.Name;
  53. if ((name == "public") || (name == "private"))
  54. continue;
  55. NetworkAddress networkAddress = NetworkAddress.Parse(name);
  56. if (networkAddress.Contains(remoteEP.Address))
  57. {
  58. jsonAddresses = jsonProperty.Value;
  59. break;
  60. }
  61. }
  62. if (jsonAddresses is null)
  63. {
  64. if (NetUtilities.IsPrivateIP(remoteEP.Address))
  65. jsonAddresses = jsonAppRecordData.@private;
  66. else
  67. jsonAddresses = jsonAppRecordData.@public;
  68. if (jsonAddresses is null)
  69. return Task.FromResult<DnsDatagram>(null);
  70. }
  71. List<DnsResourceRecord> answers = new List<DnsResourceRecord>();
  72. switch (question.Type)
  73. {
  74. case DnsResourceRecordType.A:
  75. foreach (dynamic jsonAddress in jsonAddresses)
  76. {
  77. IPAddress address = IPAddress.Parse(jsonAddress.Value);
  78. if (address.AddressFamily == AddressFamily.InterNetwork)
  79. answers.Add(new DnsResourceRecord(question.Name, DnsResourceRecordType.A, DnsClass.IN, appRecordTtl, new DnsARecordData(address)));
  80. }
  81. break;
  82. case DnsResourceRecordType.AAAA:
  83. foreach (dynamic jsonAddress in jsonAddresses)
  84. {
  85. IPAddress address = IPAddress.Parse(jsonAddress.Value);
  86. if (address.AddressFamily == AddressFamily.InterNetworkV6)
  87. answers.Add(new DnsResourceRecord(question.Name, DnsResourceRecordType.AAAA, DnsClass.IN, appRecordTtl, new DnsAAAARecordData(address)));
  88. }
  89. break;
  90. }
  91. if (answers.Count == 0)
  92. return Task.FromResult<DnsDatagram>(null);
  93. if (answers.Count > 1)
  94. answers.Shuffle();
  95. return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answers));
  96. default:
  97. return Task.FromResult<DnsDatagram>(null);
  98. }
  99. }
  100. #endregion
  101. #region properties
  102. public string Description
  103. { get { return "Returns A or AAAA records with different set of IP addresses for clients querying over public, private, or other specified networks."; } }
  104. public string ApplicationRecordDataTemplate
  105. {
  106. get
  107. {
  108. return @"{
  109. ""public"": [
  110. ""1.1.1.1"",
  111. ""2.2.2.2""
  112. ],
  113. ""private"": [
  114. ""192.168.1.1"",
  115. ""::1""
  116. ],
  117. ""10.0.0.0/8"": [
  118. ""10.1.1.1""
  119. ]
  120. }";
  121. }
  122. }
  123. #endregion
  124. }
  125. }