2020-05-28 14:57:29 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2020-05-28 14:57:29 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2023-05-05 07:49:38 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2020-05-28 14:57:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// defaultBaseURL represents the API endpoint to call.
|
2022-11-28 16:15:56 +00:00
|
|
|
const defaultBaseURL = "https://napi.arvancloud.ir"
|
2020-05-28 14:57:29 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
const authorizationHeader = "Authorization"
|
2020-05-28 14:57:29 +00:00
|
|
|
|
|
|
|
// Client the ArvanCloud client.
|
|
|
|
type Client struct {
|
|
|
|
apiKey string
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
baseURL *url.URL
|
|
|
|
HTTPClient *http.Client
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
// NewClient Creates a new Client.
|
2020-05-28 14:57:29 +00:00
|
|
|
func NewClient(apiKey string) *Client {
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL, _ := url.Parse(defaultBaseURL)
|
|
|
|
|
2020-05-28 14:57:29 +00:00
|
|
|
return &Client{
|
|
|
|
apiKey: apiKey,
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxtRecord gets a TXT record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetTxtRecord(ctx context.Context, domain, name, value string) (*DNSRecord, error) {
|
|
|
|
records, err := c.getRecords(ctx, domain, name)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
if equalsTXTRecord(record, name, value) {
|
|
|
|
return &record, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("could not find record: Domain: %s; Record: %s", domain, name)
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:15:56 +00:00
|
|
|
// https://www.arvancloud.ir/docs/api/cdn/4.0#operation/dns_records.list
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) getRecords(ctx context.Context, domain, search string) ([]DNSRecord, error) {
|
|
|
|
endpoint := c.baseURL.JoinPath("cdn", "4.0", "domains", domain, "dns-records")
|
2020-05-28 14:57:29 +00:00
|
|
|
|
|
|
|
if search != "" {
|
|
|
|
query := endpoint.Query()
|
|
|
|
query.Set("search", strings.ReplaceAll(search, "_", ""))
|
|
|
|
endpoint.RawQuery = query.Encode()
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
response := &apiResponse[[]DNSRecord]{}
|
|
|
|
err = c.do(req, http.StatusOK, response)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("could not get records %s: Domain: %s: %w", search, domain, err)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return response.Data, nil
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRecord creates a DNS record.
|
2022-11-28 16:15:56 +00:00
|
|
|
// https://www.arvancloud.ir/docs/api/cdn/4.0#operation/dns_records.create
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) CreateRecord(ctx context.Context, domain string, record DNSRecord) (*DNSRecord, error) {
|
|
|
|
endpoint := c.baseURL.JoinPath("cdn", "4.0", "domains", domain, "dns-records")
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
response := &apiResponse[*DNSRecord]{}
|
|
|
|
err = c.do(req, http.StatusCreated, response)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("could not create record; Domain: %s: %w", domain, err)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return response.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRecord deletes a DNS record.
|
|
|
|
// https://www.arvancloud.ir/docs/api/cdn/4.0#operation/dns_records.remove
|
|
|
|
func (c *Client) DeleteRecord(ctx context.Context, domain, id string) error {
|
|
|
|
endpoint := c.baseURL.JoinPath("cdn", "4.0", "domains", domain, "dns-records", id)
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return err
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, http.StatusOK, nil)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("could not delete record %s; Domain: %s: %w", id, domain, err)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-28 14:57:29 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) do(req *http.Request, expectedStatus int, result any) error {
|
|
|
|
req.Header.Set(authorizationHeader, c.apiKey)
|
2020-05-28 14:57:29 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2020-05-28 14:57:29 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if resp.StatusCode != expectedStatus {
|
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.Unmarshal(raw, result)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2020-05-28 14:57:29 +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)
|
|
|
|
}
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req.Header.Set("Accept", "application/json")
|
2020-05-28 14:57:29 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if payload != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return req, nil
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func equalsTXTRecord(record DNSRecord, name, value string) bool {
|
|
|
|
if record.Type != "txt" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if record.Name != name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
data, ok := record.Value.(map[string]any)
|
2020-05-28 14:57:29 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return data["text"] == value
|
|
|
|
}
|