From 146b628cc175f68d0a5e2b4d728177cc236b8ef4 Mon Sep 17 00:00:00 2001 From: CityFun <31820853+zhengkunwang223@users.noreply.github.com> Date: Tue, 17 Jun 2025 16:59:23 +0800 Subject: [PATCH] feat: Restrict the size of log files to read. (#9147) --- agent/i18n/lang/en.yaml | 1 + agent/i18n/lang/ja.yaml | 1 + agent/i18n/lang/ko.yaml | 1 + agent/i18n/lang/pt-BR.yaml | 1 + agent/i18n/lang/ru.yaml | 1 + agent/i18n/lang/zh-Hant.yaml | 1 + agent/i18n/lang/zh.yaml | 1 + agent/utils/files/utils.go | 11 +++++++++++ frontend/src/components/log/file/index.vue | 9 ++++++++- 9 files changed, 26 insertions(+), 1 deletion(-) diff --git a/agent/i18n/lang/en.yaml b/agent/i18n/lang/en.yaml index 9cb2c3cf2..9835df840 100644 --- a/agent/i18n/lang/en.yaml +++ b/agent/i18n/lang/en.yaml @@ -108,6 +108,7 @@ ErrSourcePathNotFound: 'Source directory does not exist' ErrFavoriteExist: 'This path has already been favorited' ErrInvalidChar: 'Illegal characters are not allowed' ErrPathNotDelete: 'The selected directory cannot be deleted' +ErrLogFileToLarge: "The log file exceeds 500MB and cannot be opened" #website ErrAliasIsExist: 'Alias already exists' diff --git a/agent/i18n/lang/ja.yaml b/agent/i18n/lang/ja.yaml index f34fc8fc0..b7b9d2586 100644 --- a/agent/i18n/lang/ja.yaml +++ b/agent/i18n/lang/ja.yaml @@ -108,6 +108,7 @@ ErrSourcePathNotFound: 'ソース ディレクトリが存在しません' ErrFavoriteExist: 'このパスはすでにお気に入りに登録されています' ErrInvalidChar: '不正な文字は許可されません' ErrPathNotDelete: '選択されたディレクトリは削除できません' +ErrLogFileToLarge: "ログファイルが500MBを超えており、開くことができません" #website ErrAliasIsExist: 'エイリアスがすでに存在します' diff --git a/agent/i18n/lang/ko.yaml b/agent/i18n/lang/ko.yaml index e16f9f156..752e984d7 100644 --- a/agent/i18n/lang/ko.yaml +++ b/agent/i18n/lang/ko.yaml @@ -108,6 +108,7 @@ ErrSourcePathNotFound: '소스 디렉토리가 존재하지 않습니다' ErrFavoriteExist: '이 경로는 이미 즐겨찾기되었습니다' ErrInvalidChar: '불법 문자는 허용되지 않습니다' ErrPathNotDelete: '선택한 디렉토리를 삭제할 수 없습니다' +ErrLogFileToLarge: "로그 파일이 500MB를 초과하여 열 수 없습니다" #웹사이트 ErrAliasIsExist: '별칭이 이미 존재합니다' diff --git a/agent/i18n/lang/pt-BR.yaml b/agent/i18n/lang/pt-BR.yaml index ce0b7f833..1e56e5976 100644 --- a/agent/i18n/lang/pt-BR.yaml +++ b/agent/i18n/lang/pt-BR.yaml @@ -108,6 +108,7 @@ ErrSourcePathNotFound: 'O diretório de origem não existe' ErrFavoriteExist: 'Este caminho já foi favorito' ErrInvalidChar: 'Caracteres ilegais não são permitidos' ErrPathNotDelete: 'O diretório selecionado não pode ser excluído' +ErrLogFileToLarge: "Fail log melebihi 500MB, tidak boleh dibuka" #site ErrAliasIsExist: 'Alias já existe' diff --git a/agent/i18n/lang/ru.yaml b/agent/i18n/lang/ru.yaml index 5e83623a9..f73054ff9 100644 --- a/agent/i18n/lang/ru.yaml +++ b/agent/i18n/lang/ru.yaml @@ -108,6 +108,7 @@ ErrSourcePathNotFound: 'Исходный каталог не существуе ErrFavoriteExist: 'Этот путь уже добавлен в избранное' ErrInvalidChar: 'Недопустимые символы не допускаются' ErrPathNotDelete: 'Выбранный каталог не может быть удален' +ErrLogFileToLarge: "Файл журнала превышает 500MB и не может быть открыт" #веб-сайт ErrAliasIsExist: 'Псевдоним уже существует' diff --git a/agent/i18n/lang/zh-Hant.yaml b/agent/i18n/lang/zh-Hant.yaml index 44cd262f4..5e98c0cdf 100644 --- a/agent/i18n/lang/zh-Hant.yaml +++ b/agent/i18n/lang/zh-Hant.yaml @@ -107,6 +107,7 @@ ErrSourcePathNotFound: '來源目錄不存在' ErrFavoriteExist: '已收藏此路徑' ErrInvalidChar: '禁止使用非法字元' ErrPathNotDelete: '所選目錄不可刪除' +ErrLogFileToLarge: "日志文件超过 500M,無法打開" #website ErrAliasIsExist: '代號已存在' diff --git a/agent/i18n/lang/zh.yaml b/agent/i18n/lang/zh.yaml index e011bdc2c..82bc7ea60 100644 --- a/agent/i18n/lang/zh.yaml +++ b/agent/i18n/lang/zh.yaml @@ -107,6 +107,7 @@ ErrSourcePathNotFound: "源目录不存在" ErrFavoriteExist: "已收藏此路径" ErrInvalidChar: "禁止使用非法字符" ErrPathNotDelete: "所选目录不可删除" +ErrLogFileToLarge: "日志文件超过 500M,无法打开" #website ErrAliasIsExist: "代号已存在" diff --git a/agent/utils/files/utils.go b/agent/utils/files/utils.go index 2c5cf3072..8e82301bc 100644 --- a/agent/utils/files/utils.go +++ b/agent/utils/files/utils.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "github.com/1Panel-dev/1Panel/agent/buserr" "github.com/1Panel-dev/1Panel/agent/constant" "github.com/1Panel-dev/1Panel/agent/utils/req_helper" "io" @@ -103,6 +104,16 @@ func ReadFileByLine(filename string, page, pageSize int, latest bool) (lines []s } defer file.Close() + fi, err := file.Stat() + if err != nil { + return + } + + if fi.Size() > 500*1024*1024 { + err = buserr.New("ErrLogFileToLarge") + return + } + totalLines, err := countLines(filename) if err != nil { return diff --git a/frontend/src/components/log/file/index.vue b/frontend/src/components/log/file/index.vue index 68f29a6f9..8679a09fa 100644 --- a/frontend/src/components/log/file/index.vue +++ b/frontend/src/components/log/file/index.vue @@ -205,7 +205,14 @@ const getContent = async (pre: boolean) => { isLoading.value = true; emit('update:isReading', true); - const res = await readByLine(readReq, props.config.operateNode || ''); + let res; + try { + res = await readByLine(readReq, props.config.operateNode || ''); + } catch (error) { + isLoading.value = false; + firstLoading.value = false; + } + logPath.value = res.data.path; firstLoading.value = false;