Skip to content

Commit 68df9af

Browse files
committed
修改1panel兼容v2、接口不存在时返回404
1 parent 8c89e1e commit 68df9af

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

backend/internal/cert/deploy/1panel.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/http"
1414
"net/url"
1515
"strconv"
16+
"strings"
1617
"time"
1718
)
1819

@@ -41,12 +42,28 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
4142
}
4243
timestamp := fmt.Sprintf("%d", time.Now().Unix())
4344
token := generateToken(timestamp, providerConfig["api_key"])
44-
45+
4546
// data, requestUrl, method := GetDeploy1PBody(cfg, Type)
4647
if requestUrl == "" || data == nil {
4748
return nil, fmt.Errorf("不支持的部署类型")
4849
}
49-
50+
if requestUrl[0] == '/' {
51+
requestUrl = requestUrl[1:]
52+
}
53+
version := providerConfig["version"]
54+
switch version {
55+
case "", "1", "v1":
56+
requestUrl = "api/v1/" + requestUrl
57+
case "2", "v2":
58+
(*data)["ssl"] = "Enable"
59+
if strings.Split(requestUrl, "/")[0] == "settings" {
60+
requestUrl = "core/" + requestUrl
61+
}
62+
requestUrl = "api/v2/" + requestUrl
63+
default:
64+
return nil, fmt.Errorf("不支持的1Panel版本: %s", version)
65+
}
66+
5067
// 编码为 JSON
5168
jsonData, err := json.Marshal(data)
5269
if err != nil {
@@ -62,12 +79,12 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
6279
// fmt.Println(err)
6380
return nil, err
6481
}
65-
82+
6683
req.Header.Set("Content-Type", "application/json")
6784
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")
6885
req.Header.Set("1Panel-Timestamp", timestamp)
6986
req.Header.Set("1Panel-Token", token)
70-
87+
7188
// 自定义 Transport,跳过 SSL 证书验证
7289
ignoreSsl := false
7390
if providerConfig["ignore_ssl"] == "1" {
@@ -76,7 +93,7 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
7693
tr := &http.Transport{
7794
TLSClientConfig: &tls.Config{InsecureSkipVerify: ignoreSsl},
7895
}
79-
96+
8097
client := &http.Client{Transport: tr}
8198
resp, err := client.Do(req)
8299
if err != nil {
@@ -85,7 +102,7 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
85102
}
86103
body, _ := io.ReadAll(resp.Body)
87104
defer resp.Body.Close()
88-
105+
89106
var res map[string]interface{}
90107
err = json.Unmarshal(body, &res)
91108
if err != nil {
@@ -103,7 +120,7 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
103120
return nil, fmt.Errorf("请求失败: %s", msg)
104121
}
105122
return res, nil
106-
123+
107124
}
108125

