Commit 4933b6fd authored by ibuler's avatar ibuler

[Update] 修改连接缓存问题

parent ac2c1d8f
...@@ -189,7 +189,7 @@ func (p *ProxyServer) preCheckRequisite() (ok bool) { ...@@ -189,7 +189,7 @@ func (p *ProxyServer) preCheckRequisite() (ok bool) {
// sendConnectErrorMsg 发送连接错误消息 // sendConnectErrorMsg 发送连接错误消息
func (p *ProxyServer) sendConnectErrorMsg(err error) { func (p *ProxyServer) sendConnectErrorMsg(err error) {
msg := fmt.Sprintf("Connect asset %s error: %s", p.Asset.Hostname, err) msg := fmt.Sprintf("Connect asset %s error: %s\r\n", p.Asset.Hostname, err)
utils.IgnoreErrWriteString(p.UserConn, msg) utils.IgnoreErrWriteString(p.UserConn, msg)
logger.Error(msg) logger.Error(msg)
password := p.SystemUser.Password password := p.SystemUser.Password
......
package srvconn package srvconn
import ( import (
"github.com/jumpserver/koko/pkg/service"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/jumpserver/koko/pkg/service"
"net" "net"
"strconv" "strconv"
"sync" "sync"
...@@ -209,21 +209,28 @@ func RecycleClient(client *gossh.Client) { ...@@ -209,21 +209,28 @@ func RecycleClient(client *gossh.Client) {
if counter, ok := clientsRefCounter[client]; ok { if counter, ok := clientsRefCounter[client]; ok {
if counter == 1 { if counter == 1 {
logger.Debug("Recycle client: close it") logger.Debug("Recycle client: close it")
_ = client.Close() CloseClient(client)
delete(clientsRefCounter, client)
var key string
for k, v := range sshClients {
if v == client {
key = k
break
}
}
if key != "" {
delete(sshClients, key)
}
} else { } else {
clientsRefCounter[client]-- clientsRefCounter[client]--
logger.Debugf("Recycle client: ref -1: %d", clientsRefCounter[client]) logger.Debugf("Recycle client: ref -1: %d", clientsRefCounter[client])
} }
} }
} }
func CloseClient(client *gossh.Client) {
clientLock.Lock()
defer clientLock.Unlock()
delete(clientsRefCounter, client)
var key string
for k, v := range sshClients {
if v == client {
key = k
break
}
}
if key != "" {
delete(sshClients, key)
}
_ = client.Close()
}
...@@ -72,10 +72,11 @@ func (sc *ServerSSHConnection) Connect(h, w int, term string) (err error) { ...@@ -72,10 +72,11 @@ func (sc *ServerSSHConnection) Connect(h, w int, term string) (err error) {
func (sc *ServerSSHConnection) TryConnectFromCache(h, w int, term string) (err error) { func (sc *ServerSSHConnection) TryConnectFromCache(h, w int, term string) (err error) {
sc.client = GetClientFromCache(sc.User, sc.Asset, sc.SystemUser) sc.client = GetClientFromCache(sc.User, sc.Asset, sc.SystemUser)
if sc.client == nil { if sc.client == nil {
return errors.New("No client in cache") return errors.New("no client in cache")
} }
err = sc.invokeShell(h, w, term) err = sc.invokeShell(h, w, term)
if err != nil { if err != nil {
CloseClient(sc.client)
return return
} }
sc.connected = true sc.connected = true
......
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