Skip to content

Commit

Permalink
Patch v1.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed May 21, 2024
1 parent 890ca17 commit 8c4ada0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 64 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
147 changes: 98 additions & 49 deletions src/KitsuneSteamRestrict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; } = "";

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -88,7 +93,7 @@ public sealed class DatabaseSettings
public class SteamRestrictPlugin : BasePlugin, IPluginConfig<PluginConfig>
{
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.";

Expand Down Expand Up @@ -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<Listeners.OnGameServerSteamAPIActivated>(() => { g_bSteamAPIActivated = true; });
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
17 changes: 2 additions & 15 deletions src/Models/SteamService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8c4ada0

Please sign in to comment.