Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -177,15 +178,20 @@ 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
}

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
Expand Down
7 changes: 6 additions & 1 deletion signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tencentcloud

import (
"errors"
"strconv"
"time"

"github.com/libdns/libdns"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down