refactor: 重构 starssh 核心运行时并补强 ssh/exec/terminal/sftp 能力
- 拆分原有单体 ssh.go,按职责重组为 types、utils、transport、login、keepalive、session、exec、pool、shell、terminal、forward、hostkey、state 等模块,并补充平台相关实现 - 重做登录与连接运行时,补齐基于 context 的建连、jump/proxy 链路、可配置认证顺序,以及 Unix/Windows 下的 ssh-agent 支持 - 新增正式非交互执行模型 ExecRequest/ExecResult,支持流式输出、溢出统计、超时控制,以及 posix/powershell/cmd/raw 多方言执行 - 保留旧 shell 风格兼容接口,同时让路径/用户探测等 helper 具备跨 shell fallback,避免 Windows 目标继续硬依赖 POSIX 命令 - 新增 TerminalSession 作为原始交互终端基座,提供 IO attach、resize、signal/control、退出状态与关闭原因管理 - 重构端口转发语义,默认复用当前 SSH 连接,并显式提供 detached 的本地/动态转发模式承载隔离场景 - 梳理 keepalive 与取消语义,区分仅取消本次操作和关闭整条连接,并统一连接状态与传输关闭路径 - 围绕新的 session/连接生命周期重做执行池与运行时支撑 - 大幅增强 SFTP 传输链路,补齐更安全的原子替换、校验、进度回调、重试隔离、可复用 client 生命周期与失败语义 - 新增取消语义、keepalive、SFTP、forward、terminal input 等关键回归测试,提升核心链路稳定性
This commit is contained in:
+135
@@ -0,0 +1,135 @@
|
||||
package starssh
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var sendKeepAliveRequest = func(ctx context.Context, client sshClientRequester) error {
|
||||
_, _, err := client.SendRequest("keepalive@openssh.com", true, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *StarSSH) Ping() error {
|
||||
return s.PingContext(context.Background())
|
||||
}
|
||||
|
||||
func (s *StarSSH) PingContext(ctx context.Context) error {
|
||||
return s.pingContext(ctx, nil)
|
||||
}
|
||||
|
||||
func (s *StarSSH) PingContextCloseOnCancel(ctx context.Context) error {
|
||||
return s.pingContext(ctx, s.Close)
|
||||
}
|
||||
|
||||
func (s *StarSSH) pingContext(ctx context.Context, onCancel func() error) error {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
client, err := s.requireSSHClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
runCancel := func() {}
|
||||
if onCancel != nil {
|
||||
var cancelOnce sync.Once
|
||||
runCancel = func() {
|
||||
cancelOnce.Do(func() {
|
||||
_ = onCancel()
|
||||
})
|
||||
}
|
||||
|
||||
cancelDone := make(chan struct{})
|
||||
defer close(cancelDone)
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
runCancel()
|
||||
case <-cancelDone:
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
requestFunc := sendKeepAliveRequest
|
||||
pingErr := make(chan error, 1)
|
||||
go func() {
|
||||
err := requestFunc(ctx, client)
|
||||
select {
|
||||
case pingErr <- err:
|
||||
default:
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-pingErr:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
runCancel()
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StarSSH) startAutoKeepAlive() {
|
||||
if s == nil || s.snapshotSSHClient() == nil {
|
||||
return
|
||||
}
|
||||
|
||||
interval := s.LoginInfo.KeepAliveInterval
|
||||
if interval <= 0 {
|
||||
return
|
||||
}
|
||||
timeout := s.LoginInfo.KeepAliveTimeout
|
||||
if timeout <= 0 {
|
||||
timeout = defaultKeepAliveTimeout
|
||||
}
|
||||
|
||||
stop := make(chan struct{})
|
||||
done := make(chan struct{})
|
||||
|
||||
s.keepaliveMu.Lock()
|
||||
if s.keepaliveStop != nil {
|
||||
s.keepaliveMu.Unlock()
|
||||
return
|
||||
}
|
||||
s.keepaliveStop = stop
|
||||
s.keepaliveDone = done
|
||||
s.keepaliveMu.Unlock()
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
defer close(done)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
case <-ticker.C:
|
||||
pingCtx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
err := s.pingContext(pingCtx, nil)
|
||||
cancel()
|
||||
if err != nil {
|
||||
s.closeFromKeepAlive()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *StarSSH) stopAutoKeepAlive() {
|
||||
stop, done := s.takeKeepaliveHandles()
|
||||
if stop != nil {
|
||||
close(stop)
|
||||
}
|
||||
if done != nil {
|
||||
<-done
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StarSSH) closeFromKeepAlive() {
|
||||
_ = s.closeTransport(false)
|
||||
}
|
||||
Reference in New Issue
Block a user