2021-06-14 00:44:13 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2021-06-14 00:44:13 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2021-06-14 00:44:13 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-08-25 09:44:11 +00:00
|
|
|
"os"
|
2021-06-14 00:44:13 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unicode"
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2021-06-14 00:44:13 +00:00
|
|
|
querystring "github.com/google/go-querystring/query"
|
|
|
|
)
|
|
|
|
|
|
|
|
const baseURL = "https://api.internet.bs"
|
|
|
|
|
|
|
|
// status SUCCESS, PENDING, FAILURE.
|
|
|
|
const statusSuccess = "SUCCESS"
|
|
|
|
|
|
|
|
// Client is the API client.
|
|
|
|
type Client struct {
|
|
|
|
apiKey string
|
|
|
|
password string
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
debug bool
|
|
|
|
|
|
|
|
baseURL *url.URL
|
|
|
|
HTTPClient *http.Client
|
2021-06-14 00:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new Client.
|
|
|
|
func NewClient(apiKey string, password string) *Client {
|
|
|
|
baseURL, _ := url.Parse(baseURL)
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
apiKey: apiKey,
|
|
|
|
password: password,
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 10 * time.Second},
|
2021-06-14 00:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRecord The command is intended to add a new DNS record to a specific zone (domain).
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c Client) AddRecord(ctx context.Context, query RecordQuery) error {
|
2021-06-14 00:44:13 +00:00
|
|
|
var r APIResponse
|
2023-05-05 07:49:38 +00:00
|
|
|
err := c.doRequest(ctx, "Add", query, &r)
|
2021-06-14 00:44:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Status != statusSuccess {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRecord The command is intended to remove a DNS record from a specific zone.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c Client) RemoveRecord(ctx context.Context, query RecordQuery) error {
|
2021-06-14 00:44:13 +00:00
|
|
|
var r APIResponse
|
2023-05-05 07:49:38 +00:00
|
|
|
err := c.doRequest(ctx, "Remove", query, &r)
|
2021-06-14 00:44:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Status != statusSuccess {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRecords The command is intended to retrieve the list of DNS records for a specific domain.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c Client) ListRecords(ctx context.Context, query ListRecordQuery) ([]Record, error) {
|
2021-06-14 00:44:13 +00:00
|
|
|
var l ListResponse
|
2023-05-05 07:49:38 +00:00
|
|
|
err := c.doRequest(ctx, "List", query, &l)
|
2021-06-14 00:44:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Status != statusSuccess {
|
|
|
|
return nil, l.APIResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
return l.Records, nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c Client) doRequest(ctx context.Context, action string, params any, result any) error {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("Domain", "DnsRecord", action)
|
2021-06-14 00:44:13 +00:00
|
|
|
|
|
|
|
values, err := querystring.Values(params)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parse query parameters: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
values.Set("apiKey", c.apiKey)
|
|
|
|
values.Set("password", c.password)
|
|
|
|
values.Set("ResponseFormat", "JSON")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(values.Encode()))
|
2021-06-14 00:44:13 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("unable to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2021-06-14 00:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode/100 != 2 {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2021-06-14 00:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.debug {
|
2023-05-05 07:49:38 +00:00
|
|
|
return dump(endpoint, resp, result)
|
2021-06-14 00:44:13 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(raw, result)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-06-14 00:44:13 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func dump(endpoint *url.URL, resp *http.Response, response any) error {
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2021-06-14 00:44:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.FieldsFunc(endpoint.Path, func(r rune) bool {
|
|
|
|
return !unicode.IsLetter(r) && !unicode.IsNumber(r)
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = os.WriteFile(filepath.Join("fixtures", strings.Join(fields, "_")+".json"), raw, 0o666)
|
2021-06-14 00:44:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return json.Unmarshal(raw, response)
|
2021-06-14 00:44:13 +00:00
|
|
|
}
|