From 961deb81ddfea64a0a119be2910b20b777464a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AC=E6=98=8E?= <83812544+Ed1s0nZ@users.noreply.github.com> Date: Fri, 10 Apr 2026 16:46:44 +0800 Subject: [PATCH] Add files via upload --- internal/handler/terminal_ws_unix.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/internal/handler/terminal_ws_unix.go b/internal/handler/terminal_ws_unix.go index 76a822a7..eaa5df67 100644 --- a/internal/handler/terminal_ws_unix.go +++ b/internal/handler/terminal_ws_unix.go @@ -3,6 +3,7 @@ package handler import ( + "encoding/json" "net/http" "os" "os/exec" @@ -13,6 +14,13 @@ import ( "github.com/gorilla/websocket" ) +// terminalResize is sent by the frontend when the xterm.js terminal is resized. +type terminalResize struct { + Type string `json:"type"` + Cols uint16 `json:"cols"` + Rows uint16 `json:"rows"` +} + // wsUpgrader 仅用于系统设置中的终端 WebSocket,会复用已有的登录保护(JWT 中间件在上层路由组) var wsUpgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { @@ -37,12 +45,13 @@ func (h *TerminalHandler) RunCommandWS(c *gin.Context) { } cmd := exec.Command(shell) cmd.Env = append(os.Environ(), - "COLUMNS=256", - "LINES=40", + "COLUMNS=80", + "LINES=24", "TERM=xterm-256color", ) - ptmx, err := pty.StartWithSize(cmd, &pty.Winsize{Cols: ptyCols, Rows: ptyRows}) + // Use 80x24 as a safe default; the frontend will send the actual size immediately after connecting. + ptmx, err := pty.StartWithSize(cmd, &pty.Winsize{Cols: 80, Rows: 24}) if err != nil { return } @@ -84,6 +93,14 @@ func (h *TerminalHandler) RunCommandWS(c *gin.Context) { if len(data) == 0 { continue } + // Check if this is a resize message (JSON with type:"resize") + if msgType == websocket.TextMessage && len(data) > 0 && data[0] == '{' { + var resize terminalResize + if json.Unmarshal(data, &resize) == nil && resize.Type == "resize" && resize.Cols > 0 && resize.Rows > 0 { + _ = pty.Setsize(ptmx, &pty.Winsize{Cols: resize.Cols, Rows: resize.Rows}) + continue + } + } if _, err := ptmx.Write(data); err != nil { _ = cmd.Process.Kill() break