sshx

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HostKeyAlgorithmsFor added in v0.1.2

func HostKeyAlgorithmsFor(knownHostsPath, host string) []string

HostKeyAlgorithmsFor returns the host-key algorithm names that the user's known_hosts file has entries for, so we can advertise only those. Without this, the server may present (say) an RSA key for a host we only have an ed25519 entry for, and the knownhosts callback errors with "key mismatch" even though the same hostname has the same key the user is happy with from their CLI. Returns nil if the file is missing or has no matching entries (caller should leave HostKeyAlgorithms unset → Go's defaults).

func HostKeyCallback

func HostKeyCallback(insecure bool, knownHostsPath string) (ssh.HostKeyCallback, error)

Types

type AuthSpec

type AuthSpec struct {
	KeyPath       string `json:"key_path,omitempty"`
	KeyPassphrase string `json:"key_passphrase,omitempty"`
	UseAgent      bool   `json:"use_agent,omitempty"`
	// AgentSocket overrides the default $SSH_AUTH_SOCK lookup. Useful when
	// the daemon was started without the env var (e.g. spawned by an MCP
	// host that doesn't propagate user env), or when you want to point at
	// a specific agent like 1Password's (~/Library/.../1password/t/agent.sock)
	// or `gpg-agent --enable-ssh-support`.
	AgentSocket string `json:"agent_socket,omitempty"`
	Password    string `json:"password,omitempty"`
}

func (AuthSpec) Build

func (a AuthSpec) Build() ([]ssh.AuthMethod, closerList, error)

Build returns the auth methods plus a Closer that releases anything Methods opened (currently just the ssh-agent socket). Always defer cl.Close() in the caller, even on error — Build closes its own state on error paths but the caller must close on the happy path too.

type ConnectSpec

type ConnectSpec struct {
	User           string     `json:"user"`
	Addresses      []string   `json:"addresses"`
	JumpHosts      []JumpHost `json:"jump_hosts,omitempty"`
	Auth           AuthSpec   `json:"auth"`
	KnownHostsPath string     `json:"known_hosts_path,omitempty"`
	Insecure       bool       `json:"insecure,omitempty"`
	Timeout        Duration   `json:"timeout,omitempty"`
	Keepalive      Duration   `json:"keepalive,omitempty"`
	AutoReconnect  bool       `json:"auto_reconnect,omitempty"`
	Persistent     bool       `json:"persistent,omitempty"`
	ForwardAgent   bool       `json:"forward_agent,omitempty"`
	// DisableSSHConfig skips the ~/.ssh/config resolution pass. By default
	// the daemon mirrors the `ssh` CLI: any field the caller doesn't set is
	// filled in from the user's OpenSSH config (Hostname, Port, User,
	// IdentityFile, IdentityAgent, ProxyJump). Set this to true for fully
	// explicit specs (e.g. CI, tests) so local user config can't influence
	// the connection.
	DisableSSHConfig bool `json:"disable_ssh_config,omitempty"`
}

type Duration

type Duration time.Duration

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

func (Duration) Std

func (d Duration) Std() time.Duration

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

type DynamicSpec

type DynamicSpec struct {
	BindAddr string `json:"bind_addr,omitempty"`
	BindPort int    `json:"bind_port"`
}

type ExecOptions

type ExecOptions struct {
	Env   map[string]string
	Stdin string
}

type ExecResult

type ExecResult struct {
	Stdout   string `json:"stdout"`
	Stderr   string `json:"stderr"`
	ExitCode int    `json:"exit_code"`
}

type FileEntry

type FileEntry struct {
	Name    string    `json:"name"`
	Size    int64     `json:"size"`
	Mode    string    `json:"mode"`
	ModTime time.Time `json:"mod_time"`
	IsDir   bool      `json:"is_dir"`
	Target  string    `json:"target,omitempty"`
}

type Forward

type Forward struct {
	ID        string
	SessionID string
	Kind      ForwardKind

	LocalSpec   *LocalSpec
	RemoteSpec  *RemoteSpec
	DynamicSpec *DynamicSpec
	// contains filtered or unexported fields
}

func (*Forward) Close

func (f *Forward) Close() error

func (*Forward) Info

func (f *Forward) Info() ForwardInfo

type ForwardInfo

