2020-01-27 23:37:28 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Common;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-01-29 16:32:23 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using WebInterface.Infrastructure;
|
2020-01-27 23:37:28 +01:00
|
|
|
|
using WebInterface.Services;
|
|
|
|
|
|
|
|
|
|
namespace WebInterface.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class PrivateController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly IPrivateServerService _privateServerService;
|
|
|
|
|
private readonly IRateLimiterService _rateLimiterService;
|
2020-01-29 16:32:23 +01:00
|
|
|
|
private readonly ICustomMapService _customMapService;
|
|
|
|
|
private readonly ILogger<PrivateController> _logger;
|
2020-01-27 23:37:28 +01:00
|
|
|
|
|
2020-01-29 16:32:23 +01:00
|
|
|
|
public PrivateController(IPrivateServerService privateServerService,
|
|
|
|
|
IRateLimiterService rateLimiterService,
|
|
|
|
|
ICustomMapService customMapService,
|
|
|
|
|
ILogger<PrivateController> logger)
|
2020-01-27 23:37:28 +01:00
|
|
|
|
{
|
|
|
|
|
_privateServerService = privateServerService;
|
|
|
|
|
_rateLimiterService = rateLimiterService;
|
2020-01-29 16:32:23 +01:00
|
|
|
|
_customMapService = customMapService;
|
|
|
|
|
_logger = logger;
|
2020-01-27 23:37:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("nblood/private")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult Index()
|
|
|
|
|
{
|
|
|
|
|
var viewModel = new PrivateViewModel
|
|
|
|
|
{
|
|
|
|
|
ModName = Constants.SupportedMods.Values.First().Name,
|
|
|
|
|
Players = 2
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("nblood/private", Name = "RequestPrivateServer")]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ValidateAntiForgeryToken]
|
|
|
|
|
public IActionResult Index(PrivateViewModel request)
|
|
|
|
|
{
|
|
|
|
|
StartServerResponse viewModel;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
2020-01-29 16:32:23 +01:00
|
|
|
|
throw new WebInterfaceException("Something went off the rails.");
|
2020-01-27 23:37:28 +01:00
|
|
|
|
|
|
|
|
|
if (!_rateLimiterService.IsRequestAllowed(HttpContext.Connection.RemoteIpAddress))
|
2020-01-29 16:32:23 +01:00
|
|
|
|
throw new WebInterfaceException("Sorry, you have requested too many servers recently, you need to wait some time.");
|
2020-01-27 23:37:28 +01:00
|
|
|
|
|
2020-01-29 16:32:23 +01:00
|
|
|
|
string tempFolderName = "";
|
|
|
|
|
if ((request.FormFile?.Length ?? 0) > 0)
|
|
|
|
|
tempFolderName = _customMapService.StoreTempCustomMap(request.FormFile);
|
|
|
|
|
|
|
|
|
|
var spawnedServer = _privateServerService.SpawnNewPrivateServer(request.Players + 1, request.ModName, tempFolderName ?? "");
|
2020-01-27 23:37:28 +01:00
|
|
|
|
string commandLine = CommandLineUtils.GetClientLaunchCommand(HttpContext.Request.Host.Host,
|
|
|
|
|
spawnedServer.Port,
|
|
|
|
|
spawnedServer.Mod.CommandLine);
|
|
|
|
|
|
|
|
|
|
viewModel = new StartServerResponse(spawnedServer.Port, commandLine);
|
|
|
|
|
}
|
2020-01-29 16:32:23 +01:00
|
|
|
|
catch (WebInterfaceException ex)
|
2020-01-27 23:37:28 +01:00
|
|
|
|
{
|
|
|
|
|
viewModel = new StartServerResponse(ex.Message);
|
|
|
|
|
}
|
2020-01-29 16:32:23 +01:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex.ToString());
|
|
|
|
|
viewModel = new StartServerResponse("Internal server error.");
|
|
|
|
|
}
|
2020-01-27 23:37:28 +01:00
|
|
|
|
|
|
|
|
|
return View("Result", viewModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|