2019-03-11 16:56:48 +00:00
|
|
|
package internal
|
2019-03-01 18:09:00 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2019-03-01 18:09:00 +00:00
|
|
|
"encoding/json"
|
2019-04-17 20:41:51 +00:00
|
|
|
"errors"
|
2019-03-01 18:09:00 +00:00
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2019-03-01 18:09:00 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2023-05-05 07:49:38 +00:00
|
|
|
"time"
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"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"
|
2019-03-01 18:09:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://api.cloudns.net/dns/"
|
|
|
|
|
2021-02-28 12:25:15 +00:00
|
|
|
// Client the ClouDNS client.
|
|
|
|
type Client struct {
|
|
|
|
authID string
|
|
|
|
subAuthID string
|
|
|
|
authPassword string
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
BaseURL *url.URL
|
|
|
|
HTTPClient *http.Client
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewClient creates a ClouDNS client.
|
2020-07-09 23:48:18 +00:00
|
|
|
func NewClient(authID, subAuthID, authPassword string) (*Client, error) {
|
2020-03-25 08:34:23 +00:00
|
|
|
if authID == "" && subAuthID == "" {
|
|
|
|
return nil, errors.New("credentials missing: authID or subAuthID")
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if authPassword == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("credentials missing: authPassword")
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
baseURL, err := url.Parse(defaultBaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
authID: authID,
|
2020-03-25 08:34:23 +00:00
|
|
|
subAuthID: subAuthID,
|
2019-03-01 18:09:00 +00:00
|
|
|
authPassword: authPassword,
|
|
|
|
BaseURL: baseURL,
|
2023-05-05 07:49:38 +00:00
|
|
|
HTTPClient: &http.Client{Timeout: 10 * time.Second},
|
2019-03-01 18:09:00 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// GetZone Get domain name information for a FQDN.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetZone(ctx context.Context, authFQDN string) (*Zone, error) {
|
2019-03-01 18:09:00 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(authFQDN)
|
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("could not find zone for FQDN %q: %w", authFQDN, err)
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
authZoneName := dns01.UnFqdn(authZone)
|
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("get-zone-info.json")
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2021-02-28 12:25:15 +00:00
|
|
|
q := endpoint.Query()
|
|
|
|
q.Set("domain-name", authZoneName)
|
|
|
|
endpoint.RawQuery = q.Encode()
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := c.newRequest(ctx, http.MethodGet, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage, err := c.do(req)
|
2019-03-01 18:09:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var zone Zone
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if len(rawMessage) > 0 {
|
|
|
|
if err = json.Unmarshal(rawMessage, &zone); err != nil {
|
|
|
|
return nil, errutils.NewUnmarshalError(req, http.StatusOK, rawMessage, err)
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if zone.Name == authZoneName {
|
|
|
|
return &zone, nil
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:41:51 +00:00
|
|
|
return nil, fmt.Errorf("zone %s not found for authFQDN %s", authZoneName, authFQDN)
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 12:25:15 +00:00
|
|
|
// FindTxtRecord returns the TXT record a zone ID and a FQDN.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) FindTxtRecord(ctx context.Context, zoneName, fqdn string) (*TXTRecord, error) {
|
2022-12-04 15:11:27 +00:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(fqdn, zoneName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("records.json")
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
q := endpoint.Query()
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("domain-name", zoneName)
|
2022-12-04 15:11:27 +00:00
|
|
|
q.Set("host", subDomain)
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("type", "TXT")
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint.RawQuery = q.Encode()
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := c.newRequest(ctx, http.MethodGet, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage, err := c.do(req)
|
2019-03-01 18:09:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:41:51 +00:00
|
|
|
// the API returns [] when there is no records.
|
2023-05-05 07:49:38 +00:00
|
|
|
if string(rawMessage) == "[]" {
|
2019-04-17 20:41:51 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 12:25:15 +00:00
|
|
|
var records map[string]TXTRecord
|
2023-05-05 07:49:38 +00:00
|
|
|
if err = json.Unmarshal(rawMessage, &records); err != nil {
|
|
|
|
return nil, errutils.NewUnmarshalError(req, http.StatusOK, rawMessage, err)
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range records {
|
2022-12-04 15:11:27 +00:00
|
|
|
if record.Host == subDomain && record.Type == "TXT" {
|
2019-03-01 18:09:00 +00:00
|
|
|
return &record, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:41:51 +00:00
|
|
|
return nil, nil
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 12:25:15 +00:00
|
|
|
// ListTxtRecords returns the TXT records a zone ID and a FQDN.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) ListTxtRecords(ctx context.Context, zoneName, fqdn string) ([]TXTRecord, error) {
|
2022-12-04 15:11:27 +00:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(fqdn, zoneName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-28 12:25:15 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("records.json")
|
2021-02-28 12:25:15 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
q := endpoint.Query()
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("domain-name", zoneName)
|
2022-12-04 15:11:27 +00:00
|
|
|
q.Set("host", subDomain)
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("type", "TXT")
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint.RawQuery = q.Encode()
|
2021-02-28 12:25:15 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := c.newRequest(ctx, http.MethodGet, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage, err := c.do(req)
|
2021-02-28 12:25:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the API returns [] when there is no records.
|
2023-05-05 07:49:38 +00:00
|
|
|
if string(rawMessage) == "[]" {
|
2021-02-28 12:25:15 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var raw map[string]TXTRecord
|
2023-05-05 07:49:38 +00:00
|
|
|
if err = json.Unmarshal(rawMessage, &raw); err != nil {
|
|
|
|
return nil, errutils.NewUnmarshalError(req, http.StatusOK, rawMessage, err)
|
2021-02-28 12:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var records []TXTRecord
|
|
|
|
for _, record := range raw {
|
2022-12-04 15:11:27 +00:00
|
|
|
if record.Host == subDomain && record.Type == "TXT" {
|
2021-02-28 12:25:15 +00:00
|
|
|
records = append(records, record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTxtRecord adds a TXT record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) AddTxtRecord(ctx context.Context, zoneName, fqdn, value string, ttl int) error {
|
2022-12-04 15:11:27 +00:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(fqdn, zoneName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("add-record.json")
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
q := endpoint.Query()
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("domain-name", zoneName)
|
2022-12-04 15:11:27 +00:00
|
|
|
q.Set("host", subDomain)
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("record", value)
|
|
|
|
q.Set("ttl", strconv.Itoa(ttlRounder(ttl)))
|
|
|
|
q.Set("record-type", "TXT")
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint.RawQuery = q.Encode()
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := c.newRequest(ctx, http.MethodPost, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage, err := c.do(req)
|
2019-04-17 20:41:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := apiResponse{}
|
2023-05-05 07:49:38 +00:00
|
|
|
if err = json.Unmarshal(rawMessage, &resp); err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, http.StatusOK, rawMessage, err)
|
2019-04-17 20:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Status != "Success" {
|
2021-02-28 12:25:15 +00:00
|
|
|
return fmt.Errorf("failed to add TXT record: %s %s", resp.Status, resp.StatusDescription)
|
2019-04-17 20:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 12:25:15 +00:00
|
|
|
// RemoveTxtRecord removes a TXT record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) RemoveTxtRecord(ctx context.Context, recordID int, zoneName string) error {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("delete-record.json")
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
q := endpoint.Query()
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("domain-name", zoneName)
|
|
|
|
q.Set("record-id", strconv.Itoa(recordID))
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint.RawQuery = q.Encode()
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := c.newRequest(ctx, http.MethodPost, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage, err := c.do(req)
|
2019-04-17 20:41:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := apiResponse{}
|
2023-05-05 07:49:38 +00:00
|
|
|
if err = json.Unmarshal(rawMessage, &resp); err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, http.StatusOK, rawMessage, err)
|
2019-04-17 20:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Status != "Success" {
|
2021-02-28 12:25:15 +00:00
|
|
|
return fmt.Errorf("failed to remove TXT record: %s %s", resp.Status, resp.StatusDescription)
|
2019-04-17 20:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 12:25:15 +00:00
|
|
|
// GetUpdateStatus gets sync progress of all CloudDNS NS servers.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetUpdateStatus(ctx context.Context, zoneName string) (*SyncProgress, error) {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("update-status.json")
|
2021-02-28 12:25:15 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
q := endpoint.Query()
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("domain-name", zoneName)
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint.RawQuery = q.Encode()
|
2021-02-28 12:25:15 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := c.newRequest(ctx, http.MethodGet, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage, err := c.do(req)
|
2021-02-28 12:25:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the API returns [] when there is no records.
|
2023-05-05 07:49:38 +00:00
|
|
|
if string(rawMessage) == "[]" {
|
2021-02-28 12:25:15 +00:00
|
|
|
return nil, errors.New("no nameservers records returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []UpdateRecord
|
2023-05-05 07:49:38 +00:00
|
|
|
if err = json.Unmarshal(rawMessage, &records); err != nil {
|
|
|
|
return nil, errutils.NewUnmarshalError(req, http.StatusOK, rawMessage, err)
|
2021-02-28 12:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updatedCount := 0
|
|
|
|
for _, record := range records {
|
|
|
|
if record.Updated {
|
|
|
|
updatedCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SyncProgress{Complete: updatedCount == len(records), Updated: updatedCount, Total: len(records)}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) newRequest(ctx context.Context, method string, endpoint *url.URL) (*http.Request, error) {
|
|
|
|
q := endpoint.Query()
|
2020-03-25 08:34:23 +00:00
|
|
|
|
|
|
|
if c.subAuthID != "" {
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("sub-auth-id", c.subAuthID)
|
2020-03-25 08:34:23 +00:00
|
|
|
} else {
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("auth-id", c.authID)
|
2020-03-25 08:34:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 12:25:15 +00:00
|
|
|
q.Set("auth-password", c.authPassword)
|
2020-03-25 08:34:23 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
endpoint.RawQuery = q.Encode()
|
2019-03-01 18:09:00 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), nil)
|
2019-03-01 18:09:00 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) do(req *http.Request) (json.RawMessage, error) {
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errutils.NewHTTPDoError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw, nil
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
2019-04-17 20:41:51 +00:00
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Rounds the given TTL in seconds to the next accepted value.
|
|
|
|
// Accepted TTL values are:
|
2022-08-22 15:05:31 +00:00
|
|
|
// - 60 = 1 minute
|
|
|
|
// - 300 = 5 minutes
|
|
|
|
// - 900 = 15 minutes
|
|
|
|
// - 1800 = 30 minutes
|
|
|
|
// - 3600 = 1 hour
|
|
|
|
// - 21600 = 6 hours
|
|
|
|
// - 43200 = 12 hours
|
|
|
|
// - 86400 = 1 day
|
|
|
|
// - 172800 = 2 days
|
|
|
|
// - 259200 = 3 days
|
|
|
|
// - 604800 = 1 week
|
|
|
|
// - 1209600 = 2 weeks
|
|
|
|
// - 2592000 = 1 month
|
|
|
|
//
|
2020-05-08 17:35:25 +00:00
|
|
|
// See https://www.cloudns.net/wiki/article/58/ for details.
|
2019-04-17 20:41:51 +00:00
|
|
|
func ttlRounder(ttl int) int {
|
|
|
|
for _, validTTL := range []int{60, 300, 900, 1800, 3600, 21600, 43200, 86400, 172800, 259200, 604800, 1209600} {
|
|
|
|
if ttl <= validTTL {
|
|
|
|
return validTTL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 2592000
|
|
|
|
}
|