2021-11-01 21:02:31 +00:00
|
|
|
package internal
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2018-12-06 21:50:17 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2021-11-01 21:02:31 +00:00
|
|
|
"net/url"
|
|
|
|
"time"
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
2021-11-01 21:02:31 +00:00
|
|
|
// DefaultBaseURL represents the API endpoint to call.
|
|
|
|
const DefaultBaseURL = "https://api.godaddy.com"
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
const authorizationHeader = "Authorization"
|
|
|
|
|
2021-11-01 21:02:31 +00:00
|
|
|
type Client struct {
|
2023-05-05 07:49:38 +00:00
|
|
|
apiKey string
|
|
|
|
apiSecret string
|
|
|
|
|
2021-11-01 21:02:31 +00:00
|
|
|
baseURL *url.URL
|
2023-05-05 07:49:38 +00:00
|
|
|
HTTPClient *http.Client
|
2021-11-01 21:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(apiKey string, apiSecret string) *Client {
|
|
|
|
baseURL, _ := url.Parse(DefaultBaseURL)
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
apiKey: apiKey,
|
|
|
|
apiSecret: apiSecret,
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2021-11-01 21:02:31 +00:00
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetRecords(ctx context.Context, domainZone, rType, recordName string) ([]DNSRecord, error) {
|
|
|
|
endpoint := c.baseURL.JoinPath("v1", "domains", domainZone, "records", rType, recordName)
|
2020-04-17 20:38:13 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2020-04-17 20:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []DNSRecord
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, &records)
|
2020-04-17 20:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) UpdateTxtRecords(ctx context.Context, records []DNSRecord, domainZone, recordName string) error {
|
|
|
|
endpoint := c.baseURL.JoinPath("v1", "domains", domainZone, "records", "TXT", recordName)
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodPut, endpoint, records)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.do(req, nil)
|
|
|
|
}
|
2020-04-17 20:38:13 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) do(req *http.Request, result any) error {
|
|
|
|
req.Header.Set(authorizationHeader, fmt.Sprintf("sso-key %s:%s", c.apiKey, c.apiSecret))
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 20:38:13 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(raw, result)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
2020-04-17 20:38:13 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
if payload != nil {
|
|
|
|
err := json.NewEncoder(buf).Encode(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:02:31 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if payload != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|