fix(api): gateway

This commit is contained in:
ttk
2024-10-10 16:09:56 +08:00
parent 12419095a9
commit 7e86c4ded4
3 changed files with 15 additions and 13 deletions

View File

@@ -360,18 +360,13 @@ func connectSsh(ctx *gin.Context, sess *gsession.Session, asset *model.Asset, ac
User: account.Account,
Auth: []gossh.AuthMethod{auth},
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
Timeout: time.Second * 3,
Timeout: time.Second,
})
if err != nil {
logger.L().Error("ssh dial failed", zap.Error(err))
return
}
if asset.GatewayId != 0 {
if err = <-ggateway.GetGatewayBySessionId(sess.SessionId).Opened; err != nil {
return
}
}
sshSess, err := sshCli.NewSession()
if err != nil {
logger.L().Error("ssh session create failed", zap.Error(err))
@@ -465,6 +460,8 @@ func connectGuacd(ctx *gin.Context, sess *gsession.Session, asset *model.Asset,
logger.L().Error("guacd tunnel failed", zap.Error(err))
return
}
defer t.Close()
sess.ConnectionId = t.ConnectionId
sess.GuacdTunnel = t

View File

@@ -89,7 +89,7 @@ func (fm *FileManager) GetFileClient(assetId, accountId int) (cli *sftp.Client,
User: account.Account,
Auth: []ssh.AuthMethod{auth},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: time.Second * 3,
Timeout: time.Second,
})
if err != nil {
return

View File

@@ -1,6 +1,7 @@
package gateway
import (
"context"
"fmt"
"io"
"net"
@@ -46,8 +47,8 @@ type GatewayTunnel struct {
func (gt *GatewayTunnel) Open() (err error) {
go func() {
<-time.After(time.Second * 5)
logger.L().Debug("timeout 5 second close listener", zap.String("sessionId", gt.SessionId))
<-time.After(time.Second * 3)
logger.L().Debug("timeout 3 second close listener", zap.String("sessionId", gt.SessionId))
gt.listener.Close()
}()
defer func() {
@@ -60,10 +61,14 @@ func (gt *GatewayTunnel) Open() (err error) {
return
}
remoteAddr := fmt.Sprintf("%s:%d", gt.RemoteIp, gt.RemotePort)
gt.RemoteConn, err = manager.sshClients[gt.GatewayId].Dial("tcp", remoteAddr)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
gt.RemoteConn, err = manager.sshClients[gt.GatewayId].DialContext(ctx, "tcp", remoteAddr)
if err != nil {
defer gt.LocalConn.Close()
defer gt.RemoteConn.Close()
logger.L().Error("dial remote failed", zap.String("sessionId", gt.SessionId), zap.Error(err))
return err
return
}
go io.Copy(gt.LocalConn, gt.RemoteConn)
go io.Copy(gt.RemoteConn, gt.LocalConn)
@@ -96,7 +101,7 @@ func (gm *GateWayManager) Open(sessionId, remoteIp string, remotePort int, gatew
sshCli, err = ssh.Dial("tcp", fmt.Sprintf("%s:%d", gateway.Host, gateway.Port), &ssh.ClientConfig{
User: gateway.Account,
Auth: []ssh.AuthMethod{auth},
Timeout: time.Second * 3,
Timeout: time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {