MaxMind.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 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;
  17. using System;
  18. using System.IO;
  19. namespace GeoContinent
  20. {
  21. class MaxMind : IDisposable
  22. {
  23. #region variables
  24. static MaxMind _maxMind;
  25. readonly DatabaseReader _mmCountryReader;
  26. readonly DatabaseReader _mmIspReader;
  27. readonly DatabaseReader _mmAsnReader;
  28. #endregion
  29. #region constructor
  30. private MaxMind(IDnsServer dnsServer)
  31. {
  32. string mmCountryFile = Path.Combine(dnsServer.ApplicationFolder, "GeoIP2-Country.mmdb");
  33. if (!File.Exists(mmCountryFile))
  34. mmCountryFile = Path.Combine(dnsServer.ApplicationFolder, "GeoLite2-Country.mmdb");
  35. if (!File.Exists(mmCountryFile))
  36. throw new FileNotFoundException("MaxMind Country file is missing!");
  37. _mmCountryReader = new DatabaseReader(mmCountryFile);
  38. string mmIspFile = Path.Combine(dnsServer.ApplicationFolder, "GeoIP2-ISP.mmdb");
  39. if (File.Exists(mmIspFile))
  40. {
  41. _mmIspReader = new DatabaseReader(mmIspFile);
  42. return;
  43. }
  44. string mmAsnFile = Path.Combine(dnsServer.ApplicationFolder, "GeoLite2-ASN.mmdb");
  45. if (File.Exists(mmAsnFile))
  46. _mmAsnReader = new DatabaseReader(mmAsnFile);
  47. }
  48. #endregion
  49. #region IDisposable
  50. bool _disposed;
  51. protected virtual void Dispose(bool disposing)
  52. {
  53. if (_disposed)
  54. return;
  55. if (disposing)
  56. {
  57. _mmCountryReader?.Dispose();
  58. _mmIspReader?.Dispose();
  59. _mmAsnReader?.Dispose();
  60. }
  61. _disposed = true;
  62. }
  63. public void Dispose()
  64. {
  65. Dispose(true);
  66. GC.SuppressFinalize(this);
  67. }
  68. #endregion
  69. #region public
  70. public static MaxMind Create(IDnsServer dnsServer)
  71. {
  72. if (_maxMind is null)
  73. _maxMind = new MaxMind(dnsServer);
  74. return _maxMind;
  75. }
  76. #endregion
  77. #region properties
  78. public DatabaseReader CountryReader
  79. { get { return _mmCountryReader; } }
  80. public DatabaseReader IspReader
  81. { get { return _mmIspReader; } }
  82. public DatabaseReader AsnReader
  83. { get { return _mmAsnReader; } }
  84. #endregion
  85. }
  86. }