2018-02-23 02:06:13 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-02-23 02:06:13 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers
|
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
public class ConsoleController : BaseController
|
2018-02-23 02:06:13 -05:00
|
|
|
|
{
|
2019-12-02 16:52:36 -05:00
|
|
|
|
public ConsoleController(IManager manager) : base(manager)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-23 02:06:13 -05:00
|
|
|
|
public IActionResult Index()
|
|
|
|
|
{
|
2018-04-08 14:48:40 -04:00
|
|
|
|
var activeServers = Manager.GetServers().Select(s => new ServerInfo()
|
2018-02-23 02:06:13 -05:00
|
|
|
|
{
|
|
|
|
|
Name = s.Hostname,
|
2018-11-27 19:31:48 -05:00
|
|
|
|
ID = s.EndPoint,
|
2018-02-23 02:06:13 -05:00
|
|
|
|
});
|
|
|
|
|
|
2018-03-13 17:30:22 -04:00
|
|
|
|
ViewBag.Description = "Use the IW4MAdmin web console to execute commands";
|
2018-05-05 18:52:04 -04:00
|
|
|
|
ViewBag.Title = Localization["WEBFRONT_CONSOLE_TITLE"];
|
2018-03-13 17:30:22 -04:00
|
|
|
|
ViewBag.Keywords = "IW4MAdmin, console, execute, commands";
|
|
|
|
|
|
2018-02-23 02:06:13 -05:00
|
|
|
|
return View(activeServers);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public async Task<IActionResult> ExecuteAsync(long serverId, string command)
|
2018-02-23 02:06:13 -05:00
|
|
|
|
{
|
2018-11-27 19:31:48 -05:00
|
|
|
|
var server = Manager.GetServers().First(s => s.EndPoint == serverId);
|
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
var client = new EFClient()
|
2018-04-05 00:38:45 -04:00
|
|
|
|
{
|
2018-04-26 02:13:04 -04:00
|
|
|
|
ClientId = Client.ClientId,
|
|
|
|
|
Level = Client.Level,
|
2019-01-03 15:39:22 -05:00
|
|
|
|
NetworkId = Client.NetworkId,
|
2018-04-13 02:32:30 -04:00
|
|
|
|
CurrentServer = server,
|
2018-12-01 13:17:53 -05:00
|
|
|
|
CurrentAlias = new EFAlias()
|
|
|
|
|
{
|
|
|
|
|
Name = Client.Name
|
|
|
|
|
}
|
2018-04-13 02:32:30 -04:00
|
|
|
|
};
|
2018-04-29 16:44:04 -04:00
|
|
|
|
|
2018-04-13 02:32:30 -04:00
|
|
|
|
var remoteEvent = new GameEvent()
|
|
|
|
|
{
|
2018-05-10 01:34:29 -04:00
|
|
|
|
Type = GameEvent.EventType.Command,
|
2018-04-13 02:32:30 -04:00
|
|
|
|
Data = command,
|
|
|
|
|
Origin = client,
|
2018-04-14 00:51:38 -04:00
|
|
|
|
Owner = server,
|
2019-01-03 15:39:22 -05:00
|
|
|
|
IsRemote = true
|
2018-04-05 00:38:45 -04:00
|
|
|
|
};
|
2018-02-23 02:06:13 -05:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
Manager.GetEventHandler().AddEvent(remoteEvent);
|
2019-07-21 18:14:44 -04:00
|
|
|
|
List<CommandResponseInfo> response = null;
|
2019-05-17 10:02:09 -04:00
|
|
|
|
|
|
|
|
|
try
|
2018-09-06 14:25:58 -04:00
|
|
|
|
{
|
2019-07-21 18:14:44 -04:00
|
|
|
|
var completedEvent = await remoteEvent.WaitAsync(Utilities.DefaultCommandTimeout, server.Manager.CancellationToken);
|
2019-05-17 10:02:09 -04:00
|
|
|
|
// wait for the event to process
|
2019-07-21 18:14:44 -04:00
|
|
|
|
if (!completedEvent.Failed)
|
2019-05-17 10:02:09 -04:00
|
|
|
|
{
|
|
|
|
|
response = server.CommandResult.Where(c => c.ClientId == client.ClientId).ToList();
|
2018-09-06 14:25:58 -04:00
|
|
|
|
|
2019-05-17 10:02:09 -04:00
|
|
|
|
// remove the added command response
|
|
|
|
|
for (int i = 0; i < response.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
server.CommandResult.Remove(response[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 18:14:44 -04:00
|
|
|
|
else if (completedEvent.FailReason == GameEvent.EventFailReason.Timeout)
|
2019-05-17 10:02:09 -04:00
|
|
|
|
{
|
|
|
|
|
response = new List<CommandResponseInfo>()
|
|
|
|
|
{
|
2019-07-21 18:14:44 -04:00
|
|
|
|
new CommandResponseInfo()
|
|
|
|
|
{
|
|
|
|
|
ClientId = client.ClientId,
|
|
|
|
|
Response = Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_COMMAND_TIMEOUT"]
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-12-01 13:17:53 -05:00
|
|
|
|
}
|
2018-09-06 14:25:58 -04:00
|
|
|
|
}
|
2018-02-23 02:06:13 -05:00
|
|
|
|
|
2019-05-17 10:02:09 -04:00
|
|
|
|
catch (System.OperationCanceledException)
|
2018-09-06 14:25:58 -04:00
|
|
|
|
{
|
|
|
|
|
response = new List<CommandResponseInfo>()
|
|
|
|
|
{
|
|
|
|
|
new CommandResponseInfo()
|
|
|
|
|
{
|
|
|
|
|
ClientId = client.ClientId,
|
2019-05-17 10:02:09 -04:00
|
|
|
|
Response = Utilities.CurrentLocalization.LocalizationIndex["COMMADS_RESTART_SUCCESS"]
|
2018-09-06 14:25:58 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-02-23 02:06:13 -05:00
|
|
|
|
|
|
|
|
|
return View("_Response", response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|