lego/providers/dns/joker/internal/svc/client.go

83 lines
1.8 KiB
Go
Raw Permalink Normal View History

2020-10-08 14:52:50 +00:00
// Package svc Client for the SVC API.
// https://joker.com/faq/content/6/496/en/let_s-encrypt-support.html
package svc
import (
2023-05-05 07:49:38 +00:00
"context"
2020-10-08 14:52:50 +00:00
"fmt"
2021-08-25 09:44:11 +00:00
"io"
2020-10-08 14:52:50 +00:00
"net/http"
"strings"
2023-05-05 07:49:38 +00:00
"time"
2020-10-08 14:52:50 +00:00
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
2020-10-08 14:52:50 +00:00
querystring "github.com/google/go-querystring/query"
)
const defaultBaseURL = "https://svc.joker.com/nic/replace"
type request struct {
Username string `url:"username"`
Password string `url:"password"`
Zone string `url:"zone"`
Label string `url:"label"`
Type string `url:"type"`
Value string `url:"value"`
}
type Client struct {
username string
password string
2023-05-05 07:49:38 +00:00
BaseURL string
HTTPClient *http.Client
2020-10-08 14:52:50 +00:00
}
func NewClient(username, password string) *Client {
return &Client{
username: username,
password: password,
2023-05-05 07:49:38 +00:00
BaseURL: defaultBaseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
2020-10-08 14:52:50 +00:00
}
}
2023-05-05 07:49:38 +00:00
func (c *Client) SendRequest(ctx context.Context, zone, label, value string) error {
payload := request{
2020-10-08 14:52:50 +00:00
Username: c.username,
Password: c.password,
Zone: zone,
Label: label,
Type: "TXT",
Value: value,
}
2023-05-05 07:49:38 +00:00
v, err := querystring.Values(payload)
2020-10-08 14:52:50 +00:00
if err != nil {
return err
}
2023-05-05 07:49:38 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL, strings.NewReader(v.Encode()))
2020-10-08 14:52:50 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("unable to create request: %w", err)
2020-10-08 14:52:50 +00:00
}
2023-05-05 07:49:38 +00:00
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.HTTPClient.Do(req)
2020-10-08 14:52:50 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return errutils.NewReadResponseError(req, resp.StatusCode, err)
2020-10-08 14:52:50 +00:00
}
2023-05-05 07:49:38 +00:00
if resp.StatusCode == http.StatusOK && strings.HasPrefix(string(raw), "OK") {
2020-10-08 14:52:50 +00:00
return nil
}
2023-05-05 07:49:38 +00:00
return fmt.Errorf("error: %d: %s", resp.StatusCode, string(raw))
2020-10-08 14:52:50 +00:00
}