1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 17:21:50 +02:00
lego/providers/dns/regru/internal/client.go

136 lines
3.2 KiB
Go
Raw Normal View History

2020-03-10 12:08:32 +02:00
package internal
import (
2023-05-05 09:49:38 +02:00
"context"
2020-03-10 12:08:32 +02:00
"encoding/json"
"fmt"
2021-08-25 11:44:11 +02:00
"io"
2020-03-10 12:08:32 +02:00
"net/http"
"net/url"
"strings"
2023-05-05 09:49:38 +02:00
"time"
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
2020-03-10 12:08:32 +02:00
)
const defaultBaseURL = "https://api.reg.ru/api/regru2/"
// Client the reg.ru client.
type Client struct {
username string
password string
2023-05-05 09:49:38 +02:00
baseURL *url.URL
2020-03-10 12:08:32 +02:00
HTTPClient *http.Client
}
// NewClient Creates a reg.ru client.
2020-07-10 01:48:18 +02:00
func NewClient(username, password string) *Client {
2023-05-05 09:49:38 +02:00
baseURL, _ := url.Parse(defaultBaseURL)
2020-03-10 12:08:32 +02:00
return &Client{
username: username,
password: password,
2023-05-05 09:49:38 +02:00
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
2020-03-10 12:08:32 +02:00
}
}
// RemoveTxtRecord removes a TXT record.
// https://www.reg.ru/support/help/api2#zone_remove_record
2023-05-05 09:49:38 +02:00
func (c Client) RemoveTxtRecord(ctx context.Context, domain, subDomain, content string) error {
2020-03-10 12:08:32 +02:00
request := RemoveRecordRequest{
2023-05-05 09:49:38 +02:00
Domains: []Domain{{DName: domain}},
2020-03-10 12:08:32 +02:00
SubDomain: subDomain,
Content: content,
RecordType: "TXT",
OutputContentType: "plain",
}
2023-05-05 09:49:38 +02:00
resp, err := c.doRequest(ctx, request, "zone", "remove_record")
2020-03-10 12:08:32 +02:00
if err != nil {
return err
}
return resp.HasError()
}
// AddTXTRecord adds a TXT record.
// https://www.reg.ru/support/help/api2#zone_add_txt
2023-05-05 09:49:38 +02:00
func (c Client) AddTXTRecord(ctx context.Context, domain, subDomain, content string) error {
2020-03-10 12:08:32 +02:00
request := AddTxtRequest{
2023-05-05 09:49:38 +02:00
Domains: []Domain{{DName: domain}},
2020-03-10 12:08:32 +02:00
SubDomain: subDomain,
Text: content,
OutputContentType: "plain",
}
2023-05-05 09:49:38 +02:00
resp, err := c.doRequest(ctx, request, "zone", "add_txt")
2020-03-10 12:08:32 +02:00
if err != nil {
return err
}
return resp.HasError()
}
2023-05-05 09:49:38 +02:00
func (c Client) doRequest(ctx context.Context, request any, fragments ...string) (*APIResponse, error) {
endpoint := c.baseURL.JoinPath(fragments...)
2020-03-10 12:08:32 +02:00
query := endpoint.Query()
query.Set("username", c.username)
query.Set("password", c.password)
endpoint.RawQuery = query.Encode()
2020-03-10 12:08:32 +02:00
inputData, err := json.Marshal(request)
if err != nil {
2023-05-05 09:49:38 +02:00
return nil, fmt.Errorf("failed to create input data: %w", err)
2020-03-10 12:08:32 +02:00
}
data := url.Values{}
data.Set("input_data", string(inputData))
data.Set("input_format", "json")
2020-03-10 12:08:32 +02:00
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
2020-03-10 12:08:32 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return nil, fmt.Errorf("unable to create request: %w", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
2023-05-05 09:49:38 +02:00
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, errutils.NewHTTPDoError(req, err)
2020-03-10 12:08:32 +02:00
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode/100 != 2 {
2023-05-05 09:49:38 +02:00
return nil, parseError(req, resp)
2020-03-10 12:08:32 +02:00
}
2023-05-05 09:49:38 +02:00
raw, err := io.ReadAll(resp.Body)
2020-03-10 12:08:32 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
2020-03-10 12:08:32 +02:00
}
var apiResp APIResponse
2023-05-05 09:49:38 +02:00
err = json.Unmarshal(raw, &apiResp)
2020-03-10 12:08:32 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
2020-03-10 12:08:32 +02:00
}
return &apiResp, nil
}
2023-05-05 09:49:38 +02:00
func parseError(req *http.Request, resp *http.Response) error {
raw, _ := io.ReadAll(resp.Body)
var errAPI APIResponse
err := json.Unmarshal(raw, &errAPI)
2020-03-10 12:08:32 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
2020-03-10 12:08:32 +02:00
}
2023-05-05 09:49:38 +02:00
return fmt.Errorf("status code: %d, %w", resp.StatusCode, errAPI)
2020-03-10 12:08:32 +02:00
}