/
alidns.go
185 lines (165 loc) · 4.13 KB
/
alidns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package alidns
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/TimothyYe/godns/internal/utils"
)
const (
baseURL = "https://alidns.aliyuncs.com/"
)
var (
publicParam = map[string]string{
"AccessKeyId": "",
"Format": "JSON",
"Version": "2015-01-09",
"SignatureMethod": "HMAC-SHA1",
"Timestamp": "",
"SignatureVersion": "1.0",
"SignatureNonce": "",
}
instance *AliDNS
once sync.Once
)
// AliDNS token.
type AliDNS struct {
AccessKeyID string
AccessKeySecret string
IPType string
}
type domainRecordsResp struct {
RequestID string `json:"RequestId"`
TotalCount int
PageNumber int
PageSize int
DomainRecords domainRecords
}
type domainRecords struct {
Record []DomainRecord
}
// DomainRecord struct.
type DomainRecord struct {
DomainName string
RecordID string `json:"RecordId"`
RR string
Type string
Value string
Line string
Priority int
TTL int
Status string
Locked bool
}
func getHTTPBody(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if resp.StatusCode == http.StatusOK {
return body, err
}
return nil, fmt.Errorf("status %d, Error:%s", resp.StatusCode, body)
}
// NewAliDNS function creates instance of AliDNS and return.
func NewAliDNS(key, secret, ipType string) *AliDNS {
once.Do(func() {
instance = &AliDNS{
AccessKeyID: key,
AccessKeySecret: secret,
IPType: ipType,
}
})
return instance
}
// GetDomainRecords gets all the domain records according to input subdomain key.
func (d *AliDNS) GetDomainRecords(domain, rr string) []DomainRecord {
resp := &domainRecordsResp{}
params := map[string]string{
"Action": "DescribeSubDomainRecords",
"SubDomain": fmt.Sprintf("%s.%s", rr, domain),
}
if d.IPType == "" || strings.ToUpper(d.IPType) == utils.IPV4 {
params["Type"] = utils.IPTypeA
} else if strings.ToUpper(d.IPType) == utils.IPV6 {
params["Type"] = utils.IPTypeAAAA
}
urlPath := d.genRequestURL(params)
body, err := getHTTPBody(urlPath)
if err != nil {
fmt.Printf("GetDomainRecords error.%+v\n", err)
} else {
if err := json.Unmarshal(body, resp); err != nil {
fmt.Printf("GetDomainRecords error. %+v\n", err)
return nil
}
return resp.DomainRecords.Record
}
return nil
}
// UpdateDomainRecord updates domain record.
func (d *AliDNS) UpdateDomainRecord(r DomainRecord) error {
params := map[string]string{
"Action": "UpdateDomainRecord",
"RecordId": r.RecordID,
"RR": r.RR,
"Value": r.Value,
"TTL": strconv.Itoa(r.TTL),
"Line": r.Line,
}
if d.IPType == "" || strings.ToUpper(d.IPType) == utils.IPV4 {
params["Type"] = utils.IPTypeA
} else if strings.ToUpper(d.IPType) == utils.IPV6 {
params["Type"] = utils.IPTypeAAAA
}
urlPath := d.genRequestURL(params)
if urlPath == "" {
return errors.New("failed to generate request URL")
}
_, err := getHTTPBody(urlPath)
if err != nil {
fmt.Printf("UpdateDomainRecord error.%+v\n", err)
}
return err
}
func (d *AliDNS) genRequestURL(params map[string]string) string {
var pArr []string
ps := map[string]string{}
for k, v := range publicParam {
ps[k] = v
}
for k, v := range params {
ps[k] = v
}
now := time.Now().UTC()
ps["AccessKeyId"] = d.AccessKeyID
ps["SignatureNonce"] = strconv.Itoa(int(now.UnixNano()) + rand.Intn(99999))
ps["Timestamp"] = now.Format("2006-01-02T15:04:05Z")
for k, v := range ps {
pArr = append(pArr, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(pArr)
path := strings.Join(pArr, "&")
s := "GET&%2F&" + url.QueryEscape(path)
s = strings.Replace(s, "%3A", "%253A", -1)
s = strings.Replace(s, "%40", "%2540", -1)
s = strings.Replace(s, "%2A", "%252A", -1)
mac := hmac.New(sha1.New, []byte(d.AccessKeySecret+"&"))
if _, err := mac.Write([]byte(s)); err != nil {
return ""
}
sign := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return fmt.Sprintf("%s?%s&Signature=%s", baseURL, path, url.QueryEscape(sign))
}