mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	华为云可以设置终端节点(endpoint)
This commit is contained in:
		@@ -17,13 +17,16 @@ import (
 | 
				
			|||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"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{
 | 
					var huaweiDNSHTTPClient = &http.Client{
 | 
				
			||||||
	Timeout: 10 * time.Second,
 | 
						Timeout: 10 * time.Second,
 | 
				
			||||||
@@ -43,6 +46,10 @@ type HuaweiDNSProvider struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	accessKeyId     string
 | 
						accessKeyId     string
 | 
				
			||||||
	accessKeySecret string
 | 
						accessKeySecret string
 | 
				
			||||||
 | 
						endpoint        string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						endpointRegionReg *regexp.Regexp
 | 
				
			||||||
 | 
						endpointDomainReg *regexp.Regexp
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Auth 认证
 | 
					// Auth 认证
 | 
				
			||||||
@@ -55,6 +62,12 @@ func (this *HuaweiDNSProvider) Auth(params maps.Map) error {
 | 
				
			|||||||
	if len(this.accessKeySecret) == 0 {
 | 
						if len(this.accessKeySecret) == 0 {
 | 
				
			||||||
		return errors.New("'accessKeySecret' should not be empty")
 | 
							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
 | 
						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 {
 | 
					func (this *HuaweiDNSProvider) doAPI(method string, apiPath string, args map[string]string, bodyMap maps.Map, respPtr interface{}) error {
 | 
				
			||||||
	apiURL := HuaweiDNSEndpoint + strings.TrimLeft(apiPath, "/")
 | 
						var endpoint = HuaweiDNSDefaultEndpoint
 | 
				
			||||||
	u, err := url.Parse(HuaweiDNSEndpoint)
 | 
						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 {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	apiHost := u.Host
 | 
						var apiHost = u.Host
 | 
				
			||||||
	argStrings := []string{}
 | 
						var argStrings = []string{}
 | 
				
			||||||
	if len(args) > 0 {
 | 
						if len(args) > 0 {
 | 
				
			||||||
		apiURL += "?"
 | 
							apiURL += "?"
 | 
				
			||||||
		for k, v := range args {
 | 
							for k, v := range args {
 | 
				
			||||||
@@ -1501,28 +1539,28 @@ func (this *HuaweiDNSProvider) doAPI(method string, apiPath string, args map[str
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	contentType := "application/json"
 | 
						var contentType = "application/json"
 | 
				
			||||||
	host := apiHost
 | 
						var host = apiHost
 | 
				
			||||||
	datetime := time.Now().UTC().Format("20060102T150405Z")
 | 
						var datetime = time.Now().UTC().Format("20060102T150405Z")
 | 
				
			||||||
	if !strings.HasSuffix(apiPath, "/") {
 | 
						if !strings.HasSuffix(apiPath, "/") {
 | 
				
			||||||
		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)
 | 
						_, err = h.Write(bodyData)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	canonicalRequest += "\n" + fmt.Sprintf("%x", h.Sum(nil))
 | 
						canonicalRequest += "\n" + fmt.Sprintf("%x", h.Sum(nil))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	h2 := sha256.New()
 | 
						var h2 = sha256.New()
 | 
				
			||||||
	_, err = h2.Write([]byte(canonicalRequest))
 | 
						_, err = h2.Write([]byte(canonicalRequest))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	source := "SDK-HMAC-SHA256\n" + datetime + "\n" + fmt.Sprintf("%x", h2.Sum(nil))
 | 
						var source = "SDK-HMAC-SHA256\n" + datetime + "\n" + fmt.Sprintf("%x", h2.Sum(nil))
 | 
				
			||||||
	h3 := hmac.New(sha256.New, []byte(this.accessKeySecret))
 | 
						var h3 = hmac.New(sha256.New, []byte(this.accessKeySecret))
 | 
				
			||||||
	h3.Write([]byte(source))
 | 
						h3.Write([]byte(source))
 | 
				
			||||||
	signString := fmt.Sprintf("%x", h3.Sum(nil))
 | 
						signString := fmt.Sprintf("%x", h3.Sum(nil))
 | 
				
			||||||
	req.Header.Set("Host", host)
 | 
						req.Header.Set("Host", host)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -139,16 +139,20 @@ func testHuaweiDNSProvider() (ProviderInterface, error) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							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 {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							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)
 | 
						err = json.Unmarshal([]byte(one.GetString("apiParams")), &apiParams)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	provider := &HuaweiDNSProvider{}
 | 
						var provider = &HuaweiDNSProvider{}
 | 
				
			||||||
	err = provider.Auth(apiParams)
 | 
						err = provider.Auth(apiParams)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user