修改1panel兼容v2、接口不存在时返回404

pull/295/head
v-me-50 2025-07-09 16:32:28 +08:00
parent 8c89e1ea2a
commit 68df9af02b
2 changed files with 44 additions and 20 deletions

View File

@ -13,6 +13,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -46,6 +47,22 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
if requestUrl == "" || data == nil { if requestUrl == "" || data == nil {
return nil, fmt.Errorf("不支持的部署类型") 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 // 编码为 JSON
jsonData, err := json.Marshal(data) jsonData, err := json.Marshal(data)
@ -136,7 +153,7 @@ func Deploy1panel(cfg map[string]any) error {
"ssl": "enable", "ssl": "enable",
"sslType": "import-paste", "sslType": "import-paste",
} }
_, err := Request1panel(&data, "POST", providerID, "api/v1/settings/ssl/update") _, err := Request1panel(&data, "POST", providerID, "settings/ssl/update")
if err != nil { if err != nil {
return fmt.Errorf("证书部署失败: %v", err) return fmt.Errorf("证书部署失败: %v", err)
} }
@ -171,7 +188,7 @@ func Deploy1panelSite(cfg map[string]any) error {
return fmt.Errorf("证书错误cert") 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 { if err != nil {
return fmt.Errorf("获取网站参数失败: %v", err) return fmt.Errorf("获取网站参数失败: %v", err)
} }
@ -219,20 +236,20 @@ func Deploy1panelSite(cfg map[string]any) error {
"privateKey": keyPem, "privateKey": keyPem,
// "certificatePath": "", // "certificatePath": "",
// "privateKeyPath": "", // "privateKeyPath": "",
"enable": true, "enable": true,
"hsts": hsts, "hsts": hsts,
"httpConfig": httpConfig, "httpConfig": httpConfig,
"importType": "paste", "importType": "paste",
"type": "manual", "type": "manual",
"websiteId": websiteId, "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 return err
} }
func OnePanelAPITest(providerID string) error { func OnePanelAPITest(providerID string) error {
data := map[string]interface{}{} data := map[string]interface{}{}
_, err := Request1panel(&data, "GET", providerID, "api/v1/settings/upgrade") _, err := Request1panel(&data, "GET", providerID, "settings/upgrade")
if err != nil { if err != nil {
return fmt.Errorf("测试请求失败: %v", err) return fmt.Errorf("测试请求失败: %v", err)
} }
@ -248,7 +265,7 @@ func OnePanelSiteList(providerID string) ([]response.AccessSiteList, error) {
"pageSize": 1000, "pageSize": 1000,
"websiteGroupId": 0, "websiteGroupId": 0,
} }
siteList, err := Request1panel(&data, "POST", providerID, "api/v1/websites/search") siteList, err := Request1panel(&data, "POST", providerID, "websites/search")
if err != nil { if err != nil {
return nil, fmt.Errorf("获取网站列表失败 %v", err) return nil, fmt.Errorf("获取网站列表失败 %v", err)
} }

View File

@ -3,10 +3,12 @@ package route
import ( import (
"ALLinSSL/backend/app/api" "ALLinSSL/backend/app/api"
"ALLinSSL/backend/app/api/monitor" "ALLinSSL/backend/app/api/monitor"
"ALLinSSL/backend/public"
"ALLinSSL/static" "ALLinSSL/static"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io/fs" "io/fs"
"net/http" "net/http"
"strings"
) )
func Register(r *gin.Engine) { func Register(r *gin.Engine) {
@ -119,6 +121,11 @@ func Register(r *gin.Engine) {
// 其他路由:返回 index.html // 其他路由:返回 index.html
r.NoRoute(func(c *gin.Context) { 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") data, err := static.BuildFS.ReadFile("build/index.html")
if err != nil { if err != nil {
c.String(http.StatusInternalServerError, "页面加载失败") c.String(http.StatusInternalServerError, "页面加载失败")