1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-10 15:20:48 -05:00

improve ban handling edge cases

This commit is contained in:
RaidMax
2022-03-25 11:28:15 -05:00
parent 58c9092888
commit 1d9c75bcd6
4 changed files with 95 additions and 14 deletions

View File

@ -153,6 +153,7 @@ namespace SharedLibraryCore.Configuration
public bool EnablePrivilegedUserPrivacy { get; set; }
[ConfigurationIgnore] public bool EnableImplicitAccountLinking { get; set; } = false;
[ConfigurationIgnore] public TimeSpan RecentAliasIpLinkTimeLimit { get; set; } = TimeSpan.FromDays(7);
[ConfigurationIgnore] public TimeSpan MaxClientHistoryTime { get; set; } = TimeSpan.FromHours(12);
@ -230,4 +231,4 @@ namespace SharedLibraryCore.Configuration
return "ApplicationConfiguration";
}
}
}
}

View File

@ -693,13 +693,14 @@ namespace SharedLibraryCore.Database.Models
if (Level != Permission.Banned)
{
Utilities.DefaultLogger.LogInformation(
"Client {client} has a ban penalty, but they're using a new GUID, we we're updating their level and kicking them",
"Client {Client} has a ban penalty, but they're using a new GUID, we we're updating their level and kicking them",
ToString());
await SetLevel(Permission.Banned, autoKickClient).WaitAsync(Utilities.DefaultCommandTimeout,
CurrentServer.Manager.CancellationToken);
}
Utilities.DefaultLogger.LogInformation("Kicking {client} because they are banned", ToString());
Utilities.DefaultLogger.LogInformation("Kicking {Client} because they are banned", ToString());
Kick(loc["WEBFRONT_PENALTY_LIST_BANNED_REASON"], autoKickClient, banPenalty);
return false;
}
@ -732,6 +733,34 @@ namespace SharedLibraryCore.Database.Models
ToString());
Unflag(Utilities.CurrentLocalization.LocalizationIndex["SERVER_AUTOFLAG_UNFLAG"], autoKickClient);
}
if (Level != Permission.Banned)
{
return true;
}
// we want to see if they've recently used a banned IP
var recentIPPenalties= await CurrentServer.Manager.GetPenaltyService().ActivePenaltiesByRecentIdentifiers(AliasLinkId);
var recentBanPenalty =
recentIPPenalties.FirstOrDefault(penalty => penalty.Type == EFPenalty.PenaltyType.Ban);
if (recentBanPenalty is null || !IPAddress.HasValue)
{
Utilities.DefaultLogger.LogInformation(
"Setting {Client} level to user because they are banned but no direct penalties or recent penalty identifiers exist for them",
ToString());
await SetLevel(Permission.User, autoKickClient).WaitAsync(Utilities.DefaultCommandTimeout,
CurrentServer.Manager.CancellationToken);
return true;
}
Utilities.DefaultLogger.LogInformation("Updating penalty for {Client} because they recently used a banned IP", this);
await CurrentServer.Manager.GetPenaltyService()
.CreatePenaltyIdentifier(recentBanPenalty.PenaltyId, NetworkId, IPAddress.Value);
Utilities.DefaultLogger.LogInformation("Kicking {Client} because they are banned", ToString());
Kick(loc["WEBFRONT_PENALTY_LIST_BANNED_REASON"], autoKickClient, recentBanPenalty);
}
return true;

View File

@ -59,6 +59,20 @@ namespace SharedLibraryCore.Services
return newEntity;
}
public async Task CreatePenaltyIdentifier(int penaltyId, long networkId, int ipv4Address)
{
await using var context = _contextFactory.CreateContext();
var penaltyIdentifiers = new EFPenaltyIdentifier
{
PenaltyId = penaltyId,
NetworkId = networkId,
IPv4Address = ipv4Address
};
context.PenaltyIdentifiers.Add(penaltyIdentifiers);
await context.SaveChangesAsync();
}
public Task<EFPenalty> Delete(EFPenalty entity)
{
throw new NotImplementedException();
@ -172,12 +186,34 @@ namespace SharedLibraryCore.Services
public async Task<List<EFPenalty>> GetActivePenaltiesByIdentifier(int? ip, long networkId)
{
await using var context = _contextFactory.CreateContext(false);
var activePenaltiesIds = context.PenaltyIdentifiers.Where(identifier =>
identifier.IPv4Address != null && identifier.IPv4Address == ip || identifier.NetworkId == networkId)
.Where(FilterById);
return await activePenaltiesIds.Select(ids => ids.Penalty).ToListAsync();
}
public async Task<List<EFPenalty>> ActivePenaltiesByRecentIdentifiers(int linkId)
{
await using var context = _contextFactory.CreateContext(false);
var recentlyUsedIps = await context.Aliases.Where(alias => alias.LinkId == linkId)
.Where(alias => alias.IPAddress != null)
.Where(alias => alias.DateAdded >= DateTime.UtcNow - _appConfig.RecentAliasIpLinkTimeLimit)
.Select(alias => alias.IPAddress).ToListAsync();
if (!recentlyUsedIps.Any())
{
return new List<EFPenalty>();
}
var activePenaltiesIds = context.PenaltyIdentifiers
.Where(identifier => recentlyUsedIps.Contains(identifier.IPv4Address))
.Where(FilterById);
return await activePenaltiesIds.Select(ids => ids.Penalty).ToListAsync();
}
public virtual async Task RemoveActivePenalties(int aliasLinkId, long networkId, int? ipAddress = null)
{
await using var context = _contextFactory.CreateContext();