Table of Contents

Method ForwardRemotePortAsync

Namespace
SshSharp
Assembly
SshSharp.dll

ForwardRemotePortAsync(string, int, string, int, CancellationToken)

Remote port forwarding (ssh -R): asks the server to listen on bindAddress:remotePort and relays every connection made to it to localHost:localPort, as reached from this machine. The bind address is localhost for the server's loopback only (what ssh uses by default), an address, or empty for every address; sshd's GatewayPorts decides whether it honours anything but loopback. Port 0 lets the server pick one (see RemotePort).

public Task<SshRemotePortForwarder> ForwardRemotePortAsync(string bindAddress, int remotePort, string localHost, int localPort, CancellationToken cancellationToken = default)

Parameters

bindAddress string
remotePort int
localHost string
localPort int
cancellationToken CancellationToken

Returns

Task<SshRemotePortForwarder>

Exceptions

SshException

The server refused (sshd's AllowTcpForwarding, or the port is in use).

ForwardRemotePortAsync(string, int, Func<SshForwardedConnection, CancellationToken, Task>, int, CancellationToken)

Remote port forwarding to a handler in this process (what Go's ssh.Client.Listen gives): asks the server to listen on bindAddress:remotePort and passes every connection made to it to handler, as a SshForwardedConnection with a duplex Stream. Nothing listens on this machine: it serves a port on the server from the application, for example a webhook receiver behind NAT, or a test server that programs on the server connect to.

public Task<SshRemotePortForwarder> ForwardRemotePortAsync(string bindAddress, int remotePort, Func<SshForwardedConnection, CancellationToken, Task> handler, int maxConnections = 100, CancellationToken cancellationToken = default)

Parameters

bindAddress string

Where the server listens: localhost for its loopback only (what ssh uses by default), an address, or empty for every address (subject to sshd's GatewayPorts).

remotePort int

The port the server listens on; 0 lets it pick one (see RemotePort).

handler Func<SshForwardedConnection, CancellationToken, Task>

Serves one connection; it runs concurrently for concurrent connections. It owns the connection's stream: disposing it closes the connection, and it is disposed when the handler returns. Its token is cancelled when the forwarder is disposed (which then waits HandlerShutdownTimeout for it) or the SSH connection ends. A handler that throws closes its own connection only; the exception is logged by type, and the forwarding goes on. Anyone who can reach the port on the server can connect, so treat what arrives as untrusted input.

maxConnections int

The most connections served at once by this forwarding; the server's channels above it are refused (resource shortage) until some end. MaxForwardedConnections still applies across all forwardings. Default 100.

cancellationToken CancellationToken

Cancels the request to the server.

Returns

Task<SshRemotePortForwarder>

The forwarding; dispose it to stop. LocalHost is empty and LocalPort 0.

Exceptions

SshException

The server refused (sshd's AllowTcpForwarding, or the port is in use).