Skip to content

Commit

Permalink
Update Rcon to Use the new Admin system.
Browse files Browse the repository at this point in the history
  • Loading branch information
LordFetznschaedl committed Nov 13, 2023
1 parent de26b40 commit be0cef2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 145 deletions.
150 changes: 11 additions & 139 deletions CS2Rcon/CS2Rcon.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,36 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Utils;

namespace CS2Rcon
{
[MinimumApiVersion(28)]
public class CS2Rcon : BasePlugin
{
public override string ModuleName => "CS2Rcon";
public override string ModuleVersion => "1.1.2";
public override string ModuleVersion => "1.2.0";
public override string ModuleAuthor => "LordFetznschaedl";
public override string ModuleDescription => "Allows for server commands to be executed from the client";


private List<ulong> _rconAccessList = new List<ulong>();
private FileSystemWatcher? _fileWatcher = null;
private DateTime _fileWatcherLastFileWriteTime = DateTime.MinValue;
private string _rconAccessPath = string.Empty;
private string RCON_NO_ACCESS = $"{ChatColors.Red} YOU DONT HAVE PERMISSION TO USE THIS COMMAND!";
public override string ModuleDescription => "Allows for server commands to be executed from the client using !rcon";

public override void Load(bool hotReload)
{
this.Log(PluginInfo());
this.Log(this.ModuleDescription);

this._rconAccessPath = Path.Join(this.ModuleDirectory, "rconAllow.cfg");

this.ReloadAccessSteamIds();
this.SetupRconAccessFileWatcher();
}

public override void Unload(bool hotReload)
{
if(this._fileWatcher != null)
{
this._fileWatcher.Dispose();
}

base.Unload(hotReload);
}

[ConsoleCommand("css_rcon", "Rcon as we know it!")]
[ConsoleCommand("css_rcon", "Chat rcon as we know it!")]
[RequiresPermissions("@css/rcon")]
public void OnRcon(CCSPlayerController? player, CommandInfo command)
{
if (player == null)
{
this.Log("Command has been called by the server.");
}

if(player != null && !this._rconAccessList.Contains(player.SteamID))
{
this.Log($"{player.PlayerName:Server} [{player.SteamID}] is trying to access RCON but has no permission!");
player.PrintToChat(RCON_NO_ACCESS);
player.PrintToConsole(RCON_NO_ACCESS);

return;
}

if(command.ArgCount <= 0)
{
Expand All @@ -70,116 +43,13 @@ public void OnRcon(CCSPlayerController? player, CommandInfo command)
Server.ExecuteCommand(serverCommand);

this.Log($"Player {player?.PlayerName??"Server"} [{player?.SteamID??0}] executed rcon! [${serverCommand}]");
}

[ConsoleCommand("css_rconreload", "Reloads who has access to rcon!")]
public void OnRconReloadAccessSteamIds(CCSPlayerController? player, CommandInfo command)
{
if (player != null && !this._rconAccessList.Contains(player.SteamID))
{
this.Log($"{player.PlayerName} [{player.SteamID}] is trying to reload the access steam ids!");
player.PrintToChat(RCON_NO_ACCESS);
player.PrintToConsole(RCON_NO_ACCESS);

return;
}

this.ReloadAccessSteamIds();

this.Log($"Player {player?.PlayerName??"Server"} [{player?.SteamID??0}] executed reload access steam ids!");
this.PrintToPlayerOrServer("Reload was successful");
}

[ConsoleCommand("css_rconwho", "List all SteamId64 that have access to the RCON commands in the console!")]
public void OnRconWho(CCSPlayerController? player, CommandInfo command)
{
if (player != null && !this._rconAccessList.Contains(player.SteamID))
{
this.Log($"{player.PlayerName} [{player.SteamID}] is trying to access the list of allowed steam ids!");
player.PrintToChat(RCON_NO_ACCESS);
player.PrintToConsole(RCON_NO_ACCESS);
return;
}

this._rconAccessList.ForEach(steamId => this.PrintToPlayerOrServer($"SteamId64: {steamId}", player));

this.Log($"Player {player?.PlayerName??"Server"} [{player?.SteamID??0}] executed reload access steam ids!");
this.PrintToPlayerOrServer($"Command executed:{ChatColors.BlueGrey}{serverCommand}", player);
}

[ConsoleCommand("css_rconinfo", "This command prints the plugin information")]
public void OnRconInfo(CCSPlayerController? player, CommandInfo command)
{
if (player == null)
{
this.Log(PluginInfo());
return;
}

player.PrintToChat(PluginInfo());
player.PrintToConsole(PluginInfo());
}

private void ReloadAccessSteamIds()
{
this.Log($"Reloading... [{this._rconAccessPath}]");

if(this._rconAccessList.Any())
{
this._rconAccessList = new List<ulong>();
}

if (File.Exists(this._rconAccessPath))
{
var lines = File.ReadAllLines(this._rconAccessPath).ToList().Where(x => !x.StartsWith('#'));

foreach (var line in lines)
{
if (ulong.TryParse(line, out var steamId))
{
this.Log($"SteamId64: {steamId}");
this._rconAccessList.Add(steamId);
}
}

this._fileWatcherLastFileWriteTime = File.GetLastWriteTime(this._rconAccessPath);
}
}

private void SetupRconAccessFileWatcher()
{
this.Log($"Setting up FileWatcher for {this._rconAccessPath}");

if (this._fileWatcher != null)
{
this._fileWatcher.Dispose();
}

this._fileWatcher = new FileSystemWatcher()
{
Path = Path.GetDirectoryName(this._rconAccessPath) ?? this.ModulePath,
Filter = Path.GetFileName(this._rconAccessPath),
NotifyFilter = NotifyFilters.LastWrite,
EnableRaisingEvents = true,
};

this._fileWatcher.Changed += this.OnFileChanged;

}

private void OnFileChanged(object sender, FileSystemEventArgs e)
{
var lastWriteTime = File.GetLastWriteTime(this._rconAccessPath);

if (lastWriteTime.Ticks - this._fileWatcherLastFileWriteTime.Ticks < 100000)
{
return;
}
this._fileWatcherLastFileWriteTime = lastWriteTime;

this.Log($"Change detected on {e.FullPath}. Reloading rconAccess...");

this.ReloadAccessSteamIds();

this.PrintToPlayerOrServer(this.PluginInfo(), player);
}

private string PluginInfo()
Expand All @@ -189,6 +59,8 @@ private string PluginInfo()

private void PrintToPlayerOrServer(string message, CCSPlayerController? player = null)
{
message = $"[{ChatColors.Red}{this.ModuleName}{ChatColors.White}] " + message;

if(player != null)
{
player.PrintToConsole(message);
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# CS2Rcon

This is a rudimentary implementation of a RCON plugin for CS2 using CounterStrikeSharp
This is an implementation of a RCON plugin for CS2 using CounterStrikeSharp
<https://docs.cssharp.dev/>

This plugin has a rconAllow.cfg config that allows you to add the permission to use RCON by adding the users SteamId64. Each Id needs a single row.
Permission to use the RCON plugin can be granted by adding the permission `@css/rcon` to the user in the admins.cfg.

---
# usage:
!rconinfo - to print the plugin information
!rcon - to run server commands from the client
!rconreload - to reload the SteamId64 that are allowed to use the rcon command.
!rconwho - to print all SteamId64 to the console that are allowed to CS2Rcon
| Command | Parameter | Description | Permissions |
|-----------|--------------------|--------------------------------------|-------------|
| !rconinfo | | prints the plugin information | |
| !rcon | <command (string)> | runs server commands from the client | @css/rcon |

---
# installation:
Expand Down

0 comments on commit be0cef2

Please sign in to comment.