diff --git a/app/http/controllers/info_controller.go b/app/http/controllers/info_controller.go index aa55ad57..9b042511 100644 --- a/app/http/controllers/info_controller.go +++ b/app/http/controllers/info_controller.go @@ -123,24 +123,32 @@ func (c *InfoController) InstalledDbAndPhp(ctx http.Context) http.Response { } type data struct { - Slug string `json:"slug"` - Name string `json:"name"` + Label string `json:"label"` + Value string `json:"value"` } var phpData []data - phpData = append(phpData, data{Slug: "0", Name: "不使用"}) + var dbData []data + phpData = append(phpData, data{Value: "0", Label: "不使用"}) + dbData = append(dbData, data{Value: "0", Label: "不使用"}) for _, p := range php { match := regexp.MustCompile(`php(\d+)`).FindStringSubmatch(p.Slug) if len(match) == 0 { continue } - phpData = append(phpData, data{Slug: strings.ReplaceAll(p.Slug, "php", ""), Name: c.plugin.GetBySlug(p.Slug).Name}) + phpData = append(phpData, data{Value: strings.ReplaceAll(p.Slug, "php", ""), Label: c.plugin.GetBySlug(p.Slug).Name}) + } + + if mysqlInstalled { + dbData = append(dbData, data{Value: "mysql", Label: "MySQL"}) + } + if postgresqlInstalled { + dbData = append(dbData, data{Value: "postgresql", Label: "PostgreSQL"}) } return Success(ctx, http.Json{ - "php": phpData, - "mysql": mysqlInstalled, - "postgresql": postgresqlInstalled, + "php": phpData, + "db": dbData, }) } diff --git a/app/http/controllers/user_controller.go b/app/http/controllers/user_controller.go index cd93c146..e1eaa361 100644 --- a/app/http/controllers/user_controller.go +++ b/app/http/controllers/user_controller.go @@ -26,7 +26,7 @@ func (r *UserController) Login(ctx http.Context) http.Response { return Error(ctx, http.StatusUnprocessableEntity, err.Error()) } if errors != nil { - return Error(ctx, http.StatusUnprocessableEntity, errors.All()) + return Error(ctx, http.StatusUnprocessableEntity, errors.One()) } var user models.User diff --git a/app/http/controllers/website_controller.go b/app/http/controllers/website_controller.go index 47d6aabb..37940194 100644 --- a/app/http/controllers/website_controller.go +++ b/app/http/controllers/website_controller.go @@ -53,7 +53,8 @@ func (c *WebsiteController) Add(ctx http.Context) http.Response { } validator, err := ctx.Request().Validate(map[string]string{ "name": "required|regex:^[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)*$|not_exists:websites,name", - "domain": "required", + "domains": "required|slice", + "ports": "required|slice", "php": "required", "db": "bool", "db_type": "required_if:db,true", @@ -70,7 +71,8 @@ func (c *WebsiteController) Add(ctx http.Context) http.Response { var website services.PanelWebsite website.Name = ctx.Request().Input("name") - website.Domain = ctx.Request().Input("domain") + website.Domains = ctx.Request().InputArray("domains") + website.Ports = ctx.Request().InputArray("ports") website.Php = ctx.Request().InputInt("php") website.Db = ctx.Request().InputBool("db") website.DbType = ctx.Request().Input("db_type") @@ -167,14 +169,13 @@ func (c *WebsiteController) SaveConfig(ctx http.Context) http.Response { return check } validator, err := ctx.Request().Validate(map[string]string{ - "id": "required", - "domains": "required", - "ports": "required", + "domains": "required|slice", + "ports": "required|slice", "hsts": "bool", "ssl": "bool", "http_redirect": "bool", "open_basedir": "bool", - "waf": "required", + "waf": "bool", "waf_cache": "required", "waf_mode": "required", "waf_cc_deny": "required", @@ -219,7 +220,7 @@ func (c *WebsiteController) SaveConfig(ctx http.Context) http.Response { // 域名 domain := "server_name" - domains := strings.Split(ctx.Request().Input("domains"), "\n") + domains := ctx.Request().InputArray("domains") if len(domains) == 0 { return Error(ctx, http.StatusBadRequest, "域名不能为空") } @@ -238,7 +239,7 @@ func (c *WebsiteController) SaveConfig(ctx http.Context) http.Response { // 端口 var port strings.Builder - ports := strings.Split(ctx.Request().Input("ports"), "\n") + ports := ctx.Request().InputArray("ports") if len(ports) == 0 { return Error(ctx, http.StatusBadRequest, "端口不能为空") } @@ -299,12 +300,16 @@ func (c *WebsiteController) SaveConfig(ctx http.Context) http.Response { } // WAF - waf := ctx.Request().Input("waf") + waf := ctx.Request().InputBool("waf") + wafStr := "off" + if waf { + wafStr = "on" + } wafMode := ctx.Request().Input("waf_mode", "DYNAMIC") wafCcDeny := ctx.Request().Input("waf_cc_deny", "rate=1000r/m duration=60m") wafCache := ctx.Request().Input("waf_cache", "capacity=50") wafConfig := `# waf标记位开始 - waf ` + waf + `; + waf ` + wafStr + `; waf_rule_path /www/server/openresty/ngx_waf/assets/rules/; waf_mode ` + wafMode + `; waf_cc_deny ` + wafCcDeny + `; diff --git a/app/http/middleware/jwt.go b/app/http/middleware/jwt.go index ebc87c65..b8fd1065 100644 --- a/app/http/middleware/jwt.go +++ b/app/http/middleware/jwt.go @@ -6,16 +6,14 @@ import ( "github.com/goravel/framework/auth" "github.com/goravel/framework/contracts/http" "github.com/goravel/framework/facades" - - "panel/app/models" ) // Jwt 确保通过 JWT 鉴权 func Jwt() http.Middleware { return func(ctx http.Context) { - token := ctx.Request().Header("access_token", ctx.Request().Input("access_token", ctx.Request().Header("Sec-WebSocket-Protocol"))) + token := ctx.Request().Header("Authorization", "") if len(token) == 0 { - ctx.Request().AbortWithStatusJson(http.StatusUnauthorized, http.Json{ + ctx.Request().AbortWithStatusJson(http.StatusOK, http.Json{ "code": 401, "message": "未登录", }) @@ -27,7 +25,7 @@ func Jwt() http.Middleware { if errors.Is(err, auth.ErrorTokenExpired) { token, err = facades.Auth().Refresh(ctx) if err != nil { - // Refresh time exceeded + // 到达刷新时间上限 ctx.Request().AbortWithStatusJson(http.StatusOK, http.Json{ "code": 401, "message": "登录已过期", @@ -45,18 +43,6 @@ func Jwt() http.Middleware { } } - // 取出用户信息 - var user models.User - if err := facades.Auth().User(ctx, &user); err != nil { - ctx.Request().AbortWithStatusJson(http.StatusForbidden, http.Json{ - "code": 403, - "message": "用户不存在", - }) - return - } - - ctx.WithValue("user", user) - ctx.Response().Header("Authorization", token) ctx.Request().Next() } diff --git a/app/services/website.go b/app/services/website.go index 51e7113b..09f29790 100644 --- a/app/services/website.go +++ b/app/services/website.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "regexp" + "strconv" "strings" "github.com/goravel/framework/facades" @@ -21,32 +22,34 @@ type Website interface { Add(website PanelWebsite) (models.Website, error) Delete(id int) error GetConfig(id int) (WebsiteSetting, error) + GetConfigByName(name string) (WebsiteSetting, error) } type PanelWebsite struct { - Name string `json:"name"` - Status bool `json:"status"` - Domain string `json:"domain"` - Path string `json:"path"` - Php int `json:"php"` - Ssl bool `json:"ssl"` - Remark string `json:"remark"` - Db bool `json:"db"` - DbType string `json:"db_type"` - DbName string `json:"db_name"` - DbUser string `json:"db_user"` - DbPassword string `json:"db_password"` + Name string `json:"name"` + Status bool `json:"status"` + Domains []string `json:"domains"` + Ports []string `json:"ports"` + Path string `json:"path"` + Php int `json:"php"` + Ssl bool `json:"ssl"` + Remark string `json:"remark"` + Db bool `json:"db"` + DbType string `json:"db_type"` + DbName string `json:"db_name"` + DbUser string `json:"db_user"` + DbPassword string `json:"db_password"` } // WebsiteSetting 网站设置 type WebsiteSetting struct { Name string `json:"name"` - Ports []string `json:"ports"` Domains []string `json:"domains"` + Ports []string `json:"ports"` Root string `json:"root"` Path string `json:"path"` Index string `json:"index"` - Php int `json:"php"` + Php string `json:"php"` OpenBasedir bool `json:"open_basedir"` Ssl bool `json:"ssl"` SslCertificate string `json:"ssl_certificate"` @@ -107,7 +110,6 @@ func (r *WebsiteImpl) Add(website PanelWebsite) (models.Website, error) { website.Ssl = false website.Status = true - website.Domain = strings.TrimSpace(website.Domain) w := models.Website{ Name: website.Name, @@ -138,32 +140,28 @@ func (r *WebsiteImpl) Add(website PanelWebsite) (models.Website, error) { ` tools.Write(website.Path+"/index.html", index, 0644) - domainArr := strings.Split(website.Domain, "\n") portList := "" - portArr := make(map[string]bool) domainList := "" - for key, value := range domainArr { - temp := strings.Split(value, ":") - domainList += " " + temp[0] + portUsed := make(map[string]bool) + domainUsed := make(map[string]bool) - if len(temp) < 2 { - if _, ok := portArr["80"]; !ok { - if key == len(domainArr)-1 { - portList += " listen 80;" - } else { - portList += " listen 80;\n" - } - portArr["80"] = true - } - } else { - if _, ok := portArr[temp[1]]; !ok { - if key == len(domainArr)-1 { - portList += " listen " + temp[1] + ";" - } else { - portList += " listen " + temp[1] + ";\n" - } - portArr[temp[1]] = true + for i, port := range website.Ports { + if _, ok := portUsed[port]; !ok { + if i == len(website.Ports)-1 { + portList += " listen " + port + ";" + } else { + portList += " listen " + port + ";\n" } + portUsed[port] = true + } + } + if len(website.Ports) == 0 { + portList += " listen 80;\n" + } + for _, domain := range website.Domains { + if _, ok := domainUsed[domain]; !ok { + domainList += " " + domain + domainUsed[domain] = true } } @@ -288,7 +286,7 @@ func (r *WebsiteImpl) GetConfig(id int) (WebsiteSetting, error) { setting.Name = website.Name setting.Path = website.Path setting.Ssl = website.Ssl - setting.Php = website.Php + setting.Php = strconv.Itoa(website.Php) setting.Raw = config ports := tools.Cut(config, "# port标记位开始", "# port标记位结束") @@ -370,3 +368,13 @@ func (r *WebsiteImpl) GetConfig(id int) (WebsiteSetting, error) { return setting, nil } + +// GetConfigByName 根据网站名称获取网站配置 +func (r *WebsiteImpl) GetConfigByName(name string) (WebsiteSetting, error) { + var website models.Website + if err := facades.Orm().Query().Where("name", name).First(&website); err != nil { + return WebsiteSetting{}, err + } + + return r.GetConfig(int(website.ID)) +} diff --git a/routes/api.go b/routes/api.go index 9dda676a..1f825c66 100644 --- a/routes/api.go +++ b/routes/api.go @@ -40,20 +40,20 @@ func Api() { websiteController := controllers.NewWebsiteController() r.Get("list", websiteController.List) r.Post("add", websiteController.Add) - r.Post("delete", websiteController.Delete) + r.Delete("delete/{id}", websiteController.Delete) r.Get("defaultConfig", websiteController.GetDefaultConfig) r.Post("defaultConfig", websiteController.SaveDefaultConfig) - r.Get("config", websiteController.GetConfig) - r.Post("config", websiteController.SaveConfig) - r.Get("clearLog", websiteController.ClearLog) - r.Post("updateRemark", websiteController.UpdateRemark) + r.Get("config/{id}", websiteController.GetConfig) + r.Post("config/{id}", websiteController.SaveConfig) + r.Delete("log/{id}", websiteController.ClearLog) + r.Post("updateRemark/{id}", websiteController.UpdateRemark) r.Get("backupList", websiteController.BackupList) r.Post("createBackup", websiteController.CreateBackup) r.Post("uploadBackup", websiteController.UploadBackup) - r.Post("restoreBackup", websiteController.RestoreBackup) - r.Post("deleteBackup", websiteController.DeleteBackup) - r.Post("resetConfig", websiteController.ResetConfig) - r.Post("status", websiteController.Status) + r.Post("restoreBackup/{id}", websiteController.RestoreBackup) + r.Post("deleteBackup/{id}", websiteController.DeleteBackup) + r.Post("resetConfig/{id}", websiteController.ResetConfig) + r.Post("status/{id}", websiteController.Status) }) r.Prefix("plugin").Middleware(middleware.Jwt()).Group(func(r route.Router) { pluginController := controllers.NewPluginController()