2019-03-11 18:56:48 +02:00
|
|
|
package internal
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-02-27 20:14:46 +02:00
|
|
|
"errors"
|
2018-09-15 19:07:24 +02:00
|
|
|
"fmt"
|
2018-10-19 11:49:17 +02:00
|
|
|
"io/ioutil"
|
2018-09-15 19:07:24 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Domain holds the DNSMadeEasy API representation of a Domain.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Domain struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Record holds the DNSMadeEasy API representation of a Domain Record.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Record struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
TTL int `json:"ttl"`
|
|
|
|
SourceID int `json:"sourceId"`
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:49:17 +02:00
|
|
|
type recordsResponse struct {
|
|
|
|
Records *[]Record `json:"data"`
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Client DNSMadeEasy client.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Client struct {
|
|
|
|
apiKey string
|
|
|
|
apiSecret string
|
|
|
|
BaseURL string
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewClient creates a DNSMadeEasy client.
|
2020-07-10 01:48:18 +02:00
|
|
|
func NewClient(apiKey, apiSecret string) (*Client, error) {
|
2018-09-15 19:07:24 +02:00
|
|
|
if apiKey == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("credentials missing: API key")
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if apiSecret == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("credentials missing: API secret")
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
apiKey: apiKey,
|
|
|
|
apiSecret: apiSecret,
|
|
|
|
HTTPClient: &http.Client{},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// GetDomain gets a domain.
|
2018-09-15 19:07:24 +02:00
|
|
|
func (c *Client) GetDomain(authZone string) (*Domain, error) {
|
|
|
|
domainName := authZone[0 : len(authZone)-1]
|
|
|
|
resource := fmt.Sprintf("%s%s", "/dns/managed/name?domainname=", domainName)
|
|
|
|
|
|
|
|
resp, err := c.sendRequest(http.MethodGet, resource, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
domain := &Domain{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// GetRecords gets all TXT records.
|
2018-09-15 19:07:24 +02:00
|
|
|
func (c *Client) GetRecords(domain *Domain, recordName, recordType string) (*[]Record, error) {
|
|
|
|
resource := fmt.Sprintf("%s/%d/%s%s%s%s", "/dns/managed", domain.ID, "records?recordName=", recordName, "&type=", recordType)
|
|
|
|
|
|
|
|
resp, err := c.sendRequest(http.MethodGet, resource, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
records := &recordsResponse{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&records)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return records.Records, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// CreateRecord creates a TXT records.
|
2018-09-15 19:07:24 +02:00
|
|
|
func (c *Client) CreateRecord(domain *Domain, record *Record) error {
|
|
|
|
url := fmt.Sprintf("%s/%d/%s", "/dns/managed", domain.ID, "records")
|
|
|
|
|
|
|
|
resp, err := c.sendRequest(http.MethodPost, url, record)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DeleteRecord deletes a TXT records.
|
2018-09-15 19:07:24 +02:00
|
|
|
func (c *Client) DeleteRecord(record Record) error {
|
|
|
|
resource := fmt.Sprintf("%s/%d/%s/%d", "/dns/managed", record.SourceID, "records", record.ID)
|
|
|
|
|
|
|
|
resp, err := c.sendRequest(http.MethodDelete, resource, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) sendRequest(method, resource string, payload interface{}) (*http.Response, error) {
|
|
|
|
url := fmt.Sprintf("%s%s", c.BaseURL, resource)
|
|
|
|
|
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp := time.Now().UTC().Format(time.RFC1123)
|
|
|
|
signature, err := computeHMAC(timestamp, c.apiSecret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, url, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("x-dnsme-apiKey", c.apiKey)
|
|
|
|
req.Header.Set("x-dnsme-requestDate", timestamp)
|
|
|
|
req.Header.Set("x-dnsme-hmac", signature)
|
|
|
|
req.Header.Set("accept", "application/json")
|
|
|
|
req.Header.Set("content-type", "application/json")
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode > 299 {
|
2018-10-19 11:49:17 +02:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("request failed with HTTP status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("request failed with HTTP status code %d: %s", resp.StatusCode, string(body))
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-07-10 01:48:18 +02:00
|
|
|
func computeHMAC(message, secret string) (string, error) {
|
2018-09-15 19:07:24 +02:00
|
|
|
key := []byte(secret)
|
|
|
|
h := hmac.New(sha1.New, key)
|
|
|
|
_, err := h.Write([]byte(message))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
|
|
}
|