Table of Contents

Method ForwardRemoteUnixSocketAsync

Namespace
SshSharp
Assembly
SshSharp.dll

ForwardRemoteUnixSocketAsync(string, string, int, CancellationToken)

Remote Unix-socket forwarding (ssh -R /remote/socket:host:port, streamlocal-forward@openssh.com): asks the server to listen on the Unix socket remoteSocketPath and relays every connection made to it to localHost:localPort, as reached from this machine. For example, a database here becomes a socket that programs on the server can open. sshd's StreamLocalBindUnlink decides whether a stale socket file is replaced.

public Task<SshRemoteSocketForwarder> ForwardRemoteUnixSocketAsync(string remoteSocketPath, string localHost, int localPort, CancellationToken cancellationToken = default)

Parameters

remoteSocketPath string
localHost string
localPort int
cancellationToken CancellationToken

Returns

Task<SshRemoteSocketForwarder>

Exceptions

SshException

The server refused (sshd's AllowStreamLocalForwarding, or the socket exists).

ForwardRemoteUnixSocketAsync(string, string, CancellationToken)

Remote Unix-socket forwarding to a Unix socket here (ssh -R /remote/socket:/local/socket): for example, a local gpg-agent or Docker socket that programs on the server can use.

public Task<SshRemoteSocketForwarder> ForwardRemoteUnixSocketAsync(string remoteSocketPath, string localSocketPath, CancellationToken cancellationToken = default)

Parameters

remoteSocketPath string
localSocketPath string
cancellationToken CancellationToken

Returns

Task<SshRemoteSocketForwarder>

Exceptions

SshException

The server refused (sshd's AllowStreamLocalForwarding, or the socket exists).

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

Remote Unix-socket forwarding to a handler in this process: asks the server to listen on the Unix socket remoteSocketPath (streamlocal-forward@openssh.com) and passes every connection made to it to handler, as a SshForwardedConnection with a duplex Stream. sshd's StreamLocalBindUnlink decides whether a stale socket file is replaced.

public Task<SshRemoteSocketForwarder> ForwardRemoteUnixSocketAsync(string remoteSocketPath, Func<SshForwardedConnection, CancellationToken, Task> handler, int maxConnections = 100, CancellationToken cancellationToken = default)

Parameters

remoteSocketPath string

The Unix socket the server listens on.

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 open the socket 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<SshRemoteSocketForwarder>

The forwarding; dispose it to stop. LocalHost and LocalSocketPath are null.

Exceptions

SshException

The server refused (sshd's AllowStreamLocalForwarding, or the socket exists).