ssh

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: 34 Imported by: 0

Documentation

Overview

Package ssh 实现 ssh binder:SSH 终端、SFTP、端口转发。共享 sshManager + 三张 pending sync.Map。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ForwardConfigWithStatus

type ForwardConfigWithStatus struct {
	forward_entity.ForwardConfig
	AssetName string           `json:"assetName"`
	Rules     []RuleWithStatus `json:"rules"`
	Status    string           `json:"status"` // "running" | "partial" | "error" | "stopped"
}

ForwardConfigWithStatus 配置 + 规则 + 运行状态

type ForwardManager

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

ForwardManager 管理端口转发的运行时状态

func NewForwardManager

func NewForwardManager(dialer SSHDialer) *ForwardManager

func (*ForwardManager) GetConfigStatus

func (m *ForwardManager) GetConfigStatus(configID int64) string

GetConfigStatus 获取指定配置的运行状态

func (*ForwardManager) GetRuleStatus

func (m *ForwardManager) GetRuleStatus(ruleID int64) RuleStatus

GetRuleStatus 获取单条规则的运行状态

func (*ForwardManager) IsConfigRunning

func (m *ForwardManager) IsConfigRunning(configID int64) bool

IsConfigRunning 检查配置是否有运行中的规则

func (*ForwardManager) StartConfig

func (m *ForwardManager) StartConfig(ctx context.Context, configID int64) error

StartConfig 启动一个转发配置的所有规则

func (*ForwardManager) StopAll

func (m *ForwardManager) StopAll()

StopAll 停止所有转发

func (*ForwardManager) StopConfig

func (m *ForwardManager) StopConfig(configID int64)

StopConfig 停止一个转发配置的所有规则

type LangProvider

type LangProvider interface {
	Lang() string
}

LangProvider 由 system binder 实现,提供当前 UI 语言。

type LocalSSHKeyInfo

type LocalSSHKeyInfo struct {
	Path        string `json:"path"`
	KeyType     string `json:"keyType"`
	Fingerprint string `json:"fingerprint"`
	IsEncrypted bool   `json:"isEncrypted"`
}

LocalSSHKeyInfo 本地 SSH 密钥信息

type RuleStatus

type RuleStatus struct {
	RuleID int64  `json:"ruleId"`
	Status string `json:"status"` // "running" | "error" | "stopped"
	Error  string `json:"error,omitempty"`
}

RuleStatus 返回给前端的规则运行状态

type RuleWithStatus

type RuleWithStatus struct {
	forward_entity.ForwardRule
	Status string `json:"status"` // "running" | "error" | "stopped"
	Error  string `json:"error,omitempty"`
}

RuleWithStatus 规则 + 运行状态

type SSH

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

SSH binder:SSH 终端 + SFTP + 端口转发。

func New

func New(appCtx context.Context, lang LangProvider, mgr *ssh_svc.Manager, sftp *sftp_svc.Service, pool *sshpool.Pool) *SSH

New 构造 ssh binder。manager/sftp/pool 由 main.go 创建后注入。

func (*SSH) CancelSSHConnect

func (s *SSH) CancelSSHConnect(connectionID string)

CancelSSHConnect 取消异步 SSH 连接

func (*SSH) ChangeSSHDirectory

func (s *SSH) ChangeSSHDirectory(sessionID, targetPath string) error

ChangeSSHDirectory 请求当前终端切换到指定目录。

func (*SSH) Cleanup

func (s *SSH) Cleanup()

Cleanup ssh manager 由前端通过 Disconnect 主动关;这里只是占位。

func (*SSH) ConnectSSH

func (s *SSH) ConnectSSH(req SSHConnectRequest) (string, error)

ConnectSSH 连接 SSH 服务器,返回会话 ID

func (*SSH) ConnectSSHAsync

func (s *SSH) ConnectSSHAsync(req SSHConnectRequest) (string, error)

