From 095a386996e2a4299e9157f502446d9a8153bdfe Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 4 Jan 2025 04:44:33 -0900 Subject: [PATCH] add support for passwords via sbox_password --- Code/GameObjectSystems/SandboxGameManager.cs | 81 +++++++++++++++++++ .../Components/Modals/CreateGameModal.razor | 17 ++++ 2 files changed, 98 insertions(+) diff --git a/Code/GameObjectSystems/SandboxGameManager.cs b/Code/GameObjectSystems/SandboxGameManager.cs index 8f66487..ba0299e 100644 --- a/Code/GameObjectSystems/SandboxGameManager.cs +++ b/Code/GameObjectSystems/SandboxGameManager.cs @@ -4,6 +4,14 @@ public partial class SandboxGameManager : GameObjectSystem, IPlayerEvent, Component.INetworkListener, ISceneStartup { + struct PlayerConnectionData + { + public string ClientPassword { get; set; } + } + + [ConVar("sbox_password", Help = "Sets the password for the sandbox.")] + public static String Password { get; set; } = ""; + public SandboxGameManager( Scene scene ) : base( scene ) { } @@ -46,8 +54,81 @@ async void ISceneStartup.OnHostInitialize() } } + [Rpc.Broadcast(NetFlags.Reliable & NetFlags.SendImmediate)] + private static void OnRequestPlayerConnectionData( SteamId playerId ) + { + if ( Connection.Local.SteamId == playerId ) + { + // In the editor (and potentially other scenarios) the password is invalid + var password = Password; + if ( password == null ) + { + password = ConsoleSystem.GetValue( "sbox_password" ); + + if ( password == null ) + { + password = ConsoleSystem.GetValue("sv_password"); + } + } + var data = new PlayerConnectionData + { + ClientPassword = password + }; + + Log.Info($"Sending player connection data"); + + OnPlayerConnectionDataReceived(data); + } + } + + [Rpc.Host(NetFlags.Reliable & NetFlags.SendImmediate)] + private static void OnPlayerConnectionDataReceived( PlayerConnectionData data ) + { + Log.Info($"Player connection data received for {Rpc.Caller.SteamId}"); + if ( Password.Length > 0 && data.ClientPassword != Password ) + { + Log.Info($"{data.ClientPassword}, {Password}"); + Rpc.Caller.Kick("Incorrect password"); + } + } + + bool Component.INetworkListener.AcceptConnection( Connection channel, ref string reason ) + { + // Always accept the host + if ( channel.IsHost ) + return true; + + // Checking the password here would be preferable, but Facepunch has locked down + // all the functions that would let us acquire the data we need at this point + /*if ( Password.Length == 0 ) + return true; + + var passwordRequest = channel.SendRequest( "sbox_request_password" ); + var startTime = RealTime.Now; + while ( !passwordRequest.IsCompleted ) + { + if ( RealTime.Now - startTime > 1.0f ) + { + reason = "Player data request timed out"; + return false; + } + } + + var userPassword = passwordRequest.Result as string; + if ( userPassword != Password ) + { + reason = "Incorrect password"; + return false; + }*/ + + return true; + } + void Component.INetworkListener.OnActive( Connection channel ) { + OnRequestPlayerConnectionData(channel.SteamId); + Log.Info($"Requesting player data for {channel.SteamId}"); + SpawnPlayerForConnection( channel ); } diff --git a/Code/UI/MainMenu/Components/Modals/CreateGameModal.razor b/Code/UI/MainMenu/Components/Modals/CreateGameModal.razor index 6096109..ab331f9 100644 --- a/Code/UI/MainMenu/Components/Modals/CreateGameModal.razor +++ b/Code/UI/MainMenu/Components/Modals/CreateGameModal.razor @@ -18,6 +18,13 @@ + + + + + + + @@ -54,6 +61,7 @@ @code { LobbyConfig CurrentConfig; + String Password; public CreateGameModal() { @@ -66,6 +74,8 @@ }; LaunchArguments.Map = "softsplit.gm_bigcity"; + + Password = ""; } async void CreateGame() @@ -93,6 +103,13 @@ Scene.Load(sceneLoadOptions); } + Password = Password.Trim(); + if ( Password.Length > 0 ) + { + SandboxGameManager.Password = Password; + Log.Info("Set sbox_password to *****"); + } + sceneLoadOptions.SetScene("scenes/engine.scene"); Scene.Load(sceneLoadOptions);