diff --git a/backend/app/api/v1/website.go b/backend/app/api/v1/website.go index 300a8f676..1b91656f6 100644 --- a/backend/app/api/v1/website.go +++ b/backend/app/api/v1/website.go @@ -428,6 +428,28 @@ func (b *BaseApi) UpdateWebsiteWafConfig(c *gin.Context) { helper.SuccessWithData(c, nil) } +// @Tags Website WAF +// @Summary Update website waf file +// @Description 更新 网站 waf 配置文件 +// @Accept json +// @Param request body request.WebsiteWafUpdate true "request" +// @Success 200 +// @Security ApiKeyAuth +// @Router /websites/waf/file/update [post] +// @x-panel-log {"bodyKeys":["websiteId"],"paramKeys":[],"BeforeFuntions":[{"input_column":"id","input_value":"websiteId","isList":false,"db":"websites","output_column":"primary_domain","output_value":"domain"}],"formatZH":"WAF 配置文件修改 [domain]","formatEN":"WAF conf file update [domain]"} +func (b *BaseApi) UpdateWebsiteWafFile(c *gin.Context) { + var req request.WebsiteWafFileUpdate + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + if err := websiteService.UpdateWafFile(req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, nil) +} + // @Tags Website Nginx // @Summary Update website nginx conf // @Description 更新 网站 nginx 配置 diff --git a/backend/app/dto/request/nginx.go b/backend/app/dto/request/nginx.go index 42f984a2f..744740ff1 100644 --- a/backend/app/dto/request/nginx.go +++ b/backend/app/dto/request/nginx.go @@ -3,9 +3,8 @@ package request import "github.com/1Panel-dev/1Panel/backend/app/dto" type NginxConfigFileUpdate struct { - Content string `json:"content" validate:"required"` - FilePath string `json:"filePath" validate:"required"` - Backup bool `json:"backup" validate:"required"` + Content string `json:"content" validate:"required"` + Backup bool `json:"backup" validate:"required"` } type NginxScopeReq struct { diff --git a/backend/app/dto/request/website.go b/backend/app/dto/request/website.go index 3dd51ea68..f87a32c90 100644 --- a/backend/app/dto/request/website.go +++ b/backend/app/dto/request/website.go @@ -75,6 +75,12 @@ type WebsiteWafReq struct { Rule string `json:"rule" validate:"required"` } +type WebsiteRedirectUpdate struct { + WebsiteID uint `json:"websiteId" validate:"required"` + Key string `json:"key" validate:"required"` + Enable bool `json:"enable" validate:"required"` +} + type WebsiteWafUpdate struct { WebsiteID uint `json:"websiteId" validate:"required"` Key string `json:"key" validate:"required"` @@ -199,3 +205,9 @@ type WebsiteProxyReq struct { type WebsiteRedirectReq struct { WebsiteID uint `json:"websiteId" validate:"required"` } + +type WebsiteWafFileUpdate struct { + WebsiteID uint `json:"websiteID" validate:"required"` + Content string `json:"content" validate:"required"` + Type string `json:"type" validate:"required,oneof=cc ip_white ip_block url_white url_block cookie_block args_check post_check ua_check file_ext_block"` +} diff --git a/backend/app/dto/response/nginx.go b/backend/app/dto/response/nginx.go index 867b4a70b..c40521a68 100644 --- a/backend/app/dto/response/nginx.go +++ b/backend/app/dto/response/nginx.go @@ -49,3 +49,7 @@ type NginxRedirectConfig struct { Content string `json:"content"` RedirectRoot bool `json:"redirectRoot"` } + +type NginxFile struct { + Content string `json:"content"` +} diff --git a/backend/app/dto/response/website.go b/backend/app/dto/response/website.go index e8284980f..e2d8f77ff 100644 --- a/backend/app/dto/response/website.go +++ b/backend/app/dto/response/website.go @@ -26,9 +26,8 @@ type WebsiteNginxConfig struct { } type WebsiteWafConfig struct { - Enable bool `json:"enable"` - FilePath string `json:"filePath"` - Content string `json:"content"` + Enable bool `json:"enable"` + Content string `json:"content"` } type WebsiteHTTPS struct { diff --git a/backend/app/service/nginx.go b/backend/app/service/nginx.go index 046c5a949..32f7f0ede 100644 --- a/backend/app/service/nginx.go +++ b/backend/app/service/nginx.go @@ -20,7 +20,7 @@ type NginxService struct { } type INginxService interface { - GetNginxConfig() (response.FileInfo, error) + GetNginxConfig() (*response.NginxFile, error) GetConfigByScope(req request.NginxScopeReq) ([]response.NginxParam, error) UpdateConfigByScope(req request.NginxConfigUpdate) error GetStatus() (response.NginxStatus, error) @@ -31,20 +31,17 @@ func NewINginxService() INginxService { return &NginxService{} } -func (n NginxService) GetNginxConfig() (response.FileInfo, error) { +func (n NginxService) GetNginxConfig() (*response.NginxFile, error) { nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) if err != nil { - return response.FileInfo{}, err + return nil, err } configPath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "nginx.conf") - info, err := files.NewFileInfo(files.FileOption{ - Path: configPath, - Expand: true, - }) + byteContent, err := files.NewFileOp().GetContent(configPath) if err != nil { - return response.FileInfo{}, err + return nil, err } - return response.FileInfo{FileInfo: *info}, nil + return &response.NginxFile{Content: string(byteContent)}, nil } func (n NginxService) GetConfigByScope(req request.NginxScopeReq) ([]response.NginxParam, error) { @@ -86,31 +83,32 @@ func (n NginxService) GetStatus() (response.NginxStatus, error) { func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error { fileOp := files.NewFileOp() + nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) + filePath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "nginx.conf") + if err != nil { + return err + } if req.Backup { - backupPath := path.Join(path.Dir(req.FilePath), "bak") + backupPath := path.Join(path.Dir(filePath), "bak") if !fileOp.Stat(backupPath) { if err := fileOp.CreateDir(backupPath, 0755); err != nil { return err } } newFile := path.Join(backupPath, "nginx.bak"+"-"+time.Now().Format("2006-01-02-15-04-05")) - if err := fileOp.Copy(req.FilePath, backupPath); err != nil { + if err := fileOp.Copy(filePath, backupPath); err != nil { return err } if err := fileOp.Rename(path.Join(backupPath, "nginx.conf"), newFile); err != nil { return err } } - oldContent, err := os.ReadFile(req.FilePath) + oldContent, err := os.ReadFile(filePath) if err != nil { return err } - if err := fileOp.WriteFile(req.FilePath, strings.NewReader(req.Content), 0644); err != nil { - return err - } - nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) - if err != nil { + if err = fileOp.WriteFile(filePath, strings.NewReader(req.Content), 0644); err != nil { return err } - return nginxCheckAndReload(string(oldContent), req.FilePath, nginxInstall.ContainerName) + return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName) } diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 88332b3ad..6a32311ec 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -71,6 +71,7 @@ type IWebsiteService interface { GetWafConfig(req request.WebsiteWafReq) (response.WebsiteWafConfig, error) UpdateWafConfig(req request.WebsiteWafUpdate) error + UpdateWafFile(req request.WebsiteWafFileUpdate) (err error) GetPHPConfig(id uint) (*response.PHPConfig, error) UpdatePHPConfig(req request.WebsitePHPConfigUpdate) error @@ -848,7 +849,6 @@ func (w WebsiteService) GetWafConfig(req request.WebsiteWafReq) (response.Websit if err != nil { return res, nil } - res.FilePath = filePath res.Content = string(content) return res, nil @@ -2282,3 +2282,20 @@ func (w WebsiteService) UpdateRedirectFile(req request.NginxRedirectUpdate) (err }() return updateNginxConfig(constant.NginxScopeServer, nil, &website) } + +func (w WebsiteService) UpdateWafFile(req request.WebsiteWafFileUpdate) (err error) { + var ( + website model.Website + nginxInstall model.AppInstall + ) + website, err = websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID)) + if err != nil { + return err + } + nginxInstall, err = getAppInstallByKey(constant.AppOpenresty) + if err != nil { + return + } + rulePath := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "waf", "rules", fmt.Sprintf("%s.json", req.Type)) + return files.NewFileOp().WriteFile(rulePath, strings.NewReader(req.Content), 0755) +} diff --git a/backend/router/ro_website.go b/backend/router/ro_website.go index 29b06eea1..ff6780a52 100644 --- a/backend/router/ro_website.go +++ b/backend/router/ro_website.go @@ -41,6 +41,7 @@ func (a *WebsiteRouter) InitWebsiteRouter(Router *gin.RouterGroup) { groupRouter.POST("/waf/config", baseApi.GetWebsiteWafConfig) groupRouter.POST("/waf/update", baseApi.UpdateWebsiteWafConfig) + groupRouter.POST("/waf/file/update", baseApi.UpdateWebsiteWafFile) groupRouter.GET("/php/config/:id", baseApi.GetWebsitePHPConfig) groupRouter.POST("/php/config", baseApi.UpdateWebsitePHPConfig) diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index 01262f069..380c8e48f 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -1,5 +1,5 @@ -// Code generated by swaggo/swag. DO NOT EDIT. - +// Package docs GENERATED BY SWAG; DO NOT EDIT +// This file was generated by swaggo/swag package docs import "github.com/swaggo/swag" @@ -11334,6 +11334,57 @@ const docTemplate = `{ } } }, + "/websites/waf/file/update": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "更新 网站 waf 配置文件", + "consumes": [ + "application/json" + ], + "tags": [ + "Website WAF" + ], + "summary": "Update website waf file", + "parameters": [ + { + "description": "request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.WebsiteWafUpdate" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + }, + "x-panel-log": { + "BeforeFuntions": [ + { + "db": "websites", + "input_column": "id", + "input_value": "websiteId", + "isList": false, + "output_column": "primary_domain", + "output_value": "domain" + } + ], + "bodyKeys": [ + "websiteId" + ], + "formatEN": "WAF conf file update [domain]", + "formatZH": "WAF 配置文件修改 [domain]", + "paramKeys": [] + } + } + }, "/websites/waf/update": { "post": { "security": [ @@ -15526,8 +15577,7 @@ const docTemplate = `{ "type": "object", "required": [ "backup", - "content", - "filePath" + "content" ], "properties": { "backup": { @@ -15535,9 +15585,6 @@ const docTemplate = `{ }, "content": { "type": "string" - }, - "filePath": { - "type": "string" } } }, @@ -17131,9 +17178,6 @@ const docTemplate = `{ }, "enable": { "type": "boolean" - }, - "filePath": { - "type": "string" } } } diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index 7b5b5b8d7..9a2d70f37 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -11327,6 +11327,57 @@ } } }, + "/websites/waf/file/update": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "更新 网站 waf 配置文件", + "consumes": [ + "application/json" + ], + "tags": [ + "Website WAF" + ], + "summary": "Update website waf file", + "parameters": [ + { + "description": "request", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/request.WebsiteWafUpdate" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + }, + "x-panel-log": { + "BeforeFuntions": [ + { + "db": "websites", + "input_column": "id", + "input_value": "websiteId", + "isList": false, + "output_column": "primary_domain", + "output_value": "domain" + } + ], + "bodyKeys": [ + "websiteId" + ], + "formatEN": "WAF conf file update [domain]", + "formatZH": "WAF 配置文件修改 [domain]", + "paramKeys": [] + } + } + }, "/websites/waf/update": { "post": { "security": [ @@ -15519,8 +15570,7 @@ "type": "object", "required": [ "backup", - "content", - "filePath" + "content" ], "properties": { "backup": { @@ -15528,9 +15578,6 @@ }, "content": { "type": "string" - }, - "filePath": { - "type": "string" } } }, @@ -17124,9 +17171,6 @@ }, "enable": { "type": "boolean" - }, - "filePath": { - "type": "string" } } } diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index bcc255267..a33b5e003 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -2773,12 +2773,9 @@ definitions: type: boolean content: type: string - filePath: - type: string required: - backup - content - - filePath type: object request.NginxConfigUpdate: properties: @@ -3846,8 +3843,6 @@ definitions: type: string enable: type: boolean - filePath: - type: string type: object host: localhost info: @@ -11038,6 +11033,39 @@ paths: summary: Load websit waf conf tags: - Website WAF + /websites/waf/file/update: + post: + consumes: + - application/json + description: 更新 网站 waf 配置文件 + parameters: + - description: request + in: body + name: request + required: true + schema: + $ref: '#/definitions/request.WebsiteWafUpdate' + responses: + "200": + description: OK + security: + - ApiKeyAuth: [] + summary: Update website waf file + tags: + - Website WAF + x-panel-log: + BeforeFuntions: + - db: websites + input_column: id + input_value: websiteId + isList: false + output_column: primary_domain + output_value: domain + bodyKeys: + - websiteId + formatEN: WAF conf file update [domain] + formatZH: WAF 配置文件修改 [domain] + paramKeys: [] /websites/waf/update: post: consumes: diff --git a/frontend/src/api/interface/nginx.ts b/frontend/src/api/interface/nginx.ts index 34a1c5f06..dab9465bf 100644 --- a/frontend/src/api/interface/nginx.ts +++ b/frontend/src/api/interface/nginx.ts @@ -26,7 +26,6 @@ export namespace Nginx { export interface NginxFileUpdate { content: string; - filePath: string; backup: boolean; } } diff --git a/frontend/src/api/interface/website.ts b/frontend/src/api/interface/website.ts index 8006b770a..752d57cc6 100644 --- a/frontend/src/api/interface/website.ts +++ b/frontend/src/api/interface/website.ts @@ -250,7 +250,6 @@ export namespace Website { export interface WafRes { enable: boolean; - filePath: string; content: string; } @@ -260,6 +259,12 @@ export namespace Website { key: string; } + export interface WafFileUpdate { + websiteId: number; + type: string; + content: string; + } + export interface DelReq { id: number; } diff --git a/frontend/src/api/modules/website.ts b/frontend/src/api/modules/website.ts index c5ed18cfa..a411f7f75 100644 --- a/frontend/src/api/modules/website.ts +++ b/frontend/src/api/modules/website.ts @@ -151,6 +151,10 @@ export const UpdateWafEnable = (req: Website.WafUpdate) => { return http.post(`/websites/waf/update`, req); }; +export const UpdateWafFile = (req: Website.WafFileUpdate) => { + return http.post(`/websites/waf/file/update`, req); +}; + export const UpdateNginxFile = (req: Website.NginxUpdate) => { return http.post(`/websites/nginx/update`, req); }; diff --git a/frontend/src/views/website/website/config/safety/ccdeny/index.vue b/frontend/src/views/website/website/config/safety/ccdeny/index.vue index e8d68843d..d170d13ef 100644 --- a/frontend/src/views/website/website/config/safety/ccdeny/index.vue +++ b/frontend/src/views/website/website/config/safety/ccdeny/index.vue @@ -33,8 +33,7 @@ diff --git a/frontend/src/views/website/website/config/safety/file-block-list/index.vue b/frontend/src/views/website/website/config/safety/file-block-list/index.vue index da0a0d4dd..aee388f74 100644 --- a/frontend/src/views/website/website/config/safety/file-block-list/index.vue +++ b/frontend/src/views/website/website/config/safety/file-block-list/index.vue @@ -32,9 +32,8 @@ diff --git a/frontend/src/views/website/website/config/safety/simple-list/index.vue b/frontend/src/views/website/website/config/safety/simple-list/index.vue index f6bbc7134..b2c4fc18d 100644 --- a/frontend/src/views/website/website/config/safety/simple-list/index.vue +++ b/frontend/src/views/website/website/config/safety/simple-list/index.vue @@ -40,9 +40,8 @@ diff --git a/frontend/src/views/website/website/config/safety/value-list/index.vue b/frontend/src/views/website/website/config/safety/value-list/index.vue index 39fc0a584..5f2518265 100644 --- a/frontend/src/views/website/website/config/safety/value-list/index.vue +++ b/frontend/src/views/website/website/config/safety/value-list/index.vue @@ -47,9 +47,8 @@ diff --git a/frontend/src/views/website/website/nginx/source/index.vue b/frontend/src/views/website/website/nginx/source/index.vue index 5ad3b85b1..c699e2e40 100644 --- a/frontend/src/views/website/website/nginx/source/index.vue +++ b/frontend/src/views/website/website/nginx/source/index.vue @@ -48,7 +48,6 @@ import { MsgSuccess } from '@/utils/message'; const extensions = [StreamLanguage.define(nginx), oneDark]; -let data = ref(); let content = ref(''); let loading = ref(false); let useOld = ref(false); @@ -56,7 +55,6 @@ let useOld = ref(false); const submit = () => { loading.value = true; UpdateNginxConfigFile({ - filePath: data.value.path, content: content.value, backup: useOld.value, }) @@ -70,17 +68,20 @@ const submit = () => { }; const getNginx = async () => { - const res = await GetNginx(); - data.value = res.data; - content.value = data.value.content; - useOld.value = false; + try { + const res = await GetNginx(); + content.value = res.data.content; + useOld.value = false; + } catch (error) {} }; const getDefaultConfig = async () => { loading.value = true; - const res = await GetAppDefaultConfig('openresty'); - content.value = res.data; - useOld.value = true; + try { + const res = await GetAppDefaultConfig('openresty'); + content.value = res.data; + useOld.value = true; + } catch (error) {} loading.value = false; };