ConnectSSHAsync 异步连接 SSH 服务器,立即返回 connectionId,通过事件推送进度

func (*SSH) CreateForwardConfig

func (s *SSH) CreateForwardConfig(name string, assetID int64, rules []forward_entity.ForwardRule) (*forward_entity.ForwardConfig, error)

CreateForwardConfig 创建转发配置

func (*SSH) DeleteForwardConfig

func (s *SSH) DeleteForwardConfig(id int64) error

DeleteForwardConfig 删除转发配置

func (*SSH) DisconnectSSH

func (s *SSH) DisconnectSSH(sessionID string)

DisconnectSSH 断开 SSH 连接

func (*SSH) EnableSSHSync

func (s *SSH) EnableSSHSync(sessionID string) error

EnableSSHSync 显式启用目录同步。

func (*SSH) GetSSHPoolConnections

func (s *SSH) GetSSHPoolConnections() []sshpool.PoolEntryInfo

GetSSHPoolConnections 返回连接池中的活跃连接信息(供前端展示)

func (*SSH) GetSSHServerStatus added in v1.9.0

func (s *SSH) GetSSHServerStatus(sessionID string) (*server_status_svc.Snapshot, error)

GetSSHServerStatus 查询当前 SSH 会话对应服务器的状态快照。

func (*SSH) GetSSHSyncState

func (s *SSH) GetSSHSyncState(sessionID string) (ssh_svc.DirectorySyncState, error)

GetSSHSyncState 返回会话当前的目录同步状态。

func (*SSH) ListForwardConfigs

func (s *SSH) ListForwardConfigs() ([]ForwardConfigWithStatus, error)

ListForwardConfigs 列出所有转发配置(含规则和运行状态)

func (*SSH) ListLocalSSHKeys

func (s *SSH) ListLocalSSHKeys() ([]LocalSSHKeyInfo, error)

ListLocalSSHKeys 扫描 ~/.ssh 目录,返回有效的私钥列表

func (*SSH) OpenSFTPSession added in v1.11.0

func (s *SSH) OpenSFTPSession(assetID int64) (string, error)

OpenSFTPSession 建立一个无终端 PTY 的 SSH 会话,用于文件管理器。

func (*SSH) ResizeSSH

func (s *SSH) ResizeSSH(sessionID string, cols int, rows int) error

ResizeSSH 调整终端尺寸

func (*SSH) RespondAuthChallenge

func (s *SSH) RespondAuthChallenge(challengeID string, answers []string)

RespondAuthChallenge 前端响应 keyboard-interactive 认证质询

func (*SSH) RespondHostKeyVerify

func (s *SSH) RespondHostKeyVerify(verifyID string, action int)

RespondHostKeyVerify 前端响应主机密钥校验 action: 0=AcceptAndSave, 1=AcceptOnce, 2=Reject

func (*SSH) SFTPApplyPermissions

func (s *SSH) SFTPApplyPermissions(sessionID string, req sftp_svc.PermissionApplyRequest) error

SFTPApplyPermissions 修改远程权限与属主。

func (*SSH) SFTPCancelTransfer

func (s *SSH) SFTPCancelTransfer(transferID string)

SFTPCancelTransfer 取消传输

func (*SSH) SFTPCreateFile

func (s *SSH) SFTPCreateFile(sessionID, remotePath string) error

SFTPCreateFile 新建空远程文件。

func (*SSH) SFTPDelete

func (s *SSH) SFTPDelete(sessionID, remotePath string, isDir bool) error

SFTPDelete 删除远程文件或目录

func (*SSH) SFTPDownload

func (s *SSH) SFTPDownload(sessionID, remotePath string) (string, error)

SFTPDownload 下载文件

func (*SSH) SFTPDownloadDir

func (s *SSH) SFTPDownloadDir(sessionID, remotePath string) (string, error)

SFTPDownloadDir 下载目录

func (*SSH) SFTPGetwd

func (s *SSH) SFTPGetwd(sessionID string) (string, error)

