-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
create.go
66 lines (55 loc) · 1.54 KB
/
create.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
package aliyun
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/netip"
"net/url"
"github.com/qdm12/ddns-updater/internal/provider/constants"
"github.com/qdm12/ddns-updater/internal/provider/errors"
"github.com/qdm12/ddns-updater/internal/provider/utils"
)
func (p *Provider) createRecord(ctx context.Context,
client *http.Client, ip netip.Addr) (recordID string, err error) {
recordType := constants.A
if ip.Is6() {
recordType = constants.AAAA
}
u := &url.URL{
Scheme: "https",
Host: "alidns.aliyuncs.com",
}
values := newURLValues(p.accessKeyID)
values.Set("Action", "AddDomainRecord")
values.Set("DomainName", p.domain)
values.Set("RR", p.host)
values.Set("Type", recordType)
values.Set("Value", ip.String())
sign(http.MethodGet, values, p.accessSecret)
u.RawQuery = values.Encode()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return "", fmt.Errorf("creating http request: %w", err)
}
setHeaders(request)
response, err := client.Do(request)
if err != nil {
return "", fmt.Errorf("doing HTTP request: %w", err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("%w: %d: %s",
errors.ErrHTTPStatusNotValid, response.StatusCode,
utils.BodyToSingleLine(response.Body))
}
var data struct {
RecordID string `json:"RecordId"`
}
decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&data)
if err != nil {
return "", fmt.Errorf("json decoding response body: %w", err)
}
return data.RecordID, nil
}