From 077101121af640ab92e7cdd6b86e1cfef8e22149 Mon Sep 17 00:00:00 2001 From: PJB3005 Date: Sun, 24 Nov 2024 23:55:16 +0100 Subject: [PATCH] Modern HWID integration This upgrades to the latest DB models and properly displays/handles modern HWIDs in all the UI. Trust score is also displayed in the connection log tables. --- SS14 | 2 +- SS14.Admin/Helpers/BanHelper.cs | 13 +++++++------ SS14.Admin/Helpers/HwidHelper.cs | 13 +++++++++++++ SS14.Admin/Helpers/SearchHelper.cs | 16 +++++++--------- SS14.Admin/Pages/Bans/Create.cshtml.cs | 2 +- SS14.Admin/Pages/Bans/Index.cshtml.cs | 2 +- SS14.Admin/Pages/RoleBans/Create.cshtml.cs | 2 +- SS14.Admin/Pages/RoleBans/Index.cshtml.cs | 2 +- SS14.Admin/Pages/Tables/ConnectionsTable.cshtml | 8 +++++++- 9 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 SS14.Admin/Helpers/HwidHelper.cs diff --git a/SS14 b/SS14 index 0c34eb6..b4ec946 160000 --- a/SS14 +++ b/SS14 @@ -1 +1 @@ -Subproject commit 0c34eb6138b9f7d1698b546d1591096fc517d989 +Subproject commit b4ec946bd9f5af7bbc27dd676f294c0c5f4c6847 diff --git a/SS14.Admin/Helpers/BanHelper.cs b/SS14.Admin/Helpers/BanHelper.cs index c8f57c9..6e8dbb1 100644 --- a/SS14.Admin/Helpers/BanHelper.cs +++ b/SS14.Admin/Helpers/BanHelper.cs @@ -3,6 +3,7 @@ using System.Net; using System.Net.Sockets; using Content.Server.Database; +using Content.Shared.Database; using Microsoft.EntityFrameworkCore; using NpgsqlTypes; @@ -74,9 +75,9 @@ public static bool IsBanActive(IBanCommon b) where TUnban : IUnb [Pure] [return: NotNullIfNotNull("hwid")] - public static string? FormatHwid(byte[]? hwid) + public static string? FormatHwid(ImmutableTypedHwid? hwid) { - return hwid is { } h ? Convert.ToBase64String(h) : null; + return hwid?.ToString(); } public sealed class BanJoin where TBan: IBanCommon where TUnban : IUnbanCommon @@ -87,7 +88,7 @@ public sealed class BanJoin where TBan: IBanCommon where T public Player? UnbanAdmin { get; set; } } - public async Task<(IPAddress address, byte[]? hwid)?> GetLastPlayerInfo(string nameOrUid) + public async Task<(IPAddress address, ImmutableTypedHwid? hwid)?> GetLastPlayerInfo(string nameOrUid) { nameOrUid = nameOrUid.Trim(); @@ -149,10 +150,10 @@ public sealed class BanJoin where TBan: IBanCommon where T if (!string.IsNullOrWhiteSpace(hwid)) { hwid = hwid.Trim(); - ban.HWId = new byte[Constants.HwidLength]; - - if (!Convert.TryFromBase64String(hwid, ban.HWId, out _)) + if (!ImmutableTypedHwid.TryParse(hwid, out var parsedHwid)) return "Invalid HWID"; + + ban.HWId = parsedHwid; } if (lengthMinutes != 0) diff --git a/SS14.Admin/Helpers/HwidHelper.cs b/SS14.Admin/Helpers/HwidHelper.cs new file mode 100644 index 0000000..ce0d426 --- /dev/null +++ b/SS14.Admin/Helpers/HwidHelper.cs @@ -0,0 +1,13 @@ +using Content.Server.Database; +using Content.Shared.Database; + +namespace SS14.Admin.Helpers; + +public static class HwidHelper +{ + public static ImmutableTypedHwid ToImmutable(this TypedHwid hwid) + { + // Easiest function of my life. + return hwid; + } +} diff --git a/SS14.Admin/Helpers/SearchHelper.cs b/SS14.Admin/Helpers/SearchHelper.cs index 1030eb1..18dad02 100644 --- a/SS14.Admin/Helpers/SearchHelper.cs +++ b/SS14.Admin/Helpers/SearchHelper.cs @@ -2,6 +2,7 @@ using System.Net; using System.Security.Claims; using Content.Server.Database; +using Content.Shared.Database; using Microsoft.EntityFrameworkCore; using WhitelistJoin = SS14.Admin.Helpers.WhitelistHelper.WhitelistJoin; @@ -28,9 +29,8 @@ public static IQueryable SearchConnectionLog(IQueryable u.Address.Equals(ip)); - var hwid = new byte[Constants.HwidLength]; - if (user.IsInRole(Constants.PIIRole) && Convert.TryFromBase64String(search, hwid, out var len) && len == Constants.HwidLength) - CombineSearch(ref expr, u => u.HWId == hwid); + if (user.IsInRole(Constants.PIIRole) && ImmutableTypedHwid.TryParse(search, out var hwid)) + CombineSearch(ref expr, u => u.HWId!.Type == hwid.Type && u.HWId.Hwid == hwid.Hwid.ToArray()); return query.Where(expr); } @@ -55,9 +55,8 @@ public static IQueryable SearchConnectionLog(IQueryable EF.Functions.ContainsOrEqual(u.Ban.Address!.Value, ip)); - var hwid = new byte[Constants.HwidLength]; - if (user.IsInRole(Constants.PIIRole) && Convert.TryFromBase64String(search, hwid, out var len) && len == Constants.HwidLength) - CombineSearch(ref expr, u => u.Ban.HWId == hwid); + if (user.IsInRole(Constants.PIIRole) && ImmutableTypedHwid.TryParse(search, out var hwid)) + CombineSearch(ref expr, u => u.Ban.HWId!.Type == hwid.Type && u.Ban.HWId.Hwid == hwid.Hwid.ToArray()); return expr; } @@ -112,9 +111,8 @@ public static IQueryable SearchPlayers(IQueryable query, string? if (user.IsInRole(Constants.PIIRole) && IPAddress.TryParse(search, out var ip)) CombineSearch(ref expr, u => u.LastSeenAddress.Equals(ip)); - var hwid = new byte[Constants.HwidLength]; - if (user.IsInRole(Constants.PIIRole) && Convert.TryFromBase64String(search, hwid, out var len) && len == Constants.HwidLength) - CombineSearch(ref expr, u => u.LastSeenHWId == hwid); + if (user.IsInRole(Constants.PIIRole) && ImmutableTypedHwid.TryParse(search, out var hwid)) + CombineSearch(ref expr, u => u.LastSeenHWId!.Type == hwid.Type && u.LastSeenHWId.Hwid == hwid.Hwid.ToArray()); return query.Where(expr); } diff --git a/SS14.Admin/Pages/Bans/Create.cshtml.cs b/SS14.Admin/Pages/Bans/Create.cshtml.cs index 1aabcb4..2b9d259 100644 --- a/SS14.Admin/Pages/Bans/Create.cshtml.cs +++ b/SS14.Admin/Pages/Bans/Create.cshtml.cs @@ -81,7 +81,7 @@ public async Task OnPostCreateAsync() } ipAddr = Input.UseLatestIp ? lastInfo.Value.address.ToString() : Input.IP; - hwid = Input.UseLatestHwid ? (lastInfo.Value.hwid is { } h ? Convert.ToBase64String(h) : null) : Input.HWid; + hwid = Input.UseLatestHwid ? lastInfo.Value.hwid?.ToString() : Input.HWid; } var error = await _banHelper.FillBanCommon( diff --git a/SS14.Admin/Pages/Bans/Index.cshtml.cs b/SS14.Admin/Pages/Bans/Index.cshtml.cs index d976137..db9a0d6 100644 --- a/SS14.Admin/Pages/Bans/Index.cshtml.cs +++ b/SS14.Admin/Pages/Bans/Index.cshtml.cs @@ -128,7 +128,7 @@ await pagination.LoadLinqAsync(bans, e => e.Select(b => b.Player, b.Ban.PlayerUserId?.ToString(), b.Ban.Address?.FormatCidr(), - b.Ban.HWId is { } h ? Convert.ToBase64String(h) : null, + b.Ban.HWId?.ToImmutable().ToString(), b.Ban.Reason, b.Ban.ExpirationTime, unbanned, diff --git a/SS14.Admin/Pages/RoleBans/Create.cshtml.cs b/SS14.Admin/Pages/RoleBans/Create.cshtml.cs index 8ece536..f15201d 100644 --- a/SS14.Admin/Pages/RoleBans/Create.cshtml.cs +++ b/SS14.Admin/Pages/RoleBans/Create.cshtml.cs @@ -48,7 +48,7 @@ public async Task OnPostFillAsync() } Input.IP = lastInfo.Value.address.ToString(); - Input.HWid = lastInfo.Value.hwid is { } h ? Convert.ToBase64String(h) : null; + Input.HWid = lastInfo.Value.hwid?.ToString(); } public async Task OnPostCreateAsync() diff --git a/SS14.Admin/Pages/RoleBans/Index.cshtml.cs b/SS14.Admin/Pages/RoleBans/Index.cshtml.cs index 5919c35..b65e196 100644 --- a/SS14.Admin/Pages/RoleBans/Index.cshtml.cs +++ b/SS14.Admin/Pages/RoleBans/Index.cshtml.cs @@ -127,7 +127,7 @@ await pagination.LoadLinqAsync(bans, e => e.Select(b => b.Player, b.Ban.PlayerUserId?.ToString(), b.Ban.Address?.FormatCidr(), - b.Ban.HWId is { } h ? Convert.ToBase64String(h) : null, + b.Ban.HWId?.ToImmutable().ToString(), b.Ban.Reason, b.Ban.ExpirationTime, unbanned, diff --git a/SS14.Admin/Pages/Tables/ConnectionsTable.cshtml b/SS14.Admin/Pages/Tables/ConnectionsTable.cshtml index 012e20f..7d38afd 100644 --- a/SS14.Admin/Pages/Tables/ConnectionsTable.cshtml +++ b/SS14.Admin/Pages/Tables/ConnectionsTable.cshtml @@ -1,4 +1,6 @@ @using Content.Server.Database +@using Microsoft.AspNetCore.Mvc.TagHelpers +@using SS14.Admin.Helpers @model SS14.Admin.Pages.Tables.ConnectionsTableModel @@ -15,6 +17,9 @@ Server + + Trust Score + @@ -40,7 +45,7 @@ @if (User.IsInRole(Constants.PIIRole)) { @log.Address - @(log.HWId is { } hwid ? Convert.ToBase64String(hwid) : null) + @(log.HWId?.ToImmutable()) } else { @@ -70,6 +75,7 @@ @log.Server.Name + @log.Trust @if (banHits != 0) {