2019-03-11 16:56:48 +00:00
|
|
|
package internal
|
2018-09-15 17:26:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-02-27 18:14:46 +00:00
|
|
|
"errors"
|
2018-09-15 17:26:45 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/challenge/dns01"
|
2018-09-15 17:26:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://www.cloudxns.net/api2/"
|
|
|
|
|
|
|
|
type apiResponse struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Data json.RawMessage `json:"data,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Data Domain information.
|
2018-09-15 17:26:45 +00:00
|
|
|
type Data struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Domain string `json:"domain"`
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// TXTRecord a TXT record.
|
2018-09-15 17:26:45 +00:00
|
|
|
type TXTRecord struct {
|
|
|
|
ID int `json:"domain_id,omitempty"`
|
|
|
|
RecordID string `json:"record_id,omitempty"`
|
|
|
|
|
|
|
|
Host string `json:"host"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
LineID int `json:"line_id,string"`
|
|
|
|
TTL int `json:"ttl,string"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewClient creates a CloudXNS client.
|
2020-07-09 23:48:18 +00:00
|
|
|
func NewClient(apiKey, secretKey string) (*Client, error) {
|
2018-09-15 17:26:45 +00:00
|
|
|
if apiKey == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("CloudXNS: credentials missing: apiKey")
|
2018-09-15 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if secretKey == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("CloudXNS: credentials missing: secretKey")
|
2018-09-15 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
apiKey: apiKey,
|
|
|
|
secretKey: secretKey,
|
|
|
|
HTTPClient: &http.Client{},
|
|
|
|
BaseURL: defaultBaseURL,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Client CloudXNS client.
|
2018-09-15 17:26:45 +00:00
|
|
|
type Client struct {
|
|
|
|
apiKey string
|
|
|
|
secretKey string
|
|
|
|
HTTPClient *http.Client
|
|
|
|
BaseURL string
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// GetDomainInformation Get domain name information for a FQDN.
|
2018-09-15 17:26:45 +00:00
|
|
|
func (c *Client) GetDomainInformation(fqdn string) (*Data, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
2018-09-15 17:26:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := c.doRequest(http.MethodGet, "domain", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var domains []Data
|
|
|
|
if len(result) > 0 {
|
|
|
|
err = json.Unmarshal(result, &domains)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("CloudXNS: domains unmarshaling error: %w", err)
|
2018-09-15 17:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, data := range domains {
|
|
|
|
if data.Domain == authZone {
|
|
|
|
return &data, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("CloudXNS: zone %s not found for domain %s", authZone, fqdn)
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// FindTxtRecord return the TXT record a zone ID and a FQDN.
|
2018-09-15 17:26:45 +00:00
|
|
|
func (c *Client) FindTxtRecord(zoneID, fqdn string) (*TXTRecord, error) {
|
|
|
|
result, err := c.doRequest(http.MethodGet, fmt.Sprintf("record/%s?host_id=0&offset=0&row_num=2000", zoneID), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []TXTRecord
|
|
|
|
err = json.Unmarshal(result, &records)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("CloudXNS: TXT record unmarshaling error: %w", err)
|
2018-09-15 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range records {
|
2018-12-06 21:50:17 +00:00
|
|
|
if record.Host == dns01.UnFqdn(fqdn) && record.Type == "TXT" {
|
2018-09-15 17:26:45 +00:00
|
|
|
return &record, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("CloudXNS: no existing record found for %q", fqdn)
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// AddTxtRecord add a TXT record.
|
2018-09-15 17:26:45 +00:00
|
|
|
func (c *Client) AddTxtRecord(info *Data, fqdn, value string, ttl int) error {
|
|
|
|
id, err := strconv.Atoi(info.ID)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("CloudXNS: invalid zone ID: %w", err)
|
2018-09-15 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
payload := TXTRecord{
|
|
|
|
ID: id,
|
2018-12-06 21:50:17 +00:00
|
|
|
Host: dns01.UnFqdn(strings.TrimSuffix(fqdn, info.Domain)),
|
2018-09-15 17:26:45 +00:00
|
|
|
Value: value,
|
|
|
|
Type: "TXT",
|
|
|
|
LineID: 1,
|
|
|
|
TTL: ttl,
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("CloudXNS: record unmarshaling error: %w", err)
|
2018-09-15 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.doRequest(http.MethodPost, "record", body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// RemoveTxtRecord remove a TXT record.
|
2018-09-15 17:26:45 +00:00
|
|
|
func (c *Client) RemoveTxtRecord(recordID, zoneID string) error {
|
|
|
|
_, err := c.doRequest(http.MethodDelete, fmt.Sprintf("record/%s/%s", recordID, zoneID), nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) doRequest(method, uri string, body []byte) (json.RawMessage, error) {
|
|
|
|
req, err := c.buildRequest(method, uri, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("CloudXNS: %w", err)
|
2018-09-15 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("CloudXNS: %s", toUnreadableBodyMessage(req, content))
|
|
|
|
}
|
|
|
|
|
|
|
|
var r apiResponse
|
|
|
|
err = json.Unmarshal(content, &r)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("CloudXNS: response unmashaling error: %w: %s", err, toUnreadableBodyMessage(req, content))
|
2018-09-15 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Code != 1 {
|
|
|
|
return nil, fmt.Errorf("CloudXNS: invalid code (%v), error: %s", r.Code, r.Message)
|
|
|
|
}
|
|
|
|
return r.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) buildRequest(method, uri string, body []byte) (*http.Request, error) {
|
|
|
|
url := c.BaseURL + uri
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, url, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("CloudXNS: invalid request: %w", err)
|
2018-09-15 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
requestDate := time.Now().Format(time.RFC1123Z)
|
|
|
|
|
|
|
|
req.Header.Set("API-KEY", c.apiKey)
|
|
|
|
req.Header.Set("API-REQUEST-DATE", requestDate)
|
|
|
|
req.Header.Set("API-HMAC", c.hmac(url, requestDate, string(body)))
|
|
|
|
req.Header.Set("API-FORMAT", "json")
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) hmac(url, date, body string) string {
|
|
|
|
sum := md5.Sum([]byte(c.apiKey + url + body + date + c.secretKey))
|
|
|
|
return hex.EncodeToString(sum[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|