Skip to content

Commit

Permalink
perf: 优化分辨率不生效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Halo1236 committed Aug 22, 2024
1 parent 2478dd0 commit 887ef52
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
37 changes: 27 additions & 10 deletions pkg/tunnel/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -159,7 +160,6 @@ func (t *Connection) Run(ctx *gin.Context) (err error) {
instruction, err := t.readTunnelInstruction()
if err != nil {
logger.Errorf("Session[%s] guacamole server read err: %+v", t, err)
exit <- err
break
}

Expand All @@ -185,7 +185,6 @@ func (t *Connection) Run(ctx *gin.Context) (err error) {
requiredErr = guacd.Instruction{}
continue
}

}
case guacd.InstructionRequired:
msg := fmt.Sprintf("required: %s", strings.Join(instruction.Args, ","))
Expand All @@ -197,7 +196,6 @@ func (t *Connection) Run(ctx *gin.Context) (err error) {

if err = t.writeWsMessage([]byte(instruction.String())); err != nil {
logger.Errorf("Session[%s] send web client err: %+v", t, err)
exit <- err
break
}
}
Expand All @@ -214,7 +212,6 @@ func (t *Connection) Run(ctx *gin.Context) (err error) {
} else {
logger.Errorf("Session[%s] web client read err: %+v", t, err1)
}
exit <- err1
break
}

Expand Down Expand Up @@ -282,7 +279,6 @@ func (t *Connection) Run(ctx *gin.Context) (err error) {
_, err = t.writeTunnelMessage(message)
if err != nil {
logger.Errorf("Session[%s] guacamole server write err: %+v", t, err)
exit <- err
break
}
}
Expand Down Expand Up @@ -463,10 +459,34 @@ func (t *Connection) generateCommandResult(item *session.ExecutedCommand) *model

func (t *Connection) ReConnect(ws *websocket.Conn) {
// 重新连接 guacamole server
info := guacd.NewClientInformation()
opts := t.Sess.AuthInfo.ConnectOptions
resolution := strings.ToLower(opts.Resolution)
switch resolution {
case "":
case "auto":
default:
logger.Infof("Session[%s] Connect options resolution: %s",
t.Sess.ID, resolution)
resolutions := strings.Split(resolution, "x")
if len(resolutions) == 2 {
width := resolutions[0]
height := resolutions[1]
if widthInt, err1 := strconv.Atoi(width); err1 == nil && widthInt > 0 {
info.OptimalScreenWidth = widthInt
}
if heightInt, err1 := strconv.Atoi(height); err1 == nil && heightInt > 0 {
info.OptimalScreenHeight = heightInt
}
}
}
conf := guacd.NewConfiguration()
conf.ConnectionID = t.guacdTunnel.UUID
for argName, argValue := range info.ExtraConfig() {
conf.SetParameter(argName, argValue)
}
guacdAddr := net.JoinHostPort(config.GlobalConfig.GuaHost, config.GlobalConfig.GuaPort)
newTunnel, err := guacd.NewTunnel(guacdAddr, conf, guacd.NewClientInformation())
newTunnel, err := guacd.NewTunnel(guacdAddr, conf, info)
if err != nil {
logger.Errorf("Session[%s] reconnect guacamole server err: %+v", t, err)
return
Expand Down Expand Up @@ -501,10 +521,7 @@ func (t *Connection) ReConnect(ws *websocket.Conn) {
default:
}
}

if err2 := wsWrite([]byte(instruction.String())); err2 != nil {
logger.Errorf("Session[%s] reconnect write ws message err: %+v", t, err2)
}
_ = wsWrite([]byte(instruction.String()))
}
t.guacdConnect.Delete(newTunnel)
}()
Expand Down
11 changes: 6 additions & 5 deletions pkg/tunnel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func (g *GuacamoleTunnelServer) Connect(ctx *gin.Context) {

// 查询缓存的 connection,未找到则创建新的 connection
if tun := g.Cache.Get(sessionId); tun != nil {
if tun.Sess.User.ID != user.ID {
logger.Errorf("No session tunnel found %s", sessionId)
_ = ws.WriteMessage(websocket.TextMessage, []byte(ErrNoSession.String()))
if user.ID != tun.Sess.User.ID {
logger.Error("No valid auth user found")
_ = ws.WriteMessage(websocket.TextMessage, []byte(ErrAuthUser.String()))
return
}
p, _ := json.Marshal(tun.Sess)
Expand Down Expand Up @@ -210,8 +210,7 @@ func (g *GuacamoleTunnelServer) Connect(ctx *gin.Context) {
}

var tunnel *guacd.Tunnel
guacdAddr := config.GlobalConfig.SelectGuacdAddr()

guacdAddr := net.JoinHostPort(config.GlobalConfig.GuaHost, config.GlobalConfig.GuaPort)
tunnel, err = guacd.NewTunnel(guacdAddr, conf, info)
if err != nil {
logger.Errorf("Connect tunnel err: %+v", err)
Expand Down Expand Up @@ -497,6 +496,8 @@ func (g *GuacamoleTunnelServer) Monitor(ctx *gin.Context) {
Id: sessionId,
guacdTunnel: tunnelCon,
ws: ws,
Service: g,
User: user,
}
logger.Infof("User %s start to monitor session %s", user, sessionId)
_ = conn.Run(ctx.Request.Context())
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/GuacamoleConnect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ export default {
if (displayHeight === optimalWidth && displayWidth === optimalHeight) {
return
}
this.client.sendSize(optimalWidth, optimalHeight)
// this.client.sendSize(optimalWidth, optimalHeight)
}
},
Expand Down

0 comments on commit 887ef52

Please sign in to comment.