Address.cs 5.4 KB

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