From ad3da396d906c742f71e2c9fc7717476e26f00cf Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 1 Jan 2023 18:29:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=8E=E4=B8=BA=E4=BA=91=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=BB=88=E7=AB=AF=E8=8A=82=E7=82=B9=EF=BC=88?= =?UTF-8?q?endpoint=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/dnsclients/provider_huawei_dns.go | 64 +++++++++++++++---- .../dnsclients/provider_huawei_dns_test.go | 10 ++- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/internal/dnsclients/provider_huawei_dns.go b/internal/dnsclients/provider_huawei_dns.go index d493b3e9..73c0d014 100644 --- a/internal/dnsclients/provider_huawei_dns.go +++ b/internal/dnsclients/provider_huawei_dns.go @@ -17,13 +17,16 @@ import ( "io" "net/http" "net/url" + "regexp" "sort" "strconv" "strings" "time" ) -const HuaweiDNSEndpoint = "https://dns.cn-north-1.myhuaweicloud.com/" +// HuaweiDNSDefaultEndpoint 默认Endpoint +// 所有Endpoints:https://developer.huaweicloud.com/endpoint?DNS +const HuaweiDNSDefaultEndpoint = "https://dns.cn-north-4.myhuaweicloud.com/" var huaweiDNSHTTPClient = &http.Client{ Timeout: 10 * time.Second, @@ -43,6 +46,10 @@ type HuaweiDNSProvider struct { accessKeyId string accessKeySecret string + endpoint string + + endpointRegionReg *regexp.Regexp + endpointDomainReg *regexp.Regexp } // Auth 认证 @@ -55,6 +62,12 @@ func (this *HuaweiDNSProvider) Auth(params maps.Map) error { if len(this.accessKeySecret) == 0 { return errors.New("'accessKeySecret' should not be empty") } + this.endpoint = params.GetString("endpoint") + + // endpoint相关正则 + this.endpointRegionReg = regexp.MustCompile(`^[\w-]+$`) + this.endpointDomainReg = regexp.MustCompile(`^([\w-]+\.)+[\w-]+$`) + return nil } @@ -1469,13 +1482,38 @@ func (this *HuaweiDNSProvider) DefaultRoute() string { } func (this *HuaweiDNSProvider) doAPI(method string, apiPath string, args map[string]string, bodyMap maps.Map, respPtr interface{}) error { - apiURL := HuaweiDNSEndpoint + strings.TrimLeft(apiPath, "/") - u, err := url.Parse(HuaweiDNSEndpoint) + var endpoint = HuaweiDNSDefaultEndpoint + if len(this.endpoint) > 0 { + // 是否直接为区域 + if this.endpointRegionReg.MatchString(this.endpoint) { + switch this.endpoint { + case "All", "all": + endpoint = "https://dns.myhuaweicloud.com/" + default: + endpoint = "https://dns." + this.endpoint + ".myhuaweicloud.com/" + } + } else if this.endpointDomainReg.MatchString(this.endpoint) { // 是否直接为域名 + endpoint = "https://" + this.endpoint + "/" + } else { + // 是否为URL + _, err := url.Parse(this.endpoint) + if err != nil { + return errors.New("invalid endpoint '" + this.endpoint + "'") + } + endpoint = this.endpoint + if !strings.HasSuffix(endpoint, "/") { + endpoint += "/" + } + } + } + + var apiURL = endpoint + strings.TrimLeft(apiPath, "/") + u, err := url.Parse(endpoint) if err != nil { return err } - apiHost := u.Host - argStrings := []string{} + var apiHost = u.Host + var argStrings = []string{} if len(args) > 0 { apiURL += "?" for k, v := range args { @@ -1501,28 +1539,28 @@ func (this *HuaweiDNSProvider) doAPI(method string, apiPath string, args map[str return err } - contentType := "application/json" - host := apiHost - datetime := time.Now().UTC().Format("20060102T150405Z") + var contentType = "application/json" + var host = apiHost + var datetime = time.Now().UTC().Format("20060102T150405Z") if !strings.HasSuffix(apiPath, "/") { apiPath += "/" } - canonicalRequest := method + "\n" + apiPath + "\n" + strings.Join(argStrings, "&") + "\ncontent-type:" + contentType + "\nhost:" + host + "\nx-sdk-date:" + datetime + "\n" + "\ncontent-type;host;x-sdk-date" + var canonicalRequest = method + "\n" + apiPath + "\n" + strings.Join(argStrings, "&") + "\ncontent-type:" + contentType + "\nhost:" + host + "\nx-sdk-date:" + datetime + "\n" + "\ncontent-type;host;x-sdk-date" - h := sha256.New() + var h = sha256.New() _, err = h.Write(bodyData) if err != nil { return err } canonicalRequest += "\n" + fmt.Sprintf("%x", h.Sum(nil)) - h2 := sha256.New() + var h2 = sha256.New() _, err = h2.Write([]byte(canonicalRequest)) if err != nil { return err } - source := "SDK-HMAC-SHA256\n" + datetime + "\n" + fmt.Sprintf("%x", h2.Sum(nil)) - h3 := hmac.New(sha256.New, []byte(this.accessKeySecret)) + var source = "SDK-HMAC-SHA256\n" + datetime + "\n" + fmt.Sprintf("%x", h2.Sum(nil)) + var h3 = hmac.New(sha256.New, []byte(this.accessKeySecret)) h3.Write([]byte(source)) signString := fmt.Sprintf("%x", h3.Sum(nil)) req.Header.Set("Host", host) diff --git a/internal/dnsclients/provider_huawei_dns_test.go b/internal/dnsclients/provider_huawei_dns_test.go index a3844841..46867003 100644 --- a/internal/dnsclients/provider_huawei_dns_test.go +++ b/internal/dnsclients/provider_huawei_dns_test.go @@ -139,16 +139,20 @@ func testHuaweiDNSProvider() (ProviderInterface, error) { if err != nil { return nil, err } - one, err := db.FindOne("SELECT * FROM edgeDNSProviders WHERE type='huaweiDNS' ORDER BY id DESC") + one, err := db.FindOne("SELECT * FROM edgeDNSProviders WHERE type='huaweiDNS' AND state=1 ORDER BY id DESC") if err != nil { return nil, err } - apiParams := maps.Map{} + var apiParams = maps.Map{} + //apiParams["endpoint"] = "" + //apiParams["endpoint"] = "cn-north-1" + //apiParams["endpoint"] = "dns.cn-north-4.myhuaweicloud.com" + //apiParams["endpoint"] = "https://dns.cn-south-1.myhuaweicloud.com/" err = json.Unmarshal([]byte(one.GetString("apiParams")), &apiParams) if err != nil { return nil, err } - provider := &HuaweiDNSProvider{} + var provider = &HuaweiDNSProvider{} err = provider.Auth(apiParams) if err != nil { return nil, err