Skip to content

Commit

Permalink
Modern HWID integration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PJB3005 committed Nov 24, 2024
1 parent 0b76ce5 commit 0771011
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion SS14
Submodule SS14 updated 6251 files
13 changes: 7 additions & 6 deletions SS14.Admin/Helpers/BanHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Net.Sockets;
using Content.Server.Database;
using Content.Shared.Database;
using Microsoft.EntityFrameworkCore;
using NpgsqlTypes;

Expand Down Expand Up @@ -74,9 +75,9 @@ public static bool IsBanActive<TUnban>(IBanCommon<TUnban> 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<TBan, TUnban> where TBan: IBanCommon<TUnban> where TUnban : IUnbanCommon
Expand All @@ -87,7 +88,7 @@ public sealed class BanJoin<TBan, TUnban> where TBan: IBanCommon<TUnban> 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();

Expand Down Expand Up @@ -149,10 +150,10 @@ public sealed class BanJoin<TBan, TUnban> where TBan: IBanCommon<TUnban> 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)
Expand Down
13 changes: 13 additions & 0 deletions SS14.Admin/Helpers/HwidHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 7 additions & 9 deletions SS14.Admin/Helpers/SearchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,9 +29,8 @@ public static IQueryable<ConnectionLog> SearchConnectionLog(IQueryable<Connectio
if (user.IsInRole(Constants.PIIRole) && IPAddress.TryParse(search, out var ip))
CombineSearch(ref expr, u => 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);
}
Expand All @@ -55,9 +55,8 @@ public static IQueryable<ConnectionLog> SearchConnectionLog(IQueryable<Connectio
if (user.IsInRole(Constants.PIIRole) && IPAddress.TryParse(search, out var ip))
CombineSearch(ref expr, u => 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;
}
Expand Down Expand Up @@ -112,9 +111,8 @@ public static IQueryable<Player> SearchPlayers(IQueryable<Player> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion SS14.Admin/Pages/Bans/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task<IActionResult> 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(
Expand Down
2 changes: 1 addition & 1 deletion SS14.Admin/Pages/Bans/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion SS14.Admin/Pages/RoleBans/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IActionResult> OnPostCreateAsync()
Expand Down
2 changes: 1 addition & 1 deletion SS14.Admin/Pages/RoleBans/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion SS14.Admin/Pages/Tables/ConnectionsTable.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@using Content.Server.Database
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using SS14.Admin.Helpers
@model SS14.Admin.Pages.Tables.ConnectionsTableModel

<partial name="Tables/Pagination" for="Pagination"/>
Expand All @@ -15,6 +17,9 @@
<th>
Server
</th>
<th>
Trust Score
</th>
<partial name="Tables/SortTabHeader" model="@(new SortTabHeaderModel(Model.SortState, "hits", "Bans Hit"))"/>
</tr>
</thead>
Expand All @@ -40,7 +45,7 @@
@if (User.IsInRole(Constants.PIIRole))
{
<td class="font-monospace constrained-150">@log.Address</td>
<td class="font-monospace constrained-150">@(log.HWId is { } hwid ? Convert.ToBase64String(hwid) : null)</td>
<td class="font-monospace constrained-150">@(log.HWId?.ToImmutable())</td>
}
else
{
Expand Down Expand Up @@ -70,6 +75,7 @@
<td>
@log.Server.Name
</td>
<td>@log.Trust</td>
<td>
@if (banHits != 0)
{
Expand Down

0 comments on commit 0771011

Please sign in to comment.