2021-09-29 18:40:35 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2021-09-29 18:40:35 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2021-09-29 18:40:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://usersapiv2.epik.com/v2"
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
// Client the Epik API client.
|
2021-09-29 18:40:35 +00:00
|
|
|
type Client struct {
|
2023-05-05 07:49:38 +00:00
|
|
|
signature string
|
|
|
|
|
2021-09-29 18:40:35 +00:00
|
|
|
baseURL *url.URL
|
2023-05-05 07:49:38 +00:00
|
|
|
HTTPClient *http.Client
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
// NewClient Creates a new Client.
|
2021-09-29 18:40:35 +00:00
|
|
|
func NewClient(signature string) *Client {
|
|
|
|
baseURL, _ := url.Parse(defaultBaseURL)
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
signature: signature,
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDNSRecords gets DNS records for a domain.
|
|
|
|
// https://docs.userapi.epik.com/v2/#/DNS%20Host%20Records/getDnsRecord
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c Client) GetDNSRecords(ctx context.Context, domain string) ([]Record, error) {
|
|
|
|
endpoint := c.createEndpoint(domain, url.Values{})
|
2021-09-29 18:40:35 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var data GetDNSRecordResponse
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, &data)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, err
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data.Data.Records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateHostRecord creates a record for a domain.
|
|
|
|
// https://docs.userapi.epik.com/v2/#/DNS%20Host%20Records/createHostRecord
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c Client) CreateHostRecord(ctx context.Context, domain string, record RecordRequest) (*Data, error) {
|
|
|
|
endpoint := c.createEndpoint(domain, url.Values{})
|
|
|
|
|
2021-09-29 18:40:35 +00:00
|
|
|
payload := CreateHostRecords{Payload: record}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, payload)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
var data Data
|
|
|
|
err = c.do(req, &data)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return &data, nil
|
|
|
|
}
|
2021-09-29 18:40:35 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
// RemoveHostRecord removes a record for a domain.
|
|
|
|
// https://docs.userapi.epik.com/v2/#/DNS%20Host%20Records/removeHostRecord
|
|
|
|
func (c Client) RemoveHostRecord(ctx context.Context, domain string, recordID string) (*Data, error) {
|
|
|
|
params := url.Values{}
|
|
|
|
params.Set("ID", recordID)
|
2021-09-29 18:40:35 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
endpoint := c.createEndpoint(domain, params)
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var data Data
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, &data)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, err
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c Client) do(req *http.Request, result any) error {
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return parseError(req, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
return nil
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.Unmarshal(raw, result)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c Client) createEndpoint(domain string, params url.Values) *url.URL {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("domains", domain, "records")
|
2021-09-29 18:40:35 +00:00
|
|
|
|
|
|
|
params.Set("SIGNATURE", c.signature)
|
|
|
|
endpoint.RawQuery = params.Encode()
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
if payload != nil {
|
2021-09-29 18:40:35 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return req, nil
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func parseError(req *http.Request, resp *http.Response) error {
|
|
|
|
raw, _ := io.ReadAll(resp.Body)
|
2021-09-29 18:40:35 +00:00
|
|
|
|
|
|
|
var apiErr APIError
|
2023-05-05 07:49:38 +00:00
|
|
|
err := json.Unmarshal(raw, &apiErr)
|
2021-09-29 18:40:35 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
|
2021-09-29 18:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &apiErr
|
|
|
|
}
|