@@ -5,11 +5,10 @@ import (
55 "context"
66 "encoding/json"
77 "fmt"
8- "net/http"
9- "strconv"
10-
118 "github.com/qiniu/go-sdk/v7/auth"
129 "github.com/qiniu/go-sdk/v7/client"
10+ "net/http"
11+ "strconv"
1312)
1413
1514type commonResponse struct {
@@ -42,7 +41,7 @@ func requestQiniu(cfg map[string]any, path string, m map[string]any, method stri
4241 if err != nil {
4342 return err
4443 }
45-
44+
4645 uri := fmt .Sprintf ("https://api.qiniu.com/%v" , path )
4746 credentials := auth .New (providerConfig ["access_key" ], providerConfig ["access_secret" ])
4847 header := http.Header {}
@@ -51,7 +50,6 @@ func requestQiniu(cfg map[string]any, path string, m map[string]any, method stri
5150 return err
5251}
5352
54-
5553func DeployQiniuCdn (cfg map [string ]any ) error {
5654 _ , ok := cfg ["certificate" ].(map [string ]any )
5755 if ! ok {
@@ -75,26 +73,103 @@ func DeployQiniuCdn(cfg map[string]any) error {
7573 return err
7674}
7775
78- func DeployQiniuOss (cfg map [string ]any ) error {
76+ func updateQiniuDomainCert (cfg map [string ]any ) error {
7977 _ , ok := cfg ["certificate" ].(map [string ]any )
8078 if ! ok {
8179 return fmt .Errorf ("证书不存在" )
8280 }
81+
8382 domain , ok := cfg ["domain" ].(string )
8483 if ! ok {
8584 return fmt .Errorf ("参数错误:domain" )
8685 }
87-
86+
87+ forceHttps , ok := cfg ["force_https" ].(bool )
88+ if ! ok {
89+ forceHttps = true
90+ }
91+
92+ http2Enable , ok := cfg ["http2_enable" ].(bool )
93+ if ! ok {
94+ http2Enable = true
95+ }
96+
8897 certId , err := uploadQiniuCert (cfg )
8998 if err != nil {
9099 return err
91100 }
92101 m := map [string ]any {
93- "certid" : certId ,
94- "domain" : domain ,
102+ "certid" : certId ,
103+ "domain" : domain ,
104+ "forceHttps" : forceHttps ,
105+ "http2Enable" : http2Enable ,
95106 }
107+
96108 var response commonResponse
97- err = requestQiniu (cfg , "cert/bind" , m , "POST" , & response )
109+ err = requestQiniu (cfg , fmt .Sprintf ("domain/%s/httpsconf" , domain ), m , "PUT" , & response )
110+ return err
111+ }
112+
113+ func DeployQiniuOss (cfg map [string ]any ) error {
114+ _ , ok := cfg ["certificate" ].(map [string ]any )
115+ if ! ok {
116+ return fmt .Errorf ("证书不存在" )
117+ }
118+ domain , ok := cfg ["domain" ].(string )
119+ if ! ok {
120+ return fmt .Errorf ("参数错误:domain" )
121+ }
122+
123+ // 判断域名是否已开启HTTPS
124+ // {
125+ // "certId": <CertID>,
126+ // "forceHttps": <ForceHttps>,
127+ // "http2Enable": <Http2Enable>
128+ // }
129+ var httpsConfig struct {
130+ Https struct {
131+ CertID string `json:"certId"`
132+ ForceHttps bool `json:"forceHttps"`
133+ Http2Enable bool `json:"http2Enable"`
134+ } `json:"https"`
135+ }
136+ err := requestQiniu (cfg , fmt .Sprintf ("domain/%s" , domain ), nil , "GET" , & httpsConfig )
137+ if err != nil {
138+ return fmt .Errorf ("获取域名HTTPS配置失败: %v" , err )
139+ }
140+
141+ certId , err := uploadQiniuCert (cfg )
142+ if err != nil {
143+ return err
144+ }
145+
146+ if httpsConfig .Https .CertID != "" {
147+ // 如果已开启HTTPS,则调用updateQiniuDomainCert更新证书
148+ cfg ["cert_id" ] = certId
149+ cfg ["force_https" ] = httpsConfig .Https .ForceHttps
150+ cfg ["http2_enable" ] = httpsConfig .Https .Http2Enable
151+ err = updateQiniuDomainCert (cfg )
152+ return err
153+ } else {
154+ // 如果未开启HTTPS,则使用POST请求绑定证书
155+ m := map [string ]any {
156+ "certid" : certId ,
157+ "domain" : domain ,
158+ }
159+ var response commonResponse
160+ err = requestQiniu (cfg , "cert/bind" , m , "POST" , & response )
161+ return err
162+ }
163+ }
164+
165+ func delQiniuCert (cfg map [string ]any ) error {
166+ certId , ok := cfg ["old_cert_id" ].(string )
167+ if ! ok {
168+ return fmt .Errorf ("参数错误:cert_id" )
169+ }
170+ m := map [string ]any {}
171+ var response commonResponse
172+ err := requestQiniu (cfg , fmt .Sprintf ("sslcert/%v" , certId ), m , "DELETE" , & response )
98173 return err
99174}
100175
@@ -128,4 +203,4 @@ func QiniuAPITest(providerID string) error {
128203 return fmt .Errorf ("测试请求失败: %v" , err )
129204 }
130205 return nil
131- }
206+ }
0 commit comments