From 180d7d504d9c512b447a7e21d5c06afbaba313f7 Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Wed, 1 May 2024 14:33:32 +0200 Subject: [PATCH] add special handling for NA-W/NA-E remove unused usings --- .../Services/GeoIPService.cs | 36 +++++++++++++------ .../Utils/ServerConfiguration.cs | 4 +-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/MareSynchronosServer/MareSynchronosServer/Services/GeoIPService.cs b/MareSynchronosServer/MareSynchronosServer/Services/GeoIPService.cs index eaa34b3..e7fc742 100644 --- a/MareSynchronosServer/MareSynchronosServer/Services/GeoIPService.cs +++ b/MareSynchronosServer/MareSynchronosServer/Services/GeoIPService.cs @@ -10,7 +10,7 @@ public class GeoIPService : IHostedService private readonly ILogger _logger; private readonly IConfigurationService _mareConfiguration; private bool _useGeoIP = false; - private string _countryFile = string.Empty; + private string _cityFile = string.Empty; private DatabaseReader? _dbReader; private DateTime _dbLastWriteTime = DateTime.Now; private CancellationTokenSource _fileWriteTimeCheckCts = new(); @@ -38,9 +38,23 @@ public class GeoIPService : IHostedService waitCts.CancelAfter(TimeSpan.FromSeconds(5)); while (_processingReload) await Task.Delay(100, waitCts.Token).ConfigureAwait(false); - if (_dbReader.TryCountry(ip, out var response)) + if (_dbReader.TryCity(ip, out var response)) { - return response.Continent.Code; + var continent = response.Continent.Code; + if (string.Equals(continent, "NA", StringComparison.Ordinal) + && response.Location.Longitude != null) + { + if (response.Location.Longitude < -102) + { + continent = "NA-W"; + } + else + { + continent = "NA-E"; + } + } + + return continent; } return "*"; @@ -71,20 +85,20 @@ public class GeoIPService : IHostedService _processingReload = true; var useGeoIP = _mareConfiguration.GetValueOrDefault(nameof(ServerConfiguration.UseGeoIP), false); - var countryFile = _mareConfiguration.GetValueOrDefault(nameof(ServerConfiguration.GeoIPDbCountryFile), string.Empty); - var lastWriteTime = new FileInfo(countryFile).LastWriteTimeUtc; - if (useGeoIP && (!string.Equals(countryFile, _countryFile, StringComparison.OrdinalIgnoreCase) || lastWriteTime != _dbLastWriteTime)) + var cityFile = _mareConfiguration.GetValueOrDefault(nameof(ServerConfiguration.GeoIPDbCityFile), string.Empty); + var lastWriteTime = new FileInfo(cityFile).LastWriteTimeUtc; + if (useGeoIP && (!string.Equals(cityFile, _cityFile, StringComparison.OrdinalIgnoreCase) || lastWriteTime != _dbLastWriteTime)) { - _countryFile = countryFile; - if (!File.Exists(_countryFile)) throw new FileNotFoundException($"Could not open GeoIP Country Database, path does not exist: {_countryFile}"); + _cityFile = cityFile; + if (!File.Exists(_cityFile)) throw new FileNotFoundException($"Could not open GeoIP City Database, path does not exist: {_cityFile}"); _dbReader?.Dispose(); _dbReader = null; - _dbReader = new DatabaseReader(_countryFile); + _dbReader = new DatabaseReader(_cityFile); _dbLastWriteTime = lastWriteTime; - _ = _dbReader.Country("8.8.8.8").Continent; + _ = _dbReader.City("8.8.8.8").Continent; - _logger.LogInformation($"Loaded GeoIP country file from {_countryFile}"); + _logger.LogInformation($"Loaded GeoIP city file from {_cityFile}"); if (_useGeoIP != useGeoIP) { diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ServerConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/ServerConfiguration.cs index 8f54d0c..abb648a 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/ServerConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/ServerConfiguration.cs @@ -24,7 +24,7 @@ public class ServerConfiguration : MareConfigurationAuthBase [RemoteConfiguration] public int PurgeUnusedAccountsPeriodInDays { get; set; } = 14; - public string GeoIPDbCountryFile { get; set; } = string.Empty; + public string GeoIPDbCityFile { get; set; } = string.Empty; public int RedisPool { get; set; } = 50; @@ -41,7 +41,7 @@ public class ServerConfiguration : MareConfigurationAuthBase sb.AppendLine($"{nameof(MaxGroupUserCount)} => {MaxGroupUserCount}"); sb.AppendLine($"{nameof(PurgeUnusedAccounts)} => {PurgeUnusedAccounts}"); sb.AppendLine($"{nameof(PurgeUnusedAccountsPeriodInDays)} => {PurgeUnusedAccountsPeriodInDays}"); - sb.AppendLine($"{nameof(GeoIPDbCountryFile)} => {GeoIPDbCountryFile}"); + sb.AppendLine($"{nameof(GeoIPDbCityFile)} => {GeoIPDbCityFile}"); return sb.ToString(); } } \ No newline at end of file