Skip to content

Commit e0a4b7c

Browse files
Add SSH server port for managed tunnels
Merged during first-pass triage after dual-model adversarial review and green reported checks. Follow-up noted: diagnostics browser-proxy SSH hint should include -p for non-default sshPort.
1 parent e821d68 commit e0a4b7c

27 files changed

Lines changed: 281 additions & 27 deletions

‎docs/CONNECTION_ARCHITECTURE.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Setup codes (from QR scan or paste) decode to `{ url, bootstrapToken }` via `Set
172172

173173
`SshTunnelService` manages an SSH local port-forward process. `SshTunnelManager` wraps it behind `ISshTunnelManager` for the connection manager.
174174

175-
When a `GatewayRecord` has `SshTunnel` config, the connection manager starts the tunnel before connecting the WebSocket client to `ws://localhost:<localPort>`.
175+
When a `GatewayRecord` has `SshTunnel` config, the connection manager starts the tunnel before connecting the WebSocket client to `ws://localhost:<localPort>`. The config stores the SSH daemon port (`sshPort`, default `22`) separately from the remote gateway port forwarded by `-L`.
176176

177177
`SshTunnelSnapshot` provides a read-only point-in-time view of tunnel state for UI consumption (avoids coupling UI to the mutable service).
178178

@@ -232,3 +232,4 @@ Connection tests live in `tests/OpenClaw.Connection.Tests/`:
232232
- `ConnectionDiagnosticsTests` — ring buffer diagnostics
233233

234234
The heaviest remaining gap is Windows shell UI behavior (tray clicks, tooltip visibility, WinUI menu routing). Cover pure decision logic in unit tests; use manual or integration smoke tests for shell behavior.
235+

‎docs/WINDOWS_NODE_TESTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ When the node connects, it advertises these capabilities:
100100
- Confirm the Browser proxy bridge toggle is enabled in Settings, then save and reconnect or re-pair if the gateway keeps an older command snapshot.
101101
- The bridge is local-only: it calls `http://127.0.0.1:<gateway-port+2>` from Windows. For a gateway on `ws://127.0.0.1:18789`, the browser-control host must listen on `127.0.0.1:18791`.
102102
- In managed SSH tunnel mode, keep Browser proxy bridge enabled so the tray forwards local gateway port + 2 to remote gateway port + 2. Settings shows a selectable preview of the exact `ssh -N -L ...` command.
103-
- If using a manual SSH tunnel, add both forwards, for example: `ssh -N -L 18789:127.0.0.1:18789 -L 18791:127.0.0.1:18791 <user>@<host>`. If local and remote gateway ports differ, forward `<local-gateway-port+2>` to `127.0.0.1:<remote-gateway-port+2>`.
103+
- If using a manual SSH tunnel, add both forwards, for example: `ssh -N -L 18789:127.0.0.1:18789 -L 18791:127.0.0.1:18791 <user>@<host>`. If the SSH daemon is not listening on port 22, include `-p <ssh-port>`. If local and remote gateway ports differ, forward `<local-gateway-port+2>` to `127.0.0.1:<remote-gateway-port+2>`.
104104
- A local SSH forward is not enough if the remote browser-control host is not running. Command Center port diagnostics should show whether the local gateway and browser-control ports are listening and which process owns them.
105105
- If Command Center shows the browser-control port listening but `browser.proxy` returns an auth error, verify the Windows Settings gateway token matches the browser-control host token/password. QR/bootstrap pairing can connect the node without saving a shared gateway token, but browser-control auth may still require one.
106106
- A local smoke can verify the host dependency without proving gateway invoke auth: start the upstream browser-control host with a temporary no-secret config, confirm `http://127.0.0.1:<gateway-port+2>/` and `/tabs` return HTTP 200, then stop the captured host process. The full parity smoke is not complete until `openclaw nodes invoke --command browser.proxy` succeeds through the active gateway.

