Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for passwords via sbox_password #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions Code/GameObjectSystems/SandboxGameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

public partial class SandboxGameManager : GameObjectSystem<SandboxGameManager>, 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 )
{
}
Expand Down Expand Up @@ -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 );
}

Expand Down
17 changes: 17 additions & 0 deletions Code/UI/MainMenu/Components/Modals/CreateGameModal.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
</Control>
</FormGroup>

<FormGroup>
<Label>Password</Label>
<Control>
<TextEntry Value:bind="@Password" />
</Control>
</FormGroup>

<FormGroup class="form-group">
<Label>Max Players</Label>
<Control>
Expand Down Expand Up @@ -54,6 +61,7 @@
@code
{
LobbyConfig CurrentConfig;
String Password;

public CreateGameModal()
{
Expand All @@ -66,6 +74,8 @@
};

LaunchArguments.Map = "softsplit.gm_bigcity";

Password = "";
}

async void CreateGame()
Expand Down Expand Up @@ -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);

Expand Down