add baninfo api

This commit is contained in:
RaidMax 2022-06-02 16:48:47 -05:00
parent 2f7eb07e39
commit 7c10e0e3de
8 changed files with 147 additions and 4 deletions

View File

@ -53,6 +53,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ScriptPlugins", "ScriptPlug
Plugins\ScriptPlugins\GameInterface.js = Plugins\ScriptPlugins\GameInterface.js
Plugins\ScriptPlugins\SubnetBan.js = Plugins\ScriptPlugins\SubnetBan.js
Plugins\ScriptPlugins\BanBroadcasting.js = Plugins\ScriptPlugins\BanBroadcasting.js
Plugins\ScriptPlugins\ParserH1MOD.js = Plugins\ScriptPlugins\ParserH1MOD.js
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomessageFeed", "Plugins\AutomessageFeed\AutomessageFeed.csproj", "{F5815359-CFC7-44B4-9A3B-C04BACAD5836}"

View File

@ -13,7 +13,7 @@ namespace SharedLibraryCore.Dtos
public int Offset { get; set; }
/// <summary>
/// how many itesm to take
/// how many items to take
/// </summary>
public int Count { get; set; } = 100;
@ -35,4 +35,4 @@ namespace SharedLibraryCore.Dtos
Ascending,
Descending
}
}
}

View File

@ -0,0 +1,25 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using WebfrontCore.QueryHelpers.Models;
namespace WebfrontCore.Controllers.API;
[Route("api/[controller]")]
public class PenaltyController : BaseController
{
private readonly IResourceQueryHelper<BanInfoRequest, BanInfo> _banInfoQueryHelper;
public PenaltyController(IManager manager, IResourceQueryHelper<BanInfoRequest, BanInfo> banInfoQueryHelper) : base(manager)
{
_banInfoQueryHelper = banInfoQueryHelper;
}
[HttpGet("BanInfo/{clientName}")]
public async Task<IActionResult> BanInfo(BanInfoRequest request)
{
var result = await _banInfoQueryHelper.QueryResource(request);
return Json(result);
}
}

View File

@ -0,0 +1,96 @@
using System.Linq;
using System.Threading.Tasks;
using Data.Abstractions;
using Data.Models.Client;
using Microsoft.EntityFrameworkCore;
using SharedLibraryCore;
using SharedLibraryCore.Helpers;
using SharedLibraryCore.Interfaces;
using WebfrontCore.QueryHelpers.Models;
namespace WebfrontCore.QueryHelpers;
public class BanInfoResourceQueryHelper : IResourceQueryHelper<BanInfoRequest, BanInfo>
{
private readonly IDatabaseContextFactory _contextFactory;
public BanInfoResourceQueryHelper(IDatabaseContextFactory contextFactory)
{
_contextFactory = contextFactory;
}
public async Task<ResourceQueryHelperResult<BanInfo>> QueryResource(BanInfoRequest query)
{
if (query.Count > 30)
{
query.Count = 30;
}
await using var context = _contextFactory.CreateContext(false);
var matchingClients = await context.Clients.Where(client =>
EF.Functions.ILike(client.CurrentAlias.SearchableName ?? client.CurrentAlias.Name, $"%{query.ClientName.Trim()}%"))
.Where(client => client.Level == EFClient.Permission.Banned)
.OrderByDescending(client => client.LastConnection)
.Skip(query.Offset)
.Take(query.Count)
.Select(client => new
{
client.CurrentAlias.Name,
client.NetworkId,
client.AliasLinkId,
client.ClientId
}).ToListAsync();
var usedIps = await context.Aliases
.Where(alias => matchingClients.Select(client => client.AliasLinkId).Contains(alias.LinkId))
.Where(alias => alias.IPAddress != null)
.Select(alias => new { alias.IPAddress, alias.LinkId })
.ToListAsync();
var usedIpsGrouped = usedIps
.GroupBy(alias => alias.LinkId)
.ToDictionary(key => key.Key, value => value.Select(alias => alias.IPAddress).Distinct());
var searchingNetworkIds = matchingClients.Select(client => client.NetworkId);
var searchingIps = usedIpsGrouped.SelectMany(group => group.Value);
var matchedPenalties = await context.PenaltyIdentifiers.Where(identifier =>
searchingNetworkIds.Contains(identifier.NetworkId) ||
searchingIps.Contains(identifier.IPv4Address))
.Select(penalty => new
{
penalty.CreatedDateTime,
PunisherName = penalty.Penalty.Punisher.CurrentAlias.Name,
Offense = string.IsNullOrEmpty(penalty.Penalty.AutomatedOffense) ? penalty.Penalty.Offense : "Anticheat Detection",
LinkId = penalty.Penalty.Offender.AliasLinkId,
penalty.Penalty.PunisherId
})
.ToListAsync();
var groupedPenalties = matchedPenalties.GroupBy(penalty => penalty.LinkId)
.ToDictionary(key => key.Key, value => value.FirstOrDefault());
var results = matchingClients.Select(client =>
{
var matchedPenalty =
groupedPenalties.ContainsKey(client.AliasLinkId) ? groupedPenalties[client.AliasLinkId] : null;
return new BanInfo
{
DateTime = matchedPenalty?.CreatedDateTime,
OffenderName = client.Name.StripColors(),
OffenderId = client.ClientId,
PunisherName = matchedPenalty?.PunisherName.StripColors(),
PunisherId = matchedPenalty?.PunisherId,
Offense = matchedPenalty?.Offense
};
}).ToList();
return new ResourceQueryHelperResult<BanInfo>
{
RetrievedResultCount = results.Count,
TotalResultCount = results.Count,
Results = results
};
}
}

View File

@ -0,0 +1,12 @@
using System;
public class BanInfo
{
public string OffenderName { get; set; }
public int OffenderId { get; set; }
public string PunisherName { get; set; }
public int? PunisherId { get; set; }
public string Offense { get; set; }
public DateTime? DateTime { get; set; }
public long? TimeStamp => DateTime.HasValue ? new DateTimeOffset(DateTime.Value, TimeSpan.Zero).ToUnixTimeSeconds() : null;
}

View File

@ -0,0 +1,8 @@
using SharedLibraryCore.Dtos;
namespace WebfrontCore.QueryHelpers.Models;
public class BanInfoRequest : PaginationRequest
{
public string ClientName { get; set; }
}

View File

@ -24,11 +24,12 @@ using System.Reflection;
using System.Threading.Tasks;
using Data.Abstractions;
using Data.Helpers;
using IW4MAdmin.Plugins.Stats.Config;
using Stats.Client.Abstractions;
using Stats.Config;
using WebfrontCore.Controllers.API.Validation;
using WebfrontCore.Middleware;
using WebfrontCore.QueryHelpers;
using WebfrontCore.QueryHelpers.Models;
namespace WebfrontCore
{
@ -127,6 +128,7 @@ namespace WebfrontCore
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IMetaServiceV2>());
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<ApplicationConfiguration>());
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<ClientService>());
services.AddSingleton<IResourceQueryHelper<BanInfoRequest, BanInfo>, BanInfoResourceQueryHelper>();
services.AddSingleton(
Program.ApplicationServiceProvider.GetRequiredService<IServerDistributionCalculator>());
services.AddSingleton(Program.ApplicationServiceProvider

View File

@ -56,7 +56,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="ref" />
<Folder Include="wwwroot\lib\canvas.js\" />
</ItemGroup>