From 2edddaf4876e243ade64e7ad83cc2bc6e19029c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=98=AD?= <81747598+lan-yonghui@users.noreply.github.com> Date: Tue, 24 Jun 2025 22:17:14 +0800 Subject: [PATCH] feat: Specify the interpretation code based on the file content (#9267) --- agent/app/service/clam.go | 5 +- agent/app/service/cronjob_helper.go | 6 +-- agent/app/service/file.go | 32 +++++++------ agent/init/hook/hook.go | 6 +-- agent/utils/files/utils.go | 72 +++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 28 deletions(-) diff --git a/agent/app/service/clam.go b/agent/app/service/clam.go index 426e92bc2..837ba6684 100644 --- a/agent/app/service/clam.go +++ b/agent/app/service/clam.go @@ -639,10 +639,7 @@ func handleAlert(stdout, clamName string, clamId uint) { EntryID: clamId, Param: strconv.Itoa(infectedFiles), } - err := xpack.PushAlert(pushAlert) - if err != nil { - global.LOG.Errorf("clamdscan push failed, err: %v", err) - } + _ = xpack.PushAlert(pushAlert) break } } diff --git a/agent/app/service/cronjob_helper.go b/agent/app/service/cronjob_helper.go index f566410f3..b1d547c37 100644 --- a/agent/app/service/cronjob_helper.go +++ b/agent/app/service/cronjob_helper.go @@ -359,9 +359,5 @@ func handleCronJobAlert(cronjob *model.Cronjob) { EntryID: cronjob.ID, Param: cronjob.Type, } - err := xpack.PushAlert(pushAlert) - if err != nil { - global.LOG.Errorf("cronjob alert push failed, err: %v", err) - return - } + _ = xpack.PushAlert(pushAlert) } diff --git a/agent/app/service/file.go b/agent/app/service/file.go index a0106ab8f..74a3ee8ba 100644 --- a/agent/app/service/file.go +++ b/agent/app/service/file.go @@ -4,7 +4,10 @@ import ( "bufio" "context" "fmt" + "github.com/1Panel-dev/1Panel/agent/app/dto" "github.com/jinzhu/copier" + "golang.org/x/text/encoding" + "golang.org/x/text/encoding/simplifiedchinese" "io" "io/fs" "os" @@ -15,9 +18,6 @@ import ( "strconv" "strings" "time" - "unicode/utf8" - - "github.com/1Panel-dev/1Panel/agent/app/dto" "github.com/1Panel-dev/1Panel/agent/app/repo" @@ -27,7 +27,6 @@ import ( "github.com/1Panel-dev/1Panel/agent/constant" "golang.org/x/net/html/charset" "golang.org/x/sys/unix" - "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform" "github.com/1Panel-dev/1Panel/agent/global" @@ -335,17 +334,22 @@ func (f *FileService) GetContent(op request.FileContentReq) (response.FileInfo, if len(content) > 1024 { content = content[:1024] } - if !utf8.Valid(content) { - _, decodeName, _ := charset.DetermineEncoding(content, "") - if decodeName == "windows-1252" || strings.ToLower(decodeName) == "gbk" { - reader := strings.NewReader(info.Content) - item := transform.NewReader(reader, simplifiedchinese.GBK.NewDecoder()) - contents, err := io.ReadAll(item) - if err != nil { - return response.FileInfo{}, err - } - info.Content = string(contents) + _, decodeName, _ := charset.DetermineEncoding(content, "") + decoder := files.GetDecoderByName(decodeName) + if decoder != nil { + reader := strings.NewReader(info.Content) + var dec *encoding.Decoder + if decodeName == "windows-1252" { + dec = simplifiedchinese.GBK.NewDecoder() + } else { + dec = decoder.NewDecoder() } + decodedReader := transform.NewReader(reader, dec) + contents, err := io.ReadAll(decodedReader) + if err != nil { + return response.FileInfo{}, err + } + info.Content = string(contents) } return response.FileInfo{FileInfo: *info}, nil } diff --git a/agent/init/hook/hook.go b/agent/init/hook/hook.go index fe339440a..1eb45b562 100644 --- a/agent/init/hook/hook.go +++ b/agent/init/hook/hook.go @@ -92,11 +92,7 @@ func handleCronJobAlert(cronjob *model.Cronjob) { EntryID: cronjob.ID, Param: cronjob.Type, } - err := xpack.PushAlert(pushAlert) - if err != nil { - global.LOG.Errorf("cronjob alert push failed, err: %v", err) - return - } + _ = xpack.PushAlert(pushAlert) } func loadLocalDir() { diff --git a/agent/utils/files/utils.go b/agent/utils/files/utils.go index 8e82301bc..18d69b733 100644 --- a/agent/utils/files/utils.go +++ b/agent/utils/files/utils.go @@ -7,6 +7,13 @@ import ( "github.com/1Panel-dev/1Panel/agent/buserr" "github.com/1Panel-dev/1Panel/agent/constant" "github.com/1Panel-dev/1Panel/agent/utils/req_helper" + "golang.org/x/text/encoding" + "golang.org/x/text/encoding/charmap" + "golang.org/x/text/encoding/japanese" + "golang.org/x/text/encoding/korean" + "golang.org/x/text/encoding/simplifiedchinese" + "golang.org/x/text/encoding/traditionalchinese" + "golang.org/x/text/encoding/unicode" "io" "net/http" "os" @@ -201,3 +208,68 @@ func DownloadFileWithProxy(url, dst string) error { } return nil } + +func GetDecoderByName(name string) encoding.Encoding { + switch strings.ToLower(name) { + case "gbk": + return simplifiedchinese.GBK + case "gb18030": + return simplifiedchinese.GB18030 + case "big5": + return traditionalchinese.Big5 + case "euc-jp": + return japanese.EUCJP + case "iso-2022-jp": + return japanese.ISO2022JP + case "shift_jis": + return japanese.ShiftJIS + case "euc-kr": + return korean.EUCKR + case "utf-16be": + return unicode.UTF16(unicode.BigEndian, unicode.ExpectBOM) + case "utf-16le": + return unicode.UTF16(unicode.LittleEndian, unicode.ExpectBOM) + case "windows-1250": + return charmap.Windows1250 + case "windows-1251": + return charmap.Windows1251 + case "windows-1252": + return charmap.Windows1252 + case "windows-1253": + return charmap.Windows1253 + case "windows-1254": + return charmap.Windows1254 + case "windows-1255": + return charmap.Windows1255 + case "windows-1256": + return charmap.Windows1256 + case "windows-1257": + return charmap.Windows1257 + case "windows-1258": + return charmap.Windows1258 + case "iso-8859-1": + return charmap.ISO8859_1 + case "iso-8859-2": + return charmap.ISO8859_2 + case "iso-8859-3": + return charmap.ISO8859_3 + case "iso-8859-4": + return charmap.ISO8859_4 + case "iso-8859-5": + return charmap.ISO8859_5 + case "iso-8859-6": + return charmap.ISO8859_6 + case "iso-8859-7": + return charmap.ISO8859_7 + case "iso-8859-8": + return charmap.ISO8859_8 + case "iso-8859-9": + return charmap.ISO8859_9 + case "iso-8859-13": + return charmap.ISO8859_13 + case "iso-8859-15": + return charmap.ISO8859_15 + default: + return encoding.Nop + } +}