mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-11 07:55:59 +08:00
feat: Specify the interpretation code based on the file content (#9267)
This commit is contained in:
parent
49cbab08c1
commit
2edddaf487
5 changed files with 93 additions and 28 deletions
|
@ -639,10 +639,7 @@ func handleAlert(stdout, clamName string, clamId uint) {
|
||||||
EntryID: clamId,
|
EntryID: clamId,
|
||||||
Param: strconv.Itoa(infectedFiles),
|
Param: strconv.Itoa(infectedFiles),
|
||||||
}
|
}
|
||||||
err := xpack.PushAlert(pushAlert)
|
_ = xpack.PushAlert(pushAlert)
|
||||||
if err != nil {
|
|
||||||
global.LOG.Errorf("clamdscan push failed, err: %v", err)
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -359,9 +359,5 @@ func handleCronJobAlert(cronjob *model.Cronjob) {
|
||||||
EntryID: cronjob.ID,
|
EntryID: cronjob.ID,
|
||||||
Param: cronjob.Type,
|
Param: cronjob.Type,
|
||||||
}
|
}
|
||||||
err := xpack.PushAlert(pushAlert)
|
_ = xpack.PushAlert(pushAlert)
|
||||||
if err != nil {
|
|
||||||
global.LOG.Errorf("cronjob alert push failed, err: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,10 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/1Panel-dev/1Panel/agent/app/dto"
|
||||||
"github.com/jinzhu/copier"
|
"github.com/jinzhu/copier"
|
||||||
|
"golang.org/x/text/encoding"
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
@ -15,9 +18,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"github.com/1Panel-dev/1Panel/agent/app/dto"
|
|
||||||
|
|
||||||
"github.com/1Panel-dev/1Panel/agent/app/repo"
|
"github.com/1Panel-dev/1Panel/agent/app/repo"
|
||||||
|
|
||||||
|
@ -27,7 +27,6 @@ import (
|
||||||
"github.com/1Panel-dev/1Panel/agent/constant"
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
||||||
"golang.org/x/net/html/charset"
|
"golang.org/x/net/html/charset"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
"golang.org/x/text/encoding/simplifiedchinese"
|
|
||||||
"golang.org/x/text/transform"
|
"golang.org/x/text/transform"
|
||||||
|
|
||||||
"github.com/1Panel-dev/1Panel/agent/global"
|
"github.com/1Panel-dev/1Panel/agent/global"
|
||||||
|
@ -335,17 +334,22 @@ func (f *FileService) GetContent(op request.FileContentReq) (response.FileInfo,
|
||||||
if len(content) > 1024 {
|
if len(content) > 1024 {
|
||||||
content = content[:1024]
|
content = content[:1024]
|
||||||
}
|
}
|
||||||
if !utf8.Valid(content) {
|
_, decodeName, _ := charset.DetermineEncoding(content, "")
|
||||||
_, decodeName, _ := charset.DetermineEncoding(content, "")
|
decoder := files.GetDecoderByName(decodeName)
|
||||||
if decodeName == "windows-1252" || strings.ToLower(decodeName) == "gbk" {
|
if decoder != nil {
|
||||||
reader := strings.NewReader(info.Content)
|
reader := strings.NewReader(info.Content)
|
||||||
item := transform.NewReader(reader, simplifiedchinese.GBK.NewDecoder())
|
var dec *encoding.Decoder
|
||||||
contents, err := io.ReadAll(item)
|
if decodeName == "windows-1252" {
|
||||||
if err != nil {
|
dec = simplifiedchinese.GBK.NewDecoder()
|
||||||
return response.FileInfo{}, err
|
} else {
|
||||||
}
|
dec = decoder.NewDecoder()
|
||||||
info.Content = string(contents)
|
|
||||||
}
|
}
|
||||||
|
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
|
return response.FileInfo{FileInfo: *info}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,11 +92,7 @@ func handleCronJobAlert(cronjob *model.Cronjob) {
|
||||||
EntryID: cronjob.ID,
|
EntryID: cronjob.ID,
|
||||||
Param: cronjob.Type,
|
Param: cronjob.Type,
|
||||||
}
|
}
|
||||||
err := xpack.PushAlert(pushAlert)
|
_ = xpack.PushAlert(pushAlert)
|
||||||
if err != nil {
|
|
||||||
global.LOG.Errorf("cronjob alert push failed, err: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadLocalDir() {
|
func loadLocalDir() {
|
||||||
|
|
|
@ -7,6 +7,13 @@ import (
|
||||||
"github.com/1Panel-dev/1Panel/agent/buserr"
|
"github.com/1Panel-dev/1Panel/agent/buserr"
|
||||||
"github.com/1Panel-dev/1Panel/agent/constant"
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
||||||
"github.com/1Panel-dev/1Panel/agent/utils/req_helper"
|
"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"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -201,3 +208,68 @@ func DownloadFileWithProxy(url, dst string) error {
|
||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue