lego/providers/dns/dynu/internal/client.go

171 lines
4.1 KiB
Go
Raw Normal View History

2020-03-10 11:31:33 +00:00
package internal
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
2020-03-10 11:31:33 +00:00
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/cenkalti/backoff/v4"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/log"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
2020-03-10 11:31:33 +00:00
)
const defaultBaseURL = "https://api.dynu.com/v2"
type Client struct {
2023-05-05 07:49:38 +00:00
baseURL *url.URL
2020-03-10 11:31:33 +00:00
HTTPClient *http.Client
}
func NewClient() *Client {
2023-05-05 07:49:38 +00:00
baseURL, _ := url.Parse(defaultBaseURL)
2020-03-10 11:31:33 +00:00
return &Client{
2023-05-05 07:49:38 +00:00
HTTPClient: &http.Client{Timeout: 5 * time.Second},
baseURL: baseURL,
2020-03-10 11:31:33 +00:00
}
}
// GetRecords Get DNS records based on a hostname and resource record type.
2023-05-05 07:49:38 +00:00
func (c Client) GetRecords(ctx context.Context, hostname, recordType string) ([]DNSRecord, error) {
endpoint := c.baseURL.JoinPath("dns", "record", hostname)
2020-03-10 11:31:33 +00:00
query := endpoint.Query()
query.Set("recordType", recordType)
endpoint.RawQuery = query.Encode()
apiResp := RecordsResponse{}
2023-05-05 07:49:38 +00:00
err := c.doRetry(ctx, http.MethodGet, endpoint.String(), nil, &apiResp)
2020-03-10 11:31:33 +00:00
if err != nil {
return nil, err
}
if apiResp.StatusCode/100 != 2 {
return nil, fmt.Errorf("API error: %w", apiResp.APIException)
}
return apiResp.DNSRecords, nil
}
// AddNewRecord Add a new DNS record for DNS service.
2023-05-05 07:49:38 +00:00
func (c Client) AddNewRecord(ctx context.Context, domainID int64, record DNSRecord) error {
endpoint := c.baseURL.JoinPath("dns", strconv.FormatInt(domainID, 10), "record")
2020-03-10 11:31:33 +00:00
reqBody, err := json.Marshal(record)
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("failed to create request JSON body: %w", err)
2020-03-10 11:31:33 +00:00
}
apiResp := RecordResponse{}
2023-05-05 07:49:38 +00:00
err = c.doRetry(ctx, http.MethodPost, endpoint.String(), reqBody, &apiResp)
2020-03-10 11:31:33 +00:00
if err != nil {
return err
}
if apiResp.StatusCode/100 != 2 {
return fmt.Errorf("API error: %w", apiResp.APIException)
}
return nil
}
// DeleteRecord Remove a DNS record from DNS service.
2023-05-05 07:49:38 +00:00
func (c Client) DeleteRecord(ctx context.Context, domainID, recordID int64) error {
endpoint := c.baseURL.JoinPath("dns", strconv.FormatInt(domainID, 10), "record", strconv.FormatInt(recordID, 10))
2020-03-10 11:31:33 +00:00
apiResp := APIException{}
2023-05-05 07:49:38 +00:00
err := c.doRetry(ctx, http.MethodDelete, endpoint.String(), nil, &apiResp)
2020-03-10 11:31:33 +00:00
if err != nil {
return err
}
if apiResp.StatusCode/100 != 2 {
return fmt.Errorf("API error: %w", apiResp)
}
return nil
}
// GetRootDomain Get the root domain name based on a hostname.
2023-05-05 07:49:38 +00:00
func (c Client) GetRootDomain(ctx context.Context, hostname string) (*DNSHostname, error) {
endpoint := c.baseURL.JoinPath("dns", "getroot", hostname)
2020-03-10 11:31:33 +00:00
apiResp := DNSHostname{}
2023-05-05 07:49:38 +00:00
err := c.doRetry(ctx, http.MethodGet, endpoint.String(), nil, &apiResp)
2020-03-10 11:31:33 +00:00
if err != nil {
return nil, err
}
if apiResp.StatusCode/100 != 2 {
return nil, fmt.Errorf("API error: %w", apiResp.APIException)
}
return &apiResp, nil
}
// doRetry the API is really unstable so we need to retry on EOF.
2023-05-05 07:49:38 +00:00
func (c Client) doRetry(ctx context.Context, method, uri string, body []byte, result any) error {
2020-03-10 11:31:33 +00:00
operation := func() error {
2023-05-05 07:49:38 +00:00
return c.do(ctx, method, uri, body, result)
2020-03-10 11:31:33 +00:00
}
notify := func(err error, duration time.Duration) {
log.Printf("client retries because of %v", err)
}
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 1 * time.Second
err := backoff.RetryNotify(operation, bo, notify)
2020-03-10 11:31:33 +00:00
if err != nil {
return err
}
2023-05-05 07:49:38 +00:00
return nil
}
func (c Client) do(ctx context.Context, method, uri string, body []byte, result any) error {
var reqBody io.Reader
if len(body) > 0 {
reqBody = bytes.NewReader(body)
}
2020-03-10 11:31:33 +00:00
2023-05-05 07:49:38 +00:00
req, err := http.NewRequestWithContext(ctx, method, uri, reqBody)
2020-03-10 11:31:33 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("unable to create request: %w", err)
2020-03-10 11:31:33 +00:00
}
2023-05-05 07:49:38 +00:00
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if errors.Is(err, io.EOF) {
return err
}
2020-03-10 11:31:33 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return backoff.Permanent(fmt.Errorf("client error: %w", errutils.NewHTTPDoError(req, err)))
2020-03-10 11:31:33 +00:00
}
2023-05-05 07:49:38 +00:00
defer func() { _ = resp.Body.Close() }()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return backoff.Permanent(errutils.NewReadResponseError(req, resp.StatusCode, err))
}
err = json.Unmarshal(raw, result)
if err != nil {
return backoff.Permanent(errutils.NewUnmarshalError(req, resp.StatusCode, raw, err))
}
return nil
2020-03-10 11:31:33 +00:00
}