Skip to content

Commit 9aa135b

Browse files
[SSH] [Bugfix] Fix RDP connection (#22188)
* Move logic to get rdp local port to a property * Update help file message for -Force parameter * Add bugfix to changelog * Update ChangeLog.md --------- Co-authored-by: Vincent Dai <[email protected]>
1 parent 65304ff commit 9aa135b

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

src/Ssh/Ssh/ChangeLog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
-->
2020

2121
## Upcoming Release
22-
* Support updating Service Configuration for Arc resources at runtime.
22+
* Supported updating Service Configuration for Arc resources at runtime.
2323
- Owners/Contributors can change what port is allowed for SSH connection at runtime by providing the -Port parameter and confirming the operation when prompted.
24+
* Fixed bug in the RDP feature in the Enter-AzVM cmdlet.
2425

2526
## Version 0.1.2
2627
* The SSH Proxy required for connection to Arc resources must be installed by the user as part of the Az.Ssh.ArcProxy PowerShell module

src/Ssh/Ssh/Common/SshBaseCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ private ResourceManagementClient ResourceManagementClient
339339
[Parameter(Mandatory = false)]
340340
public virtual SwitchParameter PassThru { get; set; }
341341

342-
[Parameter(Mandatory = false, HelpMessage = "When connecting to Arc resources, do not prompt for confirmation before updating the Service Configuration of the Connection Endpoint to match the target port or to install Az.Ssh.ArcProxy module from Gallery, if needed.")]
342+
[Parameter(Mandatory = false, HelpMessage = "When connecting to Arc resources, do not prompt for confirmation before updating the allowed port for SSH connection in the Connection Endpoint to match the target port or to install Az.Ssh.ArcProxy module from the PowerShell Gallery, if needed.")]
343343
public SwitchParameter Force { get; set; }
344344

345345
#endregion

src/Ssh/Ssh/SshCommands/EnterAzVMCommand.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Text.RegularExpressions;
2626
using Microsoft.Azure.Commands.Ssh.Properties;
2727
using Microsoft.Azure.PowerShell.Ssh.Helpers.HybridConnectivity.Models;
28+
using Microsoft.Azure.PowerShell.Cmdlets.Ssh.Common;
2829

2930
namespace Microsoft.Azure.Commands.Ssh
3031
{
@@ -55,7 +56,6 @@ public override string KeysDestinationFolder
5556
#endregion
5657

5758
#region fields
58-
private int rdpLocalPort;
5959
private EndpointAccessResource relayInfo;
6060
#endregion
6161

@@ -64,6 +64,25 @@ public override string KeysDestinationFolder
6464
private const int ServiceConfigDelayInSec = 15;
6565
#endregion
6666

67+
#region Properties
68+
private int RdpLocalPort
69+
{
70+
get
71+
{
72+
if (_rdpLocalPort == 0)
73+
{
74+
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
75+
listener.Start();
76+
_rdpLocalPort = ((IPEndPoint)listener.LocalEndpoint).Port;
77+
listener.Stop();
78+
}
79+
80+
return _rdpLocalPort;
81+
}
82+
}
83+
private int _rdpLocalPort = 0;
84+
#endregion
85+
6786
public override void ExecuteCmdlet()
6887
{
6988
base.ExecuteCmdlet();
@@ -178,12 +197,6 @@ private int StartRDPConnection(Process sshProcess)
178197

179198
try
180199
{
181-
// Get an open local port to act an a listener
182-
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
183-
listener.Start();
184-
rdpLocalPort = ((IPEndPoint)listener.LocalEndpoint).Port;
185-
listener.Stop();
186-
187200
sshProcess.StartInfo.RedirectStandardError = true;
188201
sshProcess.Start();
189202

@@ -207,7 +220,7 @@ private int StartRDPConnection(Process sshProcess)
207220

208221
Process rdpProcess = new Process();
209222
rdpProcess.StartInfo.FileName = rdpCommand;
210-
rdpProcess.StartInfo.Arguments = $"/v:localhost:{rdpLocalPort}";
223+
rdpProcess.StartInfo.Arguments = $"/v:localhost:{RdpLocalPort}";
211224
rdpProcess.Start();
212225
rdpProcess.WaitForExit();
213226
success = rdpProcess.ExitCode;
@@ -398,7 +411,7 @@ private string BuildArgs()
398411
if (Rdp)
399412
{
400413
argList.Add("-L");
401-
argList.Add($"{rdpLocalPort}:localhost:3389");
414+
argList.Add($"{RdpLocalPort}:localhost:3389");
402415
argList.Add("-N");
403416
}
404417

src/Ssh/Ssh/help/Enter-AzVM.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Accept wildcard characters: False
125125
```
126126
127127
### -Force
128-
When connecting to an Arc resource, do not ask for confirmation before updating the Service Configuration of the Connection Endpoint to match the target port.
128+
When connecting to Arc resources, do not prompt for confirmation before updating the allowed port for SSH connection in the Connection Endpoint to match the target port or to install Az.Ssh.ArcProxy module from the PowerShell Gallery, if needed.
129129
130130
```yaml
131131
Type: System.Management.Automation.SwitchParameter

src/Ssh/Ssh/help/Export-AzSshConfig.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Accept wildcard characters: False
143143
```
144144
145145
### -Force
146-
When connecting to an Arc resource, do not ask for confirmation before updating the Service Configuration of the Connection Endpoint to match the target port.
146+
When connecting to Arc resources, do not prompt for confirmation before updating the allowed port for SSH connection in the Connection Endpoint to match the target port or to install Az.Ssh.ArcProxy module from the PowerShell Gallery, if needed.
147147
148148
```yaml
149149
Type: System.Management.Automation.SwitchParameter

0 commit comments

Comments
 (0)