Work with multiple tunnels
Both TElSSHLocalPortForwarding and TElSSHRemotePortForwarding classes can be used to build many different tunnels through a single SSH connection at the same time. For example, you can forward local port 80 to some server.com:80, and port 443 to server.com:443 - and all with the single TElSSHLocalPortForwarding object.
Tunnels are configured via the Tunnels property (get_Tunnels() method on certain platforms) of the forwarding classes. The objects are created and owned by the forwarding class, so you do not need to create them explicitly. Instead, you add tunnels with AddTunnel() method and remove them with RemoveTunnel(), as shown below:
int idx = forwarding.AddTunnel(); forwarding.get_Tunnels(idx). ForwardedHost = ""; forwarding.get_Tunnels(idx). ForwardedPort = "80"; forwarding.get_Tunnels(idx).DestHost = "server.com"; forwarding.get_Tunnels(idx).DestPort = 80; idx = Forwarding.AddTunnel(); forwarding.get_Tunnels(idx). ForwardedHost = ""; forwarding.get_Tunnels(idx). ForwardedPort = "443"; forwarding.get_Tunnels(idx).DestHost = "server.com"; forwarding.get_Tunnels(idx).DestPort = 443;
Use the tunnel objects' AutoOpen property to specify whether the tunnel should be opened immediately when the SSH session starts.
Note that a forwarding object with no tunnels configured in the above way still implicitly uses the Tunnels property behind the scene. In this case, the objects' ForwardedHost, ForwardedPort, DestHost and DestPort properties are implicitly mapped to the corresponding properties of the Tunnels object. You need to consider this feature when implementing multiple-tunnel environments, as the first tunnel object might have already been created implicitly for you at the time you start configuring the tunnels.