Unverified Commit 8c252932 authored by Eric_Lee's avatar Eric_Lee Committed by GitHub

修复网域连接关闭 (#83)

parent b991c052
...@@ -32,6 +32,7 @@ var ( ...@@ -32,6 +32,7 @@ var (
type SSHClient struct { type SSHClient struct {
client *gossh.Client client *gossh.Client
proxyConn gossh.Conn
username string username string
ref int ref int
...@@ -79,6 +80,9 @@ func (s *SSHClient) Close() error { ...@@ -79,6 +80,9 @@ func (s *SSHClient) Close() error {
default: default:
close(s.closed) close(s.closed)
} }
if s.proxyConn != nil {
_ = s.proxyConn.Close()
}
s.mu.Lock() s.mu.Lock()
s.ref = 0 s.ref = 0
s.mu.Unlock() s.mu.Unlock()
...@@ -254,7 +258,8 @@ func newClient(asset *model.Asset, systemUser *model.SystemUser, timeout time.Du ...@@ -254,7 +258,8 @@ func newClient(asset *model.Asset, systemUser *model.SystemUser, timeout time.Du
return nil, err return nil, err
} }
closed := make(chan struct{}) closed := make(chan struct{})
client = &SSHClient{client: conn, username: systemUser.Username, client = &SSHClient{client: conn, proxyConn: sshConfig.proxyConn,
username: systemUser.Username,
mu: new(sync.RWMutex), mu: new(sync.RWMutex),
ref: 1, ref: 1,
closed: closed} closed: closed}
...@@ -298,8 +303,15 @@ func getClientFromCache(key string) (client *SSHClient) { ...@@ -298,8 +303,15 @@ func getClientFromCache(key string) (client *SSHClient) {
func setClientCache(key string, client *SSHClient) { func setClientCache(key string, client *SSHClient) {
clientLock.Lock() clientLock.Lock()
if _, ok := sshClients[key]; !ok {
sshClients[key] = client sshClients[key] = client
client.key = key client.key = key
} else {
newKey := fmt.Sprintf("%s_%s", key, time.Now().UTC().Format("20060102150405"))
sshClients[newKey] = client
client.key = newKey
logger.Debugf("SSH Client key already used, use new key")
}
clientLock.Unlock() clientLock.Unlock()
} }
...@@ -319,7 +331,7 @@ func RecycleClient(client *SSHClient) { ...@@ -319,7 +331,7 @@ func RecycleClient(client *SSHClient) {
} else { } else {
logger.Infof("Close ssh client %p", client) logger.Infof("Close ssh client %p", client)
} }
}else { } else {
logger.Debugf("SSH client %p ref -1, current ref: %s", client, client.refCount()) logger.Debugf("SSH client %p ref -1, current ref: %s", client, client.refCount())
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment