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

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

parent b991c052
...@@ -31,8 +31,9 @@ var ( ...@@ -31,8 +31,9 @@ var (
) )
type SSHClient struct { type SSHClient struct {
client *gossh.Client client *gossh.Client
username string proxyConn gossh.Conn
username string
ref int ref int
key string key string
...@@ -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,10 +258,11 @@ func newClient(asset *model.Asset, systemUser *model.SystemUser, timeout time.Du ...@@ -254,10 +258,11 @@ 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,
mu: new(sync.RWMutex), username: systemUser.Username,
ref: 1, mu: new(sync.RWMutex),
closed: closed} ref: 1,
closed: closed}
go KeepAlive(client, closed, 60) go KeepAlive(client, closed, 60)
return client, nil return client, nil
} }
...@@ -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()
sshClients[key] = client if _, ok := sshClients[key]; !ok {
client.key = key sshClients[key] = client
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