From 1089e69ae8093c93436cd0d018d8c5ab2a69f68a Mon Sep 17 00:00:00 2001 From: K4ryuu <104531589+K4ryuu@users.noreply.github.com> Date: Sun, 19 May 2024 23:54:55 +0200 Subject: [PATCH] Patch v1.3.1 --- CHANGELOG | 5 +++++ src/KitsuneSteamRestrict.cs | 39 ++++++++++++++++++++++++++---------- src/Models/SteamService.cs | 40 +++++++++++++++++++++---------------- 3 files changed, 56 insertions(+), 28 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e37862e..da66bdd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +-- 2024. 05. 19 - v1.3.1 + +- feat: Added detection to know if database is used +- fix: Threading issues with database + -- 2024. 05. 18 - v1.3.0 - feat: Add configuration json file to allow players to bypass specific restrictions diff --git a/src/KitsuneSteamRestrict.cs b/src/KitsuneSteamRestrict.cs index f0266b2..b3c6f69 100644 --- a/src/KitsuneSteamRestrict.cs +++ b/src/KitsuneSteamRestrict.cs @@ -88,7 +88,7 @@ public sealed class DatabaseSettings public class SteamRestrictPlugin : BasePlugin, IPluginConfig { public override string ModuleName => "Steam Restrict"; - public override string ModuleVersion => "1.3.0"; + public override string ModuleVersion => "1.3.1"; public override string ModuleAuthor => "K4ryuu, Cruze @ KitsuneLab"; public override string ModuleDescription => "Restrict certain players from connecting to your server."; @@ -117,8 +117,11 @@ public override void Load(bool hotReload) var bypassConfigService = new BypassConfigService(Path.Combine(ModuleDirectory, bypassConfigFilePath)); _bypassConfig = bypassConfigService.LoadConfig(); - var databaseService = new DatabaseService(Config.DatabaseSettings); - _ = databaseService.EnsureTablesExistAsync(); + if (!IsDatabaseConfigDefault()) + { + var databaseService = new DatabaseService(Config.DatabaseSettings); + _ = databaseService.EnsureTablesExistAsync(); + } RegisterListener(() => { g_bSteamAPIActivated = true; }); RegisterListener((int slot, string name, string ipAddress) => { g_hAuthorize[slot]?.Kill(); }); @@ -143,7 +146,6 @@ public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventIn return HookResult.Continue; OnPlayerConnectFull(player); - return HookResult.Continue; } @@ -175,14 +177,16 @@ private void OnPlayerConnectFull(CCSPlayerController player) ulong authorizedSteamID = player.AuthorizedSteamID.SteamId64; nint handle = player.Handle; - var databaseService = new DatabaseService(Config.DatabaseSettings); - Task.Run(async () => { - if (await databaseService.IsSteamIdAllowedAsync(authorizedSteamID)) + if (!IsDatabaseConfigDefault()) { - Server.NextWorldUpdate(() => Logger.LogInformation($"{player.PlayerName} ({authorizedSteamID}) was allowed to join without validations because they were found in the database.")); - return; + var databaseService = new DatabaseService(Config.DatabaseSettings); + if (await databaseService.IsSteamIdAllowedAsync(authorizedSteamID)) + { + Server.NextWorldUpdate(() => Logger.LogInformation($"{player.PlayerName} ({authorizedSteamID}) was allowed to join without validations because they were found in the database.")); + return; + } } await CheckUserViolations(handle, authorizedSteamID); @@ -192,7 +196,7 @@ private void OnPlayerConnectFull(CCSPlayerController player) private async Task CheckUserViolations(nint handle, ulong authorizedSteamID) { SteamService steamService = new SteamService(this); - await steamService.FetchSteamUserInfoAsync(handle, authorizedSteamID); + await steamService.FetchSteamUserInfo(handle, authorizedSteamID); SteamUserInfo? userInfo = steamService.UserInfo; @@ -220,7 +224,7 @@ private async Task CheckUserViolations(nint handle, ulong authorizedSteamID) { Server.ExecuteCommand($"kickid {player.UserId} \"You have been kicked for not meeting the minimum requirements.\""); } - else + else if (!IsDatabaseConfigDefault()) { ulong steamID = player.AuthorizedSteamID?.SteamId64 ?? 0; @@ -261,4 +265,17 @@ private bool IsRestrictionViolated(CCSPlayerController player, SteamUserInfo use return configChecks.Any(check => check.Item1 && check.Item2 != -1 && check.Item3 < check.Item2); } + + public bool IsDatabaseConfigDefault() + { + DatabaseSettings settings = Config.DatabaseSettings; + return settings.Host == "localhost" && + settings.Username == "root" && + settings.Database == "database" && + settings.Password == "password" && + settings.Port == 3306 && + settings.Sslmode == "none" && + settings.TablePrefix == "" && + settings.TablePurgeDays == 30; + } } diff --git a/src/Models/SteamService.cs b/src/Models/SteamService.cs index b220699..4a670d5 100644 --- a/src/Models/SteamService.cs +++ b/src/Models/SteamService.cs @@ -1,4 +1,5 @@ +using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using KitsuneSteamRestrict; using Microsoft.Extensions.Logging; @@ -35,25 +36,30 @@ public SteamService(SteamRestrictPlugin plugin) _steamWebAPIKey = _config.SteamWebAPI; } - public async Task FetchSteamUserInfoAsync(nint handle, ulong authorizedSteamID) + public async Task FetchSteamUserInfo(nint handle, ulong authorizedSteamID) { - CSteamID cSteamID = new CSteamID(authorizedSteamID); - - UserInfo = new SteamUserInfo + await Server.NextWorldUpdateAsync(() => { - 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 - }; - - string steamId = authorizedSteamID.ToString(); - - UserInfo.CS2Playtime = await FetchCS2PlaytimeAsync(steamId) / 60; - UserInfo.SteamLevel = await FetchSteamLevelAsync(steamId); - await FetchProfilePrivacyAsync(steamId, UserInfo); - await FetchTradeBanStatusAsync(steamId, UserInfo); - await FetchGameBanStatusAsync(steamId, UserInfo); - await FetchSteamGroupMembershipAsync(steamId, UserInfo); + string steamId = authorizedSteamID.ToString(); + 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 () => + { + UserInfo.CS2Playtime = await FetchCS2PlaytimeAsync(steamId) / 60; + UserInfo.SteamLevel = await FetchSteamLevelAsync(steamId); + await FetchProfilePrivacyAsync(steamId, UserInfo); + await FetchTradeBanStatusAsync(steamId, UserInfo); + await FetchGameBanStatusAsync(steamId, UserInfo); + await FetchSteamGroupMembershipAsync(steamId, UserInfo); + }); + }); } private async Task FetchCS2PlaytimeAsync(string steamId)