From 19d550429ce56d18c6578598db6ba3a2d5081f77 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 16 Mar 2024 12:38:08 +0530 Subject: [PATCH] GeoContinent.MaxMind: Implemented support for ISP/ASN database. --- Apps/GeoContinentApp/MaxMind.cs | 38 +++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Apps/GeoContinentApp/MaxMind.cs b/Apps/GeoContinentApp/MaxMind.cs index 362f7878..39121f1c 100644 --- a/Apps/GeoContinentApp/MaxMind.cs +++ b/Apps/GeoContinentApp/MaxMind.cs @@ -1,6 +1,6 @@ /* Technitium DNS Server -Copyright (C) 2021 Shreyas Zare (shreyas@technitium.com) +Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,6 +31,8 @@ namespace GeoContinent static MaxMind _maxMind; readonly DatabaseReader _mmCountryReader; + readonly DatabaseReader _mmIspReader; + readonly DatabaseReader _mmAsnReader; #endregion @@ -38,15 +40,26 @@ namespace GeoContinent private MaxMind(IDnsServer dnsServer) { - string mmFile = Path.Combine(dnsServer.ApplicationFolder, "GeoIP2-Country.mmdb"); + string mmCountryFile = Path.Combine(dnsServer.ApplicationFolder, "GeoIP2-Country.mmdb"); - if (!File.Exists(mmFile)) - mmFile = Path.Combine(dnsServer.ApplicationFolder, "GeoLite2-Country.mmdb"); + if (!File.Exists(mmCountryFile)) + mmCountryFile = Path.Combine(dnsServer.ApplicationFolder, "GeoLite2-Country.mmdb"); - if (!File.Exists(mmFile)) + if (!File.Exists(mmCountryFile)) throw new FileNotFoundException("MaxMind Country file is missing!"); - _mmCountryReader = new DatabaseReader(mmFile); + _mmCountryReader = new DatabaseReader(mmCountryFile); + + string mmIspFile = Path.Combine(dnsServer.ApplicationFolder, "GeoIP2-ISP.mmdb"); + if (File.Exists(mmIspFile)) + { + _mmIspReader = new DatabaseReader(mmIspFile); + return; + } + + string mmAsnFile = Path.Combine(dnsServer.ApplicationFolder, "GeoLite2-ASN.mmdb"); + if (File.Exists(mmAsnFile)) + _mmAsnReader = new DatabaseReader(mmAsnFile); } #endregion @@ -62,8 +75,9 @@ namespace GeoContinent if (disposing) { - if (_mmCountryReader is not null) - _mmCountryReader.Dispose(); + _mmCountryReader?.Dispose(); + _mmIspReader?.Dispose(); + _mmAsnReader?.Dispose(); } _disposed = true; @@ -91,9 +105,15 @@ namespace GeoContinent #region properties - public DatabaseReader DatabaseReader + public DatabaseReader CountryReader { get { return _mmCountryReader; } } + public DatabaseReader IspReader + { get { return _mmIspReader; } } + + public DatabaseReader AsnReader + { get { return _mmAsnReader; } } + #endregion } }