type ForwardInfo struct {
	ID          string `json:"id"`
	SessionID   string `json:"session_id"`
	Kind        string `json:"kind"`
	BindAddr    string `json:"bind_addr"`
	BindPort    int    `json:"bind_port"` // effective port (may differ from user's BindPort=0)
	RemoteAddr  string `json:"remote_addr,omitempty"`
	RemotePort  int    `json:"remote_port,omitempty"`
	Connections int64  `json:"connections"`

	// User-supplied spec, preserved verbatim so persistence can replay
	// "any free port" (BindPort=0) instead of pinning the ephemeral port.
	LocalSpec   *LocalSpec   `json:"local_spec,omitempty"`
	RemoteSpec  *RemoteSpec  `json:"remote_spec,omitempty"`
	DynamicSpec *DynamicSpec `json:"dynamic_spec,omitempty"`
}

type ForwardKind

type ForwardKind string
const (
	ForwardLocal   ForwardKind = "local"
	ForwardRemote  ForwardKind = "remote"
	ForwardDynamic ForwardKind = "dynamic"
)

type JumpHost

type JumpHost struct {
	User           string   `json:"user"`
	Addresses      []string `json:"addresses"`
	Auth           AuthSpec `json:"auth,omitempty"`
	KnownHostsPath string   `json:"known_hosts_path,omitempty"`
	Insecure       bool     `json:"insecure,omitempty"`
	Timeout        Duration `json:"timeout,omitempty"`
}

type LocalSpec

type LocalSpec struct {
	BindAddr   string `json:"bind_addr,omitempty"`
	BindPort   int    `json:"bind_port"`
	RemoteHost string `json:"remote_host"`
	RemotePort int    `json:"remote_port"`
}

type Manager

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

func NewManager

func NewManager() *Manager

func (*Manager) CloneSession

func (m *Manager) CloneSession(srcID, newID string) (*Session, error)

func (*Manager) CloseAll

func (m *Manager) CloseAll()

func (*Manager) CloseForward

func (m *Manager) CloseForward(sessionID, forwardID string) error

func (*Manager) Connect

func (m *Manager) Connect(id string, spec ConnectSpec) (*Session, error)

func (*Manager) Disconnect

func (m *Manager) Disconnect(id string) error

func (*Manager) Get

func (m *Manager) Get(id string) (*Session, error)

func (*Manager) List

func (m *Manager) List() []SessionInfo

func (*Manager) ListForwards

func (m *Manager) ListForwards(sessionID string) ([]ForwardInfo, error)

func (*Manager) OpenDynamicForward

func (m *Manager) OpenDynamicForward(sessionID, forwardID string, spec DynamicSpec) (*Forward, error)

func (*Manager) OpenLocalForward

func (m *Manager) OpenLocalForward(sessionID, forwardID string, spec LocalSpec) (*Forward, error)

func (*Manager) OpenRemoteForward

func (m *Manager) OpenRemoteForward(sessionID, forwardID string, spec RemoteSpec) (*Forward, error)

func (*Manager) Reconnect

func (m *Manager) Reconnect(id string) (*Session, error)

func (*Manager) Sessions

func (m *Manager) Sessions() map[string]*Session

type RemoteSpec

type RemoteSpec struct {
	BindAddr  string `json:"bind_addr,omitempty"`
	BindPort  int    `json:"bind_port"`
	LocalHost string `json:"local_host"`
	LocalPort int    `json:"local_port"`
}

type Session

type Session struct {
	ID   string
	Spec ConnectSpec
	// contains filtered or unexported fields
}

func (*Session) CloseShell

func (sess *Session) CloseShell(shellID string) error

func (*Session) Download

func (s *Session) Download(remotePath, localPath string) (int64, error)

Download streams a remote file OR directory from the remote host over SFTP to the local filesystem. For a directory, the tree is mirrored under localPath with intermediate dirs created as needed. Symlinks are skipped.

func (*Session) Exec

func (s *Session) Exec(ctx context.Context, command string, opts ExecOptions) (*ExecResult, error)

func (*Session) FileChmod

func (s *Session) FileChmod(path string, mode os.FileMode) error

func (*Session) FileDelete

func (s *Session) FileDelete(path string) error

func (*Session) FileList

func (s *Session) FileList(path string) ([]FileEntry, error)

func (*Session) FileMkdir

