From 39a57673196667173a881c2c1fc09ad2e178133e Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 19 Jun 2024 10:17:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E7=BD=91=E7=AB=99=E9=BB=98=E8=AE=A4=E9=A1=B5=E9=9D=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20(#5491)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/website.go | 42 +++++++ backend/app/dto/request/website.go | 9 ++ backend/app/dto/response/website.go | 4 + backend/app/service/website.go | 91 +++++++++++++++ backend/app/service/website_utils.go | 13 ++- backend/router/ro_website.go | 85 +++++++------- cmd/server/nginx_conf/404.html | 6 + cmd/server/nginx_conf/domain404.html | 6 + cmd/server/nginx_conf/nginx_conf.go | 9 ++ cmd/server/nginx_conf/stop.html | 33 ++++++ frontend/package.json | 3 + frontend/src/api/interface/website.ts | 8 ++ frontend/src/api/modules/website.ts | 8 ++ frontend/src/lang/modules/en.ts | 6 + frontend/src/lang/modules/tw.ts | 6 + frontend/src/lang/modules/zh.ts | 6 + .../src/views/website/website/html/index.vue | 107 ++++++++++++++++++ frontend/src/views/website/website/index.vue | 22 +++- 18 files changed, 416 insertions(+), 48 deletions(-) create mode 100644 cmd/server/nginx_conf/404.html create mode 100644 cmd/server/nginx_conf/domain404.html create mode 100644 cmd/server/nginx_conf/stop.html create mode 100644 frontend/src/views/website/website/html/index.vue diff --git a/backend/app/api/v1/website.go b/backend/app/api/v1/website.go index 30797f875..caef8f261 100644 --- a/backend/app/api/v1/website.go +++ b/backend/app/api/v1/website.go @@ -778,3 +778,45 @@ func (b *BaseApi) GetDirConfig(c *gin.Context) { } helper.SuccessWithData(c, res) } + +// @Tags Website +// @Summary Get default html +// @Description 获取默认 html +// @Accept json +// @Success 200 {object} response.FileInfo +// @Security ApiKeyAuth +// @Router /websites/default/html/:type [get] +func (b *BaseApi) GetDefaultHtml(c *gin.Context) { + resourceType, err := helper.GetStrParamByKey(c, "type") + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInternalServer, nil) + return + } + fileInfo, err := websiteService.GetDefaultHtml(resourceType) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, fileInfo) +} + +// @Tags Website +// @Summary Update default html +// @Description 更新默认 html +// @Accept json +// @Param request body request.WebsiteHtmlUpdate true "request" +// @Success 200 +// @Security ApiKeyAuth +// @Router /websites/default/html/update [post] +// @x-panel-log {"bodyKeys":["type"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新默认 html","formatEN":"Update default html"} +func (b *BaseApi) UpdateDefaultHtml(c *gin.Context) { + var req request.WebsiteHtmlUpdate + if err := helper.CheckBindAndValidate(&req, c); err != nil { + return + } + if err := websiteService.UpdateDefaultHtml(req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithOutData(c) +} diff --git a/backend/app/dto/request/website.go b/backend/app/dto/request/website.go index 2cd425e01..2082e1221 100644 --- a/backend/app/dto/request/website.go +++ b/backend/app/dto/request/website.go @@ -208,3 +208,12 @@ type WafWebsite struct { Domains []string `json:"domains"` Host []string `json:"host"` } + +type WebsiteHtmlReq struct { + Type string `json:"type" validate:"required"` +} + +type WebsiteHtmlUpdate struct { + Type string `json:"type" validate:"required"` + Content string `json:"content" validate:"required"` +} diff --git a/backend/app/dto/response/website.go b/backend/app/dto/response/website.go index 15cc78f85..8a31b69e8 100644 --- a/backend/app/dto/response/website.go +++ b/backend/app/dto/response/website.go @@ -82,3 +82,7 @@ type WebsiteDirConfig struct { UserGroup string `json:"userGroup"` Msg string `json:"msg"` } + +type WebsiteHtmlRes struct { + Content string `json:"content"` +} diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 45656a6b7..f94a20bd3 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -96,6 +96,9 @@ type IWebsiteService interface { OperateRedirect(req request.NginxRedirectReq) (err error) GetRedirect(id uint) (res []response.NginxRedirectConfig, err error) UpdateRedirectFile(req request.NginxRedirectUpdate) (err error) + + UpdateDefaultHtml(req request.WebsiteHtmlUpdate) error + GetDefaultHtml(resourceType string) (*response.WebsiteHtmlRes, error) } func NewIWebsiteService() IWebsiteService { @@ -2459,3 +2462,91 @@ func (w WebsiteService) LoadWebsiteDirConfig(req request.WebsiteCommonReq) (*res return res, nil } + +func (w WebsiteService) GetDefaultHtml(resourceType string) (*response.WebsiteHtmlRes, error) { + nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) + if err != nil { + return nil, err + } + rootPath := path.Join(nginxInstall.GetPath(), "root") + fileOp := files.NewFileOp() + defaultPath := path.Join(rootPath, "default") + if !fileOp.Stat(defaultPath) { + _ = fileOp.CreateDir(defaultPath, 0755) + } + + res := &response.WebsiteHtmlRes{} + + switch resourceType { + case "404": + resourcePath := path.Join(defaultPath, "404.html") + if content, _ := getResourceContent(fileOp, resourcePath); content != "" { + res.Content = content + return res, nil + } + res.Content = string(nginx_conf.NotFoundHTML) + return res, nil + case "php": + resourcePath := path.Join(defaultPath, "index.php") + if content, _ := getResourceContent(fileOp, resourcePath); content != "" { + res.Content = content + return res, nil + } + res.Content = string(nginx_conf.IndexPHP) + return res, nil + case "index": + resourcePath := path.Join(defaultPath, "index.html") + if content, _ := getResourceContent(fileOp, resourcePath); content != "" { + res.Content = content + return res, nil + } + res.Content = string(nginx_conf.Index) + return res, nil + case "domain404": + resourcePath := path.Join(rootPath, "404.html") + if content, _ := getResourceContent(fileOp, resourcePath); content != "" { + res.Content = content + return res, nil + } + res.Content = string(nginx_conf.DomainNotFoundHTML) + return res, nil + case "stop": + resourcePath := path.Join(rootPath, "stop", "index.html") + if content, _ := getResourceContent(fileOp, resourcePath); content != "" { + res.Content = content + return res, nil + } + res.Content = string(nginx_conf.StopHTML) + return res, nil + } + return res, nil +} + +func (w WebsiteService) UpdateDefaultHtml(req request.WebsiteHtmlUpdate) error { + nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) + if err != nil { + return err + } + rootPath := path.Join(nginxInstall.GetPath(), "root") + fileOp := files.NewFileOp() + defaultPath := path.Join(rootPath, "default") + if !fileOp.Stat(defaultPath) { + _ = fileOp.CreateDir(defaultPath, 0755) + } + var resourcePath string + switch req.Type { + case "404": + resourcePath = path.Join(defaultPath, "404.html") + case "php": + resourcePath = path.Join(defaultPath, "index.php") + case "index": + resourcePath = path.Join(defaultPath, "index.html") + case "domain404": + resourcePath = path.Join(rootPath, "404.html") + case "stop": + resourcePath = path.Join(rootPath, "stop", "index.html") + default: + return nil + } + return fileOp.SaveFile(resourcePath, req.Content, 0644) +} diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index 179c7024b..38c4a59e8 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -317,7 +317,7 @@ func createWafConfig(website *model.Website, domains []model.WebsiteDomain) erro for _, domain := range domains { wafWebsite.Domains = append(wafWebsite.Domains, domain.Domain) if domain.Port != 80 && domain.Port != 443 { - wafWebsite.Host = append(wafWebsite.Host, domain.Domain+":"+string(rune(domain.Port))) + wafWebsite.Host = append(wafWebsite.Host, domain.Domain+":"+strconv.Itoa(domain.Port)) } } websitesArray = append(websitesArray, wafWebsite) @@ -1092,3 +1092,14 @@ func checkSSLStatus(expireDate time.Time) string { } return "success" } + +func getResourceContent(fileOp files.FileOp, resourcePath string) (string, error) { + if fileOp.Stat(resourcePath) { + content, err := fileOp.GetContent(resourcePath) + if err != nil { + return "", err + } + return string(content), nil + } + return "", nil +} diff --git a/backend/router/ro_website.go b/backend/router/ro_website.go index c7100627c..7931c8003 100644 --- a/backend/router/ro_website.go +++ b/backend/router/ro_website.go @@ -10,59 +10,62 @@ type WebsiteRouter struct { } func (a *WebsiteRouter) InitRouter(Router *gin.RouterGroup) { - groupRouter := Router.Group("websites") - groupRouter.Use(middleware.JwtAuth()).Use(middleware.SessionAuth()).Use(middleware.PasswordExpired()) + websiteRouter := Router.Group("websites") + websiteRouter.Use(middleware.JwtAuth()).Use(middleware.SessionAuth()).Use(middleware.PasswordExpired()) baseApi := v1.ApiGroupApp.BaseApi { - groupRouter.POST("/search", baseApi.PageWebsite) - groupRouter.GET("/list", baseApi.GetWebsites) - groupRouter.POST("", baseApi.CreateWebsite) - groupRouter.POST("/operate", baseApi.OpWebsite) - groupRouter.POST("/log", baseApi.OpWebsiteLog) - groupRouter.POST("/check", baseApi.CreateWebsiteCheck) - groupRouter.GET("/options", baseApi.GetWebsiteOptions) - groupRouter.POST("/update", baseApi.UpdateWebsite) - groupRouter.GET("/:id", baseApi.GetWebsite) - groupRouter.POST("/del", baseApi.DeleteWebsite) - groupRouter.POST("/default/server", baseApi.ChangeDefaultServer) + websiteRouter.POST("/search", baseApi.PageWebsite) + websiteRouter.GET("/list", baseApi.GetWebsites) + websiteRouter.POST("", baseApi.CreateWebsite) + websiteRouter.POST("/operate", baseApi.OpWebsite) + websiteRouter.POST("/log", baseApi.OpWebsiteLog) + websiteRouter.POST("/check", baseApi.CreateWebsiteCheck) + websiteRouter.GET("/options", baseApi.GetWebsiteOptions) + websiteRouter.POST("/update", baseApi.UpdateWebsite) + websiteRouter.GET("/:id", baseApi.GetWebsite) + websiteRouter.POST("/del", baseApi.DeleteWebsite) + websiteRouter.POST("/default/server", baseApi.ChangeDefaultServer) - groupRouter.GET("/domains/:websiteId", baseApi.GetWebDomains) - groupRouter.POST("/domains/del", baseApi.DeleteWebDomain) - groupRouter.POST("/domains", baseApi.CreateWebDomain) + websiteRouter.GET("/domains/:websiteId", baseApi.GetWebDomains) + websiteRouter.POST("/domains/del", baseApi.DeleteWebDomain) + websiteRouter.POST("/domains", baseApi.CreateWebDomain) - groupRouter.GET("/:id/config/:type", baseApi.GetWebsiteNginx) - groupRouter.POST("/config", baseApi.GetNginxConfig) - groupRouter.POST("/config/update", baseApi.UpdateNginxConfig) - groupRouter.POST("/nginx/update", baseApi.UpdateWebsiteNginxConfig) + websiteRouter.GET("/:id/config/:type", baseApi.GetWebsiteNginx) + websiteRouter.POST("/config", baseApi.GetNginxConfig) + websiteRouter.POST("/config/update", baseApi.UpdateNginxConfig) + websiteRouter.POST("/nginx/update", baseApi.UpdateWebsiteNginxConfig) - groupRouter.GET("/:id/https", baseApi.GetHTTPSConfig) - groupRouter.POST("/:id/https", baseApi.UpdateHTTPSConfig) + websiteRouter.GET("/:id/https", baseApi.GetHTTPSConfig) + websiteRouter.POST("/:id/https", baseApi.UpdateHTTPSConfig) - groupRouter.GET("/php/config/:id", baseApi.GetWebsitePHPConfig) - groupRouter.POST("/php/config", baseApi.UpdateWebsitePHPConfig) - groupRouter.POST("/php/update", baseApi.UpdatePHPFile) - groupRouter.POST("/php/version", baseApi.ChangePHPVersion) + websiteRouter.GET("/php/config/:id", baseApi.GetWebsitePHPConfig) + websiteRouter.POST("/php/config", baseApi.UpdateWebsitePHPConfig) + websiteRouter.POST("/php/update", baseApi.UpdatePHPFile) + websiteRouter.POST("/php/version", baseApi.ChangePHPVersion) - groupRouter.POST("/rewrite", baseApi.GetRewriteConfig) - groupRouter.POST("/rewrite/update", baseApi.UpdateRewriteConfig) + websiteRouter.POST("/rewrite", baseApi.GetRewriteConfig) + websiteRouter.POST("/rewrite/update", baseApi.UpdateRewriteConfig) - groupRouter.POST("/dir/update", baseApi.UpdateSiteDir) - groupRouter.POST("/dir/permission", baseApi.UpdateSiteDirPermission) - groupRouter.POST("/dir", baseApi.GetDirConfig) + websiteRouter.POST("/dir/update", baseApi.UpdateSiteDir) + websiteRouter.POST("/dir/permission", baseApi.UpdateSiteDirPermission) + websiteRouter.POST("/dir", baseApi.GetDirConfig) - groupRouter.POST("/proxies", baseApi.GetProxyConfig) - groupRouter.POST("/proxies/update", baseApi.UpdateProxyConfig) - groupRouter.POST("/proxies/file", baseApi.UpdateProxyConfigFile) + websiteRouter.POST("/proxies", baseApi.GetProxyConfig) + websiteRouter.POST("/proxies/update", baseApi.UpdateProxyConfig) + websiteRouter.POST("/proxies/file", baseApi.UpdateProxyConfigFile) - groupRouter.POST("/auths", baseApi.GetAuthConfig) - groupRouter.POST("/auths/update", baseApi.UpdateAuthConfig) + websiteRouter.POST("/auths", baseApi.GetAuthConfig) + websiteRouter.POST("/auths/update", baseApi.UpdateAuthConfig) - groupRouter.POST("/leech", baseApi.GetAntiLeech) - groupRouter.POST("/leech/update", baseApi.UpdateAntiLeech) + websiteRouter.POST("/leech", baseApi.GetAntiLeech) + websiteRouter.POST("/leech/update", baseApi.UpdateAntiLeech) - groupRouter.POST("/redirect/update", baseApi.UpdateRedirectConfig) - groupRouter.POST("/redirect", baseApi.GetRedirectConfig) - groupRouter.POST("/redirect/file", baseApi.UpdateRedirectConfigFile) + websiteRouter.POST("/redirect/update", baseApi.UpdateRedirectConfig) + websiteRouter.POST("/redirect", baseApi.GetRedirectConfig) + websiteRouter.POST("/redirect/file", baseApi.UpdateRedirectConfigFile) + + websiteRouter.GET("/default/html/:type", baseApi.GetDefaultHtml) + websiteRouter.POST("/default/html/update", baseApi.UpdateDefaultHtml) } } diff --git a/cmd/server/nginx_conf/404.html b/cmd/server/nginx_conf/404.html new file mode 100644 index 000000000..d75ed7812 --- /dev/null +++ b/cmd/server/nginx_conf/404.html @@ -0,0 +1,6 @@ + +