mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-07 21:58:06 -05:00
Using IRemoteCommandService to handle redirect for Command Execution via API (#333)
This commit is contained in:
parent
b1810b0517
commit
5dca8717b9
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -14,18 +13,13 @@ namespace WebfrontCore.Controllers.API
|
|||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class Server : BaseController
|
public class Server(
|
||||||
|
IManager manager,
|
||||||
|
IServerDataViewer serverDataViewer,
|
||||||
|
ApplicationConfiguration applicationConfiguration,
|
||||||
|
IRemoteCommandService remoteCommandService)
|
||||||
|
: BaseController(manager)
|
||||||
{
|
{
|
||||||
private readonly IServerDataViewer _serverDataViewer;
|
|
||||||
private readonly ApplicationConfiguration _applicationConfiguration;
|
|
||||||
|
|
||||||
public Server(IManager manager, IServerDataViewer serverDataViewer,
|
|
||||||
ApplicationConfiguration applicationConfiguration) : base(manager)
|
|
||||||
{
|
|
||||||
_serverDataViewer = serverDataViewer;
|
|
||||||
_applicationConfiguration = applicationConfiguration;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult Index()
|
public IActionResult Index()
|
||||||
{
|
{
|
||||||
@ -41,8 +35,8 @@ namespace WebfrontCore.Controllers.API
|
|||||||
server.CurrentMap,
|
server.CurrentMap,
|
||||||
currentGameType = new
|
currentGameType = new
|
||||||
{
|
{
|
||||||
type = server.Gametype,
|
type = server.Gametype,
|
||||||
name = server.GametypeName
|
name = server.GametypeName
|
||||||
},
|
},
|
||||||
Parser = server.RconParser.Name,
|
Parser = server.RconParser.Name,
|
||||||
}));
|
}));
|
||||||
@ -97,26 +91,17 @@ namespace WebfrontCore.Controllers.API
|
|||||||
return new BadRequestObjectResult("Command cannot be empty");
|
return new BadRequestObjectResult("Command cannot be empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
var start = DateTime.Now;
|
var start = TimeProvider.System.GetLocalNow();
|
||||||
Client.CurrentServer = foundServer;
|
Client.CurrentServer = foundServer;
|
||||||
|
|
||||||
var commandEvent = new GameEvent
|
var completedResult =
|
||||||
{
|
await remoteCommandService.ExecuteWithResult(Client.ClientId, null, commandRequest.Command, null, foundServer);
|
||||||
Type = GameEvent.EventType.Command,
|
|
||||||
Owner = foundServer,
|
|
||||||
Origin = Client,
|
|
||||||
Data = commandRequest.Command,
|
|
||||||
Extra = commandRequest.Command,
|
|
||||||
IsRemote = true
|
|
||||||
};
|
|
||||||
|
|
||||||
Manager.AddEvent(commandEvent);
|
|
||||||
var completedEvent = await commandEvent.WaitAsync(Utilities.DefaultCommandTimeout, foundServer.Manager.CancellationToken);
|
|
||||||
|
|
||||||
return new JsonResult(new
|
return new JsonResult(new
|
||||||
{
|
{
|
||||||
ExecutionTimeMs = Math.Round((DateTime.Now - start).TotalMilliseconds, 0),
|
ExecutionTimeMs = Math.Round((TimeProvider.System.GetLocalNow() - start).TotalMilliseconds, 0),
|
||||||
completedEvent.Output
|
Output = completedResult.Item2.Where(x => !string.IsNullOrWhiteSpace(x.Response))
|
||||||
|
.Select(x => x.Response.Trim())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,30 +110,30 @@ namespace WebfrontCore.Controllers.API
|
|||||||
{
|
{
|
||||||
var foundServer = Manager.GetServers().FirstOrDefault(server => server.Id == id);
|
var foundServer = Manager.GetServers().FirstOrDefault(server => server.Id == id);
|
||||||
|
|
||||||
if (foundServer == null)
|
if (foundServer is null)
|
||||||
{
|
{
|
||||||
return new NotFoundResult();
|
return new NotFoundResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
var clientHistory = (await _serverDataViewer.ClientHistoryAsync(_applicationConfiguration.MaxClientHistoryTime,
|
var clientHistory =
|
||||||
CancellationToken.None))?
|
(await serverDataViewer.ClientHistoryAsync(applicationConfiguration.MaxClientHistoryTime, CancellationToken.None))?
|
||||||
.FirstOrDefault(history => history.ServerId == foundServer.LegacyDatabaseId) ??
|
.FirstOrDefault(history => history.ServerId == foundServer.LegacyDatabaseId) ??
|
||||||
new ClientHistoryInfo
|
new ClientHistoryInfo
|
||||||
{
|
{
|
||||||
ServerId = foundServer.LegacyDatabaseId,
|
ServerId = foundServer.LegacyDatabaseId,
|
||||||
ClientCounts = new List<ClientCountSnapshot>()
|
ClientCounts = []
|
||||||
};
|
};
|
||||||
|
|
||||||
var counts = clientHistory.ClientCounts?.AsEnumerable() ?? Enumerable.Empty<ClientCountSnapshot>();
|
var counts = clientHistory.ClientCounts?.AsEnumerable() ?? [];
|
||||||
|
|
||||||
if (foundServer.ClientHistory.ClientCounts.Any())
|
if (foundServer.ClientHistory.ClientCounts.Count is not 0)
|
||||||
{
|
{
|
||||||
counts = counts.Union(foundServer.ClientHistory.ClientCounts.Where(history =>
|
counts = counts.Union(foundServer.ClientHistory.ClientCounts.Where(history =>
|
||||||
history.Time > (clientHistory.ClientCounts?.LastOrDefault()?.Time ?? DateTime.MinValue)))
|
history.Time > (clientHistory.ClientCounts?.LastOrDefault()?.Time ?? DateTime.MinValue)))
|
||||||
.Where(history => history.Time >= DateTime.UtcNow - _applicationConfiguration.MaxClientHistoryTime);
|
.Where(history => history.Time >= DateTime.UtcNow - applicationConfiguration.MaxClientHistoryTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ViewBag.Maps?.Count == 0)
|
if (ViewBag.Maps?.Count is 0)
|
||||||
{
|
{
|
||||||
return Json(counts.ToList());
|
return Json(counts.ToList());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user