diff --git a/internal/pkg/core/applicant/acme-dns-01/lego-providers/gname/internal/lego.go b/internal/pkg/core/applicant/acme-dns-01/lego-providers/gname/internal/lego.go index 979a803b..03dc633f 100644 --- a/internal/pkg/core/applicant/acme-dns-01/lego-providers/gname/internal/lego.go +++ b/internal/pkg/core/applicant/acme-dns-01/lego-providers/gname/internal/lego.go @@ -173,8 +173,6 @@ func (d *DNSProvider) addOrUpdateDNSRecord(domain, subDomain, value string) erro _, err := d.client.ModifyDomainResolution(request) return err } - - return nil } func (d *DNSProvider) removeDNSRecord(domain, subDomain, value string) error { diff --git a/internal/pkg/vendors/qiniu-sdk/auth.go b/internal/pkg/vendors/qiniu-sdk/auth.go new file mode 100644 index 00000000..d27fbe03 --- /dev/null +++ b/internal/pkg/vendors/qiniu-sdk/auth.go @@ -0,0 +1,29 @@ +package qiniusdk + +import ( + "net/http" + + "github.com/qiniu/go-sdk/v7/auth" +) + +type transport struct { + http.RoundTripper + mac *auth.Credentials +} + +func newTransport(mac *auth.Credentials, tr http.RoundTripper) *transport { + if tr == nil { + tr = http.DefaultTransport + } + return &transport{tr, mac} +} + +func (t *transport) RoundTrip(req *http.Request) (resp *http.Response, err error) { + token, err := t.mac.SignRequestV2(req) + if err != nil { + return + } + + req.Header.Set("Authorization", "Qiniu "+token) + return t.RoundTripper.RoundTrip(req) +} diff --git a/internal/pkg/vendors/qiniu-sdk/client.go b/internal/pkg/vendors/qiniu-sdk/client.go index 1f036afd..4f342564 100644 --- a/internal/pkg/vendors/qiniu-sdk/client.go +++ b/internal/pkg/vendors/qiniu-sdk/client.go @@ -1,44 +1,36 @@ package qiniusdk import ( - "bytes" - "encoding/json" + "context" "fmt" - "io" "net/http" "strings" "github.com/qiniu/go-sdk/v7/auth" + "github.com/qiniu/go-sdk/v7/client" ) const qiniuHost = "https://api.qiniu.com" type Client struct { - mac *auth.Credentials + client *client.Client } func NewClient(mac *auth.Credentials) *Client { if mac == nil { mac = auth.Default() } - return &Client{mac: mac} + + client := client.DefaultClient + client.Transport = newTransport(mac, nil) + return &Client{client: &client} } func (c *Client) GetDomainInfo(domain string) (*GetDomainInfoResponse, error) { - respBytes, err := c.sendReq(http.MethodGet, fmt.Sprintf("domain/%s", domain), nil) - if err != nil { + resp := new(GetDomainInfoResponse) + if err := c.client.Call(context.Background(), resp, http.MethodGet, c.urlf("domain/%s", domain), nil); err != nil { return nil, err } - - resp := &GetDomainInfoResponse{} - err = json.Unmarshal(respBytes, resp) - if err != nil { - return nil, err - } - if resp.Code != nil && *resp.Code != 0 && *resp.Code != 200 { - return nil, fmt.Errorf("code: %d, error: %s", *resp.Code, *resp.Error) - } - return resp, nil } @@ -50,26 +42,10 @@ func (c *Client) ModifyDomainHttpsConf(domain, certId string, forceHttps, http2E Http2Enable: http2Enable, }, } - - reqBytes, err := json.Marshal(req) - if err != nil { + resp := new(ModifyDomainHttpsConfResponse) + if err := c.client.CallWithJson(context.Background(), resp, http.MethodPut, c.urlf("domain/%s/httpsconf", domain), nil, req); err != nil { return nil, err } - - respBytes, err := c.sendReq(http.MethodPut, fmt.Sprintf("domain/%s/httpsconf", domain), bytes.NewReader(reqBytes)) - if err != nil { - return nil, err - } - - resp := &ModifyDomainHttpsConfResponse{} - err = json.Unmarshal(respBytes, resp) - if err != nil { - return nil, err - } - if resp.Code != nil && *resp.Code != 0 && *resp.Code != 200 { - return nil, fmt.Errorf("code: %d, error: %s", *resp.Code, *resp.Error) - } - return resp, nil } @@ -81,26 +57,10 @@ func (c *Client) EnableDomainHttps(domain, certId string, forceHttps, http2Enabl Http2Enable: http2Enable, }, } - - reqBytes, err := json.Marshal(req) - if err != nil { + resp := new(EnableDomainHttpsResponse) + if err := c.client.CallWithJson(context.Background(), resp, http.MethodPut, c.urlf("domain/%s/sslize", domain), nil, req); err != nil { return nil, err } - - respBytes, err := c.sendReq(http.MethodPut, fmt.Sprintf("domain/%s/sslize", domain), bytes.NewReader(reqBytes)) - if err != nil { - return nil, err - } - - resp := &EnableDomainHttpsResponse{} - err = json.Unmarshal(respBytes, resp) - if err != nil { - return nil, err - } - if resp.Code != nil && *resp.Code != 0 && *resp.Code != 200 { - return nil, fmt.Errorf("code: %d, error: %s", *resp.Code, *resp.Error) - } - return resp, nil } @@ -111,53 +71,15 @@ func (c *Client) UploadSslCert(name, commonName, certificate, privateKey string) Certificate: certificate, PrivateKey: privateKey, } - - reqBytes, err := json.Marshal(req) - if err != nil { + resp := new(UploadSslCertResponse) + if err := c.client.CallWithJson(context.Background(), resp, http.MethodPost, c.urlf("sslcert"), nil, req); err != nil { return nil, err } - - respBytes, err := c.sendReq(http.MethodPost, "sslcert", bytes.NewReader(reqBytes)) - if err != nil { - return nil, err - } - - resp := &UploadSslCertResponse{} - err = json.Unmarshal(respBytes, resp) - if err != nil { - return nil, err - } - if resp.Code != nil && *resp.Code != 0 && *resp.Code != 200 { - return nil, fmt.Errorf("qiniu api error, code: %d, error: %s", *resp.Code, *resp.Error) - } - return resp, nil } -func (c *Client) sendReq(method string, path string, body io.Reader) ([]byte, error) { +func (c *Client) urlf(pathf string, pathargs ...any) string { + path := fmt.Sprintf(pathf, pathargs...) path = strings.TrimPrefix(path, "/") - - req, err := http.NewRequest(method, fmt.Sprintf("%s/%s", qiniuHost, path), body) - if err != nil { - return nil, err - } - req.Header.Add("Content-Type", "application/json") - - if err := c.mac.AddToken(auth.TokenQBox, req); err != nil { - return nil, err - } - - client := http.Client{} - resp, err := client.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - r, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - return r, nil + return qiniuHost + "/" + path } diff --git a/internal/pkg/vendors/qiniu-sdk/models.go b/internal/pkg/vendors/qiniu-sdk/models.go index 3c68ee6b..dceca028 100644 --- a/internal/pkg/vendors/qiniu-sdk/models.go +++ b/internal/pkg/vendors/qiniu-sdk/models.go @@ -13,7 +13,7 @@ type UploadSslCertRequest struct { } type UploadSslCertResponse struct { - *BaseResponse + BaseResponse CertID string `json:"certID"` }