- 拆分原有单体 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 等关键回归测试,提升核心链路稳定性
173 lines
4.7 KiB
Go
173 lines
4.7 KiB
Go
package starssh
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
func TestPingContextDoesNotCloseConnectionOnCancel(t *testing.T) {
|
|
oldSendKeepAliveRequest := sendKeepAliveRequest
|
|
oldCloseSSHClient := closeSSHClient
|
|
t.Cleanup(func() {
|
|
sendKeepAliveRequest = oldSendKeepAliveRequest
|
|
closeSSHClient = oldCloseSSHClient
|
|
})
|
|
|
|
sendKeepAliveRequest = func(ctx context.Context, client sshClientRequester) error {
|
|
<-ctx.Done()
|
|
time.Sleep(20 * time.Millisecond)
|
|
return ctx.Err()
|
|
}
|
|
|
|
var closeCalls atomic.Int32
|
|
closeSSHClient = func(client sshClientRequester) error {
|
|
closeCalls.Add(1)
|
|
return nil
|
|
}
|
|
|
|
client := &ssh.Client{}
|
|
star := &StarSSH{}
|
|
star.setTransport(client, nil)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
|
defer cancel()
|
|
|
|
err := star.PingContext(ctx)
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("expected deadline exceeded, got %v", err)
|
|
}
|
|
if closeCalls.Load() != 0 {
|
|
t.Fatalf("expected PingContext to keep connection open, close calls=%d", closeCalls.Load())
|
|
}
|
|
if got := star.snapshotSSHClient(); got != client {
|
|
t.Fatal("expected ssh client to remain attached after PingContext cancel")
|
|
}
|
|
}
|
|
|
|
func TestPingContextCloseOnCancelClosesConnection(t *testing.T) {
|
|
oldSendKeepAliveRequest := sendKeepAliveRequest
|
|
oldCloseSSHClient := closeSSHClient
|
|
t.Cleanup(func() {
|
|
sendKeepAliveRequest = oldSendKeepAliveRequest
|
|
closeSSHClient = oldCloseSSHClient
|
|
})
|
|
|
|
sendKeepAliveRequest = func(ctx context.Context, client sshClientRequester) error {
|
|
<-ctx.Done()
|
|
time.Sleep(20 * time.Millisecond)
|
|
return ctx.Err()
|
|
}
|
|
|
|
var closeCalls atomic.Int32
|
|
closeSSHClient = func(client sshClientRequester) error {
|
|
closeCalls.Add(1)
|
|
return nil
|
|
}
|
|
|
|
star := &StarSSH{}
|
|
star.setTransport(&ssh.Client{}, nil)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
|
defer cancel()
|
|
|
|
err := star.PingContextCloseOnCancel(ctx)
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("expected deadline exceeded, got %v", err)
|
|
}
|
|
if closeCalls.Load() != 1 {
|
|
t.Fatalf("expected exactly one close call, got %d", closeCalls.Load())
|
|
}
|
|
if got := star.snapshotSSHClient(); got != nil {
|
|
t.Fatal("expected ssh client to be detached after PingContextCloseOnCancel")
|
|
}
|
|
}
|
|
|
|
func TestDialTCPContextDoesNotCloseConnectionOnCancel(t *testing.T) {
|
|
oldDialSSHClient := dialSSHClient
|
|
oldCloseSSHClient := closeSSHClient
|
|
t.Cleanup(func() {
|
|
dialSSHClient = oldDialSSHClient
|
|
closeSSHClient = oldCloseSSHClient
|
|
})
|
|
|
|
dialSSHClient = func(ctx context.Context, client *ssh.Client, network, address string) (net.Conn, error) {
|
|
<-ctx.Done()
|
|
time.Sleep(20 * time.Millisecond)
|
|
return nil, ctx.Err()
|
|
}
|
|
|
|
var closeCalls atomic.Int32
|
|
closeSSHClient = func(client sshClientRequester) error {
|
|
closeCalls.Add(1)
|
|
return nil
|
|
}
|
|
|
|
client := &ssh.Client{}
|
|
star := &StarSSH{}
|
|
star.setTransport(client, nil)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
|
defer cancel()
|
|
|
|
conn, err := star.DialTCPContext(ctx, "tcp", "127.0.0.1:22")
|
|
if conn != nil {
|
|
t.Fatal("expected nil connection on canceled dial")
|
|
}
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("expected deadline exceeded, got %v", err)
|
|
}
|
|
if closeCalls.Load() != 0 {
|
|
t.Fatalf("expected DialTCPContext to keep connection open, close calls=%d", closeCalls.Load())
|
|
}
|
|
if got := star.snapshotSSHClient(); got != client {
|
|
t.Fatal("expected ssh client to remain attached after DialTCPContext cancel")
|
|
}
|
|
}
|
|
|
|
func TestDialTCPContextCloseOnCancelClosesConnection(t *testing.T) {
|
|
oldDialSSHClient := dialSSHClient
|
|
oldCloseSSHClient := closeSSHClient
|
|
t.Cleanup(func() {
|
|
dialSSHClient = oldDialSSHClient
|
|
closeSSHClient = oldCloseSSHClient
|
|
})
|
|
|
|
dialSSHClient = func(ctx context.Context, client *ssh.Client, network, address string) (net.Conn, error) {
|
|
<-ctx.Done()
|
|
time.Sleep(20 * time.Millisecond)
|
|
return nil, ctx.Err()
|
|
}
|
|
|
|
var closeCalls atomic.Int32
|
|
closeSSHClient = func(client sshClientRequester) error {
|
|
closeCalls.Add(1)
|
|
return nil
|
|
}
|
|
|
|
star := &StarSSH{}
|
|
star.setTransport(&ssh.Client{}, nil)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
|
defer cancel()
|
|
|
|
conn, err := star.DialTCPContextCloseOnCancel(ctx, "tcp", "127.0.0.1:22")
|
|
if conn != nil {
|
|
t.Fatal("expected nil connection on canceled dial")
|
|
}
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("expected deadline exceeded, got %v", err)
|
|
}
|
|
if closeCalls.Load() != 1 {
|
|
t.Fatalf("expected exactly one close call, got %d", closeCalls.Load())
|
|
}
|
|
if got := star.snapshotSSHClient(); got != nil {
|
|
t.Fatal("expected ssh client to be detached after DialTCPContextCloseOnCancel")
|
|
}
|
|
}
|