update interactions to allow building custom forms

This commit is contained in:
RaidMax 2022-10-12 21:06:18 -05:00
parent 53cbd11008
commit 24d91f228b
14 changed files with 191 additions and 118 deletions

View File

@ -452,6 +452,7 @@ namespace IW4MAdmin.Application
.AddSingleton<IAlertManager, AlertManager>()
.AddTransient<IScriptPluginTimerHelper, ScriptPluginTimerHelper>()
.AddSingleton<IInteractionRegistration, InteractionRegistration>()
.AddSingleton<IRemoteCommandService, RemoteCommandService>()
.AddSingleton(translationLookup)
.AddDatabaseContextOptions(appConfig);

View File

@ -89,8 +89,8 @@ public class InteractionRegistration : IInteractionRegistration
}))).Where(interaction => interaction is not null);
}
public async Task<string> ProcessInteraction(string interactionId, int? clientId = null,
Reference.Game? game = null, CancellationToken token = default)
public async Task<string> ProcessInteraction(string interactionId, int originId, int? targetId = null,
Reference.Game? game = null, IDictionary<string, string> meta = null, CancellationToken token = default)
{
if (!_interactions.ContainsKey(interactionId))
{
@ -99,11 +99,11 @@ public class InteractionRegistration : IInteractionRegistration
try
{
var interaction = await _interactions[interactionId](clientId, game, token);
var interaction = await _interactions[interactionId](originId, game, token);
if (interaction.Action is not null)
{
return await interaction.Action(clientId, game, token);
return await interaction.Action(originId, targetId, game, meta, token);
}
if (interaction.ScriptAction is not null)
@ -115,16 +115,15 @@ public class InteractionRegistration : IInteractionRegistration
continue;
}
return scriptPlugin.ExecuteAction<string>(interaction.ScriptAction, clientId, game, token);
return scriptPlugin.ExecuteAction<string>(interaction.ScriptAction, originId, targetId, game, token);
}
}
}
catch (Exception ex)
{
_logger.LogWarning(ex,
"Could not process interaction for interaction {InteractionName} and ClientId {ClientId}",
interactionId,
clientId);
"Could not process interaction for interaction {InteractionName} and OriginId {ClientId}",
interactionId, originId);
}
return null;

View File

@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SharedLibraryCore;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.Services;
namespace IW4MAdmin.Application.Misc;
public class RemoteCommandService : IRemoteCommandService
{
private readonly ApplicationConfiguration _appConfig;
private readonly ClientService _clientService;
public RemoteCommandService(ApplicationConfiguration appConfig, ClientService clientService)
{
_appConfig = appConfig;
_clientService = clientService;
}
public async Task<IEnumerable<CommandResponseInfo>> Execute(int originId, int? targetId, string command,
IEnumerable<string> arguments, Server server)
{
var client = await _clientService.Get(originId);
client.CurrentServer = server;
command += $" {(targetId.HasValue ? $"@{targetId} " : "")}{string.Join(" ", arguments ?? Enumerable.Empty<string>())}";
var remoteEvent = new GameEvent
{
Type = GameEvent.EventType.Command,
Data = command.StartsWith(_appConfig.CommandPrefix) ||
command.StartsWith(_appConfig.BroadcastCommandPrefix)
? command
: $"{_appConfig.CommandPrefix}{command}",
Origin = client,
Owner = server,
IsRemote = true
};
server.Manager.AddEvent(remoteEvent);
CommandResponseInfo[] response;
try
{
// wait for the event to process
var completedEvent =
await remoteEvent.WaitAsync(Utilities.DefaultCommandTimeout, server.Manager.CancellationToken);
if (completedEvent.FailReason == GameEvent.EventFailReason.Timeout)
{
response = new[]
{
new CommandResponseInfo()
{
ClientId = client.ClientId,
Response = Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_COMMAND_TIMEOUT"]
}
};
}
else
{
response = completedEvent.Output.Select(output => new CommandResponseInfo()
{
Response = output,
ClientId = client.ClientId
}).ToArray();
}
}
catch (System.OperationCanceledException)
{
response = new[]
{
new CommandResponseInfo
{
ClientId = client.ClientId,
Response = Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_RESTART_SUCCESS"]
}
};
}
return response;
}
}

