From 1a400e7f81d04887497875a2fffff63ae047a7cb Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Tue, 5 Dec 2023 15:50:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BF=81=E7=A7=BB=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=B8=85=E7=90=86=E4=BB=A3=E7=A0=81=20(#3186)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/device.go | 31 ++++ backend/app/api/v1/setting.go | 31 ---- backend/app/service/cronjob_helper.go | 2 +- backend/app/service/device.go | 4 + .../{setting_clean.go => device_clean.go} | 10 +- backend/app/service/setting.go | 7 +- backend/router/ro_setting.go | 2 - backend/router/ro_toolbox.go | 3 + cmd/server/docs/docs.go | 145 +++++++++--------- cmd/server/docs/swagger.json | 141 ++++++++--------- cmd/server/docs/swagger.yaml | 92 +++++------ frontend/src/api/interface/setting.ts | 17 -- frontend/src/api/interface/toolbox.ts | 17 ++ frontend/src/api/modules/setting.ts | 7 - frontend/src/api/modules/toolbox.ts | 8 + frontend/src/views/log/operation/index.vue | 3 + frontend/src/views/toolbox/clean/index.vue | 7 +- 17 files changed, 271 insertions(+), 256 deletions(-) rename backend/app/service/{setting_clean.go => device_clean.go} (98%) diff --git a/backend/app/api/v1/device.go b/backend/app/api/v1/device.go index 8945b8e06..4d9873809 100644 --- a/backend/app/api/v1/device.go +++ b/backend/app/api/v1/device.go @@ -203,3 +203,34 @@ func (b *BaseApi) CheckDNS(c *gin.Context) { helper.SuccessWithData(c, data) } + +// @Tags Device +// @Summary Scan system +// @Description 扫描系统垃圾文件 +// @Success 200 +// @Security ApiKeyAuth +// @Router /toolbox/scan [post] +// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"扫描系统垃圾文件","formatEN":"scan System Junk Files"} +func (b *BaseApi) ScanSystem(c *gin.Context) { + helper.SuccessWithData(c, deviceService.Scan()) +} + +// @Tags Device +// @Summary Clean system +// @Description 清理系统垃圾文件 +// @Accept json +// @Param request body []dto.Clean true "request" +// @Success 200 +// @Security ApiKeyAuth +// @Router /toolbox/clean [post] +// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理系统垃圾文件","formatEN":"Clean system junk files"} +func (b *BaseApi) SystemClean(c *gin.Context) { + var req []dto.Clean + if err := helper.CheckBind(&req, c); err != nil { + return + } + + deviceService.Clean(req) + + helper.SuccessWithData(c, nil) +} diff --git a/backend/app/api/v1/setting.go b/backend/app/api/v1/setting.go index 4e3b27eed..f697aad4e 100644 --- a/backend/app/api/v1/setting.go +++ b/backend/app/api/v1/setting.go @@ -251,37 +251,6 @@ func (b *BaseApi) CleanMonitor(c *gin.Context) { helper.SuccessWithData(c, nil) } -// @Tags System Setting -// @Summary Scan system -// @Description 扫描系统垃圾文件 -// @Success 200 -// @Security ApiKeyAuth -// @Router /settings/scan [post] -// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"扫描系统垃圾文件","formatEN":"scan System Junk Files"} -func (b *BaseApi) ScanSystem(c *gin.Context) { - helper.SuccessWithData(c, settingService.SystemScan()) -} - -// @Tags System Setting -// @Summary System clean -// @Description 清理系统垃圾文件 -// @Accept json -// @Param request body []dto.Clean true "request" -// @Success 200 -// @Security ApiKeyAuth -// @Router /settings/clean [post] -// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理系统垃圾文件","formatEN":"Clean system junk files"} -func (b *BaseApi) SystemClean(c *gin.Context) { - var req []dto.Clean - if err := helper.CheckBind(&req, c); err != nil { - return - } - - settingService.SystemClean(req) - - helper.SuccessWithData(c, nil) -} - // @Tags System Setting // @Summary Load mfa info // @Description 获取 mfa 信息 diff --git a/backend/app/service/cronjob_helper.go b/backend/app/service/cronjob_helper.go index c29f0e9f2..155fa3bd2 100644 --- a/backend/app/service/cronjob_helper.go +++ b/backend/app/service/cronjob_helper.go @@ -584,7 +584,7 @@ func (u *CronjobService) handleSnapshot(cronjob *model.Cronjob, startTime time.T } func (u *CronjobService) handleSystemClean() (string, error) { - return NewISettingService().SystemCleanForCronjob() + return NewIDeviceService().CleanForCronjob() } func (u *CronjobService) handleSystemLog(cronjob model.Cronjob, startTime time.Time) (string, error) { diff --git a/backend/app/service/device.go b/backend/app/service/device.go index acf891f75..3630a5381 100644 --- a/backend/app/service/device.go +++ b/backend/app/service/device.go @@ -37,6 +37,10 @@ type IDeviceService interface { LoadTimeZone() ([]string, error) CheckDNS(key, value string) (bool, error) LoadConf(name string) (string, error) + + Scan() dto.CleanData + Clean(req []dto.Clean) + CleanForCronjob() (string, error) } func NewIDeviceService() IDeviceService { diff --git a/backend/app/service/setting_clean.go b/backend/app/service/device_clean.go similarity index 98% rename from backend/app/service/setting_clean.go rename to backend/app/service/device_clean.go index 06b3884bf..c6fe82830 100644 --- a/backend/app/service/setting_clean.go +++ b/backend/app/service/device_clean.go @@ -33,7 +33,7 @@ const ( taskPath = "1panel/task" ) -func (u *SettingService) SystemScan() dto.CleanData { +func (u *DeviceService) Scan() dto.CleanData { var ( SystemClean dto.CleanData treeData []dto.CleanTree @@ -133,7 +133,7 @@ func (u *SettingService) SystemScan() dto.CleanData { return SystemClean } -func (u *SettingService) SystemClean(req []dto.Clean) { +func (u *DeviceService) Clean(req []dto.Clean) { size := uint64(0) restart := false for _, item := range req { @@ -235,7 +235,7 @@ func (u *SettingService) SystemClean(req []dto.Clean) { continue } for _, file := range files { - if file.Name() == "1Panel.log" { + if file.Name() == "1Panel.log" || file.IsDir() { continue } dropFileOrDir(path.Join(global.CONF.System.BaseDir, logPath, file.Name())) @@ -276,7 +276,7 @@ func (u *SettingService) SystemClean(req []dto.Clean) { } } -func (u *SettingService) SystemCleanForCronjob() (string, error) { +func (u *DeviceService) CleanForCronjob() (string, error) { logs := "" size := int64(0) fileCount := 0 @@ -541,7 +541,7 @@ func loadTreeWithAllFile(isCheck bool, originalPath, treeType, pathItem string, return lists } for _, file := range files { - if treeType == "system_log" && file.Name() == "1Panel.log" { + if treeType == "system_log" && (file.Name() == "1Panel.log" || file.IsDir()) { continue } if (treeType == "upload" || treeType == "download") && file.IsDir() && (file.Name() == "app" || file.Name() == "database" || file.Name() == "website" || file.Name() == "directory") { diff --git a/backend/app/service/setting.go b/backend/app/service/setting.go index b8e88d4ea..6c5cf55b4 100644 --- a/backend/app/service/setting.go +++ b/backend/app/service/setting.go @@ -6,7 +6,6 @@ import ( "encoding/json" "encoding/pem" "fmt" - "github.com/1Panel-dev/1Panel/backend/app/dto/request" "net" "os" "path" @@ -14,6 +13,8 @@ import ( "strings" "time" + "github.com/1Panel-dev/1Panel/backend/app/dto/request" + "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/constant" @@ -38,10 +39,6 @@ type ISettingService interface { UpdateSSL(c *gin.Context, req dto.SSLUpdate) error LoadFromCert() (*dto.SSLInfo, error) HandlePasswordExpired(c *gin.Context, old, new string) error - - SystemScan() dto.CleanData - SystemClean(req []dto.Clean) - SystemCleanForCronjob() (string, error) } func NewISettingService() ISettingService { diff --git a/backend/router/ro_setting.go b/backend/router/ro_setting.go index 6f5b6f829..b3f8aa9f9 100644 --- a/backend/router/ro_setting.go +++ b/backend/router/ro_setting.go @@ -32,8 +32,6 @@ func (s *SettingRouter) InitSettingRouter(Router *gin.RouterGroup) { settingRouter.POST("/monitor/clean", baseApi.CleanMonitor) settingRouter.POST("/mfa", baseApi.LoadMFA) settingRouter.POST("/mfa/bind", baseApi.MFABind) - settingRouter.POST("/scan", baseApi.ScanSystem) - settingRouter.POST("/clean", baseApi.SystemClean) settingRouter.POST("/snapshot", baseApi.CreateSnapshot) settingRouter.POST("/snapshot/status", baseApi.LoadSnapShotStatus) diff --git a/backend/router/ro_toolbox.go b/backend/router/ro_toolbox.go index fb86e8ca0..a2008cd8a 100644 --- a/backend/router/ro_toolbox.go +++ b/backend/router/ro_toolbox.go @@ -26,6 +26,9 @@ func (s *ToolboxRouter) InitToolboxRouter(Router *gin.RouterGroup) { toolboxRouter.POST("/device/check/dns", baseApi.CheckDNS) toolboxRouter.POST("/device/conf", baseApi.LoadDeviceConf) + toolboxRouter.POST("/scan", baseApi.ScanSystem) + toolboxRouter.POST("/clean", baseApi.SystemClean) + toolboxRouter.GET("/fail2ban/base", baseApi.LoadFail2BanBaseInfo) toolboxRouter.GET("/fail2ban/load/conf", baseApi.LoadFail2BanConf) toolboxRouter.POST("/fail2ban/search", baseApi.SearchFail2Ban) diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index fd799504e..0c10103e1 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -1,5 +1,5 @@ -// Package docs GENERATED BY SWAG; DO NOT EDIT -// This file was generated by swaggo/swag +// Code generated by swaggo/swag. DO NOT EDIT. + package docs import "github.com/swaggo/swag" @@ -9212,49 +9212,6 @@ const docTemplate = `{ } } }, - "/settings/clean": { - "post": { - "security": [ - { - "ApiKeyAuth": [] - } - ], - "description": "清理系统垃圾文件", - "consumes": [ - "application/json" - ], - "tags": [ - "System Setting" - ], - "summary": "System clean", - "parameters": [ - { - "description": "request", - "name": "request", - "in": "body", - "required": true, - "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.Clean" - } - } - } - ], - "responses": { - "200": { - "description": "OK" - } - }, - "x-panel-log": { - "BeforeFunctions": [], - "bodyKeys": [], - "formatEN": "Clean system junk files", - "formatZH": "清理系统垃圾文件", - "paramKeys": [] - } - } - }, "/settings/expired/handle": { "post": { "security": [ @@ -9501,32 +9458,6 @@ const docTemplate = `{ } } }, - "/settings/scan": { - "post": { - "security": [ - { - "ApiKeyAuth": [] - } - ], - "description": "扫描系统垃圾文件", - "tags": [ - "System Setting" - ], - "summary": "Scan system", - "responses": { - "200": { - "description": "OK" - } - }, - "x-panel-log": { - "BeforeFunctions": [], - "bodyKeys": [], - "formatEN": "scan System Junk Files", - "formatZH": "扫描系统垃圾文件", - "paramKeys": [] - } - } - }, "/settings/search": { "post": { "security": [ @@ -10127,6 +10058,49 @@ const docTemplate = `{ } } }, + "/toolbox/clean": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "清理系统垃圾文件", + "consumes": [ + "application/json" + ], + "tags": [ + "Device" + ], + "summary": "Clean system", + "parameters": [ + { + "description": "request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/dto.Clean" + } + } + } + ], + "responses": { + "200": { + "description": "OK" + } + }, + "x-panel-log": { + "BeforeFunctions": [], + "bodyKeys": [], + "formatEN": "Clean system junk files", + "formatZH": "清理系统垃圾文件", + "paramKeys": [] + } + } + }, "/toolbox/device/base": { "get": { "security": [ @@ -10644,6 +10618,32 @@ const docTemplate = `{ } } }, + "/toolbox/scan": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "扫描系统垃圾文件", + "tags": [ + "Device" + ], + "summary": "Scan system", + "responses": { + "200": { + "description": "OK" + } + }, + "x-panel-log": { + "BeforeFunctions": [], + "bodyKeys": [], + "formatEN": "scan System Junk Files", + "formatZH": "扫描系统垃圾文件", + "paramKeys": [] + } + } + }, "/websites": { "post": { "security": [ @@ -14850,6 +14850,9 @@ const docTemplate = `{ "localTime": { "type": "string" }, + "maxSize": { + "type": "integer" + }, "ntp": { "type": "string" }, diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index 7f63c75f7..b6a4fbd74 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -9205,49 +9205,6 @@ } } }, - "/settings/clean": { - "post": { - "security": [ - { - "ApiKeyAuth": [] - } - ], - "description": "清理系统垃圾文件", - "consumes": [ - "application/json" - ], - "tags": [ - "System Setting" - ], - "summary": "System clean", - "parameters": [ - { - "description": "request", - "name": "request", - "in": "body", - "required": true, - "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.Clean" - } - } - } - ], - "responses": { - "200": { - "description": "OK" - } - }, - "x-panel-log": { - "BeforeFunctions": [], - "bodyKeys": [], - "formatEN": "Clean system junk files", - "formatZH": "清理系统垃圾文件", - "paramKeys": [] - } - } - }, "/settings/expired/handle": { "post": { "security": [ @@ -9494,32 +9451,6 @@ } } }, - "/settings/scan": { - "post": { - "security": [ - { - "ApiKeyAuth": [] - } - ], - "description": "扫描系统垃圾文件", - "tags": [ - "System Setting" - ], - "summary": "Scan system", - "responses": { - "200": { - "description": "OK" - } - }, - "x-panel-log": { - "BeforeFunctions": [], - "bodyKeys": [], - "formatEN": "scan System Junk Files", - "formatZH": "扫描系统垃圾文件", - "paramKeys": [] - } - } - }, "/settings/search": { "post": { "security": [ @@ -10120,6 +10051,49 @@ } } }, + "/toolbox/clean": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "清理系统垃圾文件", + "consumes": [ + "application/json" + ], + "tags": [ + "Device" + ], + "summary": "Clean system", + "parameters": [ + { + "description": "request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/dto.Clean" + } + } + } + ], + "responses": { + "200": { + "description": "OK" + } + }, + "x-panel-log": { + "BeforeFunctions": [], + "bodyKeys": [], + "formatEN": "Clean system junk files", + "formatZH": "清理系统垃圾文件", + "paramKeys": [] + } + } + }, "/toolbox/device/base": { "get": { "security": [ @@ -10637,6 +10611,32 @@ } } }, + "/toolbox/scan": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "扫描系统垃圾文件", + "tags": [ + "Device" + ], + "summary": "Scan system", + "responses": { + "200": { + "description": "OK" + } + }, + "x-panel-log": { + "BeforeFunctions": [], + "bodyKeys": [], + "formatEN": "scan System Junk Files", + "formatZH": "扫描系统垃圾文件", + "paramKeys": [] + } + } + }, "/websites": { "post": { "security": [ @@ -14843,6 +14843,9 @@ "localTime": { "type": "string" }, + "maxSize": { + "type": "integer" + }, "ntp": { "type": "string" }, diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index 31f0f110f..1185ddae7 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -987,6 +987,8 @@ definitions: type: array localTime: type: string + maxSize: + type: integer ntp: type: string swapDetails: @@ -10610,34 +10612,6 @@ paths: formatEN: 'update system bind info => ipv6: [ipv6], 监听 IP: [bindAddress]' formatZH: '修改系统监听信息 => ipv6: [ipv6], 监听 IP: [bindAddress]' paramKeys: [] - /settings/clean: - post: - consumes: - - application/json - description: 清理系统垃圾文件 - parameters: - - description: request - in: body - name: request - required: true - schema: - items: - $ref: '#/definitions/dto.Clean' - type: array - responses: - "200": - description: OK - security: - - ApiKeyAuth: [] - summary: System clean - tags: - - System Setting - x-panel-log: - BeforeFunctions: [] - bodyKeys: [] - formatEN: Clean system junk files - formatZH: 清理系统垃圾文件 - paramKeys: [] /settings/expired/handle: post: consumes: @@ -10795,23 +10769,6 @@ paths: formatEN: update system port => [serverPort] formatZH: 修改系统端口 => [serverPort] paramKeys: [] - /settings/scan: - post: - description: 扫描系统垃圾文件 - responses: - "200": - description: OK - security: - - ApiKeyAuth: [] - summary: Scan system - tags: - - System Setting - x-panel-log: - BeforeFunctions: [] - bodyKeys: [] - formatEN: scan System Junk Files - formatZH: 扫描系统垃圾文件 - paramKeys: [] /settings/search: post: description: 加载系统配置信息 @@ -11192,6 +11149,34 @@ paths: formatEN: upgrade service => [version] formatZH: 更新系统 => [version] paramKeys: [] + /toolbox/clean: + post: + consumes: + - application/json + description: 清理系统垃圾文件 + parameters: + - description: request + in: body + name: request + required: true + schema: + items: + $ref: '#/definitions/dto.Clean' + type: array + responses: + "200": + description: OK + security: + - ApiKeyAuth: [] + summary: Clean system + tags: + - Device + x-panel-log: + BeforeFunctions: [] + bodyKeys: [] + formatEN: Clean system junk files + formatZH: 清理系统垃圾文件 + paramKeys: [] /toolbox/device/base: get: description: 获取设备基础信息 @@ -11514,6 +11499,23 @@ paths: summary: Update fail2ban conf by file tags: - Fail2ban + /toolbox/scan: + post: + description: 扫描系统垃圾文件 + responses: + "200": + description: OK + security: + - ApiKeyAuth: [] + summary: Scan system + tags: + - Device + x-panel-log: + BeforeFunctions: [] + bodyKeys: [] + formatEN: scan System Junk Files + formatZH: 扫描系统垃圾文件 + paramKeys: [] /websites: post: consumes: diff --git a/frontend/src/api/interface/setting.ts b/frontend/src/api/interface/setting.ts index d3800d9d3..0a5558e38 100644 --- a/frontend/src/api/interface/setting.ts +++ b/frontend/src/api/interface/setting.ts @@ -87,23 +87,6 @@ export namespace Setting { interval: string; } - export interface CleanData { - systemClean: Array; - uploadClean: Array; - downloadClean: Array; - systemLogClean: Array; - } - export interface CleanTree { - id: string; - label: string; - children: Array; - type: string; - name: string; - size: number; - isCheck: boolean; - isRecommend: boolean; - } - export interface SnapshotCreate { id: number; from: string; diff --git a/frontend/src/api/interface/toolbox.ts b/frontend/src/api/interface/toolbox.ts index f4ecc968c..4ad344d35 100644 --- a/frontend/src/api/interface/toolbox.ts +++ b/frontend/src/api/interface/toolbox.ts @@ -31,6 +31,23 @@ export namespace Toolbox { zones: Array; } + export interface CleanData { + systemClean: Array; + uploadClean: Array; + downloadClean: Array; + systemLogClean: Array; + } + export interface CleanTree { + id: string; + label: string; + children: Array; + type: string; + name: string; + size: number; + isCheck: boolean; + isRecommend: boolean; + } + export interface Fail2banBaseInfo { isEnable: boolean; isActive: boolean; diff --git a/frontend/src/api/modules/setting.ts b/frontend/src/api/modules/setting.ts index 231cf4b45..c63fa6886 100644 --- a/frontend/src/api/modules/setting.ts +++ b/frontend/src/api/modules/setting.ts @@ -47,13 +47,6 @@ export const handleExpired = (param: Setting.PasswordUpdate) => { return http.post(`/settings/expired/handle`, param); }; -export const scanSystem = () => { - return http.post(`/settings/scan`, {}); -}; -export const cleanSystem = (param: any) => { - return http.post(`/settings/clean`, param); -}; - export const loadTimeZone = () => { return http.get>(`/settings/time/option`); }; diff --git a/frontend/src/api/modules/toolbox.ts b/frontend/src/api/modules/toolbox.ts index 9c9f82639..0095ead5d 100644 --- a/frontend/src/api/modules/toolbox.ts +++ b/frontend/src/api/modules/toolbox.ts @@ -33,6 +33,14 @@ export const loadDeviceConf = (name: string) => { return http.post(`/toolbox/device/conf`, { name: name }); }; +// clean +export const scan = () => { + return http.post(`/toolbox/scan`, {}); +}; +export const clean = (param: any) => { + return http.post(`/toolbox/clean`, param); +}; + // fail2ban export const getFail2banBase = () => { return http.get(`/toolbox/fail2ban/base`); diff --git a/frontend/src/views/log/operation/index.vue b/frontend/src/views/log/operation/index.vue index 92880ae10..b7b2ae55d 100644 --- a/frontend/src/views/log/operation/index.vue +++ b/frontend/src/views/log/operation/index.vue @@ -192,6 +192,9 @@ const loadDetail = (log: string) => { if (log.indexOf('[get]') !== -1) { log = log.replace('[get]', '[' + i18n.global.t('commons.button.get') + ']'); } + if (log.indexOf('[operate]') !== -1) { + log = log.replace('[operate]', '[' + i18n.global.t('commons.table.operate') + ']'); + } if (log.indexOf('[UserName]') !== -1) { return log.replace('[UserName]', '[' + i18n.global.t('commons.login.username') + ']'); } diff --git a/frontend/src/views/toolbox/clean/index.vue b/frontend/src/views/toolbox/clean/index.vue index 4466ec3d6..026dd999b 100644 --- a/frontend/src/views/toolbox/clean/index.vue +++ b/frontend/src/views/toolbox/clean/index.vue @@ -228,7 +228,8 @@