Skip to content

Commit

Permalink
Add LinkIpsToBans option
Browse files Browse the repository at this point in the history
  • Loading branch information
PintTheDragon committed Aug 3, 2022
1 parent 1b4bfb0 commit e72cd32
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
9 changes: 5 additions & 4 deletions SCPStats/API/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// -----------------------------------------------------------------------

using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Exiled.API.Features;
Expand Down Expand Up @@ -186,7 +187,7 @@ public static async Task<bool> AddWarning(Player player, string message, string
/// <returns>If the warning was added successfully.</returns>
public static async Task<bool> AddWarning(string userID, string userName, string message, string issuerID = "", string issuerName = "", bool silent = false)
{
return await AddWarningWithType(0, Helper.HandleId(userID), userName, message, issuerID, issuerName, silent);
return await AddWarningWithType(0, Helper.HandleId(userID), null, userName, message, issuerID, issuerName, silent);
}

/// <summary>
Expand All @@ -213,16 +214,16 @@ public static async Task<bool> AddNote(Player player, string message, string iss
/// <returns>If the note was added successfully.</returns>
public static async Task<bool> AddNote(string userID, string userName, string message, string issuerID = "", string issuerName = "")
{
return await AddWarningWithType(5, Helper.HandleId(userID), userName, message, issuerID, issuerName);
return await AddWarningWithType(5, Helper.HandleId(userID), null, userName, message, issuerID, issuerName);
}

internal static async Task<bool> AddWarningWithType(int type, string userID, string userName, string message, string issuerID = "", string issuerName = "", bool silent = false)
internal static async Task<bool> AddWarningWithType(int type, string userID, string userIP, string userName, string message, string issuerID = "", string issuerName = "", bool silent = false)
{
var promise = new TaskCompletionSource<bool>();

var msgId = MessageIDsStore.IncrementWarnCounter();
MessageIDsStore.WarnDict[msgId] = promise;
WebsocketHandler.SendRequest(RequestType.AddWarning, "{\"type\":\"" + type + "\",\"playerId\":\"" + userID.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\",\"message\":\"" + message.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\",\"playerName\":\"" + userName.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\",\"issuer\":\"" + Helper.HandleId(issuerID).Replace("\\", "\\\\").Replace("\"", "\\\"") + "\",\"issuerName\":\"" + issuerName.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"" + (silent ? ",\"online\":true" : "") + ",\"res\":" + msgId + "}");
WebsocketHandler.SendRequest(RequestType.AddWarning, "{\"type\":\"" + type + "\",\"playerId\":\"" + userID.Replace("\\", "\\\\").Replace("\"", "\\\"") + (userIP != null ? "\",\"playerIP\":\"" + userIP : "") + "\",\"message\":\"" + message.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\",\"playerName\":\"" + userName.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\",\"issuer\":\"" + Helper.HandleId(issuerID).Replace("\\", "\\\\").Replace("\"", "\\\"") + "\",\"issuerName\":\"" + issuerName.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"" + (silent ? ",\"online\":true" : "") + ",\"res\":" + msgId + "}");

var task = await Task.WhenAny(promise.Task, Task.Delay(5000));
if (task == promise.Task) return await promise.Task;
Expand Down
2 changes: 1 addition & 1 deletion SCPStats/Commands/IPBanCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
KickPlayer(player, duration, message);
}

Helper.HandleBooleanTask(p, SCPStats.Singleton?.Translation?.IpBanSuccess ?? "Successfully banned IP!", SCPStats.Singleton?.Translation?.IpBanCommand?.ToUpper() ?? "IPBAN", API.API.AddWarningWithType(6, ip, "", message, issuerID, issuerName));
Helper.HandleBooleanTask(p, SCPStats.Singleton?.Translation?.IpBanSuccess ?? "Successfully banned IP!", SCPStats.Singleton?.Translation?.IpBanCommand?.ToUpper() ?? "IPBAN", API.API.AddWarningWithType(6, ip, null, "", message, issuerID, issuerName));

response = SCPStats.Singleton?.Translation?.PleaseWait ?? "Please wait...";
return true;
Expand Down
3 changes: 3 additions & 0 deletions SCPStats/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class Config : IConfig
[Description("If you enable this, bans will only be saved to SCPStats and will not be saved to the bans file. This fixes a bug where players must be unbanned on the server they were banned on, or else they will be unbanned on every server but that one. This does not affect already existing bans.")]
public bool DisableBasegameBans { get; set; } = false;

[Description("If you enable this, any normal bans that you issue (bans issued through the ban command, not the ipban command) will associate the banned player's IP along with their ID in the ban record (preventing the player from joining with the same ID or IP). This does not create a separate IP ban, so the process for unbanning a player is exactly the same as it is with this option off.")]
public bool LinkIpsToBans { get; set; } = false;

[Description("By default, SCPStats does not require confirmation that a user is not banned (and will only kick them if it confirms that they are banned). This is fine, but makes it possible to bypass bans with a DDOS attack. Turning this on will kick players if they are not confirmed to not be banned.")]
public bool RequireConfirmation { get; set; } = false;

Expand Down
3 changes: 2 additions & 1 deletion SCPStats/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,9 @@ internal static void OnBan(BannedEventArgs ev)
if (!(SCPStats.Singleton?.Config?.ModerationLogging ?? true) || string.IsNullOrEmpty(ev.Details.Id) || ev.Type != BanHandler.BanType.UserId) return;

var name = ev.Target?.UserId != null ? ev.Target.Nickname : ev.Details.OriginalName;
var ip = (SCPStats.Singleton?.Config?.LinkIpsToBans ?? false) ? Helper.HandleIP(ev.Target) : null;

WebsocketHandler.SendRequest(RequestType.AddWarning, "{\"type\":\"1\",\"playerId\":\""+Helper.HandleId(ev.Details.Id)+"\",\"message\":\""+ev.Details.Reason.Replace("\\", "\\\\").Replace("\"", "\\\"")+"\",\"length\":"+((long) TimeSpan.FromTicks(ev.Details.Expires-ev.Details.IssuanceTime).TotalSeconds)+",\"playerName\":\""+name.Replace("\\", "\\\\").Replace("\"", "\\\"")+"\",\"issuer\":\""+(!string.IsNullOrEmpty(ev.Issuer?.UserId) && !(ev.Issuer?.IsHost ?? false) ? Helper.HandleId(ev.Issuer) : "")+"\",\"issuerName\":\""+(!string.IsNullOrEmpty(ev.Issuer?.Nickname) && !(ev.Issuer?.IsHost ?? false) ? ev.Issuer.Nickname.Replace("\\", "\\\\").Replace("\"", "\\\"") : "")+"\"}");
WebsocketHandler.SendRequest(RequestType.AddWarning, "{\"type\":\"1\",\"playerId\":\""+Helper.HandleId(ev.Details.Id) + (ip != null ? "\",\"playerIP\":\"" + ip : "") + "\",\"message\":\""+ev.Details.Reason.Replace("\\", "\\\\").Replace("\"", "\\\"")+"\",\"length\":"+((long) TimeSpan.FromTicks(ev.Details.Expires-ev.Details.IssuanceTime).TotalSeconds)+",\"playerName\":\""+name.Replace("\\", "\\\\").Replace("\"", "\\\"")+"\",\"issuer\":\""+(!string.IsNullOrEmpty(ev.Issuer?.UserId) && !(ev.Issuer?.IsHost ?? false) ? Helper.HandleId(ev.Issuer) : "")+"\",\"issuerName\":\""+(!string.IsNullOrEmpty(ev.Issuer?.Nickname) && !(ev.Issuer?.IsHost ?? false) ? ev.Issuer.Nickname.Replace("\\", "\\\\").Replace("\"", "\\\"") : "")+"\"}");
}

private static List<string> IgnoredMessages = new List<string>()
Expand Down
10 changes: 10 additions & 0 deletions SCPStats/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ internal static string HandleId(Player player)
return HandleId(player?.UserId);
}

internal static string HandleIP(string ip)
{
return ip?.Trim()?.ToLower();
}

internal static string HandleIP(Player player)
{
return HandleIP(player?.IPAddress);
}

internal static string UserInfoData(string id, string ip)
{
return id + ((SCPStats.Singleton?.Config?.SyncBans ?? false) ? "|" + ip : "");
Expand Down

0 comments on commit e72cd32

Please sign in to comment.