2019-03-11 16:56:48 +00:00
|
|
|
package internal
|
2019-03-01 18:09:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-04-17 20:41:51 +00:00
|
|
|
"errors"
|
2019-03-01 18:09:00 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/challenge/dns01"
|
2019-03-01 18:09:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://api.cloudns.net/dns/"
|
|
|
|
|
2019-04-17 20:41:51 +00:00
|
|
|
type apiResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
StatusDescription string `json:"statusDescription"`
|
|
|
|
}
|
|
|
|
|
2019-03-01 18:09:00 +00:00
|
|
|
type Zone struct {
|
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
Zone string
|
|
|
|
Status string // is an integer, but cast as string
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// TXTRecord a TXT record.
|
2019-03-01 18:09:00 +00:00
|
|
|
type TXTRecord struct {
|
|
|
|
ID int `json:"id,string"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Record string `json:"record"`
|
|
|
|
Failover int `json:"failover,string"`
|
|
|
|
TTL int `json:"ttl,string"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TXTRecords map[string]TXTRecord
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewClient creates a ClouDNS client.
|
2020-03-25 08:34:23 +00:00
|
|
|
func NewClient(authID string, subAuthID string, authPassword string) (*Client, error) {
|
|
|
|
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,
|
|
|
|
HTTPClient: &http.Client{},
|
|
|
|
BaseURL: baseURL,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Client ClouDNS client.
|
2019-03-01 18:09:00 +00:00
|
|
|
type Client struct {
|
|
|
|
authID string
|
2020-03-25 08:34:23 +00:00
|
|
|
subAuthID string
|
2019-03-01 18:09:00 +00:00
|
|
|
authPassword string
|
|
|
|
HTTPClient *http.Client
|
|
|
|
BaseURL *url.URL
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// GetZone Get domain name information for a FQDN.
|
2019-03-01 18:09:00 +00:00
|
|
|
func (c *Client) GetZone(authFQDN string) (*Zone, error) {
|
|
|
|
authZone, err := dns01.FindZoneByFqdn(authFQDN)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
authZoneName := dns01.UnFqdn(authZone)
|
|
|
|
|
|
|
|
reqURL := *c.BaseURL
|
|
|
|
reqURL.Path += "get-zone-info.json"
|
|
|
|
|
|
|
|
q := reqURL.Query()
|
|
|
|
q.Add("domain-name", authZoneName)
|
|
|
|
reqURL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
result, err := c.doRequest(http.MethodGet, &reqURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var zone Zone
|
|
|
|
|
|
|
|
if len(result) > 0 {
|
|
|
|
if err = json.Unmarshal(result, &zone); err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("zone unmarshaling error: %w", 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
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// FindTxtRecord return the TXT record a zone ID and a FQDN.
|
2019-03-01 18:09:00 +00:00
|
|
|
func (c *Client) FindTxtRecord(zoneName, fqdn string) (*TXTRecord, error) {
|
|
|
|
host := dns01.UnFqdn(strings.TrimSuffix(dns01.UnFqdn(fqdn), zoneName))
|
|
|
|
|
|
|
|
reqURL := *c.BaseURL
|
|
|
|
reqURL.Path += "records.json"
|
|
|
|
|
|
|
|
q := reqURL.Query()
|
|
|
|
q.Add("domain-name", zoneName)
|
|
|
|
q.Add("host", host)
|
|
|
|
q.Add("type", "TXT")
|
|
|
|
reqURL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
result, err := c.doRequest(http.MethodGet, &reqURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:41:51 +00:00
|
|
|
// the API returns [] when there is no records.
|
|
|
|
if string(result) == "[]" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-03-01 18:09:00 +00:00
|
|
|
var records TXTRecords
|
|
|
|
if err = json.Unmarshal(result, &records); err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("TXT record unmarshaling error: %w: %s", err, string(result))
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
if record.Host == host && record.Type == "TXT" {
|
|
|
|
return &record, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:41:51 +00:00
|
|
|
return nil, nil
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// AddTxtRecord add a TXT record.
|
2019-03-01 18:09:00 +00:00
|
|
|
func (c *Client) AddTxtRecord(zoneName string, fqdn, value string, ttl int) error {
|
|
|
|
host := dns01.UnFqdn(strings.TrimSuffix(dns01.UnFqdn(fqdn), zoneName))
|
|
|
|
|
|
|
|
reqURL := *c.BaseURL
|
|
|
|
reqURL.Path += "add-record.json"
|
|
|
|
|
|
|
|
q := reqURL.Query()
|
|
|
|
q.Add("domain-name", zoneName)
|
|
|
|
q.Add("host", host)
|
|
|
|
q.Add("record", value)
|
2019-04-17 20:41:51 +00:00
|
|
|
q.Add("ttl", strconv.Itoa(ttlRounder(ttl)))
|
2019-03-01 18:09:00 +00:00
|
|
|
q.Add("record-type", "TXT")
|
|
|
|
reqURL.RawQuery = q.Encode()
|
|
|
|
|
2019-04-17 20:41:51 +00:00
|
|
|
raw, err := c.doRequest(http.MethodPost, &reqURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := apiResponse{}
|
|
|
|
if err = json.Unmarshal(raw, &resp); err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("apiResponse unmarshaling error: %w: %s", err, string(raw))
|
2019-04-17 20:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Status != "Success" {
|
|
|
|
return fmt.Errorf("fail to add TXT record: %s %s", resp.Status, resp.StatusDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// RemoveTxtRecord remove a TXT record.
|
2019-03-01 18:09:00 +00:00
|
|
|
func (c *Client) RemoveTxtRecord(recordID int, zoneName string) error {
|
|
|
|
reqURL := *c.BaseURL
|
|
|
|
reqURL.Path += "delete-record.json"
|
|
|
|
|
|
|
|
q := reqURL.Query()
|
|
|
|
q.Add("domain-name", zoneName)
|
|
|
|
q.Add("record-id", strconv.Itoa(recordID))
|
|
|
|
reqURL.RawQuery = q.Encode()
|
|
|
|
|
2019-04-17 20:41:51 +00:00
|
|
|
raw, err := c.doRequest(http.MethodPost, &reqURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := apiResponse{}
|
|
|
|
if err = json.Unmarshal(raw, &resp); err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("apiResponse unmarshaling error: %w: %s", err, string(raw))
|
2019-04-17 20:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Status != "Success" {
|
|
|
|
return fmt.Errorf("fail to add TXT record: %s %s", resp.Status, resp.StatusDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) doRequest(method string, url *url.URL) (json.RawMessage, error) {
|
|
|
|
req, err := c.buildRequest(method, url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2019-04-17 20:41:51 +00:00
|
|
|
return nil, err
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2019-04-17 20:41:51 +00:00
|
|
|
return nil, errors.New(toUnreadableBodyMessage(req, content))
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
2019-04-17 20:41:51 +00:00
|
|
|
return nil, fmt.Errorf("invalid code (%v), error: %s", resp.StatusCode, content)
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) buildRequest(method string, url *url.URL) (*http.Request, error) {
|
|
|
|
q := url.Query()
|
2020-03-25 08:34:23 +00:00
|
|
|
|
|
|
|
if c.subAuthID != "" {
|
|
|
|
q.Add("sub-auth-id", c.subAuthID)
|
|
|
|
} else {
|
|
|
|
q.Add("auth-id", c.authID)
|
|
|
|
}
|
|
|
|
|
2019-03-01 18:09:00 +00:00
|
|
|
q.Add("auth-password", c.authPassword)
|
2020-03-25 08:34:23 +00:00
|
|
|
|
2019-03-01 18:09:00 +00:00
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, url.String(), nil)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("invalid request: %w", err)
|
2019-03-01 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toUnreadableBodyMessage(req *http.Request, rawBody []byte) string {
|
|
|
|
return fmt.Sprintf("the request %s sent a response with a body which is an invalid format: %q", req.URL, string(rawBody))
|
|
|
|
}
|
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:
|
|
|
|
// - 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
|
|
|
|
// - 2592000 = 1 month
|
|
|
|
// 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
|
|
|
|
}
|