Merge pull request #532 from Olink/general-devel
Ban system is more robust; supports expirations
This commit is contained in:
commit
cc3d2e647e
7 changed files with 70 additions and 26 deletions
|
|
@ -562,7 +562,7 @@ namespace TShockAPI
|
|||
|
||||
user.Group = TShock.Config.DefaultRegistrationGroupName; // FIXME -- we should get this from the DB. --Why?
|
||||
|
||||
if (TShock.Users.GetUserByName(user.Name) == null) // Cheap way of checking for existance of a user
|
||||
if (TShock.Users.GetUserByName(user.Name) == null && user.Name != TSServerPlayer.AccountName) // Cheap way of checking for existance of a user
|
||||
{
|
||||
args.Player.SendSuccessMessage("Account " + user.Name + " has been registered.");
|
||||
args.Player.SendSuccessMessage("Your password is " + user.Password);
|
||||
|
|
@ -979,7 +979,7 @@ namespace TShockAPI
|
|||
string reason = args.Parameters.Count > 2
|
||||
? String.Join(" ", args.Parameters.GetRange(2, args.Parameters.Count - 2))
|
||||
: "Misbehavior.";
|
||||
if (!TShock.Utils.Ban(players[0], reason, !args.Player.RealPlayer, args.Player.Name))
|
||||
if (!TShock.Utils.Ban(players[0], reason, !args.Player.RealPlayer, args.Player.UserAccountName))
|
||||
{
|
||||
args.Player.SendErrorMessage("You can't ban another admin!");
|
||||
}
|
||||
|
|
@ -994,7 +994,7 @@ namespace TShockAPI
|
|||
string reason = args.Parameters.Count > 2
|
||||
? String.Join(" ", args.Parameters.GetRange(2, args.Parameters.Count - 2))
|
||||
: "Manually added IP address ban.";
|
||||
TShock.Bans.AddBan(ip, "", reason);
|
||||
TShock.Bans.AddBan(ip, "", reason, false, args.Player.UserAccountName);
|
||||
args.Player.SendSuccessMessage(ip + " banned.");
|
||||
return;
|
||||
#endregion Add ip ban
|
||||
|
|
|
|||
|
|
@ -34,7 +34,10 @@ namespace TShockAPI.DB
|
|||
var table = new SqlTable("Bans",
|
||||
new SqlColumn("IP", MySqlDbType.String, 16) {Primary = true},
|
||||
new SqlColumn("Name", MySqlDbType.Text),
|
||||
new SqlColumn("Reason", MySqlDbType.Text)
|
||||
new SqlColumn("Reason", MySqlDbType.Text),
|
||||
new SqlColumn("BanningUser", MySqlDbType.Text),
|
||||
new SqlColumn("Date", MySqlDbType.Text),
|
||||
new SqlColumn("Expiration", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db,
|
||||
db.GetSqlType() == SqlType.Sqlite
|
||||
|
|
@ -58,7 +61,7 @@ namespace TShockAPI.DB
|
|||
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE IP=@0", ip))
|
||||
{
|
||||
if (reader.Read())
|
||||
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"));
|
||||
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"), reader.Get<string>("BanningUser"), reader.Get<string>("Date"), reader.Get<string>("Expiration"));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -77,7 +80,7 @@ namespace TShockAPI.DB
|
|||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
banlist.Add(new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason")));
|
||||
banlist.Add(new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"), reader.Get<string>("BanningUser"), reader.Get<string>("Date"), reader.Get<string>("Expiration")));
|
||||
}
|
||||
return banlist;
|
||||
}
|
||||
|
|
@ -100,7 +103,7 @@ namespace TShockAPI.DB
|
|||
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE " + namecol + "=@0", name))
|
||||
{
|
||||
if (reader.Read())
|
||||
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"));
|
||||
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"), reader.Get<string>("BanningUser"), reader.Get<string>("Date"), reader.Get<string>("Expiration"));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -114,14 +117,14 @@ namespace TShockAPI.DB
|
|||
[Obsolete("This method is for signature compatibility for external code only")]
|
||||
public bool AddBan(string ip, string name, string reason)
|
||||
{
|
||||
return AddBan(ip, name, reason, false);
|
||||
return AddBan(ip, name, reason, false, "", "");
|
||||
}
|
||||
#endif
|
||||
public bool AddBan(string ip, string name = "", string reason = "", bool exceptions = false)
|
||||
public bool AddBan(string ip, string name = "", string reason = "", bool exceptions = false, string banner = "", string expiration = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
return database.Query("INSERT INTO Bans (IP, Name, Reason) VALUES (@0, @1, @2);", ip, name, reason) != 0;
|
||||
return database.Query("INSERT INTO Bans (IP, Name, Reason, BanningUser, Date, Expiration) VALUES (@0, @1, @2, @3, @4, @5);", ip, name, reason, banner, DateTime.Now.ToString("G"), expiration) != 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -180,11 +183,20 @@ namespace TShockAPI.DB
|
|||
|
||||
public string Reason { get; set; }
|
||||
|
||||
public Ban(string ip, string name, string reason)
|
||||
public string BanningUser { get; set; }
|
||||
|
||||
public string Date { get; set; }
|
||||
|
||||
public string Expiration { get; set; }
|
||||
|
||||
public Ban(string ip, string name, string reason, string banner, string date, string exp)
|
||||
{
|
||||
IP = ip;
|
||||
Name = name;
|
||||
Reason = reason;
|
||||
BanningUser = banner;
|
||||
Date = date;
|
||||
Expiration = exp;
|
||||
}
|
||||
|
||||
public Ban()
|
||||
|
|
@ -192,6 +204,9 @@ namespace TShockAPI.DB
|
|||
IP = string.Empty;
|
||||
Name = string.Empty;
|
||||
Reason = string.Empty;
|
||||
BanningUser = "";
|
||||
Date = "";
|
||||
Expiration = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1306,12 +1306,6 @@ namespace TShockAPI
|
|||
TShock.Utils.ForceKick(args.Player, "Empty Name.", true);
|
||||
return true;
|
||||
}
|
||||
var ban = TShock.Bans.GetBanByName(name);
|
||||
if (ban != null)
|
||||
{
|
||||
TShock.Utils.ForceKick(args.Player, string.Format("You are banned: {0}", ban.Reason), true);
|
||||
return true;
|
||||
}
|
||||
if (args.Player.ReceivedInfo)
|
||||
{
|
||||
return true;
|
||||
|
|
@ -2446,7 +2440,7 @@ namespace TShockAPI
|
|||
{
|
||||
if (TShock.Config.BanOnMediumcoreDeath)
|
||||
{
|
||||
if (!TShock.Utils.Ban(args.Player, TShock.Config.MediumcoreBanReason))
|
||||
if (!TShock.Utils.Ban(args.Player, TShock.Config.MediumcoreBanReason, false, "mediumcore-death"))
|
||||
TShock.Utils.ForceKick(args.Player, "Death results in a ban, but can't ban you.", true);
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -404,7 +404,7 @@ namespace TShockAPI
|
|||
|
||||
try
|
||||
{
|
||||
TShock.Bans.AddBan(ip, name, parameters["reason"], true);
|
||||
TShock.Bans.AddBan(ip, name, parameters["reason"], true, tokenData.Username);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -813,10 +813,12 @@ namespace TShockAPI
|
|||
|
||||
public class TSServerPlayer : TSPlayer
|
||||
{
|
||||
public static string AccountName = "ServerConsole";
|
||||
public TSServerPlayer()
|
||||
: base("Server")
|
||||
{
|
||||
Group = new SuperAdminGroup();
|
||||
UserAccountName = AccountName;
|
||||
}
|
||||
|
||||
public override void SendErrorMessage(string msg)
|
||||
|
|
|
|||
|
|
@ -785,9 +785,14 @@ namespace TShockAPI
|
|||
|
||||
if (ban != null)
|
||||
{
|
||||
Utils.ForceKick(player, string.Format("You are banned: {0}", ban.Reason), true, false);
|
||||
handler.Handled = true;
|
||||
return;
|
||||
if (!Utils.HasBanExpired(ban))
|
||||
{
|
||||
DateTime exp;
|
||||
string duration = DateTime.TryParse(ban.Expiration, out exp) ? String.Format("until {0}", exp.ToString("G")) : "forever";
|
||||
Utils.ForceKick(player, string.Format("You are banned {0}: {1}", duration, ban.Reason), true, false);
|
||||
handler.Handled = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!FileTools.OnWhitelist(player.IP))
|
||||
|
|
@ -838,9 +843,13 @@ namespace TShockAPI
|
|||
|
||||
if (ban != null)
|
||||
{
|
||||
Utils.ForceKick(player, string.Format("You are banned: {0}", ban.Reason), true, false);
|
||||
handler.Handled = true;
|
||||
return;
|
||||
if (!Utils.HasBanExpired(ban))
|
||||
{
|
||||
DateTime exp;
|
||||
string duration = DateTime.TryParse(ban.Expiration, out exp) ? String.Format("until {0}", exp.ToString("G")) : "forever";
|
||||
Utils.ForceKick(player, string.Format("You are banned {0}: {1}", duration, ban.Reason), true, false);
|
||||
handler.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ using System.Net.Sockets;
|
|||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Terraria;
|
||||
using TShockAPI.DB;
|
||||
|
||||
namespace TShockAPI
|
||||
{
|
||||
|
|
@ -673,7 +674,7 @@ namespace TShockAPI
|
|||
{
|
||||
string ip = player.IP;
|
||||
string playerName = player.Name;
|
||||
TShock.Bans.AddBan(ip, playerName, reason);
|
||||
TShock.Bans.AddBan(ip, playerName, reason, false, adminUserName);
|
||||
player.Disconnect(string.Format("Banned: {0}", reason));
|
||||
Log.ConsoleInfo(string.Format("Banned {0} for : {1}", playerName, reason));
|
||||
string verb = force ? "force " : "";
|
||||
|
|
@ -686,6 +687,29 @@ namespace TShockAPI
|
|||
return false;
|
||||
}
|
||||
|
||||
public bool HasBanExpired(Ban ban, bool byName = false)
|
||||
{
|
||||
DateTime exp;
|
||||
bool expirationExists = DateTime.TryParse(ban.Expiration, out exp);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(ban.Expiration) && (expirationExists) &&
|
||||
(DateTime.Now >= exp))
|
||||
{
|
||||
if (byName)
|
||||
{
|
||||
TShock.Bans.RemoveBan(ban.Name, true, true, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
TShock.Bans.RemoveBan(ban.IP, false, false, false);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows a file to the user.
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue