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>
This commit is contained in:
github-actions[bot] 2026-05-07 10:56:04 -04:00 committed by GitHub
parent 2fcfe76abc
commit b5d78c1b1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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);