Skip to content

Commit 272851c

Browse files
authored
Merge pull request #12 from nginxui/master
add session token and region; fix mx record parsing issue
2 parents 0127d53 + 185e79d commit 272851c

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

client.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (p *Provider) listRecords(ctx context.Context, zone string) ([]libdns.Recor
5050
Name: txRecord.Name,
5151
Value: txRecord.Value,
5252
TTL: time.Duration(txRecord.TTL) * time.Second,
53+
MX: txRecord.MX,
5354
}
5455
libdnsRecord, err := rr.libdnsRecord()
5556
if err != nil {
@@ -177,15 +178,20 @@ func (p *Provider) findRecord(ctx context.Context, zone string, record libdns.Re
177178
}
178179

179180
func (p *Provider) sendRequest(ctx context.Context, action string, data string) ([]byte, error) {
180-
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, strings.NewReader(data))
181+
endpointUrl := endpoint
182+
if p.Region != "" {
183+
endpointUrl = "https://dnspod." + p.Region + ".tencentcloudapi.com"
184+
}
185+
186+
req, err := http.NewRequestWithContext(ctx, "POST", endpointUrl, strings.NewReader(data))
181187
if err != nil {
182188
return nil, err
183189
}
184190

185191
req.Header.Set("Content-Type", "application/json")
186192
req.Header.Set("X-TC-Version", "2021-03-23")
187193

188-
SignRequest(p.SecretId, p.SecretKey, req, action, data)
194+
SignRequest(p.SecretId, p.SecretKey, p.SessionToken, req, action, data)
189195
resp, err := http.DefaultClient.Do(req)
190196
if err != nil {
191197
return nil, err

signer.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// SignRequest https://github.com/jeessy2/ddns-go/blob/master/util/tencent_cloud_signer.go
14-
func SignRequest(secretId string, secretKey string, r *http.Request, action string, payload string) {
14+
func SignRequest(secretId string, secretKey string, sessionToken string, r *http.Request, action string, payload string) {
1515
algorithm := "TC3-HMAC-SHA256"
1616
service := "dnspod"
1717
host := writeString(service, ".tencentcloudapi.com")
@@ -43,6 +43,11 @@ func SignRequest(secretId string, secretKey string, r *http.Request, action stri
4343
r.Header.Set("Host", host)
4444
r.Header.Set("X-TC-Action", action)
4545
r.Header.Set("X-TC-Timestamp", timestampStr)
46+
47+
// Add session token if provided
48+
if sessionToken != "" {
49+
r.Header.Set("X-TC-Token", sessionToken)
50+
}
4651
}
4752

4853
func sha256hex(s string) string {

types.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tencentcloud
22

33
import (
44
"errors"
5+
"strconv"
56
"time"
67

78
"github.com/libdns/libdns"
@@ -11,8 +12,10 @@ var ErrRecordNotFound = errors.New("record not found")
1112
var ErrNotValid = errors.New("returned value is not valid")
1213

1314
type Provider struct {
14-
SecretId string
15-
SecretKey string
15+
SecretId string
16+
SecretKey string
17+
SessionToken string
18+
Region string
1619
}
1720

1821
type CreateModifyRecordRequest struct {
@@ -54,6 +57,7 @@ type RecordInfo struct {
5457
Name string `json:"Name"`
5558
Value string `json:"Value"`
5659
TTL int64 `json:"TTL"`
60+
MX int `json:"MX,omitempty"`
5761
}
5862

5963
type ErrorInfo struct {
@@ -66,9 +70,13 @@ type record struct {
6670
Name string
6771
Value string
6872
TTL time.Duration
73+
MX int
6974
}
7075

7176
func (r record) libdnsRecord() (libdns.Record, error) {
77+
if r.Type == "MX" {
78+
r.Value = strconv.Itoa(r.MX) + " " + r.Value
79+
}
7280
return libdns.RR{
7381
Type: r.Type,
7482
Name: r.Name,

0 commit comments

Comments
 (0)