* Start work on PostgresNotificationManager Implement initial version of init and listening code * Finish implementing PostgresNotificationManager Implement ban insert trigger * Implement ignoring notifications if the ban was from the same server * Address reviews * Fixes and refactorings Fix typo in migration SQL Pull new code in BanManager out into its own partial file. Unify logic to kick somebody with that when a new ban is placed directly on the server. New bans are now checked against all parameters (IP, HWID) instead of just user ID. Extracted SQLite ban matching code into a new class so that it can mostly be re-used by the ban notification code. No copy-paste here. Database notifications are now not implicitly sent to the main thread, this means basic checks will happen in the thread pool beforehand. Bans without user ID are now sent to servers. Bans are rate limited to avoid undue work from mass ban imports, beyond the rate limit they are dropped. Improved error handling and logging for the whole system. Matching bans against connected players requires knowing their ban exemption flags. These are now cached when the player connects. ServerBanDef now has exemption flags, again to allow matching full ban details for ban notifications. Made database notifications a proper struct type to reduce copy pasting a tuple. Remove copy pasted connection string building code by just... passing the string into the constructor. Add lock around _notificationHandlers just in case. Fixed postgres connection wait not being called in a loop and therefore spamming LISTEN commands for every received notification. Added more error handling and logging to notification listener. Removed some copy pasting from SQLite database layer too while I was at it because god forbid we expect anybody else to do all the work in this project. Sorry Julian --------- Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
95 lines
3.3 KiB
C#
95 lines
3.3 KiB
C#
using System.Collections.Immutable;
|
|
using System.Net;
|
|
using Content.Shared.CCVar;
|
|
using Content.Shared.Database;
|
|
using Robust.Shared.Configuration;
|
|
using Robust.Shared.Network;
|
|
|
|
|
|
namespace Content.Server.Database
|
|
{
|
|
public sealed class ServerBanDef
|
|
{
|
|
public int? Id { get; }
|
|
public NetUserId? UserId { get; }
|
|
public (IPAddress address, int cidrMask)? Address { get; }
|
|
public ImmutableArray<byte>? HWId { get; }
|
|
|
|
public DateTimeOffset BanTime { get; }
|
|
public DateTimeOffset? ExpirationTime { get; }
|
|
public int? RoundId { get; }
|
|
public TimeSpan PlaytimeAtNote { get; }
|
|
public string Reason { get; }
|
|
public NoteSeverity Severity { get; set; }
|
|
public NetUserId? BanningAdmin { get; }
|
|
public ServerUnbanDef? Unban { get; }
|
|
public ServerBanExemptFlags ExemptFlags { get; }
|
|
|
|
public ServerBanDef(int? id,
|
|
NetUserId? userId,
|
|
(IPAddress, int)? address,
|
|
ImmutableArray<byte>? hwId,
|
|
DateTimeOffset banTime,
|
|
DateTimeOffset? expirationTime,
|
|
int? roundId,
|
|
TimeSpan playtimeAtNote,
|
|
string reason,
|
|
NoteSeverity severity,
|
|
NetUserId? banningAdmin,
|
|
ServerUnbanDef? unban,
|
|
ServerBanExemptFlags exemptFlags = default)
|
|
{
|
|
if (userId == null && address == null && hwId == null)
|
|
{
|
|
throw new ArgumentException("Must have at least one of banned user, banned address or hardware ID");
|
|
}
|
|
|
|
if (address is {} addr && addr.Item1.IsIPv4MappedToIPv6)
|
|
{
|
|
// Fix IPv6-mapped IPv4 addresses
|
|
// So that IPv4 addresses are consistent between separate-socket and dual-stack socket modes.
|
|
address = (addr.Item1.MapToIPv4(), addr.Item2 - 96);
|
|
}
|
|
|
|
Id = id;
|
|
UserId = userId;
|
|
Address = address;
|
|
HWId = hwId;
|
|
BanTime = banTime;
|
|
ExpirationTime = expirationTime;
|
|
RoundId = roundId;
|
|
PlaytimeAtNote = playtimeAtNote;
|
|
Reason = reason;
|
|
Severity = severity;
|
|
BanningAdmin = banningAdmin;
|
|
Unban = unban;
|
|
ExemptFlags = exemptFlags;
|
|
}
|
|
|
|
public string FormatBanMessage(IConfigurationManager cfg, ILocalizationManager loc)
|
|
{
|
|
string expires;
|
|
if (ExpirationTime is { } expireTime)
|
|
{
|
|
var duration = expireTime - BanTime;
|
|
var utc = expireTime.ToUniversalTime();
|
|
expires = loc.GetString("ban-expires", ("duration", duration.TotalMinutes.ToString("N0")), ("time", utc.ToString("f")));
|
|
}
|
|
else
|
|
{
|
|
var appeal = cfg.GetCVar(CCVars.InfoLinksAppeal);
|
|
expires = !string.IsNullOrWhiteSpace(appeal)
|
|
? loc.GetString("ban-banned-permanent-appeal", ("link", appeal))
|
|
: loc.GetString("ban-banned-permanent");
|
|
}
|
|
|
|
return $"""
|
|
{loc.GetString("ban-banned-1")}
|
|
{loc.GetString("ban-banned-2", ("reason", Reason))}
|
|
{expires}
|
|
{loc.GetString("ban-banned-3")}
|
|
""";
|
|
}
|
|
}
|
|
}
|