From 8c4ada0903dad2212feadc943eeb0ed24e15fc13 Mon Sep 17 00:00:00 2001 From: K4ryuu <104531589+K4ryuu@users.noreply.github.com> Date: Tue, 21 May 2024 23:19:38 +0200 Subject: [PATCH] Patch v1.3.2 --- CHANGELOG | 7 ++ src/KitsuneSteamRestrict.cs | 147 ++++++++++++++++++++++++------------ src/Models/SteamService.cs | 17 +---- 3 files changed, 107 insertions(+), 64 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index da66bdd..16eac68 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,10 @@ +-- 2024. 05. 21 - v1.3.2 + +- feat: Added option to disable join logs +- feat: Added the user's account oldness in days too +- fix: Lag spikes caused by database +- refactor: Violation check function readability improved + -- 2024. 05. 19 - v1.3.1 - feat: Added detection to know if database is used diff --git a/src/KitsuneSteamRestrict.cs b/src/KitsuneSteamRestrict.cs index 7c49dda..dd44e5b 100644 --- a/src/KitsuneSteamRestrict.cs +++ b/src/KitsuneSteamRestrict.cs @@ -6,11 +6,16 @@ using System.Text.Json.Serialization; using Microsoft.Extensions.Logging; +using Steamworks; +using CounterStrikeSharp.API.Modules.Cvars; namespace KitsuneSteamRestrict; public class PluginConfig : BasePluginConfig { + [JsonPropertyName("LogProfileInformations")] + public bool LogProfileInformations { get; set; } = true; + [JsonPropertyName("SteamWebAPI")] public string SteamWebAPI { get; set; } = ""; @@ -54,7 +59,7 @@ public class PluginConfig : BasePluginConfig public DatabaseSettings DatabaseSettings { get; set; } = new DatabaseSettings(); [JsonPropertyName("ConfigVersion")] - public override int Version { get; set; } = 2; + public override int Version { get; set; } = 3; } public sealed class DatabaseSettings @@ -88,7 +93,7 @@ public sealed class DatabaseSettings public class SteamRestrictPlugin : BasePlugin, IPluginConfig { public override string ModuleName => "Steam Restrict"; - public override string ModuleVersion => "1.3.1"; + public override string ModuleVersion => "1.3.2"; public override string ModuleAuthor => "K4ryuu, Cruze @ KitsuneLab"; public override string ModuleDescription => "Restrict certain players from connecting to your server."; @@ -120,7 +125,7 @@ public override void Load(bool hotReload) if (!IsDatabaseConfigDefault()) { var databaseService = new DatabaseService(Config.DatabaseSettings); - _ = databaseService.EnsureTablesExistAsync(); + Task.Run(databaseService.EnsureTablesExistAsync); } RegisterListener(() => { g_bSteamAPIActivated = true; }); @@ -198,44 +203,63 @@ private void OnPlayerConnectFull(CCSPlayerController player) private void CheckUserViolations(nint handle, ulong authorizedSteamID) { - SteamService steamService = new SteamService(this); - steamService.FetchSteamUserInfo(handle, authorizedSteamID); + CSteamID cSteamID = new CSteamID(authorizedSteamID); - SteamUserInfo? userInfo = steamService.UserInfo; + SteamUserInfo UserInfo = new SteamUserInfo + { + HasPrime = SteamGameServer.UserHasLicenseForApp(cSteamID, (AppId_t)624820) == EUserHasLicenseForAppResult.k_EUserHasLicenseResultHasLicense + || SteamGameServer.UserHasLicenseForApp(cSteamID, (AppId_t)54029) == EUserHasLicenseForAppResult.k_EUserHasLicenseResultHasLicense, + CS2Level = new CCSPlayerController_InventoryServices(handle).PersonaDataPublicLevel + }; - CCSPlayerController? player = Utilities.GetPlayerFromSteamId(authorizedSteamID); + SteamService steamService = new SteamService(this, UserInfo); - if (player?.IsValid == true && userInfo != null) + Task.Run(async () => { - Logger.LogInformation($"{player.PlayerName} info:"); - Logger.LogInformation($"CS2Playtime: {userInfo.CS2Playtime}"); - Logger.LogInformation($"CS2Level: {userInfo.CS2Level}"); - Logger.LogInformation($"SteamLevel: {userInfo.SteamLevel}"); - if ((DateTime.Now - userInfo.SteamAccountAge).TotalSeconds > 30) - Logger.LogInformation($"Steam Account Creation Date: {userInfo.SteamAccountAge:dd-MM-yyyy}"); - else - Logger.LogInformation($"Steam Account Creation Date: N/A"); - Logger.LogInformation($"HasPrime: {userInfo.HasPrime}"); - Logger.LogInformation($"HasPrivateProfile: {userInfo.IsPrivate}"); - Logger.LogInformation($"IsTradeBanned: {userInfo.IsTradeBanned}"); - Logger.LogInformation($"IsGameBanned: {userInfo.IsGameBanned}"); - Logger.LogInformation($"IsInSteamGroup: {userInfo.IsInSteamGroup}"); - - if (IsRestrictionViolated(player, userInfo)) - { - Server.ExecuteCommand($"kickid {player.UserId} \"You have been kicked for not meeting the minimum requirements.\""); - } - else if (!IsDatabaseConfigDefault()) + await steamService.FetchSteamUserInfo(authorizedSteamID.ToString()); + + SteamUserInfo? userInfo = steamService.UserInfo; + + Server.NextWorldUpdate(() => { - ulong steamID = player.AuthorizedSteamID?.SteamId64 ?? 0; + CCSPlayerController? player = Utilities.GetPlayerFromSteamId(authorizedSteamID); - if (steamID != 0) + if (player?.IsValid == true && userInfo != null) { - var databaseService = new DatabaseService(Config.DatabaseSettings); - Task.Run(async () => await databaseService.AddAllowedUserAsync(steamID, Config.DatabaseSettings.TablePurgeDays)); + if (Config.LogProfileInformations) + { + Logger.LogInformation($"{player.PlayerName} info:"); + Logger.LogInformation($"CS2Playtime: {userInfo.CS2Playtime}"); + Logger.LogInformation($"CS2Level: {userInfo.CS2Level}"); + Logger.LogInformation($"SteamLevel: {userInfo.SteamLevel}"); + if ((DateTime.Now - userInfo.SteamAccountAge).TotalSeconds > 30) + Logger.LogInformation($"Steam Account Creation Date: {userInfo.SteamAccountAge:dd-MM-yyyy} ({(int)(DateTime.Now - userInfo.SteamAccountAge).TotalDays} days ago)"); + else + Logger.LogInformation($"Steam Account Creation Date: N/A"); + Logger.LogInformation($"HasPrime: {userInfo.HasPrime}"); + Logger.LogInformation($"HasPrivateProfile: {userInfo.IsPrivate}"); + Logger.LogInformation($"IsTradeBanned: {userInfo.IsTradeBanned}"); + Logger.LogInformation($"IsGameBanned: {userInfo.IsGameBanned}"); + Logger.LogInformation($"IsInSteamGroup: {userInfo.IsInSteamGroup}"); + } + + if (IsRestrictionViolated(player, userInfo)) + { + Server.ExecuteCommand($"kickid {player.UserId} \"You have been kicked for not meeting the minimum requirements.\""); + } + else if (!IsDatabaseConfigDefault()) + { + ulong steamID = player.AuthorizedSteamID?.SteamId64 ?? 0; + + if (steamID != 0) + { + var databaseService = new DatabaseService(Config.DatabaseSettings); + Task.Run(async () => await databaseService.AddAllowedUserAsync(steamID, Config.DatabaseSettings.TablePurgeDays)); + } + } } - } - } + }); + }); } private bool IsRestrictionViolated(CCSPlayerController player, SteamUserInfo userInfo) @@ -245,25 +269,50 @@ private bool IsRestrictionViolated(CCSPlayerController player, SteamUserInfo use BypassConfig bypassConfig = _bypassConfig ?? new BypassConfig(); PlayerBypassConfig? playerBypassConfig = bypassConfig.GetPlayerConfig(player.AuthorizedSteamID?.SteamId64 ?? 0); - bool isPrime = userInfo.HasPrime; - var configChecks = new[] + + if (isPrime) { - (isPrime && (playerBypassConfig?.BypassMinimumCS2Level ?? false), Config.MinimumCS2LevelPrime, userInfo.CS2Level), - (!isPrime && (playerBypassConfig?.BypassMinimumCS2Level ?? false), Config.MinimumCS2LevelNonPrime, userInfo.CS2Level), - (isPrime && (playerBypassConfig?.BypassMinimumHours ?? false), Config.MinimumHourPrime, userInfo.CS2Playtime), - (!isPrime && (playerBypassConfig?.BypassMinimumHours ?? false), Config.MinimumHourNonPrime, userInfo.CS2Playtime), - (isPrime && (playerBypassConfig?.BypassMinimumLevel ?? false), Config.MinimumLevelPrime, userInfo.SteamLevel), - (!isPrime && (playerBypassConfig?.BypassMinimumLevel ?? false), Config.MinimumLevelNonPrime, userInfo.SteamLevel), - (playerBypassConfig?.BypassMinimumSteamAccountAge ?? false, Config.MinimumSteamAccountAgeInDays, (DateTime.Now - userInfo.SteamAccountAge).TotalDays), - (Config.BlockPrivateProfile && (playerBypassConfig?.BypassPrivateProfile ?? false), 1, userInfo.IsPrivate ? 0 : 1), - (Config.BlockTradeBanned && (playerBypassConfig?.BypassTradeBanned ?? false), 1, userInfo.IsTradeBanned ? 0 : 1), - (Config.BlockGameBanned && (playerBypassConfig?.BypassGameBanned ?? false), 1, userInfo.IsGameBanned ? 0 : 1), - (!string.IsNullOrEmpty(Config.SteamGroupID) && (playerBypassConfig?.BypassSteamGroupCheck ?? false), 1, userInfo.IsInSteamGroup ? 1 : 0), - (Config.BlockVACBanned && (playerBypassConfig?.BypassVACBanned ?? false), 1, userInfo.IsVACBanned ? 0 : 1), - }; + if (!(playerBypassConfig?.BypassMinimumCS2Level ?? false) && Config.MinimumCS2LevelPrime != -1 && userInfo.CS2Level < Config.MinimumCS2LevelPrime) + return true; + + if (!(playerBypassConfig?.BypassMinimumHours ?? false) && Config.MinimumHourPrime != -1 && userInfo.CS2Playtime < Config.MinimumHourPrime) + return true; + + if (!(playerBypassConfig?.BypassMinimumLevel ?? false) && Config.MinimumLevelPrime != -1 && userInfo.SteamLevel < Config.MinimumLevelPrime) + return true; + } + else + { + if (!(playerBypassConfig?.BypassMinimumCS2Level ?? false) && Config.MinimumCS2LevelNonPrime != -1 && userInfo.CS2Level < Config.MinimumCS2LevelNonPrime) + return true; + + if (!(playerBypassConfig?.BypassMinimumHours ?? false) && Config.MinimumHourNonPrime != -1 && userInfo.CS2Playtime < Config.MinimumHourNonPrime) + return true; + + if (!(playerBypassConfig?.BypassMinimumLevel ?? false) && Config.MinimumLevelNonPrime != -1 && userInfo.SteamLevel < Config.MinimumLevelNonPrime) + return true; + } + + if (!(playerBypassConfig?.BypassMinimumSteamAccountAge ?? false) && Config.MinimumSteamAccountAgeInDays != -1 && (DateTime.Now - userInfo.SteamAccountAge).TotalDays < Config.MinimumSteamAccountAgeInDays) + return true; + + if (Config.BlockPrivateProfile && !(playerBypassConfig?.BypassPrivateProfile ?? false) && userInfo.IsPrivate) + return true; + + if (Config.BlockTradeBanned && !(playerBypassConfig?.BypassTradeBanned ?? false) && userInfo.IsTradeBanned) + return true; + + if (Config.BlockGameBanned && !(playerBypassConfig?.BypassGameBanned ?? false) && userInfo.IsGameBanned) + return true; + + if (!string.IsNullOrEmpty(Config.SteamGroupID) && !(playerBypassConfig?.BypassSteamGroupCheck ?? false) && !userInfo.IsInSteamGroup) + return true; + + if (Config.BlockVACBanned && !(playerBypassConfig?.BypassVACBanned ?? false) && userInfo.IsVACBanned) + return true; - return configChecks.Any(check => check.Item1 && check.Item2 != -1 && check.Item3 < check.Item2); + return false; } public bool IsDatabaseConfigDefault() diff --git a/src/Models/SteamService.cs b/src/Models/SteamService.cs index 3c34c28..935388e 100644 --- a/src/Models/SteamService.cs +++ b/src/Models/SteamService.cs @@ -28,26 +28,13 @@ public class SteamService private readonly ILogger _logger; public SteamUserInfo? UserInfo = null; - public SteamService(SteamRestrictPlugin plugin) + public SteamService(SteamRestrictPlugin plugin, SteamUserInfo userInfo) { _httpClient = plugin.Client; _config = plugin.Config; _logger = plugin.Logger; _steamWebAPIKey = _config.SteamWebAPI; - } - - public void FetchSteamUserInfo(nint handle, ulong authorizedSteamID) - { - CSteamID cSteamID = new CSteamID(authorizedSteamID); - - UserInfo = new SteamUserInfo - { - HasPrime = SteamGameServer.UserHasLicenseForApp(cSteamID, (AppId_t)624820) == EUserHasLicenseForAppResult.k_EUserHasLicenseResultHasLicense - || SteamGameServer.UserHasLicenseForApp(cSteamID, (AppId_t)54029) == EUserHasLicenseForAppResult.k_EUserHasLicenseResultHasLicense, - CS2Level = new CCSPlayerController_InventoryServices(handle).PersonaDataPublicLevel - }; - - Task.Run(async () => await FetchSteamUserInfo(authorizedSteamID.ToString())).Wait(); + UserInfo = userInfo; } public async Task FetchSteamUserInfo(string steamId)