- 拆分原有单体 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 等关键回归测试,提升核心链路稳定性
163 lines
3.7 KiB
Go
163 lines
3.7 KiB
Go
package starssh
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
ansiCSIRegexp = regexp.MustCompile(`\x1b\[[0-9;?]*[ -/]*[@-~]`)
|
|
ansiOSCRegexp = regexp.MustCompile(`\x1b\][^\x07]*(\x07|\x1b\\)`)
|
|
leadingIntRegexp = regexp.MustCompile(`^[+-]?\d+`)
|
|
)
|
|
|
|
func SedColor(str string) string {
|
|
return stripControlSequences(str)
|
|
}
|
|
|
|
func normalizeShellOutput(raw string) string {
|
|
return strings.TrimSpace(strings.ReplaceAll(raw, "\r\n", "\n"))
|
|
}
|
|
|
|
func stripControlSequences(raw string) string {
|
|
cleaned := ansiOSCRegexp.ReplaceAllString(raw, "")
|
|
cleaned = ansiCSIRegexp.ReplaceAllString(cleaned, "")
|
|
cleaned = strings.ReplaceAll(cleaned, "\r", "")
|
|
cleaned = strings.Map(func(r rune) rune {
|
|
if r == '\n' || r == '\t' {
|
|
return r
|
|
}
|
|
if r < 0x20 || r == 0x7f {
|
|
return -1
|
|
}
|
|
return r
|
|
}, cleaned)
|
|
return cleaned
|
|
}
|
|
|
|
func stripLeadingEcho(output string, command string, markerCommand string) string {
|
|
result := output
|
|
if command == "" {
|
|
return result
|
|
}
|
|
|
|
withMarker := command
|
|
if markerCommand != "" {
|
|
withMarker += "\n" + markerCommand
|
|
}
|
|
if strings.HasPrefix(result, withMarker) {
|
|
return strings.TrimSpace(strings.TrimPrefix(result, withMarker))
|
|
}
|
|
if strings.HasPrefix(result, command) {
|
|
return strings.TrimSpace(strings.TrimPrefix(result, command))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func collectLinesWithoutTokens(output string, tokens ...string) string {
|
|
lines := strings.Split(output, "\n")
|
|
filtered := make([]string, 0, len(lines))
|
|
|
|
for _, line := range lines {
|
|
skip := false
|
|
for _, token := range tokens {
|
|
if token != "" && strings.Contains(line, token) {
|
|
skip = true
|
|
break
|
|
}
|
|
}
|
|
if !skip {
|
|
filtered = append(filtered, line)
|
|
}
|
|
}
|
|
|
|
return strings.TrimSpace(strings.Join(filtered, "\n"))
|
|
}
|
|
|
|
func collectLinesForCommandOutput(output string, promptToken string, tokens ...string) string {
|
|
lines := strings.Split(output, "\n")
|
|
filtered := make([]string, 0, len(lines))
|
|
|
|
for _, line := range lines {
|
|
skip := false
|
|
for _, token := range tokens {
|
|
if token != "" && strings.Contains(line, token) {
|
|
skip = true
|
|
break
|
|
}
|
|
}
|
|
if !skip && promptToken != "" && strings.Contains(line, promptToken) {
|
|
skip = true
|
|
}
|
|
if !skip {
|
|
filtered = append(filtered, line)
|
|
}
|
|
}
|
|
|
|
return strings.TrimSpace(strings.Join(filtered, "\n"))
|
|
}
|
|
|
|
func shellSingleQuote(s string) string {
|
|
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
|
|
}
|
|
|
|
func normalizeBufferSize(bufcap int) int {
|
|
if bufcap <= 0 {
|
|
return defaultTransferBufferSize
|
|
}
|
|
return bufcap
|
|
}
|
|
|
|
func newCommandTokens() (beginToken string, endToken string) {
|
|
nonce := newNonce(8)
|
|
return "__STARSSH_BEGIN_" + nonce + "__", "__STARSSH_END_" + nonce + "__"
|
|
}
|
|
|
|
func newNonce(size int) string {
|
|
if size <= 0 {
|
|
size = 8
|
|
}
|
|
|
|
buf := make([]byte, size)
|
|
if _, err := rand.Read(buf); err != nil {
|
|
return fmt.Sprintf("%d", time.Now().UnixNano())
|
|
}
|
|
return strings.ToUpper(hex.EncodeToString(buf))
|
|
}
|
|
|
|
func splitByEndToken(output string, endToken string) (before string, exitCode int, found bool, parseErr error) {
|
|
prefix := endToken + ":"
|
|
lines := strings.Split(output, "\n")
|
|
beforeLines := make([]string, 0, len(lines))
|
|
|
|
for _, line := range lines {
|
|
trimmedLine := strings.TrimSpace(line)
|
|
if !strings.HasPrefix(trimmedLine, prefix) {
|
|
beforeLines = append(beforeLines, line)
|
|
continue
|
|
}
|
|
|
|
codeText := strings.TrimSpace(trimmedLine[len(prefix):])
|
|
match := leadingIntRegexp.FindString(codeText)
|
|
if match == "" || match != codeText {
|
|
beforeLines = append(beforeLines, line)
|
|
continue
|
|
}
|
|
|
|
code, err := strconv.Atoi(match)
|
|
if err != nil {
|
|
beforeLines = append(beforeLines, line)
|
|
continue
|
|
}
|
|
|
|
return strings.Join(beforeLines, "\n"), code, true, nil
|
|
}
|
|
|
|
return strings.Join(beforeLines, "\n"), 0, false, nil
|
|
}
|