From ed373453170a790343494791b1dff4d332dcd515 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Mon, 10 Dec 2018 14:39:38 +0200 Subject: [PATCH] Added unban & unbanip commands (see #868) --- .../Source/Networking/GameClient.cs | 10 +++++ .../Data/permissionpresets.xml | 2 + .../BarotraumaShared/Source/DebugConsole.cs | 31 ++++++++++++++ .../Source/Networking/BanList.cs | 40 +++++++++++++++++++ .../Source/Networking/ClientPermissions.cs | 12 +++--- .../Source/Networking/GameServer.cs | 21 +++++++++- .../Source/Networking/NetworkMember.cs | 2 + 7 files changed, 112 insertions(+), 6 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs b/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs index 8f769d797..8f2d15a30 100644 --- a/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs +++ b/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs @@ -1522,6 +1522,16 @@ namespace Barotrauma.Networking client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered); } + public override void UnbanPlayer(string playerName, string playerIP) + { + NetOutgoingMessage msg = client.CreateMessage(); + msg.Write((byte)ClientPacketHeader.SERVER_COMMAND); + msg.Write((byte)ClientPermissions.Unban); + msg.Write(string.IsNullOrEmpty(playerName) ? "" : playerName); + msg.Write(string.IsNullOrEmpty(playerIP) ? "" : playerIP); + client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered); + } + public void SendCampaignState() { MultiPlayerCampaign campaign = GameMain.GameSession.GameMode as MultiPlayerCampaign; diff --git a/Barotrauma/BarotraumaShared/Data/permissionpresets.xml b/Barotrauma/BarotraumaShared/Data/permissionpresets.xml index ab3f2b98d..68add1f41 100644 --- a/Barotrauma/BarotraumaShared/Data/permissionpresets.xml +++ b/Barotrauma/BarotraumaShared/Data/permissionpresets.xml @@ -38,6 +38,8 @@ + + diff --git a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs index 7caa8a4ef..84ab3289c 100644 --- a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs +++ b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs @@ -1079,6 +1079,37 @@ namespace Barotrauma }; })); + commands.Add(new Command("unban", "unban [name]: Unban a specific client.", (string[] args) => + { + if (GameMain.NetworkMember == null || args.Length == 0) return; + + string clientName = string.Join(" ", args); + GameMain.NetworkMember.UnbanPlayer(clientName, ""); + }, + () => + { + if (GameMain.Server == null) return null; + return new string[][] + { + GameMain.Server.BanList.BannedNames.Where(name => !string.IsNullOrEmpty(name)).ToArray() + }; + })); + + commands.Add(new Command("unbanip", "unbanip [ip]: Unban a specific IP.", (string[] args) => + { + if (GameMain.NetworkMember == null || args.Length == 0) return; + + GameMain.NetworkMember.UnbanPlayer("", args[0]); + }, + () => + { + if (GameMain.Server == null) return null; + return new string[][] + { + GameMain.Server.BanList.BannedIPs.Where(ip => !string.IsNullOrEmpty(ip)).ToArray() + }; + })); + commands.Add(new Command("banid", "banid [id]: Kick and ban the player with the specified client ID from the server.", (string[] args) => { if (GameMain.NetworkMember == null || args.Length == 0) return; diff --git a/Barotrauma/BarotraumaShared/Source/Networking/BanList.cs b/Barotrauma/BarotraumaShared/Source/Networking/BanList.cs index 397243650..d01ec993f 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/BanList.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/BanList.cs @@ -41,6 +41,16 @@ namespace Barotrauma.Networking private List bannedPlayers; + public IEnumerable BannedNames + { + get { return bannedPlayers.Select(bp => bp.Name); } + } + + public IEnumerable BannedIPs + { + get { return bannedPlayers.Select(bp => bp.IP); } + } + public BanList() { bannedPlayers = new List(); @@ -106,6 +116,36 @@ namespace Barotrauma.Networking Save(); } + public void UnbanPlayer(string name) + { + var player = bannedPlayers.Find(bp => bp.Name == name); + if (player == null) + { + DebugConsole.Log("Could not unban player \""+name+"\". Matching player not found."); + } + else + { + DebugConsole.Log("Unbanned \"" + name + "."); + bannedPlayers.Remove(player); + Save(); + } + } + + public void UnbanIP(string ip) + { + var player = bannedPlayers.Find(bp => bp.IP == ip); + if (player == null) + { + DebugConsole.Log("Could not unban IP \"" + ip + "\". Matching player not found."); + } + else + { + DebugConsole.Log("Unbanned \"" + ip + "."); + bannedPlayers.Remove(player); + Save(); + } + } + public bool IsBanned(string IP) { bannedPlayers.RemoveAll(bp => bp.ExpirationTime.HasValue && DateTime.Now > bp.ExpirationTime.Value); diff --git a/Barotrauma/BarotraumaShared/Source/Networking/ClientPermissions.cs b/Barotrauma/BarotraumaShared/Source/Networking/ClientPermissions.cs index ae4edc2a9..74afa5d2c 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/ClientPermissions.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/ClientPermissions.cs @@ -16,16 +16,18 @@ namespace Barotrauma.Networking Kick = 2, [Description("Ban")] Ban = 4, + [Description("Revoke Ban")] + Unban = 8, [Description("Select submarine")] - SelectSub = 8, + SelectSub = 16, [Description("Select game mode")] - SelectMode = 16, + SelectMode = 32, [Description("Manage campaign")] - ManageCampaign = 32, + ManageCampaign = 64, [Description("Console commands")] - ConsoleCommands = 64, + ConsoleCommands = 128, [Description("Access server log")] - ServerLog = 128 + ServerLog = 256 } class PermissionPreset diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs index e1d2b39f7..c486695d5 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs @@ -872,6 +872,12 @@ namespace Barotrauma.Networking } } break; + case ClientPermissions.Unban: + string unbannedName = inc.ReadString().ToLowerInvariant(); + string unbannedIP = inc.ReadString(); + + UnbanPlayer(unbannedIP, unbannedIP); + break; case ClientPermissions.EndRound: if (gameStarted) { @@ -1662,7 +1668,20 @@ namespace Barotrauma.Networking BanClient(client, reason, range, duration); } - + + public override void UnbanPlayer(string playerName, string playerIP) + { + playerName = playerName.ToLowerInvariant(); + if (!string.IsNullOrEmpty(playerIP)) + { + banList.UnbanIP(playerIP); + } + else if (!string.IsNullOrEmpty(playerName)) + { + banList.UnbanPlayer(playerName); + } + } + public void BanClient(Client client, string reason, bool range = false, TimeSpan? duration = null) { if (client == null) return; diff --git a/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs b/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs index aabf4f4aa..2a6b2f12f 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs @@ -216,6 +216,8 @@ namespace Barotrauma.Networking public virtual void BanPlayer(string kickedName, string reason, bool range = false, TimeSpan? duration = null) { } + public virtual void UnbanPlayer(string playerName, string playerIP) { } + public virtual void Update(float deltaTime) { #if CLIENT