2026-04-26 10:45:39 +08:00
|
|
|
package starssh
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-26 20:27:10 +08:00
|
|
|
var errSSHClientClosing = errors.New("ssh client is closing")
|
|
|
|
|
|
2026-04-26 10:45:39 +08:00
|
|
|
type sshClientRequester interface {
|
|
|
|
|
SendRequest(name string, wantReply bool, payload []byte) (bool, []byte, error)
|
|
|
|
|
Close() error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var closeSSHClient = func(client sshClientRequester) error {
|
|
|
|
|
if client == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return client.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StarSSH) snapshotSSHClient() *ssh.Client {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.stateMu.RLock()
|
|
|
|
|
defer s.stateMu.RUnlock()
|
|
|
|
|
return s.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StarSSH) requireSSHClient() (*ssh.Client, error) {
|
2026-04-26 20:27:10 +08:00
|
|
|
if s == nil {
|
|
|
|
|
return nil, errors.New("ssh client is nil")
|
|
|
|
|
}
|
|
|
|
|
if s.closing.Load() {
|
|
|
|
|
return nil, errSSHClientClosing
|
|
|
|
|
}
|
2026-04-26 10:45:39 +08:00
|
|
|
client := s.snapshotSSHClient()
|
|
|
|
|
if client == nil {
|
|
|
|
|
return nil, errors.New("ssh client is nil")
|
|
|
|
|
}
|
2026-04-26 20:27:10 +08:00
|
|
|
if s.closing.Load() {
|
|
|
|
|
return nil, errSSHClientClosing
|
|
|
|
|
}
|
2026-04-26 10:45:39 +08:00
|
|
|
return client, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StarSSH) setTransport(client *ssh.Client, upstream *StarSSH) {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.stateMu.Lock()
|
|
|
|
|
defer s.stateMu.Unlock()
|
|
|
|
|
s.Client = client
|
|
|
|
|
s.upstream = upstream
|
|
|
|
|
s.online = client != nil
|
2026-04-26 20:27:10 +08:00
|
|
|
s.closing.Store(false)
|
2026-04-26 10:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StarSSH) detachTransport() (*ssh.Client, *StarSSH) {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.stateMu.Lock()
|
|
|
|
|
defer s.stateMu.Unlock()
|
|
|
|
|
|
|
|
|
|
client := s.Client
|
|
|
|
|
upstream := s.upstream
|
|
|
|
|
s.Client = nil
|
|
|
|
|
s.upstream = nil
|
|
|
|
|
s.online = false
|
|
|
|
|
return client, upstream
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StarSSH) takeKeepaliveHandles() (chan struct{}, chan struct{}) {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.keepaliveMu.Lock()
|
|
|
|
|
defer s.keepaliveMu.Unlock()
|
|
|
|
|
|
|
|
|
|
stop := s.keepaliveStop
|
|
|
|
|
done := s.keepaliveDone
|
|
|
|
|
s.keepaliveStop = nil
|
|
|
|
|
s.keepaliveDone = nil
|
|
|
|
|
return stop, done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StarSSH) closeTransport(waitKeepalive bool) error {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 20:27:10 +08:00
|
|
|
s.closing.Store(true)
|
2026-04-26 10:45:39 +08:00
|
|
|
_ = s.closeReusableSFTPClient()
|
2026-04-26 20:27:10 +08:00
|
|
|
agentForwarder := s.takeAgentForwarder()
|
2026-04-26 10:45:39 +08:00
|
|
|
|
|
|
|
|
client, upstream := s.detachTransport()
|
|
|
|
|
stop, done := s.takeKeepaliveHandles()
|
|
|
|
|
if stop != nil {
|
|
|
|
|
close(stop)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var closeErr error
|
2026-04-26 20:27:10 +08:00
|
|
|
if agentForwarder != nil {
|
|
|
|
|
closeErr = normalizeAlreadyClosedError(agentForwarder.Close())
|
|
|
|
|
}
|
2026-04-26 10:45:39 +08:00
|
|
|
if client != nil {
|
2026-04-26 20:27:10 +08:00
|
|
|
if err := normalizeAlreadyClosedError(closeSSHClient(client)); closeErr == nil {
|
|
|
|
|
closeErr = err
|
|
|
|
|
}
|
2026-04-26 10:45:39 +08:00
|
|
|
}
|
|
|
|
|
if waitKeepalive && done != nil {
|
|
|
|
|
<-done
|
|
|
|
|
}
|
|
|
|
|
if upstreamErr := closeUpstream(upstream); closeErr == nil {
|
|
|
|
|
closeErr = upstreamErr
|
|
|
|
|
}
|
|
|
|
|
return closeErr
|
|
|
|
|
}
|
2026-04-26 20:27:10 +08:00
|
|
|
|
|
|
|
|
func (s *StarSSH) canAttachAgentForwarder(client *ssh.Client) bool {
|
|
|
|
|
if s == nil || client == nil || s.closing.Load() {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.stateMu.RLock()
|
|
|
|
|
defer s.stateMu.RUnlock()
|
|
|
|
|
return !s.closing.Load() && s.Client == client
|
|
|
|
|
}
|