SFTPGetwd 获取远程工作目录(用户 home)

func (*SSH) SFTPListDir

func (s *SSH) SFTPListDir(sessionID, dirPath string) ([]sftp_svc.FileEntry, error)

SFTPListDir 列出远程目录内容

func (*SSH) SFTPMkdir

func (s *SSH) SFTPMkdir(sessionID, remotePath string) error

SFTPMkdir 新建远程目录。

func (*SSH) SFTPPaste

func (s *SSH) SFTPPaste(req sftp_svc.PasteRequest) error

SFTPPaste 粘贴远程文件/目录剪贴板内容。

func (*SSH) SFTPProperties

func (s *SSH) SFTPProperties(sessionID, remotePath string) (sftp_svc.FileProperties, error)

SFTPProperties 获取远程文件/目录属性。 目录递归统计有 5s 超时保护,超时时返回近似值并置 truncated=true。

func (*SSH) SFTPRename

func (s *SSH) SFTPRename(sessionID, oldPath, newPath string) error

SFTPRename 重命名或移动远程路径。

func (*SSH) SFTPUpload

func (s *SSH) SFTPUpload(sessionID, remotePath string) (string, error)

SFTPUpload 上传文件:弹出本地文件选择 → 上传到 remotePath

func (*SSH) SFTPUploadDir

func (s *SSH) SFTPUploadDir(sessionID, remotePath string) (string, error)

SFTPUploadDir 上传目录

func (*SSH) SFTPUploadFile

func (s *SSH) SFTPUploadFile(sessionID, localPath, remotePath string) (string, error)

SFTPUploadFile 直接上传本地文件或目录(不弹对话框,用于拖拽上传)

func (*SSH) SelectSSHKeyFile

func (s *SSH) SelectSSHKeyFile() (*LocalSSHKeyInfo, error)

SelectSSHKeyFile 打开文件选择框选择密钥文件,默认定位到 ~/.ssh

func (*SSH) SplitSSH

func (s *SSH) SplitSSH(existingSessionID string, cols, rows int) (string, error)

SplitSSH 在已有会话的连接上创建新会话(分割窗格复用连接)

func (*SSH) StartForwardConfig

func (s *SSH) StartForwardConfig(id int64) error

StartForwardConfig 启动转发配置

func (*SSH) Startup

func (s *SSH) Startup(ctx context.Context)

Startup 保存 Wails ctx,方便 EventsEmit 用。

func (*SSH) StopForwardConfig

func (s *SSH) StopForwardConfig(id int64)

StopForwardConfig 停止转发配置

func (*SSH) UpdateAssetPassword

func (s *SSH) UpdateAssetPassword(assetID int64, password string) error

UpdateAssetPassword 更新资产的保存密码

func (*SSH) UpdateForwardConfig

func (s *SSH) UpdateForwardConfig(id int64, name string, assetID int64, rules []forward_entity.ForwardRule) (*forward_entity.ForwardConfig, error)

UpdateForwardConfig 更新转发配置(如果正在运行,先停止再更新再启动)

func (*SSH) WriteSSH

func (s *SSH) WriteSSH(sessionID string, dataB64 string) error

WriteSSH 向 SSH 终端写入数据(base64 编码)

func (*SSH) ZmodemAbortDownload added in v1.9.0

func (s *SSH) ZmodemAbortDownload(transferID string) error

ZmodemAbortDownload 取消下载:删除半截文件,置为已取消状态。

func (*SSH) ZmodemAbortUpload added in v1.9.0

func (s *SSH) ZmodemAbortUpload(transferID string) error

ZmodemAbortUpload 取消上传:关闭读句柄(不删本地源文件),置为已取消状态。

func (*SSH) ZmodemAppendChunk added in v1.9.0

func (s *SSH) ZmodemAppendChunk(transferID, dataB64 string) error

ZmodemAppendChunk 把一段已接收的 base64 字节追加写入下载文件。

