2019-03-11 18:56:48 +02:00
|
|
|
package internal
|
2018-09-08 14:08:07 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 09:49:38 +02:00
|
|
|
"context"
|
2018-09-08 14:08:07 +02:00
|
|
|
"encoding/json"
|
2020-02-27 20:14:46 +02:00
|
|
|
"errors"
|
2018-09-08 14:08:07 +02:00
|
|
|
"fmt"
|
2021-08-25 11:44:11 +02:00
|
|
|
"io"
|
2018-09-08 14:08:07 +02:00
|
|
|
"net/http"
|
2018-09-15 19:07:24 +02:00
|
|
|
"time"
|
2023-05-05 09:49:38 +02:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2018-09-08 14:08:07 +02:00
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// defaultBaseURL for reaching the jSON-based API-Endpoint of netcup.
|
2018-09-15 19:07:24 +02:00
|
|
|
const defaultBaseURL = "https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON"
|
2018-09-08 14:08:07 +02:00
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Client netcup DNS client.
|
2018-09-08 14:08:07 +02:00
|
|
|
type Client struct {
|
|
|
|
customerNumber string
|
|
|
|
apiKey string
|
|
|
|
apiPassword string
|
2023-05-05 09:49:38 +02:00
|
|
|
|
|
|
|
baseURL string
|
|
|
|
HTTPClient *http.Client
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewClient creates a netcup DNS client.
|
2020-07-10 01:48:18 +02:00
|
|
|
func NewClient(customerNumber, apiKey, apiPassword string) (*Client, error) {
|
2018-10-23 02:01:13 +02:00
|
|
|
if customerNumber == "" || apiKey == "" || apiPassword == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("credentials missing")
|
2018-10-23 02:01:13 +02:00
|
|
|
}
|
|
|
|
|
2018-09-08 14:08:07 +02:00
|
|
|
return &Client{
|
|
|
|
customerNumber: customerNumber,
|
|
|
|
apiKey: apiKey,
|
|
|
|
apiPassword: apiPassword,
|
2023-05-05 09:49:38 +02:00
|
|
|
baseURL: defaultBaseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 10 * time.Second},
|
2018-10-23 02:01:13 +02:00
|
|
|
}, nil
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// UpdateDNSRecord performs an update of the DNSRecords as specified by the netcup WSDL.
|
2018-09-08 14:08:07 +02:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
2023-05-05 09:49:38 +02:00
|
|
|
func (c *Client) UpdateDNSRecord(ctx context.Context, domainName string, records []DNSRecord) error {
|
2018-09-08 14:08:07 +02:00
|
|
|
payload := &Request{
|
|
|
|
Action: "updateDnsRecords",
|
2018-10-23 02:01:13 +02:00
|
|
|
Param: UpdateDNSRecordsRequest{
|
2018-09-08 14:08:07 +02:00
|
|
|
DomainName: domainName,
|
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
2023-05-05 09:49:38 +02:00
|
|
|
APISessionID: getSessionID(ctx),
|
2018-09-08 14:08:07 +02:00
|
|
|
ClientRequestID: "",
|
2018-10-23 02:01:13 +02:00
|
|
|
DNSRecordSet: DNSRecordSet{DNSRecords: records},
|
2018-09-08 14:08:07 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err := c.doRequest(ctx, payload, nil)
|
2018-09-08 14:08:07 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("error when sending the request: %w", err)
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDNSRecords retrieves all dns records of an DNS-Zone as specified by the netcup WSDL
|
2020-05-08 19:35:25 +02:00
|
|
|
// returns an array of DNSRecords.
|
2018-09-08 14:08:07 +02:00
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
2023-05-05 09:49:38 +02:00
|
|
|
func (c *Client) GetDNSRecords(ctx context.Context, hostname string) ([]DNSRecord, error) {
|
2018-09-08 14:08:07 +02:00
|
|
|
payload := &Request{
|
|
|
|
Action: "infoDnsRecords",
|
2018-10-23 02:01:13 +02:00
|
|
|
Param: InfoDNSRecordsRequest{
|
2018-09-08 14:08:07 +02:00
|
|
|
DomainName: hostname,
|
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
2023-05-05 09:49:38 +02:00
|
|
|
APISessionID: getSessionID(ctx),
|
2018-09-08 14:08:07 +02:00
|
|
|
ClientRequestID: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-23 02:01:13 +02:00
|
|
|
var responseData InfoDNSRecordsResponse
|
2023-05-05 09:49:38 +02:00
|
|
|
err := c.doRequest(ctx, payload, &responseData)
|
2018-09-08 14:08:07 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("error when sending the request: %w", err)
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:01:13 +02:00
|
|
|
return responseData.DNSRecords, nil
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:01:13 +02:00
|
|
|
// doRequest marshals given body to JSON, send the request to netcup API
|
2020-05-08 19:35:25 +02:00
|
|
|
// and returns body of response.
|
2023-05-05 09:49:38 +02:00
|
|
|
func (c *Client) doRequest(ctx context.Context, payload, result any) error {
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, c.baseURL, payload)
|
2018-09-08 14:08:07 +02:00
|
|
|
if err != nil {
|
2018-10-23 02:01:13 +02:00
|
|
|
return err
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:01:13 +02:00
|
|
|
req.Close = true
|
2018-09-08 14:08:07 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2018-09-08 14:08:07 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode >= http.StatusMultipleChoices {
|
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
respMsg, err := unmarshalResponseMsg(req, resp)
|
2018-09-08 14:08:07 +02:00
|
|
|
if err != nil {
|
2018-10-23 02:01:13 +02:00
|
|
|
return err
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:01:13 +02:00
|
|
|
if respMsg.Status != success {
|
|
|
|
return respMsg
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(respMsg.ResponseData, result)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, respMsg.ResponseData, err)
|
2018-10-23 02:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
// GetDNSRecordIdx searches a given array of DNSRecords for a given DNSRecord
|
|
|
|
// equivalence is determined by Destination and RecortType attributes
|
|
|
|
// returns index of given DNSRecord in given array of DNSRecords.
|
|
|
|
func GetDNSRecordIdx(records []DNSRecord, record DNSRecord) (int, error) {
|
|
|
|
for index, element := range records {
|
|
|
|
if record.Destination == element.Destination && record.RecordType == element.RecordType {
|
|
|
|
return index, nil
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
2023-05-05 09:49:38 +02:00
|
|
|
}
|
|
|
|
return -1, errors.New("no DNS Record found")
|
|
|
|
}
|
2018-10-23 02:01:13 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
func newJSONRequest(ctx context.Context, method string, endpoint string, payload any) (*http.Request, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
2018-10-23 02:01:13 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
if payload != nil {
|
|
|
|
err := json.NewEncoder(buf).Encode(payload)
|
2018-10-23 02:01:13 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
|
2018-10-23 02:01:13 +02:00
|
|
|
}
|
2023-05-05 09:49:38 +02:00
|
|
|
}
|
2018-10-23 02:01:13 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint, buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
2018-10-23 02:01:13 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
req.Header.Set("Accept", "application/json")
|
2018-09-08 14:08:07 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
if payload != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2018-10-23 02:01:13 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
return req, nil
|
|
|
|
}
|
2018-10-23 02:01:13 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
func unmarshalResponseMsg(req *http.Request, resp *http.Response) (*ResponseMsg, error) {
|
2021-08-25 11:44:11 +02:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2018-10-23 02:01:13 +02:00
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|
2018-10-23 02:01:13 +02:00
|
|
|
|
|
|
|
var respMsg ResponseMsg
|
|
|
|
err = json.Unmarshal(raw, &respMsg)
|
|
|
|
if err != nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2018-10-23 02:01:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &respMsg, nil
|
2018-09-08 14:08:07 +02:00
|
|
|
}
|