fix: 解决日志中出现非UTF-8编码字符导致日志错误 (#3715) (#3716)

This commit is contained in:
Leif 2024-01-26 16:07:37 +08:00 committed by GitHub
parent d5432c8e59
commit 338ade095a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"os"
"os/exec"
@ -15,6 +16,7 @@ import (
"sync"
"syscall"
"time"
"unicode/utf8"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/buserr"
@ -637,6 +639,9 @@ func (u *ContainerService) ContainerLogs(wsConn *websocket.Conn, containerType,
cmd := exec.Command("bash", "-c", command)
if !follow {
stdout, _ := cmd.CombinedOutput()
if !utf8.Valid(stdout) {
return errors.New("invalid utf8")
}
if err := wsConn.WriteMessage(websocket.TextMessage, stdout); err != nil {
global.LOG.Errorf("send message with log to ws failed, err: %v", err)
}
@ -678,6 +683,10 @@ func (u *ContainerService) ContainerLogs(wsConn *websocket.Conn, containerType,
global.LOG.Errorf("read bytes from log failed, err: %v", err)
continue
}
// check if the bytes is valid utf8
if !utf8.Valid(buffer[:n]) {
continue
}
if err = wsConn.WriteMessage(websocket.TextMessage, buffer[:n]); err != nil {
global.LOG.Errorf("send message with log to ws failed, err: %v", err)
return err