From ca409af99ffd24a29c880a25de51f6b20e304b34 Mon Sep 17 00:00:00 2001 From: Jacky Date: Thu, 8 May 2025 15:39:45 +0800 Subject: [PATCH 1/2] feat: add support for session token and region in request signing --- client.go | 9 +++++++-- signer.go | 7 ++++++- types.go | 6 ++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index 30a3aa5..62035cd 100644 --- a/client.go +++ b/client.go @@ -177,7 +177,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 +190,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..b02da0d 100644 --- a/types.go +++ b/types.go @@ -11,8 +11,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 { From 185e79d3e279778f420ce634d9a2ca3f3c1af634 Mon Sep 17 00:00:00 2001 From: Jacky Date: Sat, 10 May 2025 10:21:34 +0800 Subject: [PATCH 2/2] fix: mx record parsing issue --- client.go | 1 + types.go | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/client.go b/client.go index 62035cd..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 { diff --git a/types.go b/types.go index b02da0d..1bdb419 100644 --- a/types.go +++ b/types.go @@ -2,6 +2,7 @@ package tencentcloud import ( "errors" + "strconv" "time" "github.com/libdns/libdns" @@ -56,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 { @@ -68,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,