diff --git a/backend/internal/cert/deploy/1panel.go b/backend/internal/cert/deploy/1panel.go index c6faf31..26fbca6 100644 --- a/backend/internal/cert/deploy/1panel.go +++ b/backend/internal/cert/deploy/1panel.go @@ -13,6 +13,7 @@ import ( "net/http" "net/url" "strconv" + "strings" "time" ) @@ -41,12 +42,28 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string) } timestamp := fmt.Sprintf("%d", time.Now().Unix()) token := generateToken(timestamp, providerConfig["api_key"]) - + // data, requestUrl, method := GetDeploy1PBody(cfg, Type) if requestUrl == "" || data == nil { return nil, fmt.Errorf("不支持的部署类型") } - + if requestUrl[0] == '/' { + requestUrl = requestUrl[1:] + } + version := providerConfig["version"] + switch version { + case "", "1", "v1": + requestUrl = "api/v1/" + requestUrl + case "2", "v2": + (*data)["ssl"] = "Enable" + if strings.Split(requestUrl, "/")[0] == "settings" { + requestUrl = "core/" + requestUrl + } + requestUrl = "api/v2/" + requestUrl + default: + return nil, fmt.Errorf("不支持的1Panel版本: %s", version) + } + // 编码为 JSON jsonData, err := json.Marshal(data) if err != nil { @@ -62,12 +79,12 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string) // fmt.Println(err) return nil, err } - + req.Header.Set("Content-Type", "application/json") req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36") req.Header.Set("1Panel-Timestamp", timestamp) req.Header.Set("1Panel-Token", token) - + // 自定义 Transport,跳过 SSL 证书验证 ignoreSsl := false if providerConfig["ignore_ssl"] == "1" { @@ -76,7 +93,7 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string) tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: ignoreSsl}, } - + client := &http.Client{Transport: tr} resp, err := client.Do(req) if err != nil { @@ -85,7 +102,7 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string) } body, _ := io.ReadAll(resp.Body) defer resp.Body.Close() - + var res map[string]interface{} err = json.Unmarshal(body, &res) if err != nil { @@ -103,7 +120,7 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string) return nil, fmt.Errorf("请求失败: %s", msg) } return res, nil - + } func Deploy1panel(cfg map[string]any) error { @@ -129,14 +146,14 @@ func Deploy1panel(cfg map[string]any) error { if !ok { return fmt.Errorf("证书错误:cert") } - + data := map[string]interface{}{ "cert": certPem, "key": keyPem, "ssl": "enable", "sslType": "import-paste", } - _, err := Request1panel(&data, "POST", providerID, "api/v1/settings/ssl/update") + _, err := Request1panel(&data, "POST", providerID, "settings/ssl/update") if err != nil { return fmt.Errorf("证书部署失败: %v", err) } @@ -171,7 +188,7 @@ func Deploy1panelSite(cfg map[string]any) error { return fmt.Errorf("证书错误:cert") } // 获取网站参数 - siteData, err := Request1panel(&map[string]any{}, "GET", providerID, fmt.Sprintf("api/v1/websites/%s/https", siteId)) + siteData, err := Request1panel(&map[string]any{}, "GET", providerID, fmt.Sprintf("websites/%s/https", siteId)) if err != nil { return fmt.Errorf("获取网站参数失败: %v", err) } @@ -180,12 +197,12 @@ func Deploy1panelSite(cfg map[string]any) error { if err != nil { return fmt.Errorf("获取网站参数失败: %v", err) } - + siteData, ok = siteData["data"].(map[string]any) if !ok { return fmt.Errorf("获取网站参数失败: data") } - + var SSLProtocol []any if siteData["SSLProtocol"] == nil { SSLProtocol = []any{"TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1"} @@ -210,7 +227,7 @@ func Deploy1panelSite(cfg map[string]any) error { if httpConfig == "" { httpConfig = "HTTPToHTTPS" } - + data := map[string]any{ "SSLProtocol": SSLProtocol, // "acmeAccountId": siteData["SSL"].(map[string]any)["acmeAccountId"].(float64), @@ -219,20 +236,20 @@ func Deploy1panelSite(cfg map[string]any) error { "privateKey": keyPem, // "certificatePath": "", // "privateKeyPath": "", - "enable": true, + "enable": true, "hsts": hsts, "httpConfig": httpConfig, "importType": "paste", "type": "manual", "websiteId": websiteId, } - _, err = Request1panel(&data, "POST", providerID, fmt.Sprintf("api/v1/websites/%s/https", siteId)) + _, err = Request1panel(&data, "POST", providerID, fmt.Sprintf("websites/%s/https", siteId)) return err } func OnePanelAPITest(providerID string) error { data := map[string]interface{}{} - _, err := Request1panel(&data, "GET", providerID, "api/v1/settings/upgrade") + _, err := Request1panel(&data, "GET", providerID, "settings/upgrade") if err != nil { return fmt.Errorf("测试请求失败: %v", err) } @@ -248,7 +265,7 @@ func OnePanelSiteList(providerID string) ([]response.AccessSiteList, error) { "pageSize": 1000, "websiteGroupId": 0, } - siteList, err := Request1panel(&data, "POST", providerID, "api/v1/websites/search") + siteList, err := Request1panel(&data, "POST", providerID, "websites/search") if err != nil { return nil, fmt.Errorf("获取网站列表失败 %v", err) } @@ -258,7 +275,7 @@ func OnePanelSiteList(providerID string) ([]response.AccessSiteList, error) { if !ok { return nil, fmt.Errorf("获取网站列表失败: data.items") } - + for _, site := range sites { siteMap, ok := site.(map[string]any) if !ok { @@ -267,6 +284,6 @@ func OnePanelSiteList(providerID string) ([]response.AccessSiteList, error) { siteId := strconv.FormatFloat(siteMap["id"].(float64), 'f', -1, 64) result = append(result, response.AccessSiteList{Id: siteId, SiteName: siteMap["alias"].(string), Domain: []string{}}) } - + return result, nil -} \ No newline at end of file +} diff --git a/backend/route/route.go b/backend/route/route.go index d31053c..579e383 100644 --- a/backend/route/route.go +++ b/backend/route/route.go @@ -3,10 +3,12 @@ package route import ( "ALLinSSL/backend/app/api" "ALLinSSL/backend/app/api/monitor" + "ALLinSSL/backend/public" "ALLinSSL/static" "github.com/gin-gonic/gin" "io/fs" "net/http" + "strings" ) func Register(r *gin.Engine) { @@ -119,6 +121,11 @@ func Register(r *gin.Engine) { // 其他路由:返回 index.html r.NoRoute(func(c *gin.Context) { + // 如果是 API 请求,返回 JSON 的 404 + if strings.HasPrefix(c.Request.URL.Path, "/v1/") { + public.FailMsg(c, "请求的资源不存在") + return + } data, err := static.BuildFS.ReadFile("build/index.html") if err != nil { c.String(http.StatusInternalServerError, "页面加载失败")