forked from TrueCloudLab/lego
d4200c6d4c
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
142 lines
3.1 KiB
Go
142 lines
3.1 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
|
)
|
|
|
|
const defaultBaseURL = "https://ipv64.net"
|
|
|
|
const authorizationHeader = "Authorization"
|
|
|
|
type Client struct {
|
|
apiKey string
|
|
|
|
baseURL *url.URL
|
|
HTTPClient *http.Client
|
|
}
|
|
|
|
func NewClient(apiKey string) *Client {
|
|
baseURL, _ := url.Parse(defaultBaseURL)
|
|
|
|
return &Client{
|
|
apiKey: apiKey,
|
|
baseURL: baseURL,
|
|
HTTPClient: &http.Client{Timeout: 15 * time.Second},
|
|
}
|
|
}
|
|
|
|
func (c Client) GetDomains(ctx context.Context) (*Domains, error) {
|
|
endpoint := c.baseURL.JoinPath("api")
|
|
|
|
query := endpoint.Query()
|
|
query.Set("get_domains", "")
|
|
endpoint.RawQuery = query.Encode()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
|
}
|
|
|
|
results := &Domains{}
|
|
|
|
err = c.do(req, results)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
func (c Client) AddRecord(ctx context.Context, domain, prefix, recordType, content string) error {
|
|
endpoint := c.baseURL.JoinPath("api")
|
|
|
|
data := make(url.Values)
|
|
data.Set("add_record", domain)
|
|
data.Set("praefix", prefix)
|
|
data.Set("type", recordType)
|
|
data.Set("content", content)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create request: %w", err)
|
|
}
|
|
|
|
return c.do(req, nil)
|
|
}
|
|
|
|
func (c Client) DeleteRecord(ctx context.Context, domain, prefix, recordType, content string) error {
|
|
endpoint := c.baseURL.JoinPath("api")
|
|
|
|
data := make(url.Values)
|
|
data.Set("del_record", domain)
|
|
data.Set("praefix", prefix)
|
|
data.Set("type", recordType)
|
|
data.Set("content", content)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, endpoint.String(), strings.NewReader(data.Encode()))
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create request: %w", err)
|
|
}
|
|
|
|
return c.do(req, nil)
|
|
}
|
|
|
|
func (c Client) do(req *http.Request, result any) error {
|
|
req.Header.Set(authorizationHeader, fmt.Sprintf("Bearer %s", c.apiKey))
|
|
|
|
if req.Method != http.MethodGet {
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
}
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return errutils.NewHTTPDoError(req, err)
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode/100 != 2 {
|
|
return parseError(req, resp)
|
|
}
|
|
|
|
if result == nil {
|
|
return nil
|
|
}
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
|
}
|
|
|
|
if string(raw) == "null" {
|
|
return fmt.Errorf("unexpected response: %s", string(raw))
|
|
}
|
|
|
|
err = json.Unmarshal(raw, result)
|
|
if err != nil {
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func parseError(req *http.Request, resp *http.Response) error {
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
|
|
errAPI := &APIError{}
|
|
err := json.Unmarshal(raw, errAPI)
|
|
if err != nil {
|
|
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
|
|
}
|
|
|
|
return errAPI
|
|
}
|