Add IP ban exemption flag (#15815)

This commit is contained in:
Chief-Engineer
2023-04-27 13:59:18 -05:00
committed by GitHub
parent 8704707dbd
commit 5eba1d230a
3 changed files with 16 additions and 7 deletions

View File

@@ -119,7 +119,7 @@ namespace Content.Server.Database
query = query == null ? newQ : query.Union(newQ);
}
if (address != null)
if (address != null && !exemptFlags.GetValueOrDefault(ServerBanExemptFlags.None).HasFlag(ServerBanExemptFlags.IP))
{
var newQ = db.PgDbContext.Ban
.Include(p => p.Unban)

View File

@@ -74,7 +74,7 @@ namespace Content.Server.Database
// So just pull down the whole list into memory.
var bans = await GetAllBans(db.SqliteDbContext, includeUnbanned: false, exempt);
return bans.FirstOrDefault(b => BanMatches(b, address, userId, hwId)) is { } foundBan
return bans.FirstOrDefault(b => BanMatches(b, address, userId, hwId, exempt)) is { } foundBan
? ConvertBan(foundBan)
: null;
}
@@ -92,7 +92,7 @@ namespace Content.Server.Database
var queryBans = await GetAllBans(db.SqliteDbContext, includeUnbanned, exempt);
return queryBans
.Where(b => BanMatches(b, address, userId, hwId))
.Where(b => BanMatches(b, address, userId, hwId, exempt))
.Select(ConvertBan)
.ToList()!;
}
@@ -117,13 +117,14 @@ namespace Content.Server.Database
return await query.ToListAsync();
}
private static bool BanMatches(
ServerBan ban,
private static bool BanMatches(ServerBan ban,
IPAddress? address,
NetUserId? userId,
ImmutableArray<byte>? hwId)
ImmutableArray<byte>? hwId,
ServerBanExemptFlags? exemptFlags)
{
if (address != null && ban.Address is not null && IPAddressExt.IsInSubnet(address, ban.Address.Value))
if (!exemptFlags.GetValueOrDefault(ServerBanExemptFlags.None).HasFlag(ServerBanExemptFlags.IP)
&& address != null && ban.Address is not null && IPAddressExt.IsInSubnet(address, ban.Address.Value))
{
return true;
}