pipe

package
v0.0.0-...-4429484 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2024 License: MIT Imports: 14 Imported by: 1

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

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.

func Pub

func Pub(ctx context.Context, logger *slog.Logger, info *SSHClientInfo, cmd string) (io.WriteCloser, error)

Pub creates a new session with the given command.

func Sub

func Sub(ctx context.Context, logger *slog.Logger, info *SSHClientInfo, cmd string) (io.Reader, error)

Sub creates a new session with the given command..

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 NewClient

func NewClient(ctx context.Context, logger *slog.Logger, info *SSHClientInfo) (*Client, error)

NewClient creates a new pipe client.

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) Close

func (c *Client) Close() error

Close closes the SSH connection.

func (*Client) Open

func (c *Client) Open() error

Open opens the SSH connection.

func (*Client) RemoveSession

func (c *Client) RemoveSession(id string) error

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.

func (*ReconnectReadWriteCloser) Write

func (r *ReconnectReadWriteCloser) Write(p []byte) (n int, err error)

Write writes to the reconnect connection.

type SSHClientInfo

type SSHClientInfo struct {
	RemoteHost     string
	RemoteHostname string
	RemoteUser     string
	KeyLocation    string
	KeyPassphrase  string
}

SSHClientInfo represents the SSH connection information.

type SendData

type SendData struct {
	Data  []byte
	Error error
	N     int
}

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.

func (*Session) Cancel

func (s *Session) Cancel()

Cancel cancels the session.

func (*Session) Close

func (s *Session) Close() error

Close closes the session.

func (*Session) Open

func (s *Session) Open() error

Open opens a new session.

func (*Session) Read

func (s *Session) Read(data []byte) (int, error)

Read reads data from the session.

func (*Session) Reconnect

func (s *Session) Reconnect()

Reconnect reconnects the session.

func (*Session) Start

func (s *Session) Start()

Start starts the session handling.

func (*Session) Write

func (s *Session) Write(data []byte) (int, error)

Write writes data to the session.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL