From 1caf69998d7955c007cfad4647a3c5013b6ca223 Mon Sep 17 00:00:00 2001 From: high Date: Sun, 5 Jun 2011 19:05:38 -0400 Subject: [PATCH] Replaced the 3 ban files with a simple ban manager which stores ip, name and reason. --- TShockAPI/BanManager.cs | 121 ++++++++++++++++++ TShockAPI/Commands.cs | 12 +- TShockAPI/FileTools.cs | 268 ++++++++++++++-------------------------- TShockAPI/TShock.cs | 74 +++++------ TShockAPI/Tools.cs | 52 ++------ 5 files changed, 262 insertions(+), 265 deletions(-) create mode 100644 TShockAPI/BanManager.cs diff --git a/TShockAPI/BanManager.cs b/TShockAPI/BanManager.cs new file mode 100644 index 00000000..dac419f4 --- /dev/null +++ b/TShockAPI/BanManager.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace TShockAPI +{ + public class BanManager + { + DateTime LastLoad; + string Path; + /// + /// IP - Name - Reason + /// + List Bans = new List(); + + public BanManager(string path) + { + Path = path; + } + + public Ban GetBanByIp(string ip) + { + EnsureChanges(); + foreach (var ban in Bans) + { + if (ban.IP.Equals(ip)) + return ban; + } + return null; + } + + public Ban GetBanByName(string name, bool casesensitive = true) + { + EnsureChanges(); + foreach (var ban in Bans) + { + if (ban.Name.Equals(name, casesensitive ? StringComparison.Ordinal : StringComparison.InvariantCultureIgnoreCase)) + return ban; + } + return null; + } + + public void AddBan(string ip, string name = "", string reason = "") + { + Bans.Add(new Ban(ip, name, reason)); + SaveBans(); + } + + /// + /// Reloads the file if it was changed + /// + public void EnsureChanges() + { + if (new FileInfo(Path).LastWriteTime > LastLoad) + LoadBans(); + } + + /// + /// Removes | from the string + /// + /// + /// + public string MakeSafe(string str) + { + return str.Replace("|", " "); + } + + public void LoadBans() + { + if (!File.Exists(Path)) + return; + + Bans.Clear(); + LastLoad = new FileInfo(Path).LastWriteTime; + + foreach (var line in File.ReadAllLines(Path)) + { + var bansp = line.Split('|'); + if (bansp.Length != 3) + continue; + Bans.Add(new Ban(bansp[0], bansp[1], bansp[2])); + } + } + + public void SaveBans() + { + var output = new StringBuilder(); + foreach (var ban in Bans) + { + output.AppendFormat("{0}|{1}|{2}\r\n", MakeSafe(ban.IP), MakeSafe(ban.Name), MakeSafe(ban.Reason)); + } + + File.WriteAllText(Path, output.ToString()); + } + } + + public class Ban + { + public string IP { get; set; } + + public string Name { get; set; } + + public string Reason { get; set; } + + public Ban(string ip, string name, string reason) + { + IP = ip; + Name = name; + Reason = reason; + } + + public Ban() + { + IP = string.Empty; + Name = string.Empty; + Reason = string.Empty; + } + } +} \ No newline at end of file diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index d25efdda..94bcf89c 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -171,23 +171,23 @@ namespace TShockAPI public static void Ban(CommandArgs args) { string plStr = args.Message.Remove(0, 4).Trim(); - int ply = args.PlayerID; + int adminplr = args.PlayerID; int player = Tools.FindPlayer(plStr); if (!(player == -1 || player == -2 || plStr == "")) { if (!TShock.players[Tools.FindPlayer(plStr)].group.HasPermission("immunetoban")) { - FileTools.WriteBan(player); + TShock.Bans.AddBan(Tools.GetPlayerIP(player), Main.player[player].name); Tools.Kick(player, "You were banned."); - Tools.Broadcast(Tools.FindPlayer(ply) + " banned " + Tools.FindPlayer(player) + "!"); + Tools.Broadcast(Tools.FindPlayer(adminplr) + " banned " + Tools.FindPlayer(player) + "!"); } else - Tools.SendMessage(ply, "You can't ban another admin!", new float[] { 255f, 0f, 0f }); + Tools.SendMessage(adminplr, "You can't ban another admin!", new float[] { 255f, 0f, 0f }); } else if (Tools.FindPlayer(plStr) == -2) - Tools.SendMessage(ply, "More than one player matched!", new float[] { 255f, 0f, 0f }); + Tools.SendMessage(adminplr, "More than one player matched!", new float[] { 255f, 0f, 0f }); else - Tools.SendMessage(ply, "Invalid player!", new float[] { 255f, 0f, 0f }); + Tools.SendMessage(adminplr, "Invalid player!", new float[] { 255f, 0f, 0f }); } public static void Off(CommandArgs args) diff --git a/TShockAPI/FileTools.cs b/TShockAPI/FileTools.cs index c7c5fd1a..ef1c61a0 100644 --- a/TShockAPI/FileTools.cs +++ b/TShockAPI/FileTools.cs @@ -1,174 +1,94 @@ -using System; -using System.IO; -using Terraria; -using System.Web; -using System.Runtime.InteropServices; - -namespace TShockAPI -{ - class FileTools - { - public static string SaveDir = "./tshock/"; - public static void CreateFile(string file) - { - using (FileStream fs = File.Create(file)) { } - } - /// - /// Adds a 'cheater' to cheaters.txt - /// - /// You should know what this does by now. - public static void WriteCheater(int ply) - { - string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); - string cheaters = ""; - TextReader tr = new StreamReader(SaveDir + "cheaters.txt"); - cheaters = tr.ReadToEnd(); - tr.Close(); - if (cheaters.Contains(Main.player[ply].name) && cheaters.Contains(ip)) { return; } - TextWriter sw = new StreamWriter(SaveDir + "cheaters.txt", true); - sw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]"); - sw.Close(); - } - /// - /// Writes a 'banned idiot' to the ban list - /// - /// - public static void WriteBan(int ply) - { - string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); - TextWriter tw = new StreamWriter(SaveDir + "bans.txt", true); - tw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]"); - tw.Close(); - } - /// - /// Writes a tnt user to grief.txt - /// - /// int player - public static void WriteGrief(int ply) - { - TextWriter tw = new StreamWriter(SaveDir + "grief.txt", true); - tw.WriteLine("[" + Main.player[ply].name + "] [" + Tools.GetRealIP(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint.ToString()) + "]"); - tw.Close(); - } - - /// - /// Writes an error message to errors.txt - /// - /// string message - public static void WriteError(string err) - { - if (System.IO.File.Exists(SaveDir + "errors.txt")) - { - TextWriter tw = new StreamWriter(SaveDir + "errors.txt", true); - tw.WriteLine(err); - tw.Close(); - } - else - { - FileTools.CreateFile(SaveDir + "errors.txt"); - TextWriter tw = new StreamWriter(SaveDir + "errors.txt", true); - tw.WriteLine(err); - tw.Close(); - } - } - - /// - /// Sets up the configuration file for all variables, and creates any missing files. - /// - public static void SetupConfig() - { - if (!System.IO.Directory.Exists(SaveDir)) { System.IO.Directory.CreateDirectory(SaveDir); } - if (!System.IO.File.Exists(SaveDir + "motd.txt")) - { - FileTools.CreateFile(SaveDir + "motd.txt"); - TextWriter tw = new StreamWriter(SaveDir + "motd.txt"); - tw.WriteLine("This server is running TShock. Type /help for a list of commands."); - tw.WriteLine("%255,000,000%Current map: %map%"); - tw.WriteLine("Current players: %players%"); - tw.Close(); - } - if (!System.IO.File.Exists(SaveDir + "bans.txt")) { FileTools.CreateFile(SaveDir + "bans.txt"); } - if (!System.IO.File.Exists(SaveDir + "cheaters.txt")) { FileTools.CreateFile(SaveDir + "cheaters.txt"); } - if (!System.IO.File.Exists(SaveDir + "grief.txt")) { FileTools.CreateFile(SaveDir + "grief.txt"); } - if (!System.IO.File.Exists(SaveDir + "whitelist.txt")) { FileTools.CreateFile(SaveDir + "whitelist.txt"); } - if (!System.IO.File.Exists(SaveDir + "groups.txt")) - { - FileTools.CreateFile(SaveDir + "groups.txt"); - StreamWriter sw = new StreamWriter(SaveDir + "groups.txt"); - sw.Write(Resources.groups); - sw.Close(); - } - if (!System.IO.File.Exists(SaveDir + "users.txt")) - { - FileTools.CreateFile(SaveDir + "users.txt"); - StreamWriter sw = new StreamWriter(SaveDir + "users.txt"); - sw.Write(Resources.users); - sw.Close(); - } - ConfigurationManager.WriteJsonConfiguration(); - ConfigurationManager.ReadJsonConfiguration(); - Netplay.serverPort = ConfigurationManager.serverPort; - } - /// - /// Checks if a user is banned - /// - /// string ip - /// true/false - public static bool CheckBanned(String p) - { - String ip = p.Split(':')[0]; - TextReader tr = new StreamReader(SaveDir + "bans.txt"); - string banlist = tr.ReadToEnd(); - tr.Close(); - banlist = banlist.Trim(); - if (banlist.Contains(ip)) - return true; - return false; - } - /// - /// Tells if a user is on the whitelist - /// - /// string ip of the user - /// true/false - public static bool OnWhitelist(string ip) - { - if (!ConfigurationManager.enableWhitelist) { return true; } - if (!System.IO.File.Exists(SaveDir + "whitelist.txt")) { FileTools.CreateFile(SaveDir + "whitelist.txt"); TextWriter tw = new StreamWriter(SaveDir + "whitelist.txt"); tw.WriteLine("127.0.0.1"); tw.Close(); } - TextReader tr = new StreamReader(SaveDir + "whitelist.txt"); - string whitelist = tr.ReadToEnd(); - ip = Tools.GetRealIP(ip); - return whitelist.Contains(ip); - } - /// - /// Tells if the user is on grief.txt - /// - /// - /// - public static bool Checkgrief(String ip) - { - ip = Tools.GetRealIP(ip); - if (!ConfigurationManager.banTnt) { return false; } - TextReader tr = new StreamReader(SaveDir + "grief.txt"); - string list = tr.ReadToEnd(); - tr.Close(); - - return list.Contains(ip); - } - - public static bool CheckCheat(String ip) - { - ip = Tools.GetRealIP(ip); - if (!ConfigurationManager.banCheater) { return false; } - TextReader tr = new StreamReader(SaveDir + "cheaters.txt"); - string trr = tr.ReadToEnd(); - tr.Close(); - if (trr.Contains(ip)) - { - return true; - } - return false; - } - - public FileTools() { } - } -} +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Web; +using Terraria; + +namespace TShockAPI +{ + class FileTools + { + public static string SaveDir = "./tshock/"; + + public static void CreateFile(string file) + { + using (FileStream fs = File.Create(file)) { } + } + + /// + /// Writes an error message to errors.txt + /// + /// string message + public static void WriteError(string err) + { + if (System.IO.File.Exists(SaveDir + "errors.txt")) + { + TextWriter tw = new StreamWriter(SaveDir + "errors.txt", true); + tw.WriteLine(err); + tw.Close(); + } + else + { + FileTools.CreateFile(SaveDir + "errors.txt"); + TextWriter tw = new StreamWriter(SaveDir + "errors.txt", true); + tw.WriteLine(err); + tw.Close(); + } + } + + /// + /// Sets up the configuration file for all variables, and creates any missing files. + /// + public static void SetupConfig() + { + if (!System.IO.Directory.Exists(SaveDir)) { System.IO.Directory.CreateDirectory(SaveDir); } + if (!System.IO.File.Exists(SaveDir + "motd.txt")) + { + FileTools.CreateFile(SaveDir + "motd.txt"); + TextWriter tw = new StreamWriter(SaveDir + "motd.txt"); + tw.WriteLine("This server is running TShock. Type /help for a list of commands."); + tw.WriteLine("%255,000,000%Current map: %map%"); + tw.WriteLine("Current players: %players%"); + tw.Close(); + } + if (!System.IO.File.Exists(SaveDir + "bans.txt")) { FileTools.CreateFile(SaveDir + "bans.txt"); } + if (!System.IO.File.Exists(SaveDir + "cheaters.txt")) { FileTools.CreateFile(SaveDir + "cheaters.txt"); } + if (!System.IO.File.Exists(SaveDir + "grief.txt")) { FileTools.CreateFile(SaveDir + "grief.txt"); } + if (!System.IO.File.Exists(SaveDir + "whitelist.txt")) { FileTools.CreateFile(SaveDir + "whitelist.txt"); } + if (!System.IO.File.Exists(SaveDir + "groups.txt")) + { + FileTools.CreateFile(SaveDir + "groups.txt"); + StreamWriter sw = new StreamWriter(SaveDir + "groups.txt"); + sw.Write(Resources.groups); + sw.Close(); + } + if (!System.IO.File.Exists(SaveDir + "users.txt")) + { + FileTools.CreateFile(SaveDir + "users.txt"); + StreamWriter sw = new StreamWriter(SaveDir + "users.txt"); + sw.Write(Resources.users); + sw.Close(); + } + ConfigurationManager.WriteJsonConfiguration(); + ConfigurationManager.ReadJsonConfiguration(); + Netplay.serverPort = ConfigurationManager.serverPort; + } + + /// + /// Tells if a user is on the whitelist + /// + /// string ip of the user + /// true/false + public static bool OnWhitelist(string ip) + { + if (!ConfigurationManager.enableWhitelist) { return true; } + if (!System.IO.File.Exists(SaveDir + "whitelist.txt")) { FileTools.CreateFile(SaveDir + "whitelist.txt"); TextWriter tw = new StreamWriter(SaveDir + "whitelist.txt"); tw.WriteLine("127.0.0.1"); tw.Close(); } + TextReader tr = new StreamReader(SaveDir + "whitelist.txt"); + string whitelist = tr.ReadToEnd(); + ip = Tools.GetRealIP(ip); + return whitelist.Contains(ip); + } + + public FileTools() { } + } +} \ No newline at end of file diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 36949739..1211ff5d 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -23,6 +23,8 @@ namespace TShockAPI static bool[] BlacklistTiles; + public static BanManager Bans = new BanManager(Path.Combine(saveDir, "bans.txt")); + public override Version Version { get { return VersionNum; } @@ -127,6 +129,8 @@ namespace TShockAPI AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + Bans.LoadBans(); + Log.Info("Hooks initialized"); Commands.InitCommands(); Log.Info("Commands initialized"); @@ -134,10 +138,14 @@ namespace TShockAPI void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { - if (Main.worldPathName != null) + if (e.IsTerminating) { - Main.worldPathName += ".crash"; - WorldGen.saveWorld(); + if (Main.worldPathName != null) + { + Main.worldPathName += ".crash"; + WorldGen.saveWorld(); + } + DeInitialize(); } Log.Error(e.ExceptionObject.ToString()); } @@ -148,6 +156,8 @@ namespace TShockAPI public override void DeInitialize() { + Bans.SaveBans(); + GameHooks.OnPreInitialize -= OnPreInit; GameHooks.OnPostInitialize -= OnPostInit; GameHooks.OnUpdate -= new Action(OnUpdate); @@ -258,7 +268,7 @@ namespace TShockAPI if (players[ply].syncHP) { if (maxLife > Main.player[ply].statLifeMax + 20 || life > maxLife) - Tools.HandleCheater(ply); + TShock.Ban(ply, "Cheater"); } else { @@ -279,7 +289,7 @@ namespace TShockAPI { if (players[ply].syncMP) { - Tools.HandleCheater(ply); + TShock.Ban(ply, "Cheater"); Log.Info(Tools.FindPlayer(ply) + " had increased max mana by more than 20 or increased mana more than max"); } @@ -300,7 +310,7 @@ namespace TShockAPI { //fuck you faggot Log.Info(Tools.FindPlayer(e.Msg.whoAmI) + " was kicked for trying to fake chat as someone else."); - Tools.HandleCheater(ply); + TShock.Ban(ply, "Faking Chat"); } } } @@ -326,7 +336,7 @@ namespace TShockAPI { int i = e.Msg.whoAmI; if (ConfigurationManager.banBoom) - FileTools.WriteGrief((int)i); + TShock.Ban(i, "Explosives"); Tools.Kick((int)i, "Explosives were thrown."); Tools.Broadcast(Main.player[i].name + " was " + (ConfigurationManager.banBoom ? "banned" : "kicked") + @@ -348,7 +358,7 @@ namespace TShockAPI if (id != e.Msg.whoAmI) { - Tools.HandleGriefer(e.Msg.whoAmI); + TShock.Ban(e.Msg.whoAmI, "Griefer"); Log.Info(Tools.FindPlayer(e.Msg.whoAmI) + " was kicked for trying to execute KillMe on someone else."); e.Handled = true; @@ -391,7 +401,7 @@ namespace TShockAPI Tools.ShowMOTD(who); if (Main.player[plr].statLifeMax > 400 || Main.player[plr].statManaMax > 200 || Main.player[plr].statLife > 400 || Main.player[plr].statMana > 200 || CheckInventory(plr)) { - Tools.HandleCheater(plr); + TShock.Ban(plr, "Cheater"); } if (ConfigurationManager.permaPvp) { @@ -444,34 +454,23 @@ namespace TShockAPI { if (Main.netMode != 2) { return; } - int count = 1; - for (int i = 0; i < Main.player.Length; i++) - count += Main.player[i].active ? 1 : 0; - - if (count > ConfigurationManager.maxSlots) + if (Tools.activePlayers() + 1 > ConfigurationManager.maxSlots) { Tools.Kick(ply, "Server is full"); return; } - string ip = Tools.GetRealIP((Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint))); - if (FileTools.CheckBanned(ip)) + string ip = Tools.GetRealIP(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint.ToString()); + var ban = Bans.GetBanByIp(ip); + if (ban != null) { - Tools.Kick(ply, "You are banned."); + Tools.Kick(ply, "You are banned: " + ban.Reason); } else if (Tools.FindPlayer(ply).Length > 32) { Tools.Kick(ply, "Your name was too long."); Tools.Broadcast(ip + " was kicked because their name exceeded 32 characters."); } - else if (FileTools.CheckCheat(ip)) - { - Tools.Kick(ply, "You were flagged for cheating."); - } - else if (FileTools.Checkgrief(ip)) - { - Tools.Kick(ply, "You were flagged for griefing."); - } if (!FileTools.OnWhitelist(ip)) { Tools.Kick(ply, "Not on whitelist."); @@ -513,7 +512,7 @@ namespace TShockAPI if (ConfigurationManager.kickTnt || ConfigurationManager.banTnt) { if (ConfigurationManager.banTnt) - FileTools.WriteGrief((int)i); + TShock.Ban((int)i, "Explosives"); Tools.Kick((int)i, "Kill tile abuse detected."); Tools.Broadcast(Main.player[i].name + " was " + (ConfigurationManager.banTnt ? "banned" : "kicked") + " for kill tile abuse."); RevertKillTile((int)i); @@ -570,12 +569,6 @@ namespace TShockAPI public static void Teleport(int ply, int x, int y) { - /*Main.player[ply].velocity = new Vector2(0, 0); - NetMessage.SendData(0x0d, -1, -1, "", ply); - Main.player[ply].position.X = x; - Main.player[ply].position.Y = y - 0x2a; - NetMessage.SendData(0x0d, -1, -1, "", ply); - UpdatePlayers();*/ Main.player[ply].position.X = (float)x; Main.player[ply].position.Y = (float)y; NetMessage.SendData(0x0d, -1, ply, "", ply); @@ -585,18 +578,6 @@ namespace TShockAPI public static void Teleport(int ply, float x, float y) { - /*Main.player[ply].position.X = x; - Main.player[ply].position.Y = y - 0x2a; - NetMessage.SendData(0x14, -1, -1, "", 10, x, y); - NetMessage.SendData(0x0d, -1, -1, "", ply); - int oldx = Main.player[ply].SpawnX; - int oldy = Main.player[ply].SpawnY; - Main.player[ply].SpawnX = (int)(x / 16); - Main.player[ply].SpawnY = (int)((y - 0x2a) / 16); - NetMessage.SendData(0xC, -1, -1, "", ply); - Main.player[ply].SpawnX = oldx; - Main.player[ply].SpawnY = oldy; - UpdatePlayers();*/ Main.player[ply].position.X = x; Main.player[ply].position.Y = y; NetMessage.SendData(0x0d, -1, ply, "", ply); @@ -747,6 +728,11 @@ namespace TShockAPI return true; } + public static void Ban(int plr, string reason = "") + { + Bans.AddBan(Tools.GetPlayerIP(plr), Main.player[plr].name, reason); + } + public class Position { public float X; diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs index 419f5b42..2fd0159c 100644 --- a/TShockAPI/Tools.cs +++ b/TShockAPI/Tools.cs @@ -1,7 +1,7 @@ using System; +using System.Collections.Generic; using System.IO; using Terraria; -using System.Collections.Generic; namespace TShockAPI { @@ -19,6 +19,16 @@ namespace TShockAPI return mess.Split(':')[0]; } + /// + /// Gets the IP from a player index + /// + /// Player Index + /// IP + public static string GetPlayerIP(int plr) + { + return GetRealIP(Netplay.serverSock[plr].tcpClient.Client.RemoteEndPoint.ToString()); + } + /// /// Used for some places where a list of players might be used. /// @@ -201,46 +211,6 @@ namespace TShockAPI Log.Info("Kicked " + Tools.FindPlayer(ply) + " for : " + reason); } - /// - /// Adds someone to cheaters.txt - /// - /// int player - public static void HandleCheater(int ply) - { - if (!TShock.players[ply].group.HasPermission("ignorecheatdetection")) - { - string cheater = Tools.FindPlayer(ply); - string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); - - FileTools.WriteCheater(ply); - if (!ConfigurationManager.kickCheater) { return; } - Netplay.serverSock[ply].kill = true; - Netplay.serverSock[ply].Reset(); - NetMessage.syncPlayers(); - Tools.Broadcast(cheater + " was " + (ConfigurationManager.banCheater ? "banned " : "kicked ") + "for cheating."); - } - } - - /// - /// Adds someone to griefers.txt - /// - /// int player - public static void HandleGriefer(int ply) - { - if (!TShock.players[ply].group.HasPermission("ignoregriefdetection")) - { - string cheater = Tools.FindPlayer(ply); - string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); - - FileTools.WriteGrief(ply); - if (!ConfigurationManager.kickGriefer) { return; } - Netplay.serverSock[ply].kill = true; - Netplay.serverSock[ply].Reset(); - NetMessage.syncPlayers(); - Tools.Broadcast(cheater + " was " + (ConfigurationManager.banGriefer ? "banned " : "kicked ") + "for griefing."); - } - } - /// /// Shows a MOTD to the player ///