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
bindAddressstringremotePortintlocalHoststringlocalPortintcancellationTokenCancellationToken
Returns
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
bindAddressstringWhere the server listens:
localhostfor its loopback only (what ssh uses by default), an address, or empty for every address (subject to sshd'sGatewayPorts).remotePortintThe port the server listens on; 0 lets it pick one (see RemotePort).
handlerFunc<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.
maxConnectionsintThe 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.
cancellationTokenCancellationTokenCancels 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).