Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ban post #836

Merged
merged 7 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions Content.Server/Administration/Managers/BanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Content.Server.Chat.Managers;
using Content.Server.Database;
using Content.Server.GameTicking;
using Content.Server.SS220.Discord;
using Content.Shared.CCVar;
using Content.Shared.Database;
using Content.Shared.Players;
Expand All @@ -32,6 +33,7 @@ public sealed class BanManager : IBanManager, IPostInjectInit
[Dependency] private readonly IChatManager _chat = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly DiscordBanPostManager _discordBanPostManager = default!;

private ISawmill _sawmill = default!;

Expand Down Expand Up @@ -59,7 +61,7 @@ private async void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs
SendRoleBans(e.Session);
}

private async Task<bool> AddRoleBan(ServerRoleBanDef banDef)
private async Task<ServerRoleBanDef> AddRoleBan(ServerRoleBanDef banDef)
{
banDef = await _db.AddServerRoleBanAsync(banDef);

Expand All @@ -68,7 +70,7 @@ private async Task<bool> AddRoleBan(ServerRoleBanDef banDef)
_cachedRoleBans.GetOrNew(banDef.UserId.Value).Add(banDef);
}

return true;
return banDef;
}

public HashSet<string>? GetRoleBans(NetUserId playerUserId)
Expand Down Expand Up @@ -140,7 +142,7 @@ public async void CreateServerBan(NetUserId? target, string? targetUsername, Net
statedRound,
null);

