CNAME.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Net;
  23. using System.Threading.Tasks;
  24. using TechnitiumLibrary.Net.Dns;
  25. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  26. namespace GeoContinent
  27. {
  28. public sealed class CNAME : 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. dynamic jsonAppRecordData = JsonConvert.DeserializeObject(appRecordData);
  68. dynamic jsonContinent;
  69. if (_mmCountryReader.TryCountry(remoteEP.Address, out CountryResponse response))
  70. {
  71. jsonContinent = jsonAppRecordData[response.Continent.Code];
  72. if (jsonContinent == null)
  73. jsonContinent = jsonAppRecordData["default"];
  74. }
  75. else
  76. {
  77. jsonContinent = jsonAppRecordData["default"];
  78. }
  79. if (jsonContinent == null)
  80. return Task.FromResult<DnsDatagram>(null);
  81. string cname = jsonContinent.Value;
  82. if (string.IsNullOrEmpty(cname))
  83. return Task.FromResult<DnsDatagram>(null);
  84. IReadOnlyList<DnsResourceRecord> answers;
  85. if (request.Question[0].Name.Equals(zoneName, StringComparison.OrdinalIgnoreCase)) //check for zone apex
  86. answers = new DnsResourceRecord[] { new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.ANAME, DnsClass.IN, appRecordTtl, new DnsANAMERecord(cname)) }; //use ANAME
  87. else
  88. answers = new DnsResourceRecord[] { new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.CNAME, DnsClass.IN, appRecordTtl, new DnsCNAMERecord(cname)) };
  89. return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answers));
  90. }
  91. #endregion
  92. #region properties
  93. public string Description
  94. { get { return "Returns CNAME record based on the continent the client queries from using MaxMind GeoIP2 Country database. Note that the app will return ANAME record for an APP record at zone apex. Use the two character continent code like \"NA\" (North America) or \"OC\" (Oceania)."; } }
  95. public string ApplicationRecordDataTemplate
  96. {
  97. get
  98. {
  99. return @"{
  100. ""EU"": ""eu.example.com"",
  101. ""default"": ""example.com""
  102. }";
  103. }
  104. }
  105. #endregion
  106. }
  107. }