Documentation
¶
Overview ¶
Package ssh 实现 ssh binder:SSH 终端、SFTP、端口转发。共享 sshManager + 三张 pending sync.Map。
Index ¶
- type ForwardConfigWithStatus
- type ForwardManager
- func (m *ForwardManager) GetConfigStatus(configID int64) string
- func (m *ForwardManager) GetRuleStatus(ruleID int64) RuleStatus
- func (m *ForwardManager) IsConfigRunning(configID int64) bool
- func (m *ForwardManager) StartConfig(ctx context.Context, configID int64) error
- func (m *ForwardManager) StopAll()
- func (m *ForwardManager) StopConfig(configID int64)
- type LangProvider
- type LocalSSHKeyInfo
- type RuleStatus
- type RuleWithStatus
- type SSH
- func (s *SSH) CancelSSHConnect(connectionID string)
- func (s *SSH) ChangeSSHDirectory(sessionID, targetPath string) error
- func (s *SSH) Cleanup()
- func (s *SSH) ConnectSSH(req SSHConnectRequest) (string, error)
- func (s *SSH) ConnectSSHAsync(req SSHConnectRequest) (string, error)
- func (s *SSH) CreateForwardConfig(name string, assetID int64, rules []forward_entity.ForwardRule) (*forward_entity.ForwardConfig, error)
- func (s *SSH) DeleteForwardConfig(id int64) error
- func (s *SSH) DisconnectSSH(sessionID string)
- func (s *SSH) EnableSSHSync(sessionID string) error
- func (s *SSH) GetSSHPoolConnections() []sshpool.PoolEntryInfo
- func (s *SSH) GetSSHServerStatus(sessionID string) (*server_status_svc.Snapshot, error)
- func (s *SSH) GetSSHSyncState(sessionID string) (ssh_svc.DirectorySyncState, error)
- func (s *SSH) ListForwardConfigs() ([]ForwardConfigWithStatus, error)
- func (s *SSH) ListLocalSSHKeys() ([]LocalSSHKeyInfo, error)
- func (s *SSH) OpenSFTPSession(assetID int64) (string, error)
- func (s *SSH) ResizeSSH(sessionID string, cols int, rows int) error
- func (s *SSH) RespondAuthChallenge(challengeID string, answers []string)
- func (s *SSH) RespondHostKeyVerify(verifyID string, action int)
- func (s *SSH) SFTPApplyPermissions(sessionID string, req sftp_svc.PermissionApplyRequest) error
- func (s *SSH) SFTPCancelTransfer(transferID string)
- func (s *SSH) SFTPCreateFile(sessionID, remotePath string) error
- func (s *SSH) SFTPDelete(sessionID, remotePath string, isDir bool) error
- func (s *SSH) SFTPDownload(sessionID, remotePath string) (string, error)
- func (s *SSH) SFTPDownloadDir(sessionID, remotePath string) (string, error)
- func (s *SSH) SFTPGetwd(sessionID string) (string, error)
- func (s *SSH) SFTPListDir(sessionID, dirPath string) ([]sftp_svc.FileEntry, error)
- func (s *SSH) SFTPMkdir(sessionID, remotePath string) error
- func (s *SSH) SFTPPaste(req sftp_svc.PasteRequest) error
- func (s *SSH) SFTPProperties(sessionID, remotePath string) (sftp_svc.FileProperties, error)
- func (s *SSH) SFTPRename(sessionID, oldPath, newPath string) error
- func (s *SSH) SFTPUpload(sessionID, remotePath string) (string, error)
- func (s *SSH) SFTPUploadDir(sessionID, remotePath string) (string, error)
- func (s *SSH) SFTPUploadFile(sessionID, localPath, remotePath string) (string, error)
- func (s *SSH) SelectSSHKeyFile() (*LocalSSHKeyInfo, error)
- func (s *SSH) SplitSSH(existingSessionID string, cols, rows int) (string, error)
- func (s *SSH) StartForwardConfig(id int64) error
- func (s *SSH) Startup(ctx context.Context)
- func (s *SSH) StopForwardConfig(id int64)
- func (s *SSH) UpdateAssetPassword(assetID int64, password string) error
- func (s *SSH) UpdateForwardConfig(id int64, name string, assetID int64, rules []forward_entity.ForwardRule) (*forward_entity.ForwardConfig, error)
- func (s *SSH) WriteSSH(sessionID string, dataB64 string) error
- func (s *SSH) ZmodemAbortDownload(transferID string) error
- func (s *SSH) ZmodemAbortUpload(transferID string) error
- func (s *SSH) ZmodemAppendChunk(transferID, dataB64 string) error
- func (s *SSH) ZmodemBeginDownload(sessionID, suggestedName string, size int64) (string, error)
- func (s *SSH) ZmodemFinishDownload(transferID string) error
- func (s *SSH) ZmodemFinishUpload(transferID string) error
- func (s *SSH) ZmodemOpenUploadFiles(sessionID string, paths []string) ([]ZmodemUploadFile, error)
- func (s *SSH) ZmodemPickUploadFiles(sessionID string) ([]ZmodemUploadFile, error)
- func (s *SSH) ZmodemReadChunk(transferID string, n int) (ZmodemChunk, error)
- type SSHConnectEvent
- type SSHConnectRequest
- type SSHDialer
- type ZmodemChunk
- type ZmodemUploadFile
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) 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 ¶
CancelSSHConnect 取消异步 SSH 连接
func (*SSH) ChangeSSHDirectory ¶
ChangeSSHDirectory 请求当前终端切换到指定目录。
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 ¶
DeleteForwardConfig 删除转发配置
func (*SSH) EnableSSHSync ¶
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
OpenSFTPSession 建立一个无终端 PTY 的 SSH 会话,用于文件管理器。
func (*SSH) RespondAuthChallenge ¶
RespondAuthChallenge 前端响应 keyboard-interactive 认证质询
func (*SSH) RespondHostKeyVerify ¶
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 ¶
SFTPCancelTransfer 取消传输
func (*SSH) SFTPCreateFile ¶
SFTPCreateFile 新建空远程文件。
func (*SSH) SFTPDelete ¶
SFTPDelete 删除远程文件或目录
func (*SSH) SFTPDownload ¶
SFTPDownload 下载文件
func (*SSH) SFTPDownloadDir ¶
SFTPDownloadDir 下载目录
func (*SSH) SFTPListDir ¶
SFTPListDir 列出远程目录内容
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 ¶
SFTPRename 重命名或移动远程路径。
func (*SSH) SFTPUpload ¶
SFTPUpload 上传文件:弹出本地文件选择 → 上传到 remotePath
func (*SSH) SFTPUploadDir ¶
SFTPUploadDir 上传目录
func (*SSH) SFTPUploadFile ¶
SFTPUploadFile 直接上传本地文件或目录(不弹对话框,用于拖拽上传)
func (*SSH) SelectSSHKeyFile ¶
func (s *SSH) SelectSSHKeyFile() (*LocalSSHKeyInfo, error)
SelectSSHKeyFile 打开文件选择框选择密钥文件,默认定位到 ~/.ssh
func (*SSH) StartForwardConfig ¶
StartForwardConfig 启动转发配置
func (*SSH) UpdateAssetPassword ¶
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) ZmodemAbortDownload ¶ added in v1.9.0
ZmodemAbortDownload 取消下载:删除半截文件,置为已取消状态。
func (*SSH) ZmodemAbortUpload ¶ added in v1.9.0
ZmodemAbortUpload 取消上传:关闭读句柄(不删本地源文件),置为已取消状态。
func (*SSH) ZmodemAppendChunk ¶ added in v1.9.0
ZmodemAppendChunk 把一段已接收的 base64 字节追加写入下载文件。
func (*SSH) ZmodemBeginDownload ¶ added in v1.9.0
ZmodemBeginDownload 弹原生 Save 对话框并创建本地文件,返回 transferID。 用户取消对话框时返回空字符串(前端据此 offer.skip())。
func (*SSH) ZmodemFinishDownload ¶ added in v1.9.0
ZmodemFinishDownload 关闭下载文件,置 done。
func (*SSH) ZmodemFinishUpload ¶ added in v1.9.0
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
ZmodemChunk 是一次上传读块的结果:base64 数据 + 是否已读到文件尾。