From 5dc8a5f8b7295729cc350e1bca0ba96930eb9087 Mon Sep 17 00:00:00 2001 From: Regalis Date: Thu, 18 Aug 2016 21:02:03 +0300 Subject: [PATCH] Ban console command, clients with spaces in their name can be kicked/banned, fixed banlist not loading correctly if a client has commas in their name --- Subsurface/Source/DebugConsole.cs | 12 ++++- Subsurface/Source/Networking/BanList.cs | 60 ++++++++++++++----------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/Subsurface/Source/DebugConsole.cs b/Subsurface/Source/DebugConsole.cs index 1d5c52f84..a7a61d1a2 100644 --- a/Subsurface/Source/DebugConsole.cs +++ b/Subsurface/Source/DebugConsole.cs @@ -189,6 +189,9 @@ namespace Barotrauma NewMessage(" ", Color.Cyan); + + NewMessage("kick [name]: kick a player out from the server", Color.Cyan); + NewMessage("ban [name]: kick and ban the player", Color.Cyan); NewMessage("debugdraw: toggles the ''debug draw mode''", Color.Cyan); NewMessage("netstats: toggles the visibility of the network statistics panel", Color.Cyan); @@ -278,8 +281,13 @@ namespace Barotrauma HumanAIController.DisableCrewAI = false; break; case "kick": - if (GameMain.Server == null) break; - GameMain.Server.KickPlayer(commands[1]); + if (GameMain.Server == null || commands.Length < 2) break; + GameMain.Server.KickPlayer(string.Join(" ", commands.Skip(1))); + + break; + case "ban": + if (GameMain.Server == null || commands.Length < 2) break; + GameMain.Server.KickPlayer(string.Join(" ", commands.Skip(1)), true); break; case "startclient": if (commands.Length == 1) return; diff --git a/Subsurface/Source/Networking/BanList.cs b/Subsurface/Source/Networking/BanList.cs index d17591ba2..310e99084 100644 --- a/Subsurface/Source/Networking/BanList.cs +++ b/Subsurface/Source/Networking/BanList.cs @@ -7,6 +7,18 @@ using System.Text; namespace Barotrauma.Networking { + class BannedPlayer + { + public string Name; + public string IP; + + public BannedPlayer(string name, string ip) + { + this.Name = name; + this.IP = ip; + } + } + class BanList { const string SavePath = "Data/bannedplayers.txt"; @@ -31,52 +43,55 @@ namespace Barotrauma.Networking { lines = File.ReadAllLines(SavePath); } - catch (Exception e) + catch (Exception e) { - DebugConsole.ThrowError("Failed to open the list of banned players in "+SavePath, e); + DebugConsole.ThrowError("Failed to open the list of banned players in " + SavePath, e); return; } - + foreach (string line in lines) { string[] separatedLine = line.Split(','); - if (separatedLine.Length != 2) continue; + if (separatedLine.Length < 2) continue; - bannedPlayers.Add(new BannedPlayer(separatedLine[0],separatedLine[1])); + string name = String.Join(",", separatedLine.Take(separatedLine.Length - 1)); + string ip = separatedLine.Last(); + + bannedPlayers.Add(new BannedPlayer(name, ip)); } } } public void BanPlayer(string name, string ip) { - if (bannedPlayers.FirstOrDefault(bp => bp.IP == ip)!=null) return; + if (bannedPlayers.Any(bp => bp.IP == ip)) return; - bannedPlayers.Add(new BannedPlayer(name,ip)); + bannedPlayers.Add(new BannedPlayer(name, ip)); } public bool IsBanned(string IP) { - return bannedPlayers.FirstOrDefault(bp => bp.IP == IP)!=null; + return bannedPlayers.Any(bp => bp.IP == IP); } - + public GUIComponent CreateBanFrame(GUIComponent parent) { //GUIFrame banFrame = new GUIFrame(new Rectangle(0,0,0,0), null, Alignment.Center, GUI.Style, parent); //new GUITextBlock(new Rectangle(0, -10, 0, 30), "Banned IPs:", GUI.Style, Alignment.Left, Alignment.Left, banFrame, false, GUI.LargeFont); - banFrame = new GUIListBox(new Rectangle(0,0,0,0), GUI.Style, parent); + banFrame = new GUIListBox(new Rectangle(0, 0, 0, 0), GUI.Style, parent); foreach (BannedPlayer bannedPlayer in bannedPlayers) { GUITextBlock textBlock = new GUITextBlock( new Rectangle(0, 0, 0, 25), - bannedPlayer.IP+" ("+bannedPlayer.Name+")", + bannedPlayer.IP + " (" + bannedPlayer.Name + ")", GUI.Style, Alignment.Left, Alignment.Left, banFrame); textBlock.Padding = new Vector4(10.0f, 0.0f, 0.0f, 0.0f); textBlock.UserData = banFrame; - var removeButton = new GUIButton(new Rectangle(0,00,100,20), "Remove", Alignment.Right | Alignment.CenterY, GUI.Style, textBlock); + var removeButton = new GUIButton(new Rectangle(0, 00, 100, 20), "Remove", Alignment.Right | Alignment.CenterY, GUI.Style, textBlock); removeButton.UserData = bannedPlayer; removeButton.OnClicked = RemoveBan; } @@ -93,8 +108,9 @@ namespace Barotrauma.Networking BannedPlayer banned = obj as BannedPlayer; if (banned == null) return false; - bannedPlayers.Remove(banned); + GameServer.Log("Removing ban from " + banned.Name, null); + bannedPlayers.Remove(banned); if (banFrame != null) { @@ -114,6 +130,9 @@ namespace Barotrauma.Networking public void Save() { + + GameServer.Log("Saving banlist", null); + List lines = new List(); foreach (BannedPlayer banned in bannedPlayers) @@ -127,21 +146,8 @@ namespace Barotrauma.Networking } catch (Exception e) { - DebugConsole.ThrowError("Saving the list of banned players to "+SavePath+" failed", e); + DebugConsole.ThrowError("Saving the list of banned players to " + SavePath + " failed", e); } } - - } - - class BannedPlayer - { - public string Name; - public string IP; - - public BannedPlayer(string name, string ip) - { - this.Name = name; - this.IP = ip; - } } }