await _db.AddServerBanAsync(banDef);
var banId = await _db.AddServerBanAsync(banDef);
var adminName = banningAdmin == null
? Loc.GetString("system-user")
: (await _db.GetPlayerRecordByUserId(banningAdmin.Value))?.LastSeenUserName ?? Loc.GetString("system-user");
Expand Down Expand Up @@ -168,6 +170,10 @@ public async void CreateServerBan(NetUserId? target, string? targetUsername, Net
_sawmill.Info(logMessage);
_chat.SendAdminAlert(logMessage);

// SS220 user ban info post start
await _discordBanPostManager.PostUserBanInfo(banId);
// SS220 user ban info post end

// If we're not banning a player we don't care about disconnecting people
if (target == null)
return;
Expand Down Expand Up @@ -217,12 +223,21 @@ public async void CreateRoleBan(NetUserId? target, string? targetUsername, NetUs
null,
role);

if (!await AddRoleBan(banDef))
banDef = await AddRoleBan(banDef);

if (banDef is null)
{
_chat.SendAdminAlert(Loc.GetString("cmd-roleban-existing", ("target", targetUsername ?? "null"), ("role", role)));
return;
}

// SS220 user ban info post start
if (banDef.Id.HasValue)
{
await _discordBanPostManager.PostUserJobBanInfo(banDef.Id.Value, targetUsername);
}
// SS220 user ban info post end

var length = expires == null ? Loc.GetString("cmd-roleban-inf") : Loc.GetString("cmd-roleban-until", ("expires", expires));
_chat.SendAdminAlert(Loc.GetString("cmd-roleban-success", ("target", targetUsername ?? "null"), ("role", role), ("reason", reason), ("length", length)));

Expand Down
2 changes: 1 addition & 1 deletion Content.Server/Database/ServerDbBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public abstract Task<List<ServerBanDef>> GetServerBansAsync(
ImmutableArray<byte>? hwId,
bool includeUnbanned);

public abstract Task AddServerBanAsync(ServerBanDef serverBan);
public abstract Task<int> AddServerBanAsync(ServerBanDef serverBan);
public abstract Task AddServerUnbanAsync(ServerUnbanDef serverUnban);

public async Task EditServerBan(int id, string reason, NoteSeverity severity, DateTimeOffset? expiration, Guid editedBy, DateTimeOffset editedAt)
Expand Down
4 changes: 2 additions & 2 deletions Content.Server/Database/ServerDbManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Task<List<ServerBanDef>> GetServerBansAsync(
ImmutableArray<byte>? hwId,
bool includeUnbanned=true);

Task AddServerBanAsync(ServerBanDef serverBan);
Task<int> AddServerBanAsync(ServerBanDef serverBan);
Task AddServerUnbanAsync(ServerUnbanDef serverBan);

public Task EditServerBan(
Expand Down Expand Up @@ -418,7 +418,7 @@ public Task<List<ServerBanDef>> GetServerBansAsync(
return RunDbCommand(() => _db.GetServerBansAsync(address, userId, hwId, includeUnbanned));
}

public Task AddServerBanAsync(ServerBanDef serverBan)
public Task<int> AddServerBanAsync(ServerBanDef serverBan)
{
DbWriteOpsMetric.Inc();
return RunDbCommand(() => _db.AddServerBanAsync(serverBan));
Expand Down
6 changes: 4 additions & 2 deletions Content.Server/Database/ServerDbPostgres.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ private static IQueryable<ServerBan> MakeBanLookupQuery(
unban.UnbanTime);
}

public override async Task AddServerBanAsync(ServerBanDef serverBan)
public override async Task<int> AddServerBanAsync(ServerBanDef serverBan)
{
await using var db = await GetDbImpl();

db.PgDbContext.Ban.Add(new ServerBan
var ban = db.PgDbContext.Ban.Add(new ServerBan
{
Address = serverBan.Address.ToNpgsqlInet(),
HWId = serverBan.HWId?.ToArray(),
Expand All @@ -251,6 +251,8 @@ public override async Task AddServerBanAsync(ServerBanDef serverBan)
});

await db.PgDbContext.SaveChangesAsync();

return ban.Entity.Id;
}

public override async Task AddServerUnbanAsync(ServerUnbanDef serverUnban)
Expand Down
6 changes: 4 additions & 2 deletions Content.Server/Database/ServerDbSqlite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ private static bool BanMatches(ServerBan ban,
return hwId is { Length: > 0 } hwIdVar && hwIdVar.AsSpan().SequenceEqual(ban.HWId);
}

public override async Task AddServerBanAsync(ServerBanDef serverBan)
public override async Task<int> AddServerBanAsync(ServerBanDef serverBan)
{
await using var db = await GetDbImpl();

db.SqliteDbContext.Ban.Add(new ServerBan
var ban = db.SqliteDbContext.Ban.Add(new ServerBan
{
Address = serverBan.Address.ToNpgsqlInet(),
Reason = serverBan.Reason,
Expand All @@ -174,6 +174,8 @@ public override async Task AddServerBanAsync(ServerBanDef serverBan)
});

await db.SqliteDbContext.SaveChangesAsync();

return ban.Entity.Id;
}

public override async Task AddServerUnbanAsync(ServerUnbanDef serverUnban)
Expand Down
1 change: 1 addition & 0 deletions Content.Server/Entry/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public override void Init()
IoCManager.Resolve<ServerInfoManager>().Initialize();
IoCManager.Resolve<Primelist>().Initialize();
IoCManager.Resolve<DiscordPlayerManager>().Initialize(); // SS220 discord player manager
IoCManager.Resolve<DiscordBanPostManager>().Initialize(); // SS220 discord ban post manager
IoCManager.Resolve<ServerControlController>().Initialize();

_voteManager.Initialize();
Expand Down
3 changes: 2 additions & 1 deletion Content.Server/IoC/ServerContentIoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public static void Register()
IoCManager.Register<ServerInfoManager>();
IoCManager.Register<PoissonDiskSampler>();
IoCManager.Register<Primelist>();
IoCManager.Register<DiscordPlayerManager>();
IoCManager.Register<DiscordPlayerManager>(); // SS220 discord player manager
IoCManager.Register<DiscordBanPostManager>(); // SS220 discord ban post manager
IoCManager.Register<ServerControlController>();
IoCManager.Register<DiscordWebhook>();
IoCManager.Register<ServerDbEntryManager>();
Expand Down
155 changes: 155 additions & 0 deletions Content.Server/SS220/Discord/DiscordBanPostManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt

using Content.Shared.Corvax.CCCVars;
using Robust.Shared.Configuration;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using System.Text.Json;

namespace Content.Server.SS220.Discord;

public sealed class DiscordBanPostManager
{
[Dependency] private readonly IConfigurationManager _cfg = default!;

private ISawmill _sawmill = default!;

private readonly HttpClient _httpClient = new();
private string _apiUrl = string.Empty;

public void Initialize()
{
_sawmill = Logger.GetSawmill("DiscordPlayerManager");

_cfg.OnValueChanged(CCCVars.DiscordAuthApiUrl, v => _apiUrl = v, true);
_cfg.OnValueChanged(CCCVars.DiscordAuthApiKey, v =>
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", v);
},
true);
}

public async Task PostUserBanInfo(int banId)
{
if (string.IsNullOrEmpty(_apiUrl))
{
return;
}

try
{
var url = $"{_apiUrl}/userBan/{banId}";

var response = await _httpClient.PostAsync(url, content: null);

if (response.StatusCode != HttpStatusCode.OK)
{
var errorText = await response.Content.ReadAsStringAsync();

_sawmill.Error(
"Failed to post user ban: [{StatusCode}] {Response}",
response.StatusCode,
errorText);
}
}
catch (Exception exc)
{
_sawmill.Error($"Error while posting user ban. {exc.Message}");
}
}

private readonly Dictionary<string, List<int>> _userBanCache = new();

private readonly Dictionary<string, Timer> _userJobBanPostTimers = new();

public async Task PostUserJobBanInfo(int banId, string? targetUserName)
{
try
{
if (!string.IsNullOrWhiteSpace(targetUserName))
{
AddUserJobBanToCache(banId, targetUserName);
AddUserJobBanTimer(targetUserName);
}
}
catch (Exception exc)
{
_sawmill.Error($"Error while cached user role ban. {exc.Message}");
}
}

private void AddUserJobBanTimer(string targetUserName)
{
if (!_userJobBanPostTimers.TryGetValue(targetUserName, out var timer))
{
timer = new()
{
AutoReset = false
};

timer.Elapsed += async (sender, e) => await JobBanProccessComplete(targetUserName);

_userJobBanPostTimers[targetUserName] = timer;
}

timer.Stop();

timer.Interval = TimeSpan.FromMinutes(1).TotalMilliseconds;
timer.Start();
}

private async Task JobBanProccessComplete(string userName)
{
if (string.IsNullOrEmpty(_apiUrl))
{
return;
}

_userBanCache.Remove(userName, out var bans);

if (bans is null)
{
return;
}

try
{
var url = $"{_apiUrl}/userBan/roleBan";

var response = await _httpClient.PostAsync(url,
new StringContent(
JsonSerializer.Serialize(bans),
Encoding.UTF8,
"application/json"));

if (response.StatusCode != HttpStatusCode.OK)
{
var errorText = await response.Content.ReadAsStringAsync();

_sawmill.Error(
"Failed to post user role ban: [{StatusCode}] {Response}",
response.StatusCode,
errorText);
}
}
catch (Exception exc)
{
_sawmill.Error($"Error while posting user role ban. {exc.Message}");
}
}

private void AddUserJobBanToCache(int banId, string targetUsername)
{
if (!_userBanCache.TryGetValue(targetUsername, out var cache))
{
cache = [];
_userBanCache[targetUsername] = cache;
}

cache.Add(banId);
}
}
Loading