add special handling for NA-W/NA-E
remove unused usings
This commit is contained in:
@@ -10,7 +10,7 @@ public class GeoIPService : IHostedService
|
|||||||
private readonly ILogger<GeoIPService> _logger;
|
private readonly ILogger<GeoIPService> _logger;
|
||||||
private readonly IConfigurationService<ServerConfiguration> _mareConfiguration;
|
private readonly IConfigurationService<ServerConfiguration> _mareConfiguration;
|
||||||
private bool _useGeoIP = false;
|
private bool _useGeoIP = false;
|
||||||
private string _countryFile = string.Empty;
|
private string _cityFile = string.Empty;
|
||||||
private DatabaseReader? _dbReader;
|
private DatabaseReader? _dbReader;
|
||||||
private DateTime _dbLastWriteTime = DateTime.Now;
|
private DateTime _dbLastWriteTime = DateTime.Now;
|
||||||
private CancellationTokenSource _fileWriteTimeCheckCts = new();
|
private CancellationTokenSource _fileWriteTimeCheckCts = new();
|
||||||
@@ -38,9 +38,23 @@ public class GeoIPService : IHostedService
|
|||||||
waitCts.CancelAfter(TimeSpan.FromSeconds(5));
|
waitCts.CancelAfter(TimeSpan.FromSeconds(5));
|
||||||
while (_processingReload) await Task.Delay(100, waitCts.Token).ConfigureAwait(false);
|
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 "*";
|
return "*";
|
||||||
@@ -71,20 +85,20 @@ public class GeoIPService : IHostedService
|
|||||||
_processingReload = true;
|
_processingReload = true;
|
||||||
|
|
||||||
var useGeoIP = _mareConfiguration.GetValueOrDefault(nameof(ServerConfiguration.UseGeoIP), false);
|
var useGeoIP = _mareConfiguration.GetValueOrDefault(nameof(ServerConfiguration.UseGeoIP), false);
|
||||||
var countryFile = _mareConfiguration.GetValueOrDefault(nameof(ServerConfiguration.GeoIPDbCountryFile), string.Empty);
|
var cityFile = _mareConfiguration.GetValueOrDefault(nameof(ServerConfiguration.GeoIPDbCityFile), string.Empty);
|
||||||
var lastWriteTime = new FileInfo(countryFile).LastWriteTimeUtc;
|
var lastWriteTime = new FileInfo(cityFile).LastWriteTimeUtc;
|
||||||
if (useGeoIP && (!string.Equals(countryFile, _countryFile, StringComparison.OrdinalIgnoreCase) || lastWriteTime != _dbLastWriteTime))
|
if (useGeoIP && (!string.Equals(cityFile, _cityFile, StringComparison.OrdinalIgnoreCase) || lastWriteTime != _dbLastWriteTime))
|
||||||
{
|
{
|
||||||
_countryFile = countryFile;
|
_cityFile = cityFile;
|
||||||
if (!File.Exists(_countryFile)) throw new FileNotFoundException($"Could not open GeoIP Country Database, path does not exist: {_countryFile}");
|
if (!File.Exists(_cityFile)) throw new FileNotFoundException($"Could not open GeoIP City Database, path does not exist: {_cityFile}");
|
||||||
_dbReader?.Dispose();
|
_dbReader?.Dispose();
|
||||||
_dbReader = null;
|
_dbReader = null;
|
||||||
_dbReader = new DatabaseReader(_countryFile);
|
_dbReader = new DatabaseReader(_cityFile);
|
||||||
_dbLastWriteTime = lastWriteTime;
|
_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)
|
if (_useGeoIP != useGeoIP)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public class ServerConfiguration : MareConfigurationAuthBase
|
|||||||
|
|
||||||
[RemoteConfiguration]
|
[RemoteConfiguration]
|
||||||
public int PurgeUnusedAccountsPeriodInDays { get; set; } = 14;
|
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;
|
public int RedisPool { get; set; } = 50;
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ public class ServerConfiguration : MareConfigurationAuthBase
|
|||||||
sb.AppendLine($"{nameof(MaxGroupUserCount)} => {MaxGroupUserCount}");
|
sb.AppendLine($"{nameof(MaxGroupUserCount)} => {MaxGroupUserCount}");
|
||||||
sb.AppendLine($"{nameof(PurgeUnusedAccounts)} => {PurgeUnusedAccounts}");
|
sb.AppendLine($"{nameof(PurgeUnusedAccounts)} => {PurgeUnusedAccounts}");
|
||||||
sb.AppendLine($"{nameof(PurgeUnusedAccountsPeriodInDays)} => {PurgeUnusedAccountsPeriodInDays}");
|
sb.AppendLine($"{nameof(PurgeUnusedAccountsPeriodInDays)} => {PurgeUnusedAccountsPeriodInDays}");
|
||||||
sb.AppendLine($"{nameof(GeoIPDbCountryFile)} => {GeoIPDbCountryFile}");
|
sb.AppendLine($"{nameof(GeoIPDbCityFile)} => {GeoIPDbCityFile}");
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user