IW4M-Admin/WebfrontCore/Controllers/ConsoleController.cs

63 lines
2.1 KiB
C#
Raw Normal View History

2018-02-23 02:06:13 -05:00
using Microsoft.AspNetCore.Mvc;
using SharedLibrary;
using SharedLibrary.Dtos;
using SharedLibrary.Objects;
using System;
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
{
public IActionResult Index()
{
2018-03-06 02:22:19 -05:00
var activeServers = Manager.Servers.Select(s => new ServerInfo()
2018-02-23 02:06:13 -05:00
{
Name = s.Hostname,
ID = s.GetHashCode(),
});
ViewBag.Description = "Use the IW4MAdmin web console to execute commands";
2018-02-23 02:06:13 -05:00
ViewBag.Title = "Web Console";
ViewBag.Keywords = "IW4MAdmin, console, execute, commands";
2018-02-23 02:06:13 -05:00
return View(activeServers);
}
public async Task<IActionResult> ExecuteAsync(int serverId, string command)
{
var requestIPAddress = Request.HttpContext.Connection.RemoteIpAddress;
var intIP = requestIPAddress.ToString().ConvertToIP();
#if !DEBUG
2018-02-23 02:06:13 -05:00
var origin = (await IW4MAdmin.ApplicationManager.GetInstance().GetClientService().GetClientByIP(intIP))
.OrderByDescending(c => c.Level)
.FirstOrDefault()?.AsPlayer() ?? new Player()
{
Name = "WebConsoleUser",
Level = Player.Permission.User,
IPAddress = intIP
};
#else
var origin = (await Manager.GetClientService().GetUnique(0)).AsPlayer();
#endif
2018-02-23 02:06:13 -05:00
2018-03-06 02:22:19 -05:00
var server = Manager.Servers.First(s => s.GetHashCode() == serverId);
2018-02-23 02:06:13 -05:00
origin.CurrentServer = server;
var remoteEvent = new Event(Event.GType.Say, command, origin, null, server);
await server.ExecuteEvent(remoteEvent);
var response = server.CommandResult.Where(c => c.ClientId == origin.ClientId).ToList();
// remove the added command response
for (int i = 0; i < response.Count; i++)
server.CommandResult.Remove(response[i]);
return View("_Response", response);
}
}
}