func (s *Session) FileMkdir(path string, recursive bool) error

func (*Session) FileRead

func (s *Session) FileRead(path string, offset int64, length int64) ([]byte, error)

func (*Session) FileRename

func (s *Session) FileRename(from, to string) error

func (*Session) FileStat

func (s *Session) FileStat(path string) (*FileEntry, error)

func (*Session) FileWrite

func (s *Session) FileWrite(path string, data []byte, append bool) error

func (*Session) GetShell

func (sess *Session) GetShell(shellID string) (*Shell, error)

func (*Session) Info

func (s *Session) Info() SessionInfo

func (*Session) ListShells

func (sess *Session) ListShells() []ShellInfo

func (*Session) OpenShell

func (sess *Session) OpenShell(shellID string, opts ShellOptions) (*Shell, error)

func (*Session) Upload

func (s *Session) Upload(localPath, remotePath string) (int64, error)

Upload streams a local file OR directory to the remote host over SFTP. For a directory, the tree is mirrored under remotePath with intermediate dirs created as needed; permission bits are preserved on a best-effort basis. Symlinks are skipped (not followed, not recreated) to avoid escaping the source tree via a hostile or accidental local link.

type SessionInfo

type SessionInfo struct {
	ID            string    `json:"id"`
	User          string    `json:"user"`
	Addresses     []string  `json:"addresses"`
	ActiveAddress string    `json:"active_address,omitempty"`
	State         string    `json:"state"`
	LastError     string    `json:"last_error,omitempty"`
	ConnectedAt   time.Time `json:"connected_at,omitempty"`
	Forwards      []string  `json:"forwards"`
	Shells        []string  `json:"shells"`
	Persistent    bool      `json:"persistent"`
	AutoReconnect bool      `json:"auto_reconnect"`
}

func (SessionInfo) Row added in v0.1.5

func (i SessionInfo) Row() SessionRow

type SessionRow added in v0.1.5

type SessionRow struct {
	ID            string `json:"id"`
	User          string `json:"user"`
	Address       string `json:"address"` // active_address, else addresses[0]
	State         string `json:"state"`
	LastError     string `json:"last_error"` // empty when none; kept (no omitempty) so tabular form stays uniform
	Persistent    bool   `json:"persistent"`
	AutoReconnect bool   `json:"auto_reconnect"`
	Forwards      int    `json:"forwards"`
	Shells        int    `json:"shells"`
}

SessionRow is the compact, primitive-only projection used by ssh_list and status — every field is a scalar so TOON can emit the array in its compact tabular form (`[N]{fields}: row,row,row`) instead of the expanded per-element form. Detailed nested info (addresses list, attached forwards, shells) is available via ssh_info.

type SessionState

type SessionState string
const (
	StateConnecting   SessionState = "connecting"
	StateConnected    SessionState = "connected"
	StateReconnecting SessionState = "reconnecting"
	StateDisconnected SessionState = "disconnected"
)

type Shell

type Shell struct {
	ID        string
	SessionID string

	OpenedAt time.Time
	// contains filtered or unexported fields
}

func (*Shell) Close

func (s *Shell) Close() error

func (*Shell) Info

func (s *Shell) Info() ShellInfo

func (*Shell) Read

func (s *Shell) Read(timeout time.Duration) ([]byte, error)

Read drains any buffered output. If the buffer is empty, it waits up to `timeout` for new data and re-checks the buffer after each wake — the wake channel is a hint, the buffer is the source of truth.

func (*Shell) Resize

func (s *Shell) Resize(rows, cols int) error

func (*Shell) Write

func (s *Shell) Write(p []byte) (int, error)

type ShellInfo

type ShellInfo struct {
	ID         string    `json:"id"`
	SessionID  string    `json:"session_id"`
	OpenedAt   time.Time `json:"opened_at"`
	BufferSize int       `json:"buffered_bytes"`
	Closed     bool      `json:"closed"`
}

type ShellOptions

type ShellOptions struct {
	Term       string            `json:"term,omitempty"`
	Rows       int               `json:"rows,omitempty"`
	Cols       int               `json:"cols,omitempty"`
	Env        map[string]string `json:"env,omitempty"`
	Command    string            `json:"command,omitempty"`
	BufferSize int               `json:"buffer_size,omitempty"`
}

Jump to

Keyboard shortcuts

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