From b5d78c1b1c10269c8f42197af595e4bd1a28bd81 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 7 May 2026 10:56:04 -0400 Subject: [PATCH] refactor: extract BaseOptions constant in SshTunnelCommandLine (#290) The six individual sb.Append() calls that set fixed SSH connection options (-o BatchMode=yes, ExitOnForwardFailure=yes, ServerAliveInterval, ServerAliveCountMax, TCPKeepAlive, -N) are replaced by a single const string BaseOptions passed to the StringBuilder constructor. Benefits: - The full set of SSH connection options is visible in one place, making it easy to review the connection policy or adjust an option without scanning the Append chain. - The compiler folds all the string literals at compile time (no runtime allocation or concatenation for the static portion). - BuildArguments is shorter and the dynamic parts (port forwards, user@host) stand out more clearly. No functional change; all existing SshTunnelCommandLineTests pass. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/OpenClaw.Shared/SshTunnelCommandLine.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/OpenClaw.Shared/SshTunnelCommandLine.cs b/src/OpenClaw.Shared/SshTunnelCommandLine.cs index e9a26d7..54bc5cb 100644 --- a/src/OpenClaw.Shared/SshTunnelCommandLine.cs +++ b/src/OpenClaw.Shared/SshTunnelCommandLine.cs @@ -8,6 +8,16 @@ public static class SshTunnelCommandLine private static readonly Regex s_validSshUser = new(@"^[a-zA-Z0-9._-]+$", RegexOptions.Compiled); private static readonly Regex s_validSshHost = new(@"^[a-zA-Z0-9._-]+$", RegexOptions.Compiled); + // Fixed SSH options shared by every tunnel invocation. + // Centralised here so the connection policy is visible and easy to review or adjust. + private const string BaseOptions = + "-o BatchMode=yes " + + "-o ExitOnForwardFailure=yes " + + "-o ServerAliveInterval=15 " + + "-o ServerAliveCountMax=3 " + + "-o TCPKeepAlive=yes " + + "-N "; + public static string BuildArguments(string user, string host, int remotePort, int localPort) => BuildArguments(user, host, remotePort, localPort, includeBrowserProxyForward: false); @@ -33,13 +43,7 @@ public static class SshTunnelCommandLine ValidateBrowserProxyPort(localPort, nameof(localPort)); } - var sb = new StringBuilder(); - sb.Append("-o BatchMode=yes "); - sb.Append("-o ExitOnForwardFailure=yes "); - sb.Append("-o ServerAliveInterval=15 "); - sb.Append("-o ServerAliveCountMax=3 "); - sb.Append("-o TCPKeepAlive=yes "); - sb.Append("-N "); + var sb = new StringBuilder(BaseOptions); AppendLocalForward(sb, localPort, remotePort); if (includeBrowserProxyForward) AppendLocalForward(sb, localPort + 2, remotePort + 2);