2020-01-27 13:23:00 +01:00
|
|
|
|
using Common;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Model;
|
|
|
|
|
using System;
|
2020-01-23 21:03:54 +01:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
2020-01-27 13:23:00 +01:00
|
|
|
|
using WebInterface.Services;
|
2020-01-23 21:03:54 +01:00
|
|
|
|
|
|
|
|
|
namespace WebInterface.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class NBloodController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private static bool _isBusy = false;
|
|
|
|
|
private static DateTime _lastRefresh = DateTime.MinValue;
|
|
|
|
|
private static ListServersResponse _lastServerList = null;
|
|
|
|
|
|
|
|
|
|
private readonly ILogger<NBloodController> _logger;
|
|
|
|
|
private readonly IConfiguration _config;
|
2020-01-27 14:48:53 +01:00
|
|
|
|
private readonly IStateService _listServersService;
|
2020-01-23 21:03:54 +01:00
|
|
|
|
|
|
|
|
|
private static readonly Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
|
|
|
private static readonly IPEndPoint webApiListenerEndPoint = new IPEndPoint(IPAddress.Loopback, 11028);
|
|
|
|
|
|
2020-01-27 14:48:53 +01:00
|
|
|
|
public NBloodController(ILogger<NBloodController> logger, IConfiguration config, IStateService listServersService)
|
2020-01-23 21:03:54 +01:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_config = config;
|
2020-01-27 13:23:00 +01:00
|
|
|
|
_listServersService = listServersService;
|
2020-01-23 21:03:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("[controller]/api/startserver")]
|
|
|
|
|
public StartServerResponse StartServer([FromQuery] ServerParameters parameters)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Stopwatch sw = Stopwatch.StartNew();
|
|
|
|
|
while (_isBusy)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(TimeSpan.FromSeconds(1));
|
|
|
|
|
if (sw.Elapsed.TotalSeconds > 5)
|
|
|
|
|
throw new Exception("Request timeout: the previous request hasn't finished yet.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_isBusy = true;
|
|
|
|
|
|
2020-01-24 08:48:59 +01:00
|
|
|
|
if (parameters.Players < 3)
|
|
|
|
|
parameters.Players = 3;
|
2020-01-23 21:03:54 +01:00
|
|
|
|
|
|
|
|
|
if (parameters.ApiKey != _config.GetValue<string>("ApiKey"))
|
|
|
|
|
return new StartServerResponse("Invalid ApiKey.");
|
|
|
|
|
|
2020-01-27 17:25:45 +01:00
|
|
|
|
SpawnedServerInfo serverProcess = ProcessSpawner.SpawnServer(parameters.Players, parameters.ModName);
|
|
|
|
|
byte[] payload = Encoding.ASCII.GetBytes($"B{serverProcess.Port}\t{serverProcess.Process.Id}\0");
|
2020-01-23 21:03:54 +01:00
|
|
|
|
socket.SendTo(payload, webApiListenerEndPoint);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Server started waiting for {0} players on port {1}.",
|
2020-01-27 17:25:45 +01:00
|
|
|
|
parameters.Players, serverProcess.Port);
|
2020-01-23 21:03:54 +01:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(TimeSpan.FromSeconds(2));
|
2020-01-27 17:25:45 +01:00
|
|
|
|
return new StartServerResponse(serverProcess.Port)
|
|
|
|
|
{
|
|
|
|
|
CommandLine = CommandLineUtils.GetClientLaunchCommand(HttpContext.Request.Host.Host,
|
|
|
|
|
serverProcess.Port,
|
|
|
|
|
serverProcess.Mod.CommandLine)
|
|
|
|
|
};
|
2020-01-23 21:03:54 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex.ToString());
|
|
|
|
|
return new StartServerResponse("Unhandled exception has been occured. Check the logs for details.");
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_isBusy = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("[controller]/api/listservers")]
|
|
|
|
|
public ListServersResponse ListServers()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-01-27 13:23:00 +01:00
|
|
|
|
if (DateTime.UtcNow - _lastRefresh > TimeSpan.FromSeconds(1)
|
2020-01-23 21:03:54 +01:00
|
|
|
|
|| _lastServerList == null)
|
|
|
|
|
{
|
2020-01-27 13:23:00 +01:00
|
|
|
|
_lastServerList = _listServersService.ListServers(HttpContext.Request.Host.Host);
|
2020-01-23 21:03:54 +01:00
|
|
|
|
_lastRefresh = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _lastServerList;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex.ToString());
|
|
|
|
|
return new ListServersResponse("Unhandled exception has been occured. Check the logs for details.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|