Address.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 MaxMind.GeoIP2.Responses;
  17. using Newtonsoft.Json;
  18. using System.Collections.Generic;
  19. using System.Net;
  20. using System.Net.Sockets;
  21. using System.Threading.Tasks;
  22. using TechnitiumLibrary;
  23. using TechnitiumLibrary.Net.Dns;
  24. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  25. namespace GeoCountry
  26. {
  27. public sealed class Address : IDnsApplication, IDnsAppRecordRequestHandler
  28. {
  29. #region variables
  30. MaxMind _maxMind;
  31. #endregion
  32. #region IDisposable
  33. bool _disposed;
  34. private void Dispose(bool disposing)
  35. {
  36. if (_disposed)
  37. return;
  38. if (disposing)
  39. {
  40. if (_maxMind is not null)
  41. _maxMind.Dispose();
  42. }
  43. _disposed = true;
  44. }
  45. public void Dispose()
  46. {
  47. Dispose(true);
  48. }
  49. #endregion
  50. #region public
  51. public Task InitializeAsync(IDnsServer dnsServer, string config)
  52. {
  53. _maxMind = MaxMind.Create(dnsServer);
  54. return Task.CompletedTask;
  55. }
  56. public Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, string zoneName, uint appRecordTtl, string appRecordData)
  57. {
  58. DnsQuestionRecord question = request.Question[0];
  59. switch (question.Type)
  60. {
  61. case DnsResourceRecordType.A:
  62. case DnsResourceRecordType.AAAA:
  63. dynamic jsonAppRecordData = JsonConvert.DeserializeObject(appRecordData);
  64. dynamic jsonCountry;
  65. if (_maxMind.DatabaseReader.TryCountry(remoteEP.Address, out CountryResponse response))
  66. {
  67. jsonCountry = jsonAppRecordData[response.Country.IsoCode];
  68. if (jsonCountry == null)
  69. jsonCountry = jsonAppRecordData["default"];
  70. }
  71. else
  72. {
  73. jsonCountry = jsonAppRecordData["default"];
  74. }
  75. if (jsonCountry == null)
  76. return Task.FromResult<DnsDatagram>(null);
  77. List<DnsResourceRecord> answers = new List<DnsResourceRecord>();
  78. switch (question.Type)
  79. {
  80. case DnsResourceRecordType.A:
  81. foreach (dynamic jsonAddress in jsonCountry)
  82. {
  83. IPAddress address = IPAddress.Parse(jsonAddress.Value);
  84. if (address.AddressFamily == AddressFamily.InterNetwork)
  85. answers.Add(new DnsResourceRecord(question.Name, DnsResourceRecordType.A, DnsClass.IN, appRecordTtl, new DnsARecordData(address)));
  86. }
  87. break;
  88. case DnsResourceRecordType.AAAA:
  89. foreach (dynamic jsonAddress in jsonCountry)
  90. {
  91. IPAddress address = IPAddress.Parse(jsonAddress.Value);
  92. if (address.AddressFamily == AddressFamily.InterNetworkV6)
  93. answers.Add(new DnsResourceRecord(question.Name, DnsResourceRecordType.AAAA, DnsClass.IN, appRecordTtl, new DnsAAAARecordData(address)));
  94. }
  95. break;
  96. }
  97. if (answers.Count == 0)
  98. return Task.FromResult<DnsDatagram>(null);
  99. if (answers.Count > 1)
  100. answers.Shuffle();
  101. return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answers));
  102. default:
  103. return Task.FromResult<DnsDatagram>(null);
  104. }
  105. }
  106. #endregion
  107. #region properties
  108. public string Description
  109. { get { return "Returns A or AAAA records based on the country the client queries from using MaxMind GeoIP2 Country database. Use the two-character ISO 3166-1 alpha code for the country."; } }
  110. public string ApplicationRecordDataTemplate
  111. {
  112. get
  113. {
  114. return @"{
  115. ""IN"": [
  116. ""1.1.1.1"",
  117. ""2.2.2.2""
  118. ],
  119. ""default"": [
  120. ""3.3.3.3""
  121. ]
  122. }";
  123. }
  124. }
  125. #endregion
  126. }
  127. }