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"
 | 
			
		||||
	"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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user