Documentation ¶
Overview ¶
Package pipe provides a simple way to create a bi-directional pipes over SSH. It uses the golang.org/x/crypto/ssh package to create a secure connection to the remote host and provides reconnect logic to all sessions in case of a connection drop.
Index ¶
- func Base(ctx context.Context, logger *slog.Logger, info *SSHClientInfo, id, cmd string) (io.ReadWriteCloser, error)
- func NewSSHClient(info *SSHClientInfo) (*ssh.Client, error)
- func Pub(ctx context.Context, logger *slog.Logger, info *SSHClientInfo, cmd string) (io.WriteCloser, error)
- func Sub(ctx context.Context, logger *slog.Logger, info *SSHClientInfo, cmd string) (io.Reader, error)
- type Client
- type ReconnectReadWriteCloser
- type SSHClientInfo
- type SendData
- type Session
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Base ¶
func Base(ctx context.Context, logger *slog.Logger, info *SSHClientInfo, id, cmd string) (io.ReadWriteCloser, error)
Base is the base command for a simple bidirectional pipe.
func NewSSHClient ¶
func NewSSHClient(info *SSHClientInfo) (*ssh.Client, error)
NewSSHClient creates a new SSH client.
Types ¶
type Client ¶
type Client struct { // Logger is the logger used by the client. Logger *slog.Logger // Info is the connection information. Info *SSHClientInfo // SSHClient is the underlying SSH client. SSHClient *ssh.Client // Sessions is a map of all sessions. Sessions *syncmap.Map[string, *Session] // ctx is the context of the client. If the context is canceled, the client will close // all sessions and the SSH connection. No reconnect will be attempted. Context context.Context // CtxDone is a channel that will send a time.Time when the main context is canceled. CtxDone chan time.Time // contains filtered or unexported fields }
Client represents a connection to a remote host.
func (*Client) AddSession ¶
func (c *Client) AddSession(id string, command string, buffer int, readTimeout, writeTimeout time.Duration) (*Session, error)
AddSession represents a bi-directional pipe over SSH. It creates a new session with the given id, command, buffer size and timeout. The buffer size is the size of the channel buffer for the input and output channels. Session implemnts the io.ReadWriteCloser interface and is resilient to network issues.
func (*Client) RemoveSession ¶
RemoveSession removes a session by id.
type ReconnectReadWriteCloser ¶
type ReconnectReadWriteCloser struct { Context context.Context Logger *slog.Logger SSHClientInfo *SSHClientInfo Buffer int Timeout time.Duration ID string Command string Client *Client Session *Session // contains filtered or unexported fields }
ReconnectReadWriteCloser reconnects even if the initial connection fails.
func NewReconnectReadWriteCloser ¶
func NewReconnectReadWriteCloser(context context.Context, logger *slog.Logger, info *SSHClientInfo, id, command string, buffer int, timeout time.Duration) *ReconnectReadWriteCloser
NewReconnectWriter creates a new writer and reconnects even if the initial connection fails.
func (*ReconnectReadWriteCloser) Close ¶
func (r *ReconnectReadWriteCloser) Close() error
Close closes the reconnect connection.
func (*ReconnectReadWriteCloser) Read ¶
func (r *ReconnectReadWriteCloser) Read(p []byte) (n int, err error)
Read reads from reconnect connection.
func (*ReconnectReadWriteCloser) Setup ¶
func (r *ReconnectReadWriteCloser) Setup() error
Setup sets up the reconnect writer.
type SSHClientInfo ¶
type SSHClientInfo struct { RemoteHost string RemoteHostname string RemoteUser string KeyLocation string KeyPassphrase string }
SSHClientInfo represents the SSH connection information.
type Session ¶
type Session struct { Context context.Context Logger *slog.Logger Client *Client Done chan struct{} In chan SendData Out chan SendData ReadTimeout time.Duration WriteTimeout time.Duration ID string Command string BufferSize int Session *ssh.Session StdinPipe io.WriteCloser StdoutPipe io.Reader // contains filtered or unexported fields }
Session represents a session to a remote command.