Skip to content

Commit

Permalink
Patch v1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed May 19, 2024
1 parent dd1eb63 commit 1089e69
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 28 additions & 11 deletions src/KitsuneSteamRestrict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public sealed class DatabaseSettings
public class SteamRestrictPlugin : BasePlugin, IPluginConfig<PluginConfig>
{
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.";

Expand Down Expand Up @@ -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<Listeners.OnGameServerSteamAPIActivated>(() => { g_bSteamAPIActivated = true; });
RegisterListener<Listeners.OnClientConnect>((int slot, string name, string ipAddress) => { g_hAuthorize[slot]?.Kill(); });
Expand All @@ -143,7 +146,6 @@ public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventIn
return HookResult.Continue;

OnPlayerConnectFull(player);

return HookResult.Continue;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
40 changes: 23 additions & 17 deletions src/Models/SteamService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using KitsuneSteamRestrict;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -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<int> FetchCS2PlaytimeAsync(string steamId)
Expand Down

0 comments on commit 1089e69

Please sign in to comment.