mirror of https://github.com/allinssl/allinssl
修改1panel兼容v2、接口不存在时返回404
parent
8c89e1ea2a
commit
68df9af02b
|
@ -13,6 +13,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -41,12 +42,28 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
|
||||||
}
|
}
|
||||||
timestamp := fmt.Sprintf("%d", time.Now().Unix())
|
timestamp := fmt.Sprintf("%d", time.Now().Unix())
|
||||||
token := generateToken(timestamp, providerConfig["api_key"])
|
token := generateToken(timestamp, providerConfig["api_key"])
|
||||||
|
|
||||||
// data, requestUrl, method := GetDeploy1PBody(cfg, Type)
|
// data, requestUrl, method := GetDeploy1PBody(cfg, Type)
|
||||||
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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -62,12 +79,12 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
|
||||||
// fmt.Println(err)
|
// fmt.Println(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
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("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-Timestamp", timestamp)
|
||||||
req.Header.Set("1Panel-Token", token)
|
req.Header.Set("1Panel-Token", token)
|
||||||
|
|
||||||
// 自定义 Transport,跳过 SSL 证书验证
|
// 自定义 Transport,跳过 SSL 证书验证
|
||||||
ignoreSsl := false
|
ignoreSsl := false
|
||||||
if providerConfig["ignore_ssl"] == "1" {
|
if providerConfig["ignore_ssl"] == "1" {
|
||||||
|
@ -76,7 +93,7 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
|
||||||
tr := &http.Transport{
|
tr := &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: ignoreSsl},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: ignoreSsl},
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{Transport: tr}
|
client := &http.Client{Transport: tr}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -85,7 +102,7 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
|
||||||
}
|
}
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
var res map[string]interface{}
|
var res map[string]interface{}
|
||||||
err = json.Unmarshal(body, &res)
|
err = json.Unmarshal(body, &res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -103,7 +120,7 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string)
|
||||||
return nil, fmt.Errorf("请求失败: %s", msg)
|
return nil, fmt.Errorf("请求失败: %s", msg)
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Deploy1panel(cfg map[string]any) error {
|
func Deploy1panel(cfg map[string]any) error {
|
||||||
|
@ -129,14 +146,14 @@ func Deploy1panel(cfg map[string]any) error {
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("证书错误:cert")
|
return fmt.Errorf("证书错误:cert")
|
||||||
}
|
}
|
||||||
|
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"cert": certPem,
|
"cert": certPem,
|
||||||
"key": keyPem,
|
"key": keyPem,
|
||||||
"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)
|
||||||
}
|
}
|
||||||
|
@ -180,12 +197,12 @@ func Deploy1panelSite(cfg map[string]any) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("获取网站参数失败: %v", err)
|
return fmt.Errorf("获取网站参数失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
siteData, ok = siteData["data"].(map[string]any)
|
siteData, ok = siteData["data"].(map[string]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("获取网站参数失败: data")
|
return fmt.Errorf("获取网站参数失败: data")
|
||||||
}
|
}
|
||||||
|
|
||||||
var SSLProtocol []any
|
var SSLProtocol []any
|
||||||
if siteData["SSLProtocol"] == nil {
|
if siteData["SSLProtocol"] == nil {
|
||||||
SSLProtocol = []any{"TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1"}
|
SSLProtocol = []any{"TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1"}
|
||||||
|
@ -210,7 +227,7 @@ func Deploy1panelSite(cfg map[string]any) error {
|
||||||
if httpConfig == "" {
|
if httpConfig == "" {
|
||||||
httpConfig = "HTTPToHTTPS"
|
httpConfig = "HTTPToHTTPS"
|
||||||
}
|
}
|
||||||
|
|
||||||
data := map[string]any{
|
data := map[string]any{
|
||||||
"SSLProtocol": SSLProtocol,
|
"SSLProtocol": SSLProtocol,
|
||||||
// "acmeAccountId": siteData["SSL"].(map[string]any)["acmeAccountId"].(float64),
|
// "acmeAccountId": siteData["SSL"].(map[string]any)["acmeAccountId"].(float64),
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -258,7 +275,7 @@ func OnePanelSiteList(providerID string) ([]response.AccessSiteList, error) {
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("获取网站列表失败: data.items")
|
return nil, fmt.Errorf("获取网站列表失败: data.items")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, site := range sites {
|
for _, site := range sites {
|
||||||
siteMap, ok := site.(map[string]any)
|
siteMap, ok := site.(map[string]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -267,6 +284,6 @@ func OnePanelSiteList(providerID string) ([]response.AccessSiteList, error) {
|
||||||
siteId := strconv.FormatFloat(siteMap["id"].(float64), 'f', -1, 64)
|
siteId := strconv.FormatFloat(siteMap["id"].(float64), 'f', -1, 64)
|
||||||
result = append(result, response.AccessSiteList{Id: siteId, SiteName: siteMap["alias"].(string), Domain: []string{}})
|
result = append(result, response.AccessSiteList{Id: siteId, SiteName: siteMap["alias"].(string), Domain: []string{}})
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -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, "页面加载失败")
|
||||||
|
|
Loading…
Reference in New Issue