func (*SSH) ZmodemBeginDownload added in v1.9.0

func (s *SSH) ZmodemBeginDownload(sessionID, suggestedName string, size int64) (string, error)

ZmodemBeginDownload 弹原生 Save 对话框并创建本地文件,返回 transferID。 用户取消对话框时返回空字符串(前端据此 offer.skip())。

func (*SSH) ZmodemFinishDownload added in v1.9.0

func (s *SSH) ZmodemFinishDownload(transferID string) error

ZmodemFinishDownload 关闭下载文件,置 done。

func (*SSH) ZmodemFinishUpload added in v1.9.0

func (s *SSH) ZmodemFinishUpload(transferID string) error

ZmodemFinishUpload 关闭上传读句柄,置 done。

func (*SSH) ZmodemOpenUploadFiles added in v1.9.0

func (s *SSH) ZmodemOpenUploadFiles(sessionID string, paths []string) ([]ZmodemUploadFile, error)

ZmodemOpenUploadFiles 打开前端拖拽传入的本地文件路径,逐个登记上传句柄。 目录会被跳过;用户可使用 SFTP 面板上传目录。

func (*SSH) ZmodemPickUploadFiles added in v1.9.0

func (s *SSH) ZmodemPickUploadFiles(sessionID string) ([]ZmodemUploadFile, error)

ZmodemPickUploadFiles 弹原生多选对话框,逐个打开并登记上传句柄,返回文件列表。 用户取消时返回空列表(前端据此 session.close())。

func (*SSH) ZmodemReadChunk added in v1.9.0

func (s *SSH) ZmodemReadChunk(transferID string, n int) (ZmodemChunk, error)

ZmodemReadChunk 读取至多 n 字节的上传内容,base64 返回,并标记是否读到文件尾。

type SSHConnectEvent

type SSHConnectEvent struct {
	Type        string   `json:"type"`
	Step        string   `json:"step,omitempty"`
	Message     string   `json:"message,omitempty"`
	SessionID   string   `json:"sessionId,omitempty"`
	Error       string   `json:"error,omitempty"`
	AuthFailed  bool     `json:"authFailed,omitempty"`
	ChallengeID string   `json:"challengeId,omitempty"`
	Prompts     []string `json:"prompts,omitempty"`
	Echo        []bool   `json:"echo,omitempty"`

	HostKeyVerifyID string                `json:"hostKeyVerifyId,omitempty"`
	HostKeyEvent    *ssh_svc.HostKeyEvent `json:"hostKeyEvent,omitempty"`
}

SSHConnectEvent SSH 异步连接进度事件

type SSHConnectRequest

type SSHConnectRequest struct {
	AssetID  int64  `json:"assetId"`
	Password string `json:"password"`
	Key      string `json:"key"`
	Cols     int    `json:"cols"`
	Rows     int    `json:"rows"`
	// InitialWorkdir 仅重连时由前端携带上次已知 cwd,请求新会话 cd 回该目录;
	// 首次连接为空。是否真正恢复由资产的 RestoreCwdOnReconnect 开关决定。
	InitialWorkdir string `json:"initialWorkdir"`
}

SSHConnectRequest 前端 SSH 连接请求

type SSHDialer

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

SSHDialer SSH 连接接口,供 ForwardManager 使用

type ZmodemChunk added in v1.9.0

type ZmodemChunk struct {
	Data string `json:"data"` // base64
	EOF  bool   `json:"eof"`
}

ZmodemChunk 是一次上传读块的结果:base64 数据 + 是否已读到文件尾。

type ZmodemUploadFile added in v1.9.0

type ZmodemUploadFile struct {
	TransferID string `json:"transferId"`
	Name       string `json:"name"`
	Size       int64  `json:"size"`
	Mtime      int64  `json:"mtime"` // Unix 秒
}

ZmodemUploadFile 是一个待上传文件的句柄信息,回传给前端逐个 send_offer。

Jump to

Keyboard shortcuts

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