‎src/OpenClaw.Connection/ConnectionSettingsSnapshot.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public sealed record ConnectionSettingsSnapshot(
99
bool UseSshTunnel,
1010
string? SshTunnelUser,
1111
string? SshTunnelHost,
12+
int SshTunnelSshPort,
1213
int SshTunnelRemotePort,
1314
int SshTunnelLocalPort,
1415
bool EnableNodeMode,

‎src/OpenClaw.Connection/GatewayConnectionManager.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ private async Task ConnectCoreAsync(string? gatewayId = null)
201201
{
202202
var tunnel = record.SshTunnel;
203203
if (string.IsNullOrWhiteSpace(tunnel.User) || string.IsNullOrWhiteSpace(tunnel.Host) ||
204+
tunnel.SshPort is < 1 or > 65535 ||
204205
tunnel.RemotePort is < 1 or > 65535 || tunnel.LocalPort is < 1 or > 65535)
205206
{
206207
_logger.Warn("[ConnMgr] SSH tunnel config is incomplete");

‎src/OpenClaw.Connection/GatewayRecord.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ public sealed record SshTunnelConfig(
4949
string Host,
5050
int RemotePort,
5151
int LocalPort,
52-
bool IncludeBrowserProxyForward = false);
52+
bool IncludeBrowserProxyForward = false,
53+
int SshPort = 22);

‎src/OpenClaw.Connection/GatewayRegistry.cs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,31 @@ public bool MigrateFromSettings(
194194
int sshRemotePort,
195195
int sshLocalPort,
196196
string settingsDir,
197+
IOpenClawLogger? logger = null) =>
198+
MigrateFromSettings(
199+
gatewayUrl,
200+
token,
201+
bootstrapToken,
202+
useSshTunnel,
203+
sshUser,
204+
sshHost,
205+
sshPort: 22,
206+
sshRemotePort,
207+
sshLocalPort,
208+
settingsDir,
209+
logger);
210+
211+
public bool MigrateFromSettings(
212+
string? gatewayUrl,
213+
string? token,
214+
string? bootstrapToken,
215+
bool useSshTunnel,
216+
string? sshUser,
217+
string? sshHost,
218+
int sshPort,
219+
int sshRemotePort,
220+
int sshLocalPort,
221+
string settingsDir,
197222
IOpenClawLogger? logger = null)
198223
{
199224
if (string.IsNullOrWhiteSpace(gatewayUrl))
@@ -215,7 +240,7 @@ public bool MigrateFromSettings(
215240
SharedGatewayToken = string.IsNullOrWhiteSpace(bootstrapToken) ? token : null,
216241
BootstrapToken = !string.IsNullOrWhiteSpace(bootstrapToken) ? bootstrapToken : null,
217242
SshTunnel = useSshTunnel
218-
? new SshTunnelConfig(sshUser ?? "", sshHost ?? "", sshRemotePort, sshLocalPort)
243+
? new SshTunnelConfig(sshUser ?? "", sshHost ?? "", sshRemotePort, sshLocalPort, SshPort: sshPort)
219244
: null
220245
};
221246

‎src/OpenClaw.Connection/SettingsChangeImpact.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static SettingsChangeImpact Classify(ConnectionSettingsSnapshot? prev, Co
3838
if (prev.UseSshTunnel != next.UseSshTunnel ||
3939
prev.SshTunnelUser != next.SshTunnelUser ||
4040
prev.SshTunnelHost != next.SshTunnelHost ||
41+
prev.SshTunnelSshPort != next.SshTunnelSshPort ||
4142
prev.SshTunnelRemotePort != next.SshTunnelRemotePort ||
4243
prev.SshTunnelLocalPort != next.SshTunnelLocalPort)
4344
return SettingsChangeImpact.OperatorReconnectRequired;

‎src/OpenClaw.Connection/SshTunnelService.cs‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ public void EnsureStarted(string user, string host, int remotePort, int localPor
5757
=> EnsureStarted(user, host, remotePort, localPort, includeBrowserProxyForward: false);
5858

5959
public void EnsureStarted(string user, string host, int remotePort, int localPort, bool includeBrowserProxyForward)
60+
=> EnsureStarted(user, host, remotePort, localPort, includeBrowserProxyForward, sshPort: 22);
61+
62+
public void EnsureStarted(string user, string host, int remotePort, int localPort, bool includeBrowserProxyForward, int sshPort)
6063
{
6164
user = user.Trim();
6265
host = host.Trim();
6366

64-
var spec = BuildSpec(user, host, remotePort, localPort, includeBrowserProxyForward);
67+
var spec = BuildSpec(user, host, remotePort, localPort, includeBrowserProxyForward, sshPort);
6568

6669
if (IsRunning && string.Equals(_lastSpec, spec, StringComparison.Ordinal))
6770
{
@@ -71,7 +74,7 @@ public void EnsureStarted(string user, string host, int remotePort, int localPor
7174

7275
Stop();
7376
Status = TunnelStatus.Starting;
74-
StartProcess(user, host, remotePort, localPort, includeBrowserProxyForward);
77+
StartProcess(user, host, remotePort, localPort, includeBrowserProxyForward, sshPort);
7578
_lastSpec = spec;
7679
}
7780

@@ -122,12 +125,12 @@ public void ResetNotConfigured()
122125
Status = TunnelStatus.NotConfigured;
123126
}
124127

125-
private void StartProcess(string user, string host, int remotePort, int localPort, bool includeBrowserProxyForward)
128+
private void StartProcess(string user, string host, int remotePort, int localPort, bool includeBrowserProxyForward, int sshPort)
126129
{
127130
var psi = new ProcessStartInfo
128131
{
129132
FileName = "ssh",
130-
Arguments = SshTunnelCommandLine.BuildArguments(user, host, remotePort, localPort, includeBrowserProxyForward),
133+
Arguments = SshTunnelCommandLine.BuildArguments(user, host, remotePort, localPort, includeBrowserProxyForward, sshPort),
131134
UseShellExecute = false,
132135
RedirectStandardOutput = true,
133136
RedirectStandardError = true,
@@ -206,15 +209,15 @@ private void StartProcess(string user, string host, int remotePort, int localPor
206209
LastError = null;
207210
Status = TunnelStatus.Up;
208211

209-
_logger.Info($"SSH tunnel started: 127.0.0.1:{localPort} -> 127.0.0.1:{remotePort} via {user}@{host}");
212+
_logger.Info($"SSH tunnel started: 127.0.0.1:{localPort} -> 127.0.0.1:{remotePort} via {user}@{host}:{sshPort}");
210213
if (includeBrowserProxyForward)
211214
{
212-
_logger.Info($"SSH tunnel browser proxy forward started: 127.0.0.1:{localPort + 2} -> 127.0.0.1:{remotePort + 2} via {user}@{host}");
215+
_logger.Info($"SSH tunnel browser proxy forward started: 127.0.0.1:{localPort + 2} -> 127.0.0.1:{remotePort + 2} via {user}@{host}:{sshPort}");
213216
}
214217
}
215218

216-
private static string BuildSpec(string user, string host, int remotePort, int localPort, bool includeBrowserProxyForward)
217-
=> $"{user}@{host}:{localPort}:{remotePort}:browserProxy={includeBrowserProxyForward}";
219+
private static string BuildSpec(string user, string host, int remotePort, int localPort, bool includeBrowserProxyForward, int sshPort)
220+
=> $"{user}@{host}:{sshPort}:{localPort}:{remotePort}:browserProxy={includeBrowserProxyForward}";
218221

219222
public void Dispose()
220223
{
@@ -223,7 +226,7 @@ public void Dispose()
223226

224227
public Task<string> StartAsync(SshTunnelConfig config, CancellationToken ct)
225228
{
226-
EnsureStarted(config.User, config.Host, config.RemotePort, config.LocalPort, config.IncludeBrowserProxyForward);
229+
EnsureStarted(config.User, config.Host, config.RemotePort, config.LocalPort, config.IncludeBrowserProxyForward, config.SshPort);
227230
var localUrl = $"ws://localhost:{config.LocalPort}";
228231
return Task.FromResult(localUrl);
229232
}

‎src/OpenClaw.Shared/SettingsData.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public record class SettingsData
1212
public bool UseSshTunnel { get; set; } = false;
1313
public string? SshTunnelUser { get; set; }
1414
public string? SshTunnelHost { get; set; }
15+
public int SshTunnelSshPort { get; set; } = 22;
1516
public int SshTunnelRemotePort { get; set; } = 18789;
1617
public int SshTunnelLocalPort { get; set; } = 18789;
1718
public bool AutoStart { get; set; } = true;

‎src/OpenClaw.Shared/SshTunnelCommandLine.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public static string BuildArguments(
2727
int remotePort,
2828
int localPort,
2929
bool includeBrowserProxyForward)
30+
=> BuildArguments(user, host, remotePort, localPort, includeBrowserProxyForward, sshPort: 22);
31+
32+
public static string BuildArguments(
33+
string user,
34+
string host,
35+
int remotePort,
36+
int localPort,
37+
bool includeBrowserProxyForward,
38+
int sshPort)
3039
{
3140
user = user.Trim();
3241
host = host.Trim();
@@ -37,6 +46,7 @@ public static string BuildArguments(
3746
throw new ArgumentException($"SSH host contains invalid characters: '{host}'", nameof(host));
3847
ValidatePort(remotePort, nameof(remotePort));
3948
ValidatePort(localPort, nameof(localPort));
49+
ValidatePort(sshPort, nameof(sshPort));
4050
if (includeBrowserProxyForward)
4151
{
4252
ValidateBrowserProxyPort(remotePort, nameof(remotePort));
@@ -47,6 +57,12 @@ public static string BuildArguments(
4757
AppendLocalForward(sb, localPort, remotePort);
4858
if (includeBrowserProxyForward)
4959
AppendLocalForward(sb, localPort + 2, remotePort + 2);
60+
if (sshPort != 22)
61+
{
62+
sb.Append("-p ");
63+
sb.Append(sshPort);
64+
sb.Append(' ');
65+
}
5066
sb.Append(user);
5167
sb.Append('@');
5268
sb.Append(host);

0 commit comments

Comments
 (0)