109126
func Deploy1panel(cfg map[string]any) error {
@@ -129,14 +146,14 @@ func Deploy1panel(cfg map[string]any) error {
129146
if !ok {
130147
return fmt.Errorf("证书错误:cert")
131148
}
132-
149+
133150
data := map[string]interface{}{
134151
"cert": certPem,
135152
"key": keyPem,
136153
"ssl": "enable",
137154
"sslType": "import-paste",
138155
}
139-
_, err := Request1panel(&data, "POST", providerID, "api/v1/settings/ssl/update")
156+
_, err := Request1panel(&data, "POST", providerID, "settings/ssl/update")
140157
if err != nil {
141158
return fmt.Errorf("证书部署失败: %v", err)
142159
}
@@ -171,7 +188,7 @@ func Deploy1panelSite(cfg map[string]any) error {
171188
return fmt.Errorf("证书错误:cert")
172189
}
173190
// 获取网站参数
174-
siteData, err := Request1panel(&map[string]any{}, "GET", providerID, fmt.Sprintf("api/v1/websites/%s/https", siteId))
191+
siteData, err := Request1panel(&map[string]any{}, "GET", providerID, fmt.Sprintf("websites/%s/https", siteId))
175192
if err != nil {
176193
return fmt.Errorf("获取网站参数失败: %v", err)
177194
}
@@ -180,12 +197,12 @@ func Deploy1panelSite(cfg map[string]any) error {
180197
if err != nil {
181198
return fmt.Errorf("获取网站参数失败: %v", err)
182199
}
183-
200+
184201
siteData, ok = siteData["data"].(map[string]any)
185202
if !ok {
186203
return fmt.Errorf("获取网站参数失败: data")
187204
}
188-
205+
189206
var SSLProtocol []any
190207
if siteData["SSLProtocol"] == nil {
191208
SSLProtocol = []any{"TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1"}
@@ -210,7 +227,7 @@ func Deploy1panelSite(cfg map[string]any) error {
210227
if httpConfig == "" {
211228
httpConfig = "HTTPToHTTPS"
212229
}
213-
230+
214231
data := map[string]any{
215232
"SSLProtocol": SSLProtocol,
216233
// "acmeAccountId": siteData["SSL"].(map[string]any)["acmeAccountId"].(float64),
@@ -219,20 +236,20 @@ func Deploy1panelSite(cfg map[string]any) error {
219236
"privateKey": keyPem,
220237
// "certificatePath": "",
221238
// "privateKeyPath": "",
222-
"enable": true,
239+
"enable": true,
223240
"hsts": hsts,
224241
"httpConfig": httpConfig,
225242
"importType": "paste",
226243
"type": "manual",
227244
"websiteId": websiteId,
228245
}
229-
_, err = Request1panel(&data, "POST", providerID, fmt.Sprintf("api/v1/websites/%s/https", siteId))
246+
_, err = Request1panel(&data, "POST", providerID, fmt.Sprintf("websites/%s/https", siteId))
230247
return err
231248
}
232249

233250
func OnePanelAPITest(providerID string) error {
234251
data := map[string]interface{}{}
235-
_, err := Request1panel(&data, "GET", providerID, "api/v1/settings/upgrade")
252+
_, err := Request1panel(&data, "GET", providerID, "settings/upgrade")
236253
if err != nil {
237254
return fmt.Errorf("测试请求失败: %v", err)
238255
}
@@ -248,7 +265,7 @@ func OnePanelSiteList(providerID string) ([]response.AccessSiteList, error) {
248265
"pageSize": 1000,
249266
"websiteGroupId": 0,
250267
}
251-
siteList, err := Request1panel(&data, "POST", providerID, "api/v1/websites/search")
268+
siteList, err := Request1panel(&data, "POST", providerID, "websites/search")
252269
if err != nil {
253270
return nil, fmt.Errorf("获取网站列表失败 %v", err)
254271
}
@@ -258,7 +275,7 @@ func OnePanelSiteList(providerID string) ([]response.AccessSiteList, error) {
258275
if !ok {
259276
return nil, fmt.Errorf("获取网站列表失败: data.items")
260277
}
261-
278+
262279
for _, site := range sites {
263280
siteMap, ok := site.(map[string]any)
264281
if !ok {
@@ -267,6 +284,6 @@ func OnePanelSiteList(providerID string) ([]response.AccessSiteList, error) {
267284
siteId := strconv.FormatFloat(siteMap["id"].(float64), 'f', -1, 64)
268285
result = append(result, response.AccessSiteList{Id: siteId, SiteName: siteMap["alias"].(string), Domain: []string{}})
269286
}
270-
287+
271288
return result, nil
272-
}
289+
}

backend/route/route.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package route
33
import (
44
"ALLinSSL/backend/app/api"
55
"ALLinSSL/backend/app/api/monitor"
6+
"ALLinSSL/backend/public"
67
"ALLinSSL/static"
78
"github.com/gin-gonic/gin"
89
"io/fs"
910
"net/http"
11+
"strings"
1012
)
1113

1214
func Register(r *gin.Engine) {
@@ -119,6 +121,11 @@ func Register(r *gin.Engine) {
119121

120122
// 其他路由:返回 index.html
121123
r.NoRoute(func(c *gin.Context) {
124+
// 如果是 API 请求,返回 JSON 的 404
125+
if strings.HasPrefix(c.Request.URL.Path, "/v1/") {
126+
public.FailMsg(c, "请求的资源不存在")
127+
return
128+
}
122129
data, err := static.BuildFS.ReadFile("build/index.html")
123130
if err != nil {
124131
c.String(http.StatusInternalServerError, "页面加载失败")

0 commit comments

Comments
 (0)