View File

@ -51,7 +51,7 @@ namespace IW4MAdmin.Plugins.Login
manager.CommandInterceptors.Add(gameEvent =>
{
if (gameEvent.Type != GameEvent.EventType.Command)
if (gameEvent.Type != GameEvent.EventType.Command || gameEvent.Extra is null)
{
return true;
}

View File

@ -1,12 +1,12 @@
let vpnExceptionIds = [];
const commands = [{
name: "whitelistvpn",
description: "whitelists a player's client id from VPN detection",
alias: "wv",
permission: "SeniorAdmin",
name: 'whitelistvpn',
description: 'whitelists a player\'s client id from VPN detection',
alias: 'wv',
permission: 'SeniorAdmin',
targetRequired: true,
arguments: [{
name: "player",
name: 'player',
required: true
}],
execute: (gameEvent) => {
@ -19,12 +19,11 @@ const commands = [{
const plugin = {
author: 'RaidMax',
version: 1.4,
version: 1.5,
name: 'VPN Detection Plugin',
manager: null,
logger: null,
checkForVpn: function (origin) {
let exempt = false;
// prevent players that are exempt from being kicked
@ -80,24 +79,24 @@ const plugin = {
this.logger = manager.GetLogger(0);
this.configHandler = _configHandler;
this.configHandler.GetValue('vpnExceptionIds').forEach(element => vpnExceptionIds.push(element));
this.configHandler.GetValue('vpnExceptionIds').forEach(element => vpnExceptionIds.push(parseInt(element)));
this.logger.WriteInfo(`Loaded ${vpnExceptionIds.length} ids into whitelist`);
this.interactionRegistration = _serviceResolver.ResolveService('IInteractionRegistration');
this.interactionRegistration.RegisterScriptInteraction('WhitelistVPN', this.name, (clientId, game, token) => {
if (vpnExceptionIds.includes(clientId)) {
this.interactionRegistration.RegisterScriptInteraction('WhitelistVPN', this.name, (originId, targetId, game, token) => {
if (vpnExceptionIds.includes(targetId)) {
return;
}
const helpers = importNamespace('SharedLibraryCore.Helpers');
const interactionData = new helpers.InteractionData();
interactionData.EntityId = clientId;
interactionData.EntityId = targetId;
interactionData.Name = 'Whitelist VPN';
interactionData.DisplayMeta = 'oi-circle-check';
interactionData.ActionMeta.Add('InteractionId', 'command');
interactionData.ActionMeta.Add('Data', `whitelistvpn @${clientId}`);
interactionData.ActionMeta.Add('Data', `whitelistvpn`);
interactionData.ActionMeta.Add('ActionButtonLabel', 'Allow');
interactionData.ActionMeta.Add('Name', 'Allow VPN Connection');
interactionData.ActionMeta.Add('ShouldRefresh', true.toString());

View File

@ -3,8 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Data.Models.Client;
using SharedLibraryCore.Interfaces;
using InteractionCallback = System.Func<int?, Data.Models.Reference.Game?, System.Threading.CancellationToken, System.Threading.Tasks.Task<string>>;
using ScriptInteractionCallback = System.Func<int?, Data.Models.Reference.Game?, System.Threading.CancellationToken, System.Threading.Tasks.Task<string>>;
using InteractionCallback = System.Func<int, int?, Data.Models.Reference.Game?, System.Collections.Generic.IDictionary<string,string>, System.Threading.CancellationToken, System.Threading.Tasks.Task<string>>;
namespace SharedLibraryCore.Helpers;

View File

@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
using Data.Models.Client;
using InteractionCallback = System.Func<int?, Data.Models.Reference.Game?, System.Threading.CancellationToken, System.Threading.Tasks.Task<string>>;
using ScriptInteractionCallback = System.Func<int?, Data.Models.Reference.Game?, System.Threading.CancellationToken, System.Threading.Tasks.Task<string>>;
using InteractionCallback = System.Func<int, int?, Data.Models.Reference.Game?, System.Collections.Generic.IDictionary<string,string>, System.Threading.CancellationToken, System.Threading.Tasks.Task<string>>;
namespace SharedLibraryCore.Interfaces;

View File

@ -13,5 +13,5 @@ public interface IInteractionRegistration
void UnregisterInteraction(string interactionName);
Task<IEnumerable<IInteractionData>> GetInteractions(int? clientId = null,
Reference.Game? game = null, CancellationToken token = default);
Task<string> ProcessInteraction(string interactionId, int? clientId = null, Reference.Game? game = null, CancellationToken token = default);
Task<string> ProcessInteraction(string interactionId, int originId, int? targetId = null, Reference.Game? game = null, IDictionary<string, string> meta = null, CancellationToken token = default);
}

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using SharedLibraryCore.Dtos;
namespace SharedLibraryCore.Interfaces;
public interface IRemoteCommandService
{
Task<IEnumerable<CommandResponseInfo>> Execute(int originId, int? targetId, string command, IEnumerable<string> arguments, Server server);
}

View File

@ -4,7 +4,7 @@
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<PackageId>RaidMax.IW4MAdmin.SharedLibraryCore</PackageId>
<Version>2022.10.12.1</Version>
<Version>2022.10.12.2</Version>
<Authors>RaidMax</Authors>
<Company>Forever None</Company>
<Configurations>Debug;Release;Prerelease</Configurations>
@ -19,7 +19,7 @@
<IsPackable>true</IsPackable>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>Shared Library for IW4MAdmin</Description>
<PackageVersion>2022.10.12.1</PackageVersion>
<PackageVersion>2022.10.12.2</PackageVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

View File

@ -26,6 +26,7 @@ namespace WebfrontCore.Controllers
private readonly ApplicationConfiguration _appConfig;
private readonly IMetaServiceV2 _metaService;
private readonly IInteractionRegistration _interactionRegistration;
private readonly IRemoteCommandService _remoteCommandService;
private readonly string _banCommandName;
private readonly string _tempbanCommandName;
private readonly string _unbanCommandName;
@ -40,11 +41,12 @@ namespace WebfrontCore.Controllers
public ActionController(IManager manager, IEnumerable<IManagerCommand> registeredCommands,
ApplicationConfiguration appConfig, IMetaServiceV2 metaService,
IInteractionRegistration interactionRegistration) : base(manager)
IInteractionRegistration interactionRegistration, IRemoteCommandService remoteCommandService) : base(manager)
{
_appConfig = appConfig;
_metaService = metaService;
_interactionRegistration = interactionRegistration;
_remoteCommandService = remoteCommandService;
foreach (var cmd in registeredCommands)
{
@ -104,6 +106,20 @@ namespace WebfrontCore.Controllers
metaDict.TryGetValue(nameof(ActionInfo.ShouldRefresh), out var refresh);
metaDict.TryGetValue("Data", out var data);
metaDict.TryGetValue("InteractionId", out var interactionId);
metaDict.TryGetValue("Inputs", out var template);
List<InputInfo> additionalInputs = null;
var inputKeys = string.Empty;
if (!string.IsNullOrWhiteSpace(template))
{
additionalInputs = JsonSerializer.Deserialize<List<InputInfo>>(template);
}
if (additionalInputs is not null)
{
inputKeys = string.Join(",", additionalInputs.Select(input => input.Name));
}
bool.TryParse(refresh, out var shouldRefresh);
@ -126,9 +142,20 @@ namespace WebfrontCore.Controllers
Name = "TargetId",
Value = id?.ToString(),
Type = "hidden"
},
new()
{
Name = "CustomInputKeys",
Value = inputKeys,
Type = "hidden"
}
};
if (additionalInputs?.Any() ?? false)
{
inputs.AddRange(additionalInputs);
}
var info = new ActionInfo
{
ActionButtonLabel = label,
@ -141,28 +168,51 @@ namespace WebfrontCore.Controllers
return View("_ActionForm", info);
}
public async Task<IActionResult> DynamicActionAsync(string interactionId, string data, int? targetId,
CancellationToken token = default)
public async Task<IActionResult> DynamicActionAsync(CancellationToken token = default)
{
if (interactionId == "command")
{
var server = Manager.GetServers().First();
HttpContext.Request.Query.TryGetValue("InteractionId", out var interactionId);
HttpContext.Request.Query.TryGetValue("CustomInputKeys", out var inputKeys);
HttpContext.Request.Query.TryGetValue("Data", out var data);
HttpContext.Request.Query.TryGetValue("TargetId", out var targetIdString);
return await Task.FromResult(RedirectToAction("Execute", "Console", new
var inputs = new Dictionary<string, string>();
if (!string.IsNullOrWhiteSpace(inputKeys.ToString()))
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}{data}"
}));
foreach (var key in inputKeys.ToString().Split(","))
{
HttpContext.Request.Query.TryGetValue(key, out var input);
if (string.IsNullOrWhiteSpace(input))
{
continue;
}
inputs.Add(key, HttpContext.Request.Query[key]);
}
}
var game = (Reference.Game?)null;
var targetId = (int?)null;
if (int.TryParse(targetIdString.ToString().Split(",").Last(), out var parsedTargetId))
{
targetId = parsedTargetId;
}
if (targetId.HasValue)
{
game = (await Manager.GetClientService().Get(targetId.Value))?.GameName;
}
return Ok(await _interactionRegistration.ProcessInteraction(interactionId, targetId, game, token));
if (interactionId.ToString() != "command")
{
return Ok(await _interactionRegistration.ProcessInteraction(interactionId, Client.ClientId, targetId, game, inputs,
token));
}
var server = Manager.GetServers().First();
return Ok(await _remoteCommandService.Execute(Client.ClientId, targetId, data, inputs.Values.Select(input => input), server));
}
public IActionResult BanForm()

View File

@ -1,22 +1,19 @@
using Microsoft.AspNetCore.Mvc;
using SharedLibraryCore;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.Interfaces;
using System.Linq;
using System.Threading.Tasks;
using Data.Models;
namespace WebfrontCore.Controllers
{
public class ConsoleController : BaseController
{
private readonly ApplicationConfiguration _appconfig;
private readonly IRemoteCommandService _remoteCommandService;
public ConsoleController(IManager manager) : base(manager)
public ConsoleController(IManager manager, IRemoteCommandService remoteCommandService) : base(manager)
{
_appconfig = manager.GetApplicationSettings().Configuration();
_remoteCommandService = remoteCommandService;
}
public IActionResult Index()
@ -37,75 +34,8 @@ namespace WebfrontCore.Controllers
public async Task<IActionResult> Execute(long serverId, string command)
{
var server = Manager.GetServers().First(s => s.EndPoint == serverId);
var client = new EFClient
{
ClientId = Client.ClientId,
Level = Client.Level,
NetworkId = Client.NetworkId,
CurrentServer = server,
CurrentAlias = new EFAlias()
{
Name = Client.Name
}
};
var remoteEvent = new GameEvent
{
Type = GameEvent.EventType.Command,
Data = command.StartsWith(_appconfig.CommandPrefix) ||
command.StartsWith(_appconfig.BroadcastCommandPrefix)
? command
: $"{_appconfig.CommandPrefix}{command}",
Origin = client,
Owner = server,
IsRemote = true
};
Manager.AddEvent(remoteEvent);
CommandResponseInfo[] response = null;
try
{
// wait for the event to process
var completedEvent =
await remoteEvent.WaitAsync(Utilities.DefaultCommandTimeout, server.Manager.CancellationToken);
if (completedEvent.FailReason == GameEvent.EventFailReason.Timeout)
{
response = new[]
{
new CommandResponseInfo()
{
ClientId = client.ClientId,
Response = Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_COMMAND_TIMEOUT"]
}
};
}
else
{
response = completedEvent.Output.Select(output => new CommandResponseInfo()
{
Response = output,
ClientId = client.ClientId
}).ToArray();
}
}
catch (System.OperationCanceledException)
{
response = new[]
{
new CommandResponseInfo
{
ClientId = client.ClientId,
Response = Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_RESTART_SUCCESS"]
}
};
}
return remoteEvent.Failed ? StatusCode(400, response) : Ok(response);
var response = await _remoteCommandService.Execute(Client.ClientId, null, command, Enumerable.Empty<string>(), server);
return response.Any() ? StatusCode(400, response) : Ok(response);
}
}
}

View File

@ -141,6 +141,7 @@ namespace WebfrontCore
.GetRequiredService<StatsConfiguration>());
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IServerDataViewer>());
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IInteractionRegistration>());
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IRemoteCommandService>());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace WebfrontCore.ViewModels
{