2021-01-17 22:58:18 -05:00
|
|
|
|
using System;
|
2023-05-30 19:12:57 -04:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
using System.Linq;
|
2023-05-30 19:12:57 -04:00
|
|
|
|
using System.Threading;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using SharedLibraryCore;
|
2023-05-30 19:12:57 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using WebfrontCore.Controllers.API.Models;
|
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers.API
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class Server : BaseController
|
|
|
|
|
{
|
2023-05-30 19:12:57 -04:00
|
|
|
|
private readonly IServerDataViewer _serverDataViewer;
|
|
|
|
|
private readonly ApplicationConfiguration _applicationConfiguration;
|
|
|
|
|
|
|
|
|
|
public Server(IManager manager, IServerDataViewer serverDataViewer,
|
|
|
|
|
ApplicationConfiguration applicationConfiguration) : base(manager)
|
2021-01-17 22:58:18 -05:00
|
|
|
|
{
|
2023-05-30 19:12:57 -04:00
|
|
|
|
_serverDataViewer = serverDataViewer;
|
|
|
|
|
_applicationConfiguration = applicationConfiguration;
|
2021-01-17 22:58:18 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult Index()
|
|
|
|
|
{
|
|
|
|
|
return new JsonResult(Manager.GetServers().Select(server => new
|
|
|
|
|
{
|
|
|
|
|
Id = server.EndPoint,
|
2023-04-04 23:21:18 -04:00
|
|
|
|
server.ServerName,
|
|
|
|
|
server.ListenAddress,
|
|
|
|
|
server.ListenPort,
|
2022-01-26 16:17:05 -05:00
|
|
|
|
Game = server.GameName.ToString(),
|
|
|
|
|
server.ClientNum,
|
|
|
|
|
server.MaxClients,
|
|
|
|
|
server.CurrentMap,
|
|
|
|
|
currentGameType = new
|
|
|
|
|
{
|
|
|
|
|
type = server.Gametype,
|
|
|
|
|
name = server.GametypeName
|
|
|
|
|
},
|
|
|
|
|
Parser = server.RconParser.Name,
|
2021-01-17 22:58:18 -05:00
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public IActionResult GetServerById(string id)
|
|
|
|
|
{
|
|
|
|
|
var foundServer = Manager.GetServers().FirstOrDefault(server => server.EndPoint == long.Parse(id));
|
|
|
|
|
|
|
|
|
|
if (foundServer == null)
|
|
|
|
|
{
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new JsonResult(new
|
|
|
|
|
{
|
|
|
|
|
Id = foundServer.EndPoint,
|
2023-04-04 23:21:18 -04:00
|
|
|
|
foundServer.ServerName,
|
|
|
|
|
foundServer.ListenAddress,
|
|
|
|
|
foundServer.ListenPort,
|
2022-01-26 16:17:05 -05:00
|
|
|
|
Game = foundServer.GameName.ToString(),
|
|
|
|
|
foundServer.ClientNum,
|
|
|
|
|
foundServer.MaxClients,
|
|
|
|
|
foundServer.CurrentMap,
|
|
|
|
|
currentGameType = new
|
|
|
|
|
{
|
|
|
|
|
type = foundServer.Gametype,
|
|
|
|
|
name = foundServer.GametypeName
|
|
|
|
|
},
|
|
|
|
|
Parser = foundServer.RconParser.Name,
|
2021-01-17 22:58:18 -05:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("{id}/execute")]
|
|
|
|
|
public async Task<IActionResult> ExecuteCommandForServer(string id, [FromBody] CommandRequest commandRequest)
|
|
|
|
|
{
|
|
|
|
|
if (!Authorized)
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var foundServer = Manager.GetServers().FirstOrDefault(server => server.EndPoint == long.Parse(id));
|
|
|
|
|
|
|
|
|
|
if (foundServer == null)
|
|
|
|
|
{
|
|
|
|
|
return new BadRequestObjectResult($"No server with id '{id}' was found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(commandRequest.Command))
|
|
|
|
|
{
|
|
|
|
|
return new BadRequestObjectResult("Command cannot be empty");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var start = DateTime.Now;
|
|
|
|
|
Client.CurrentServer = foundServer;
|
|
|
|
|
|
2023-04-05 10:54:57 -04:00
|
|
|
|
var commandEvent = new GameEvent
|
2021-01-17 22:58:18 -05:00
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Command,
|
|
|
|
|
Owner = foundServer,
|
|
|
|
|
Origin = Client,
|
|
|
|
|
Data = commandRequest.Command,
|
2021-05-04 20:01:09 -04:00
|
|
|
|
Extra = commandRequest.Command,
|
|
|
|
|
IsRemote = true
|
2021-01-17 22:58:18 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Manager.AddEvent(commandEvent);
|
|
|
|
|
var completedEvent = await commandEvent.WaitAsync(Utilities.DefaultCommandTimeout, foundServer.Manager.CancellationToken);
|
|
|
|
|
|
|
|
|
|
return new JsonResult(new
|
|
|
|
|
{
|
|
|
|
|
ExecutionTimeMs = Math.Round((DateTime.Now - start).TotalMilliseconds, 0),
|
|
|
|
|
completedEvent.Output
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-05-30 19:12:57 -04:00
|
|
|
|
|
|
|
|
|
[HttpGet("{id}/history")]
|
|
|
|
|
public async Task<IActionResult> GetClientHistory(string id)
|
|
|
|
|
{
|
|
|
|
|
var foundServer = Manager.GetServers().FirstOrDefault(server => server.Id == id);
|
|
|
|
|
|
|
|
|
|
if (foundServer == null)
|
|
|
|
|
{
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clientHistory = (await _serverDataViewer.ClientHistoryAsync(_applicationConfiguration.MaxClientHistoryTime,
|
|
|
|
|
CancellationToken.None))?
|
|
|
|
|
.FirstOrDefault(history => history.ServerId == foundServer.LegacyDatabaseId) ??
|
|
|
|
|
new ClientHistoryInfo
|
|
|
|
|
{
|
|
|
|
|
ServerId = foundServer.LegacyDatabaseId,
|
|
|
|
|
ClientCounts = new List<ClientCountSnapshot>()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var counts = clientHistory.ClientCounts?.AsEnumerable() ?? Enumerable.Empty<ClientCountSnapshot>();
|
|
|
|
|
|
|
|
|
|
if (foundServer.ClientHistory.ClientCounts.Any())
|
|
|
|
|
{
|
|
|
|
|
counts = counts.Union(foundServer.ClientHistory.ClientCounts.Where(history =>
|
|
|
|
|
history.Time > (clientHistory.ClientCounts?.LastOrDefault()?.Time ?? DateTime.MinValue)))
|
|
|
|
|
.Where(history => history.Time >= DateTime.UtcNow - _applicationConfiguration.MaxClientHistoryTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ViewBag.Maps?.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return Json(counts.ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clientCountSnapshots = counts.ToList();
|
|
|
|
|
foreach (var count in clientCountSnapshots)
|
|
|
|
|
{
|
|
|
|
|
count.MapAlias = foundServer.Maps.FirstOrDefault(map => map.Name == count.Map)?.Alias ??
|
|
|
|
|
count.Map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Json(clientCountSnapshots);
|
|
|
|
|
}
|
2021-01-17 22:58:18 -05:00
|
|
|
|
}
|
2022-01-26 16:17:05 -05:00
|
|
|
|
}
|