From 77d82ad8a0086dcc324bf47e86b746507397fe04 Mon Sep 17 00:00:00 2001 From: wantoper <305986045@qq.com> Date: Tue, 20 May 2025 17:27:42 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0API=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=8E=A5=E5=8F=A3=20=E4=B8=BA=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BE=9B=E5=BA=94=E5=95=86API=E5=A2=9E=E5=8A=A0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/access.go | 45 ++++++++++++++ backend/internal/cert/deploy/1p_test.go | 9 +++ backend/internal/cert/deploy/1panel.go | 13 ++++- backend/internal/cert/deploy/ali_test.go | 9 +++ backend/internal/cert/deploy/aliyun.go | 44 +++++++++++++- backend/internal/cert/deploy/bt_test.go | 13 ++++- backend/internal/cert/deploy/btpanel.go | 9 +++ backend/internal/cert/deploy/btwaf.go | 9 +++ backend/internal/cert/deploy/btwaf_test.go | 8 +++ backend/internal/cert/deploy/safelinewaf.go | 8 +++ .../internal/cert/deploy/safelinewaf_test.go | 9 +++ backend/internal/cert/deploy/ssh.go | 58 +++++++++++++++++++ backend/internal/cert/deploy/ssh_test.go | 9 +++ backend/internal/cert/deploy/tencentcloud.go | 30 ++++++++++ .../internal/cert/deploy/tencentcloud_test.go | 12 ++++ backend/route/route.go | 1 + go.mod | 1 + go.sum | 2 + 18 files changed, 284 insertions(+), 5 deletions(-) create mode 100644 backend/internal/cert/deploy/tencentcloud_test.go diff --git a/backend/app/api/access.go b/backend/app/api/access.go index fecad4a..c90eafc 100644 --- a/backend/app/api/access.go +++ b/backend/app/api/access.go @@ -2,6 +2,7 @@ package api import ( "ALLinSSL/backend/internal/access" + "ALLinSSL/backend/internal/cert/deploy" "ALLinSSL/backend/public" "github.com/gin-gonic/gin" "strings" @@ -133,3 +134,47 @@ func DelAccess(c *gin.Context) { public.SuccessMsg(c, "删除成功") return } + +func TestAccess(c *gin.Context) { + var form struct { + ID string `form:"id"` + Type string `form:"type"` + } + err := c.Bind(&form) + if err != nil { + public.FailMsg(c, err.Error()) + return + } + if form.Type == "" { + public.FailMsg(c, "类型不能为空") + return + } + + var result error + switch form.Type { + case "btwaf": + result = deploy.BtWafAPITest(form.ID) + case "btpanel": + result = deploy.BtPanelAPITest(form.ID) + case "ssh": + result = deploy.SSHAPITest(form.ID) + case "safeline": + result = deploy.SafeLineAPITest(form.ID) + case "1panel": + result = deploy.OnePanelAPITest(form.ID) + case "tencentcloud": + result = deploy.TencentCloudAPITest(form.ID) + case "aliyun": + result = deploy.AliyunCdnAPITest(form.ID) + default: + public.FailMsg(c, "不支持测试的提供商") + } + + if result != nil { + public.FailMsg(c, result.Error()) + return + } + + public.SuccessMsg(c, "请求测试成功!") + return +} diff --git a/backend/internal/cert/deploy/1p_test.go b/backend/internal/cert/deploy/1p_test.go index 93e4269..b14c34b 100644 --- a/backend/internal/cert/deploy/1p_test.go +++ b/backend/internal/cert/deploy/1p_test.go @@ -29,3 +29,12 @@ func TestP(t *testing.T) { err := Deploy1panel(cfg) println(err) } + +func TestOnePanelAPITest(t *testing.T) { + result := OnePanelAPITest("8") + if result != nil { + t.Fatalf("SSHAPITest failed: %v", result) + } else { + t.Log("SSHAPITest success") + } +} diff --git a/backend/internal/cert/deploy/1panel.go b/backend/internal/cert/deploy/1panel.go index 19f9b43..8b15342 100644 --- a/backend/internal/cert/deploy/1panel.go +++ b/backend/internal/cert/deploy/1panel.go @@ -97,9 +97,9 @@ func Request1panel(data *map[string]any, method, providerID, requestUrl string) if code != 200 { msg, ok := res["message"].(string) if !ok { - return nil, fmt.Errorf("证书部署失败") + return nil, fmt.Errorf("请求失败") } - return nil, fmt.Errorf("证书部署失败: %s", msg) + return nil, fmt.Errorf("请求失败: %s", msg) } return res, nil @@ -223,3 +223,12 @@ func Deploy1panelSite(cfg map[string]any) error { _, err = Request1panel(&data, "POST", providerID, fmt.Sprintf("api/v1/websites/%s/https", siteId)) return err } + +func OnePanelAPITest(providerID string) error { + data := map[string]interface{}{} + _, err := Request1panel(&data, "GET", providerID, "api/v1/settings/upgrade") + if err != nil { + return fmt.Errorf("测试请求失败: %v", err) + } + return nil +} \ No newline at end of file diff --git a/backend/internal/cert/deploy/ali_test.go b/backend/internal/cert/deploy/ali_test.go index 94b59c5..9d0e079 100644 --- a/backend/internal/cert/deploy/ali_test.go +++ b/backend/internal/cert/deploy/ali_test.go @@ -39,3 +39,12 @@ func TestALiOss(t *testing.T) { t.Logf("DeployAliCdn succeeded") } } + +func TestAliyunCdnAPITest(t *testing.T) { + result := AliyunCdnAPITest("10") + if result != nil { + t.Fatalf("SSHAPITest failed: %v", result) + } else { + t.Log("SSHAPITest success") + } +} \ No newline at end of file diff --git a/backend/internal/cert/deploy/aliyun.go b/backend/internal/cert/deploy/aliyun.go index 5a085c2..fe8cd79 100644 --- a/backend/internal/cert/deploy/aliyun.go +++ b/backend/internal/cert/deploy/aliyun.go @@ -6,11 +6,11 @@ import ( "fmt" aliyuncdn "github.com/alibabacloud-go/cdn-20180510/v6/client" aliyunopenapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" + aliyunmarket "github.com/alibabacloud-go/market-20151101/v4/client" "github.com/alibabacloud-go/tea/tea" + "github.com/aliyun/aliyun-oss-go-sdk/oss" "strconv" "strings" - - "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func ClientAliCdn(accessKey, accessSecret string) (_result *aliyuncdn.Client, err error) { @@ -183,3 +183,43 @@ func DeployOss(cfg map[string]any) error { err = client.PutBucketCnameWithCertificate(bucket, putBucketCnameWithCertificateRequest) return err } + +func ClientMaker(accessKeyId, accessKeySecret string) (*aliyunmarket.Client, error) { + config := &aliyunopenapi.Config{ + AccessKeyId: tea.String(accessKeyId), + AccessKeySecret: tea.String(accessKeySecret), + Endpoint: tea.String("market.aliyuncs.com"), + } + + client, _ := aliyunmarket.NewClient(config) + return client, nil +} + +func AliyunCdnAPITest(providerID string) error { + providerData, err := access.GetAccess(providerID) + if err != nil { + return err + } + providerConfigStr, ok := providerData["config"].(string) + if !ok { + return fmt.Errorf("api配置错误") + } + // 解析 JSON 配置 + var providerConfig map[string]string + err = json.Unmarshal([]byte(providerConfigStr), &providerConfig) + if err != nil { + return err + } + + client, err := ClientMaker(providerConfig["access_key_id"], providerConfig["access_key_secret"]) + describeApiMeteringRequest := &aliyunmarket.DescribeApiMeteringRequest{ + PageNum: tea.Int32(1), + } + + _, err = client.DescribeApiMetering(describeApiMeteringRequest) + + if err != nil { + return fmt.Errorf("测试请求失败: %v", err) + } + return nil +} \ No newline at end of file diff --git a/backend/internal/cert/deploy/bt_test.go b/backend/internal/cert/deploy/bt_test.go index a1ac736..036a5d5 100644 --- a/backend/internal/cert/deploy/bt_test.go +++ b/backend/internal/cert/deploy/bt_test.go @@ -1,6 +1,8 @@ package deploy -import "testing" +import ( + "testing" +) func TestBTSite(t *testing.T) { cfg := map[string]any{ @@ -43,3 +45,12 @@ func TestBTDockerSite(t *testing.T) { err := DeployBtDockerSite(cfg) println(err) } + +func TestBtPanelAPITest(t *testing.T) { + result := BtPanelAPITest("4") + if result != nil { + t.Fatalf("SSHAPITest failed: %v", result) + } else { + t.Log("SSHAPITest success") + } +} diff --git a/backend/internal/cert/deploy/btpanel.go b/backend/internal/cert/deploy/btpanel.go index a734f07..f5273f3 100644 --- a/backend/internal/cert/deploy/btpanel.go +++ b/backend/internal/cert/deploy/btpanel.go @@ -197,3 +197,12 @@ func DeployBtDockerSite(cfg map[string]any) error { } return nil } + +func BtPanelAPITest(providerID string) error { + data := url.Values{} + _, err := RequestBt(&data, "POST", providerID, "mod/push/task/get_task_list") + if err != nil { + return fmt.Errorf("测试请求失败: %v", err) + } + return nil +} \ No newline at end of file diff --git a/backend/internal/cert/deploy/btwaf.go b/backend/internal/cert/deploy/btwaf.go index ae498a2..99145e7 100644 --- a/backend/internal/cert/deploy/btwaf.go +++ b/backend/internal/cert/deploy/btwaf.go @@ -199,3 +199,12 @@ func DeployBtWafSite(cfg map[string]any) error { } return nil } + +func BtWafAPITest(providerID string) error { + data := map[string]any{} + _, err := RequestBtWaf(&data, "POST", providerID, "api/overview/infos") + if err != nil { + return fmt.Errorf("测试请求失败: %v", err) + } + return nil +} \ No newline at end of file diff --git a/backend/internal/cert/deploy/btwaf_test.go b/backend/internal/cert/deploy/btwaf_test.go index 463f304..25b4c3b 100644 --- a/backend/internal/cert/deploy/btwaf_test.go +++ b/backend/internal/cert/deploy/btwaf_test.go @@ -40,3 +40,11 @@ func TestGetBTWAFSiteList(t *testing.T) { } fmt.Println(err) } + +func TestBtWafAPITest(t *testing.T) { + result := BtWafAPITest("1") + if result != nil { + fmt.Println(result) + return + } +} \ No newline at end of file diff --git a/backend/internal/cert/deploy/safelinewaf.go b/backend/internal/cert/deploy/safelinewaf.go index 2eb518a..c91e2a2 100644 --- a/backend/internal/cert/deploy/safelinewaf.go +++ b/backend/internal/cert/deploy/safelinewaf.go @@ -204,3 +204,11 @@ func DeploySafeLineWafSite(cfg map[string]any, logger *public.Logger) error { return nil } + +func SafeLineAPITest(providerID string) error { + _, err := RequestSafeLineWaf(&map[string]any{}, "GET", providerID, "api/open/site/group") + if err != nil { + return fmt.Errorf("测试请求失败: %v", err) + } + return nil +} \ No newline at end of file diff --git a/backend/internal/cert/deploy/safelinewaf_test.go b/backend/internal/cert/deploy/safelinewaf_test.go index 9a5ca3f..2678a26 100644 --- a/backend/internal/cert/deploy/safelinewaf_test.go +++ b/backend/internal/cert/deploy/safelinewaf_test.go @@ -45,3 +45,12 @@ func TestGetSafeLineWAFSiteList(t *testing.T) { siteId := matchSafeLineSiteByColumn(res, "comment", "测得3") fmt.Println(siteId) } + +func TestSafeLineAPITest(t *testing.T) { + result := SafeLineAPITest("5") + if result != nil { + t.Fatalf("SafeLineAPITest failed: %v", result) + } else { + t.Log("SafeLineAPITest success") + } +} \ No newline at end of file diff --git a/backend/internal/cert/deploy/ssh.go b/backend/internal/cert/deploy/ssh.go index 9a86090..0ca74d5 100644 --- a/backend/internal/cert/deploy/ssh.go +++ b/backend/internal/cert/deploy/ssh.go @@ -235,3 +235,61 @@ func DeployLocalhost(cfg map[string]any) error { } return nil } + +func SSHAPITest(providerID string) error { + providerData, err := access.GetAccess(providerID) + if err != nil { + return err + } + + providerConfigStr, ok := providerData["config"].(string) + if !ok { + return fmt.Errorf("api配置错误") + } + + // 解析 JSON 配置 + var providerConfig SSHConfig + err = json.Unmarshal([]byte(providerConfigStr), &providerConfig) + if err != nil { + return err + } + + var port string + switch v := providerConfig.Port.(type) { + case float64: + port = strconv.Itoa(int(v)) + case string: + port = v + case int: + port = strconv.Itoa(v) + default: + port = "22" + } + addr := fmt.Sprintf("%s:%s", providerConfig.Host, port) + + authMethods, err := buildAuthMethods(providerConfig.Password, providerConfig.PrivateKey) + if err != nil { + return err + } + + sshConfig := &ssh.ClientConfig{ + User: providerConfig.User, + Auth: authMethods, + HostKeyCallback: ssh.InsecureIgnoreHostKey(), + } + + client, err := ssh.Dial("tcp", addr, sshConfig) + if err != nil { + return fmt.Errorf("SSH连接失败: %v", err) + } + defer client.Close() + + // 尝试创建会话来验证连接 + session, err := client.NewSession() + if err != nil { + return fmt.Errorf("SSH会话创建失败: %v", err) + } + defer session.Close() + + return nil +} diff --git a/backend/internal/cert/deploy/ssh_test.go b/backend/internal/cert/deploy/ssh_test.go index ecdde4f..5db0456 100644 --- a/backend/internal/cert/deploy/ssh_test.go +++ b/backend/internal/cert/deploy/ssh_test.go @@ -20,3 +20,12 @@ func TestSSH(t *testing.T) { } // println(err.Error()) } + +func TestSSHAPITest(t *testing.T) { + result := SSHAPITest("7") + if result != nil { + t.Fatalf("SSHAPITest failed: %v", result) + } else { + t.Log("SSHAPITest success") + } +} \ No newline at end of file diff --git a/backend/internal/cert/deploy/tencentcloud.go b/backend/internal/cert/deploy/tencentcloud.go index c637be7..696b4f5 100644 --- a/backend/internal/cert/deploy/tencentcloud.go +++ b/backend/internal/cert/deploy/tencentcloud.go @@ -130,3 +130,33 @@ func DeployToTX(cfg map[string]any) error { fmt.Println(response.Response.DeployRecordId) return nil } + +func TencentCloudAPITest(providerID string) error { + providerData, err := access.GetAccess(providerID) + if err != nil { + return err + } + + providerConfigStr, ok := providerData["config"].(string) + if !ok { + return fmt.Errorf("api配置错误") + } + + // 解析 JSON 配置 + var providerConfig map[string]string + err = json.Unmarshal([]byte(providerConfigStr), &providerConfig) + if err != nil { + return err + } + + // 创建客户端 + client := ClientTencentcloud(providerConfig["secret_id"], providerConfig["secret_key"], "") + + request := ssl.NewDescribeCertificatesRequest() + _, err = client.DescribeCertificates(request) + if err != nil { + return fmt.Errorf("测试请求失败: %v", err) + } + + return nil +} diff --git a/backend/internal/cert/deploy/tencentcloud_test.go b/backend/internal/cert/deploy/tencentcloud_test.go new file mode 100644 index 0000000..d9554d0 --- /dev/null +++ b/backend/internal/cert/deploy/tencentcloud_test.go @@ -0,0 +1,12 @@ +package deploy + +import "testing" + +func TestTencentCloudAPITest(t *testing.T) { + result := TencentCloudAPITest("9") + if result != nil { + t.Fatalf("SSHAPITest failed: %v", result) + } else { + t.Log("SSHAPITest success") + } +} diff --git a/backend/route/route.go b/backend/route/route.go index 503d284..2a7f9b5 100644 --- a/backend/route/route.go +++ b/backend/route/route.go @@ -43,6 +43,7 @@ func Register(r *gin.Engine) { access.POST("/del_access", api.DelAccess) access.POST("/upd_access", api.UpdateAccess) access.POST("/get_all", api.GetAllAccess) + access.POST("/test_access", api.TestAccess) } cert := v1.Group("/cert") { diff --git a/go.mod b/go.mod index 015f0c8..7a23fad 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23.2 require ( github.com/alibabacloud-go/cdn-20180510/v6 v6.0.0 github.com/alibabacloud-go/darabonba-openapi/v2 v2.1.7 + github.com/alibabacloud-go/market-20151101/v4 v4.1.0 github.com/alibabacloud-go/tea v1.3.9 github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible github.com/gin-contrib/gzip v1.2.3 diff --git a/go.sum b/go.sum index 8e9040a..b22e74a 100644 --- a/go.sum +++ b/go.sum @@ -81,6 +81,8 @@ github.com/alibabacloud-go/debug v1.0.1 h1:MsW9SmUtbb1Fnt3ieC6NNZi6aEwrXfDksD4QA github.com/alibabacloud-go/debug v1.0.1/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc= github.com/alibabacloud-go/endpoint-util v1.1.0 h1:r/4D3VSw888XGaeNpP994zDUaxdgTSHBbVfZlzf6b5Q= github.com/alibabacloud-go/endpoint-util v1.1.0/go.mod h1:O5FuCALmCKs2Ff7JFJMudHs0I5EBgecXXxZRyswlEjE= +github.com/alibabacloud-go/market-20151101/v4 v4.1.0 h1:o5e9gCxVOKOGeslAFRtaRHsQAveH1i3aGFDNfyCUsVU= +github.com/alibabacloud-go/market-20151101/v4 v4.1.0/go.mod h1:PCt2sE7Y0SknDc0oMxjdTmNCB1qGY4ZyREyiXjHvmY4= github.com/alibabacloud-go/openapi-util v0.1.0/go.mod h1:sQuElr4ywwFRlCCberQwKRFhRzIyG4QTP/P4y1CJ6Ws= github.com/alibabacloud-go/openapi-util v0.1.1 h1:ujGErJjG8ncRW6XtBBMphzHTvCxn4DjrVw4m04HsS28= github.com/alibabacloud-go/openapi-util v0.1.1/go.mod h1:/UehBSE2cf1gYT43GV4E+RxTdLRzURImCYY0aRmlXpw=