2021-12-20 14:15:49 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2021-12-20 14:15:49 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
2023-05-05 07:49:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2021-12-20 14:15:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://api.ukfast.io/safedns/v1"
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
const authorizationHeader = "Authorization"
|
|
|
|
|
2021-12-20 14:15:49 +00:00
|
|
|
// Client the UKFast SafeDNS client.
|
|
|
|
type Client struct {
|
|
|
|
authToken string
|
|
|
|
|
|
|
|
baseURL *url.URL
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient Creates a new Client.
|
|
|
|
func NewClient(authToken string) *Client {
|
|
|
|
baseURL, _ := url.Parse(defaultBaseURL)
|
2023-05-05 07:49:38 +00:00
|
|
|
|
2021-12-20 14:15:49 +00:00
|
|
|
return &Client{
|
|
|
|
authToken: authToken,
|
|
|
|
baseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRecord adds a DNS record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) AddRecord(ctx context.Context, zone string, record Record) (*AddRecordResponse, error) {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("zones", dns01.UnFqdn(zone), "records")
|
2021-12-20 14:15:49 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
|
2021-12-20 14:15:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
respData := &AddRecordResponse{}
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, respData)
|
2021-12-20 14:15:49 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("remove record: %w", err)
|
2021-12-20 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return respData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRecord removes a DNS record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) RemoveRecord(ctx context.Context, zone string, recordID int) error {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("zones", dns01.UnFqdn(zone), "records", strconv.Itoa(recordID))
|
2021-12-20 14:15:49 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2021-12-20 14:15:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("remove record: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) do(req *http.Request, result any) error {
|
|
|
|
req.Header.Set(authorizationHeader, c.authToken)
|
|
|
|
|
2021-12-20 14:15:49 +00:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2021-12-20 14:15:49 +00:00
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
|
2021-12-20 14:15:49 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
return parseError(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)
|
2021-12-20 14:15:49 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
|
2021-12-20 14:15:49 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2021-12-20 14:15:49 +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")
|
|
|
|
}
|
2021-12-20 14:15:49 +00:00
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func parseError(req *http.Request, resp *http.Response) error {
|
|
|
|
raw, _ := io.ReadAll(resp.Body)
|
2021-12-20 14:15:49 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
var errAPI APIError
|
|
|
|
err := json.Unmarshal(raw, &errAPI)
|
2021-12-20 14:15:49 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
|
2021-12-20 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("[status code: %d] %w", resp.StatusCode, errAPI)
|
2021-12-20 14:15:49 +00:00
|
|
|
}
|