Skip to content

Commit 2daa53b

Browse files
Setup SSH: adding -4 option only of ForceIP4 is selected (#95)
* Adding -4 option only of ForceIPvç is selected * Document intentional spacing in SSH format strings (#96) Co-authored-by: MichaelKoster70 <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: MichaelKoster70 <[email protected]>
1 parent 28bd6f8 commit 2daa53b

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

src/Extension/RemoteDebuggerLauncher/PackageConstants.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,18 @@ public static class SecureShell
181181
public const string DefaultKnownHostsFileName = "known_hosts";
182182

183183
/// <summary>SSH key scanner arguments.</summary>
184-
public const string KeyScanArguments = "-4 -p {1} {0}";
184+
/// <remarks>No space after {2} because SshForceIPv4 includes a trailing space.</remarks>
185+
public const string KeyScanArguments = "{2}-p {1} {0}";
186+
187+
/// <summary>Argument for SSH to force IPv4 usage.</summary>
188+
public const string SshForceIPv4 = "-4 ";
185189

186190
/// <summary>SSH executable.</summary>
187191
public const string SshExecutable = "ssh.exe";
188192

189193
/// <summary>The SSH arguments to add server fingerprint to known_hosts file.</summary>
190-
public const string SshArguments = "{0}@{1} -p {2} -i \"{3}\" \"echo DONE\"";
194+
/// <remarks>No space after {4} because SshForceIPv4 includes a trailing space.</remarks>
195+
public const string SshArguments = "{0}@{1} {4}-p {2} -i \"{3}\" \"echo DONE\"";
191196

192197
/// <summary>HTTPS Developer Certificate name.</summary>
193198
public const string HttpsCertificateName = "DevCert.pfx";

src/Extension/RemoteDebuggerLauncher/RemoteOperations/SecureShellKeySetupService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.IO;
1212
using System.Text;
1313
using System.Threading.Tasks;
14+
using Microsoft.VisualStudio.Shell;
1415
using Microsoft.VisualStudio.Threading;
1516
using RemoteDebuggerLauncher.Infrastructure;
1617
using Renci.SshNet;
@@ -106,7 +107,7 @@ public async Task AuthorizeKeyAsync(SecureShellKeySetupSettings settings)
106107
var defaultKeysFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), PackageConstants.SecureShell.DefaultKeyPairFolder);
107108
var knownHostsFilePath = Path.Combine(defaultKeysFolder, PackageConstants.SecureShell.DefaultKnownHostsFileName);
108109

109-
var arguments = string.Format(PackageConstants.SecureShell.KeyScanArguments, settings.HostName, settings.HostPort);
110+
var arguments = string.Format(PackageConstants.SecureShell.KeyScanArguments, settings.HostName, settings.HostPort, settings.ForceIPv4 ? PackageConstants.SecureShell.SshForceIPv4 : string.Empty);
110111
var startInfo = new ProcessStartInfo(PackageConstants.SecureShell.KeyScanExecutable, arguments)
111112
{
112113
CreateNoWindow = true,
@@ -124,6 +125,8 @@ public async Task AuthorizeKeyAsync(SecureShellKeySetupSettings settings)
124125
var stdError = await process.StandardError.ReadToEndAsync();
125126
var exitCode = await process.WaitForExitAsync();
126127

128+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
129+
127130
if (exitCode == 0)
128131
{
129132
if (FileHelper.ContainsText(knownHostsFilePath, settings.HostName))
@@ -155,7 +158,7 @@ private async Task<bool> RegisterServerFingerprintWithConnectionAsync(SecureShel
155158
const string SshDoneMarker = "DONE";
156159

157160
// open a pseudo console windows to run the ssh command
158-
var arguments = string.Format(PackageConstants.SecureShell.SshArguments, settings.UserName, settings.HostName, settings.HostPort, settings.PrivateKeyFile);
161+
var arguments = string.Format(PackageConstants.SecureShell.SshArguments, settings.UserName, settings.HostName, settings.HostPort, settings.PrivateKeyFile, settings.ForceIPv4 ? PackageConstants.SecureShell.SshForceIPv4 : string.Empty);
159162
var startInfo = new ProcessStartInfo(PackageConstants.SecureShell.SshExecutable, arguments)
160163
{
161164
CreateNoWindow = false,

src/Extension/RemoteDebuggerLauncher/RemoteOperations/SecureShellKeySetupSettings.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ----------------------------------------------------------------------------
1+
// ----------------------------------------------------------------------------
22
// <copyright company="Michael Koster">
33
// Copyright (c) Michael Koster. All rights reserved.
44
// Licensed under the MIT License.
@@ -17,6 +17,7 @@ namespace RemoteDebuggerLauncher.RemoteOperations
1717
internal class SecureShellKeySetupSettings
1818
{
1919
private readonly bool forceIPv4;
20+
2021
public SecureShellKeySetupSettings(SetupSshViewModel viewModel)
2122
{
2223
HostName = viewModel.HostName;
@@ -67,5 +68,10 @@ public SecureShellKeySetupSettings(SetupSshViewModel viewModel)
6768
/// Gets the private key file.
6869
/// </summary>
6970
public string PrivateKeyFile { get; }
71+
72+
/// <summary>
73+
/// Gets a value indicating whether to force IPv4 usage.
74+
/// </summary>
75+
public bool ForceIPv4 => forceIPv4;
7076
}
7177
}

src/Extension/RemoteDebuggerLauncher/RemoteOperations/SecureShellPassphraseDialog.xaml

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)