ssh

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package ssh provides SSH tunnel connectivity for database drivers.

Index

Constants

View Source
const (
	DefaultKeepaliveInterval = 30 * time.Second
	DefaultKeepaliveCountMax = 3
)

Variables

This section is empty.

Functions

func DefaultKnownHostsPath

func DefaultKnownHostsPath() string

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps ssh.Client and provides DialContext for use by database drivers.

func Connect

func Connect(ctx context.Context, opts Options) (*Client, *errors.XError)

Connect establishes an SSH connection.

func (*Client) Alive added in v0.1.0

func (c *Client) Alive() bool

Alive reports whether the SSH connection is believed to be alive.

func (*Client) Close

func (c *Client) Close() error

Close closes the SSH connection.

func (*Client) DialContext

func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext establishes a connection to the target through the SSH tunnel.

func (*Client) SendKeepalive added in v0.1.0

func (c *Client) SendKeepalive() error

SendKeepalive sends a single SSH keepalive request and returns any error. A nil error means the connection is alive.

type Options

type Options struct {
	Host           string
	Port           int
	User           string
	IdentityFile   string // path to the private key
	Passphrase     string // private key passphrase (if any)
	KnownHostsFile string // defaults to ~/.ssh/known_hosts

	// SkipKnownHostsCheck disables known_hosts verification (strongly discouraged!).
	SkipKnownHostsCheck bool

	// KeepaliveInterval is the interval between SSH keepalive probes.
	// Zero or negative disables keepalive. Default: DefaultKeepaliveInterval.
	KeepaliveInterval time.Duration

	// KeepaliveCountMax is the maximum number of consecutive missed
	// keepalive responses before the connection is considered dead.
	// Default: DefaultKeepaliveCountMax.
	KeepaliveCountMax int
}

Options contains the parameters required for an SSH connection.

type ReconnectDialer added in v0.1.0

type ReconnectDialer struct {
	// contains filtered or unexported fields
}

ReconnectDialer wraps SSH Connect with automatic reconnection and keepalive. It implements the same DialContext/Close interface as *Client, making it a drop-in replacement wherever a Dialer is expected (e.g. proxy.Dialer).

func NewReconnectDialer added in v0.1.0

func NewReconnectDialer(ctx context.Context, opts Options, ropts ...ReconnectOption) (*ReconnectDialer, error)

NewReconnectDialer creates a ReconnectDialer that automatically reconnects on SSH connection failures. It establishes the initial connection and starts keepalive monitoring.

func (*ReconnectDialer) Close added in v0.1.0

func (rd *ReconnectDialer) Close() error

Close shuts down the dialer, stops keepalive, and closes the SSH connection.

func (*ReconnectDialer) DialContext added in v0.1.0

func (rd *ReconnectDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext dials the remote address through the SSH tunnel. If the dial fails, it attempts to reconnect and retry once.

type ReconnectOption added in v0.1.0

type ReconnectOption func(*ReconnectDialer)

ReconnectOption configures a ReconnectDialer.

func WithStatusCallback added in v0.1.0

func WithStatusCallback(fn func(StatusEvent)) ReconnectOption

WithStatusCallback sets a callback for connection status events.

type StatusEvent added in v0.1.0

type StatusEvent struct {
	Type    StatusType
	Message string
	Error   error
}

StatusEvent is emitted by ReconnectDialer when connection state changes.

type StatusType added in v0.1.0

type StatusType int

StatusType describes the kind of status event emitted by ReconnectDialer.

const (
	StatusConnected       StatusType = iota // initial connection established
	StatusDisconnected                      // connection detected as dead
	StatusReconnecting                      // reconnection attempt in progress
	StatusReconnected                       // reconnection succeeded
	StatusReconnectFailed                   // reconnection attempt failed
)

Jump to

Keyboard shortcuts

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