sshpool

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FrameStdout   byte = 0x01 // 远程 stdout (S→C)
	FrameStderr   byte = 0x02 // 远程 stderr (S→C)
	FrameStdin    byte = 0x03 // 输入到远程 stdin (C→S)
	FrameExitCode byte = 0x04 // 退出码 4 bytes int32 BE (S→C)
	FrameResize   byte = 0x05 // 终端大小 4 bytes: uint16 cols + uint16 rows (C→S)
	FrameError    byte = 0x06 // 错误信息 UTF-8 (S→C)
	FrameFileData byte = 0x07 // 文件数据块 (双向)
	FrameFileEOF  byte = 0x08 // 文件传输结束 (双向)
	FrameFileErr  byte = 0x09 // 文件操作错误 (S→C)
	FrameOK       byte = 0x0A // 操作成功 (S→C)

	// MaxFramePayload 最大帧负载大小 64KB
	MaxFramePayload = 64 * 1024
)

帧类型常量

Variables

This section is empty.

Functions

func ParseExitCode

func ParseExitCode(payload []byte) (int, error)

ParseExitCode 解析退出码帧负载

func ParseResize

func ParseResize(payload []byte) (cols, rows uint16, err error)

ParseResize 解析终端大小帧负载

func ReadFrame

func ReadFrame(r io.Reader) (byte, []byte, error)

ReadFrame 从 reader 读取一个帧 返回帧类型和负载

func SocketPath

func SocketPath(dataDir string) string

SocketPath 返回 sshpool socket 路径

func WriteError

func WriteError(w io.Writer, msg string) error

WriteError 写入错误帧

func WriteExitCode

func WriteExitCode(w io.Writer, code int) error

WriteExitCode 写入退出码帧

func WriteFrame

func WriteFrame(w io.Writer, frameType byte, payload []byte) error

WriteFrame 写入一个帧到 writer 帧格式: [Length: 4 bytes uint32 BE] [Type: 1 byte] [Payload: Length-1 bytes]

func WriteResize

func WriteResize(w io.Writer, cols, rows uint16) error

WriteResize 写入终端大小变更帧

Types

type Client

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

Client 连接到 SSH 代理 socket 的客户端

func NewClient

func NewClient(sockPath string) *Client

NewClient 创建客户端

func NewClientWithToken

func NewClientWithToken(sockPath, token string) *Client

NewClientWithToken 创建带认证 token 的客户端

func (*Client) Copy

func (c *Client) Copy(req ProxyRequest) error

Copy 通过代理进行远程到远程复制

func (*Client) Download

func (c *Client) Download(req ProxyRequest, localFile io.Writer) error

Download 通过代理下载文件

func (*Client) Exec

func (c *Client) Exec(req ProxyRequest, stdin io.Reader, stdout, stderr io.Writer) (int, error)

Exec 通过代理执行命令,返回退出码

func (*Client) InteractiveSSH

func (c *Client) InteractiveSSH(req ProxyRequest, stdin io.Reader, stdout io.Writer, resizeCh <-chan [2]uint16) (int, error)

InteractiveSSH 通过代理建立交互式 SSH 会话 resizeCh 传入终端大小变更通知,格式 [cols, rows]

func (*Client) IsAvailable

func (c *Client) IsAvailable() bool

IsAvailable 检测 proxy socket 是否可连

func (*Client) Upload

func (c *Client) Upload(req ProxyRequest, localFile io.Reader) error

Upload 通过代理上传文件

type Pool

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

Pool SSH 连接池

func NewPool

func NewPool(dialer PoolDialer, idleTimeout time.Duration) *Pool

NewPool 创建连接池

func (*Pool) Close

func (p *Pool) Close()

Close 关闭连接池(可安全多次调用)

func (*Pool) Get

func (p *Pool) Get(ctx context.Context, assetID int64) (*ssh.Client, error)

Get 获取或创建一个 SSH 连接,调用方用完后必须调用 Release

func (*Pool) List

func (p *Pool) List() []PoolEntryInfo

List 返回所有连接池条目信息

func (*Pool) Release

func (p *Pool) Release(assetID int64)

Release 释放连接引用

func (*Pool) Remove

func (p *Pool) Remove(assetID int64)

Remove 强制移除并关闭连接

type PoolDialer

type PoolDialer interface {
	DialAsset(ctx context.Context, assetID int64) (*ssh.Client, []io.Closer, error)
}

PoolDialer 创建 SSH 连接的接口,由调用方实现以解耦凭据解析和跳板机逻辑

type PoolEntryInfo

type PoolEntryInfo struct {
	AssetID  int64 `json:"asset_id"`
	RefCount int   `json:"ref_count"`
	LastUsed int64 `json:"last_used"` // Unix timestamp
}

PoolEntryInfo 连接池条目信息(用于 UI 展示)

type ProxyRequest

type ProxyRequest struct {
	Token      string `json:"token,omitempty"` // 认证 token
	Op         string `json:"op"`              // "exec" | "upload" | "download" | "copy"
	AssetID    int64  `json:"asset_id"`
	Command    string `json:"command,omitempty"`
	Cols       int    `json:"cols,omitempty"`
	Rows       int    `json:"rows,omitempty"`
	PTY        bool   `json:"pty,omitempty"`
	SrcAssetID int64  `json:"src_asset_id,omitempty"` // copy: 源资产
	SrcPath    string `json:"src_path,omitempty"`
	DstPath    string `json:"dst_path,omitempty"`
}

ProxyRequest 代理请求(JSON 握手消息)

type ProxyResponse

type ProxyResponse struct {
	OK    bool   `json:"ok"`
	Error string `json:"error,omitempty"`
}

ProxyResponse 代理响应(JSON 握手响应)

type Server

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

Server SSH 代理 Unix socket 服务端

func NewServer

func NewServer(pool *Pool, authToken string) *Server

NewServer 创建代理服务端

func (*Server) Start

func (s *Server) Start(socketPath string) error

Start 开始监听 Unix socket

func (*Server) Stop

func (s *Server) Stop()

Stop 停止服务

Jump to

Keyboard shortcuts

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