2020-05-10 04:03:54 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2020-05-10 04:03:54 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-05-05 07:49:38 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2020-05-10 04:03:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// defaultBaseURL represents the API endpoint to call.
|
|
|
|
const defaultBaseURL = "https://api.luadns.com"
|
|
|
|
|
|
|
|
// Client Lua DNS API client.
|
|
|
|
type Client struct {
|
|
|
|
apiUsername string
|
|
|
|
apiToken string
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
baseURL *url.URL
|
|
|
|
HTTPClient *http.Client
|
2020-05-10 04:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new Client.
|
|
|
|
func NewClient(apiUsername, apiToken string) *Client {
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL, _ := url.Parse(defaultBaseURL)
|
|
|
|
|
2020-05-10 04:03:54 +00:00
|
|
|
return &Client{
|
|
|
|
apiUsername: apiUsername,
|
|
|
|
apiToken: apiToken,
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2020-05-10 04:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListZones gets all the hosted zones.
|
|
|
|
// https://luadns.com/api.html#list-zones
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) ListZones(ctx context.Context) ([]DNSZone, error) {
|
|
|
|
endpoint := c.baseURL.JoinPath("v1", "zones")
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2020-05-10 04:03:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var zones []DNSZone
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, &zones)
|
2020-05-10 04:03:54 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("could not list zones: %w", err)
|
2020-05-10 04:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return zones, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRecord creates a new record in a zone.
|
|
|
|
// https://luadns.com/api.html#create-a-record
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) CreateRecord(ctx context.Context, zone DNSZone, newRecord DNSRecord) (*DNSRecord, error) {
|
|
|
|
endpoint := c.baseURL.JoinPath("v1", "zones", strconv.Itoa(zone.ID), "records")
|
2020-05-10 04:03:54 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, newRecord)
|
2020-05-10 04:03:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var record *DNSRecord
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, &record)
|
2020-05-10 04:03:54 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("could not create record %#v: %w", record, err)
|
2020-05-10 04:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRecord deletes a record.
|
|
|
|
// https://luadns.com/api.html#delete-a-record
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) DeleteRecord(ctx context.Context, record *DNSRecord) error {
|
|
|
|
endpoint := c.baseURL.JoinPath("v1", "zones", strconv.Itoa(record.ZoneID), "records", strconv.Itoa(record.ID))
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, record)
|
2020-05-10 04:03:54 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.do(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not delete record %#v: %w", record, err)
|
2020-05-10 04:03:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) do(req *http.Request, result any) error {
|
|
|
|
req.SetBasicAuth(c.apiUsername, c.apiToken)
|
2020-05-10 04:03:54 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2020-05-10 04:03:54 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2020-05-10 04:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-05-05 07:49:38 +00:00
|
|
|
return parseError(req, resp)
|
|
|
|
}
|
2020-05-10 04:03:54 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-10 04:03:54 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
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)
|
2020-05-10 04:03:54 +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)
|
2020-05-10 04:03:54 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2020-05-10 04:03:54 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseError(req *http.Request, resp *http.Response) error {
|
|
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
var errResp errorResponse
|
|
|
|
err := json.Unmarshal(raw, &errResp)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("status=%d: %w", resp.StatusCode, errResp)
|
2020-05-10 04:03:54 +00:00
|
|
|
}
|