diff --git a/client.go b/client.go index 30a3aa5..8fc353d 100644 --- a/client.go +++ b/client.go @@ -50,6 +50,7 @@ func (p *Provider) listRecords(ctx context.Context, zone string) ([]libdns.Recor Name: txRecord.Name, Value: txRecord.Value, TTL: time.Duration(txRecord.TTL) * time.Second, + MX: txRecord.MX, } libdnsRecord, err := rr.libdnsRecord() if err != nil { @@ -177,7 +178,12 @@ func (p *Provider) findRecord(ctx context.Context, zone string, record libdns.Re } func (p *Provider) sendRequest(ctx context.Context, action string, data string) ([]byte, error) { - req, err := http.NewRequestWithContext(ctx, "POST", endpoint, strings.NewReader(data)) + endpointUrl := endpoint + if p.Region != "" { + endpointUrl = "https://dnspod." + p.Region + ".tencentcloudapi.com" + } + + req, err := http.NewRequestWithContext(ctx, "POST", endpointUrl, strings.NewReader(data)) if err != nil { return nil, err } @@ -185,7 +191,7 @@ func (p *Provider) sendRequest(ctx context.Context, action string, data string) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-TC-Version", "2021-03-23") - SignRequest(p.SecretId, p.SecretKey, req, action, data) + SignRequest(p.SecretId, p.SecretKey, p.SessionToken, req, action, data) resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err diff --git a/signer.go b/signer.go index e4fd6f2..9f41cad 100644 --- a/signer.go +++ b/signer.go @@ -11,7 +11,7 @@ import ( ) // SignRequest https://github.com/jeessy2/ddns-go/blob/master/util/tencent_cloud_signer.go -func SignRequest(secretId string, secretKey string, r *http.Request, action string, payload string) { +func SignRequest(secretId string, secretKey string, sessionToken string, r *http.Request, action string, payload string) { algorithm := "TC3-HMAC-SHA256" service := "dnspod" host := writeString(service, ".tencentcloudapi.com") @@ -43,6 +43,11 @@ func SignRequest(secretId string, secretKey string, r *http.Request, action stri r.Header.Set("Host", host) r.Header.Set("X-TC-Action", action) r.Header.Set("X-TC-Timestamp", timestampStr) + + // Add session token if provided + if sessionToken != "" { + r.Header.Set("X-TC-Token", sessionToken) + } } func sha256hex(s string) string { diff --git a/types.go b/types.go index 8f0d6cc..1bdb419 100644 --- a/types.go +++ b/types.go @@ -2,6 +2,7 @@ package tencentcloud import ( "errors" + "strconv" "time" "github.com/libdns/libdns" @@ -11,8 +12,10 @@ var ErrRecordNotFound = errors.New("record not found") var ErrNotValid = errors.New("returned value is not valid") type Provider struct { - SecretId string - SecretKey string + SecretId string + SecretKey string + SessionToken string + Region string } type CreateModifyRecordRequest struct { @@ -54,6 +57,7 @@ type RecordInfo struct { Name string `json:"Name"` Value string `json:"Value"` TTL int64 `json:"TTL"` + MX int `json:"MX,omitempty"` } type ErrorInfo struct { @@ -66,9 +70,13 @@ type record struct { Name string Value string TTL time.Duration + MX int } func (r record) libdnsRecord() (libdns.Record, error) { + if r.Type == "MX" { + r.Value = strconv.Itoa(r.MX) + " " + r.Value + } return libdns.RR{ Type: r.Type, Name: r.Name,