Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
0.0.5
Browse files Browse the repository at this point in the history
Closes #99 Check the box in the pavlov server settings. Be aware thatthat this feature is not available for rented or im premise roles.
Closes #100 You can import bans on every server.
Closes #95 Be aware that you need to install https://wkhtmltopdf.org/ to get this function up and running.
If you use it in discord you would need to change the url every time you post otherwise it will get cached.
like &timeStamp=4466444 and than + 1 every time you send the link.
  • Loading branch information
devinSpitz authored Feb 27, 2022
2 parents 3ac17e1 + 2243fb8 commit e7fb128
Show file tree
Hide file tree
Showing 17 changed files with 391 additions and 66 deletions.
77 changes: 77 additions & 0 deletions PavlovRconWebserver/Controllers/PavlovServerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AspNetCoreHero.ToastNotification.Abstractions;
using LiteDB;
using LiteDB.Identity.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using PavlovRconWebserver.Exceptions;
Expand All @@ -28,6 +30,7 @@ public class PavlovServerController : Controller
private readonly SshServerSerivce _service;
private readonly SteamIdentityService _steamIdentityService;
private readonly UserService _userservice;
private readonly ServerBansService _serverBansService;
private readonly ServerSelectedWhitelistService _whitelistService;
private readonly SteamIdentityStatsServerService _steamIdentityStatsServerService;
private readonly UserManager<LiteDbUser> UserManager;
Expand All @@ -36,6 +39,7 @@ public class PavlovServerController : Controller
public PavlovServerController(SshServerSerivce service,
UserService userService,
PavlovServerService pavlovServerService,
ServerBansService serverBansService,
ServerSelectedMapService serverSelectedMapService,
MapsService mapsService,
SshServerSerivce sshServerSerivce,
Expand All @@ -51,6 +55,7 @@ public PavlovServerController(SshServerSerivce service,
_userservice = userService;
_pavlovServerService = pavlovServerService;
_serverSelectedMapService = serverSelectedMapService;
_serverBansService = serverBansService;
_steamIdentityStatsServerService = steamIdentityStatsServerService;
_mapsService = mapsService;
_whitelistService = whitelistService;
Expand Down Expand Up @@ -429,6 +434,78 @@ await _userservice.getUserFromCp(HttpContext.User), serverId, _service, _pavlovS

return RedirectToAction("Index", "SshServer");
}

[HttpGet("[controller]/ImportBansView/{id}")]
public async Task<IActionResult> ImportBansView(int id)
{
if (!await RightsHandler.HasRightsToThisPavlovServer(HttpContext.User,
await _userservice.getUserFromCp(HttpContext.User), id, _service, _pavlovServerService))
return Forbid();
var server = await _pavlovServerService.FindOne(id);
if (server == null) return BadRequest("There is no Server with this id!");


return View("ImportBans",new PavlovServerImportBansListViewModel()
{
ServerId = id
});
}

[HttpPost("[controller]/ImportBans")]
public async Task<IActionResult> ImportBans(PavlovServerImportBansListViewModel viewModel)
{

if (viewModel.ServerId <= 0) return BadRequest("Please choose a server!");

var user = await _userservice.getUserFromCp(HttpContext.User);
if (!await RightsHandler.HasRightsToThisPavlovServer(HttpContext.User,user
, viewModel.ServerId, _service,
_pavlovServerService))
return Forbid();
var server = await _pavlovServerService.FindOne(viewModel.ServerId);
await using var ms = new MemoryStream();
await viewModel.BansFromFile.CopyToAsync(ms);
var fileBytes = ms.ToArray();

var table = (Encoding.Default.GetString(
fileBytes,
0,
fileBytes.Length - 1)).Split(new string[] { "\r\n", "\r", "\n" },
StringSplitOptions.None);
var timespan = "unlimited";
Statics.BanList.TryGetValue(timespan, out var timespans);
if (string.IsNullOrEmpty(timespan)) return BadRequest("TimeSpan must be set!");
var banlist = Array.Empty<ServerBans>();
if (server.GlobalBan)
{

ViewBag.GlobalBan = true;
banlist = await _serverBansService.FindAllGlobal(true);
}
else
{
banlist = await _serverBansService.FindAllFromPavlovServerId(viewModel.ServerId, true);
}

foreach (var steamId in table)
{
var singleSteamId = steamId.Replace(";","").Trim();
var ban = new ServerBans
{
SteamId = singleSteamId,
BanSpan = timespans,
BannedDateTime = DateTime.Now,
PavlovServer = server
};

if (banlist.FirstOrDefault(x => x.SteamId == ban.SteamId) == null)
{
await _serverBansService.Upsert(ban);
}
}
return new ObjectResult(true);
}


[HttpPost("[controller]/SaveServerSettings/")]
public async Task<IActionResult> SaveServerSettings(PavlovServerGameIni pavlovServerGameIni)
Expand Down
32 changes: 32 additions & 0 deletions PavlovRconWebserver/Controllers/PublicViewListsController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CoreHtmlToImage;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using PavlovRconWebserver.Extensions.CC.Web.Helpers;
using PavlovRconWebserver.Models;
using PavlovRconWebserver.Services;

Expand Down Expand Up @@ -49,6 +54,33 @@ public async Task<IActionResult> PlayersFromServers([FromQuery] int[] servers,
ViewBag.background = backgroundColorHex;
ViewBag.textColor = fontColorHex;
return PartialView(result);
}
[HttpGet("[controller]/PlayersFromServersAsImage/")]
// GET
public async Task<IActionResult> PlayersFromServersAsImage([FromQuery] int[] servers,
[FromQuery] string backgroundColorHex, [FromQuery] string fontColorHex)
{
var result = new List<PavlovServerPlayerListPublicViewModel>();
foreach (var serverId in servers)
{
var server = await _pavlovServerService.FindOne(serverId);
if (server == null) continue;
if (server.ServerServiceState != ServerServiceState.active &&
server.ServerType == ServerType.Community) continue;
if (server.ServerType == ServerType.Event) continue;
result.Add(await _publicViewListsService.GetPavlovServerPlayerListPublicViewModel(serverId,false));
}

ViewBag.background = backgroundColorHex;
ViewBag.textColor = fontColorHex;
ViewBag.bigger = true;
var partialViewHtml = await this.RenderViewAsync("PlayersFromServers", result, true);
var converter = new HtmlConverter();
var bytes = converter.FromHtmlString(partialViewHtml,512,ImageFormat.Png);

return base.File(
bytes,
"image/png");
}


Expand Down
89 changes: 61 additions & 28 deletions PavlovRconWebserver/Controllers/RconController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AspNetCoreHero.ToastNotification.Abstractions;
using LiteDB.Identity.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using PavlovRconWebserver.Exceptions;
Expand Down Expand Up @@ -346,7 +349,17 @@ public async Task<IActionResult> GetBansFromServers(int serverId)

if (serverId <= 0) return BadRequest("Please choose a server!");
var server = await _pavlovServerService.FindOne(serverId);
var banlist = await _serverBansService.FindAllFromPavlovServerId(serverId, true);
var banlist = Array.Empty<ServerBans>();
if (server.GlobalBan)
{

ViewBag.GlobalBan = true;
banlist = await _serverBansService.FindAllGlobal(true);
}
else
{
banlist = await _serverBansService.FindAllFromPavlovServerId(serverId, true);
}

ViewBag.ServerId = serverId;
return PartialView("/Views/Rcon/BanList.cshtml", banlist.ToList());
Expand Down Expand Up @@ -438,10 +451,11 @@ public async Task<IActionResult> AddBanPlayer(int serverId, string steamId, stri

return new ObjectResult(true);
}

[HttpPost("[controller]/RemoveBanPlayer")]
public async Task<IActionResult> RemoveBanPlayer(int serverId, string steamId)
{
//Todo search for the right server if its a global ban
var servers = await GiveServerWhichTheUserHasRightsTo();

if (!servers.Select(x => x.Id).Contains(serverId))
Expand All @@ -453,48 +467,67 @@ public async Task<IActionResult> RemoveBanPlayer(int serverId, string steamId)
if (string.IsNullOrEmpty(steamId) || steamId == "-") return BadRequest("SteamID must be set!");
var pavlovServer = await _pavlovServerService.FindOne(serverId);

var banlist = new List<ServerBans>();
//Remove from blacklist file
try
{
banlist = _service.GetServerBansFromBlackList(pavlovServer, new List<ServerBans>());
}
catch (CommandException e)
{
return BadRequest(e.Message);
}

var toRemove = banlist.FirstOrDefault(x => x.SteamId == steamId);
if (toRemove != null)
if (!pavlovServer.GlobalBan) // remove it from this single blacklist file
{
//write to BlackList.txt
banlist.Remove(toRemove);

var banlist = new List<ServerBans>();
//Remove from blacklist file
try
{
_service.SaveBlackListEntry(pavlovServer, banlist);
banlist = _service.GetServerBansFromBlackList(pavlovServer, new List<ServerBans>());
}
catch (CommandException e)
{
return BadRequest(e.Message);
}

var toRemove = banlist.FirstOrDefault(x => x.SteamId == steamId);
if (toRemove != null)
{
//write to BlackList.txt
banlist.Remove(toRemove);
try
{
_service.SaveBlackListEntry(pavlovServer, banlist);
}
catch (CommandException e)
{
return BadRequest(e.Message);
}
}
}


// remove from Database
var actualBans = await _serverBansService.FindAllFromPavlovServerId(serverId, true);
var toRemoveBan = actualBans.FirstOrDefault(x => x.SteamId == steamId);
if (toRemoveBan != null) await _serverBansService.Delete(toRemoveBan.Id);

Task.Delay(1000).Wait(); // If you not wait it may just don't work. Don't know why
//unban command
try
if (!pavlovServer.GlobalBan)
{
await RconStatic.SendCommandSShTunnel(pavlovServer, "Unban " + steamId, _toastifyService);
// remove from Database
var actualBans = await _serverBansService.FindAllFromPavlovServerId(serverId, true);
var toRemoveBan = actualBans.FirstOrDefault(x => x.SteamId == steamId);
if (toRemoveBan != null) await _serverBansService.Delete(toRemoveBan.Id);

Task.Delay(1000).Wait(); // If you not wait it may just don't work. Don't know why
//unban command
try
{
await RconStatic.SendCommandSShTunnel(pavlovServer, "Unban " + steamId, _toastifyService);
}
catch (CommandException)
{
return BadRequest("Could not unban player from in memory blacklist!");
}
}
catch (CommandException)
else
{
return BadRequest("Could not unban player from in memory blacklist!");
var actualBans = await _serverBansService.FindAllGlobal(true);
var toRemoveBans = actualBans.Where(x => x.SteamId == steamId);
foreach (var toRemoveBan in toRemoveBans)
{
await _serverBansService.Delete(toRemoveBan.Id);
}
}


return new ObjectResult(true);
}

Expand Down
51 changes: 51 additions & 0 deletions PavlovRconWebserver/Extensions/ControllerExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;

namespace PavlovRconWebserver.Extensions
{
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System.IO;
using System.Threading.Tasks;

namespace CC.Web.Helpers
{
public static class ControllerExtensions
{
public static async Task<string> RenderViewAsync<TModel>(this Controller controller, string viewName, TModel model, bool partial = false)
{
if (string.IsNullOrEmpty(viewName))
{
viewName = controller.ControllerContext.ActionDescriptor.ActionName;
}

controller.ViewData.Model = model;

using (var writer = new StringWriter())
{
IViewEngine viewEngine = controller.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
ViewEngineResult viewResult = viewEngine.FindView(controller.ControllerContext, viewName, !partial);

if (viewResult.Success == false)
{
return $"A view with the name {viewName} could not be found";
}

ViewContext viewContext = new ViewContext(
controller.ControllerContext,
viewResult.View,
controller.ViewData,
controller.TempData,
writer,
new HtmlHelperOptions()
);

await viewResult.View.RenderAsync(viewContext);

return writer.GetStringBuilder().ToString();
}
}
}
}
}
2 changes: 2 additions & 0 deletions PavlovRconWebserver/Extensions/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public static string CutLineAfter(this string fmt, char tag)
fmt = fmt.Substring(0, index); // This will remove all text after character ?
return fmt;
}


}
}
6 changes: 4 additions & 2 deletions PavlovRconWebserver/Models/PavlovServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ public class PavlovServer
public DateTime? AutoBalanceLast { get; set; } = null;
[DisplayName("AB cooldown (min)")]
public int AutoBalanceCooldown { get; set; } = 15;
[DisplayName("Save Stats(WIP)**")]
[DisplayName("Save Stats**")]
[CanBeNull]
public bool SaveStats { get; set; } = false;
[DisplayName("Shack (WIP)")]
[DisplayName("Shack")]
[CanBeNull]
public bool Shack { get; set; } = false;
[DisplayName("Use Global Bans")]
public bool GlobalBan { get; set; } = false;



Expand Down
Loading

0 comments on commit e7fb128

Please sign in to comment.