2023-01-27 09:28:38 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2023-01-27 09:28:38 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2023-01-27 09:28:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
removeAction = "rm"
|
|
|
|
addAction = "add"
|
|
|
|
)
|
|
|
|
|
|
|
|
const successCode = "successfully"
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://www.dnshome.de/dyndns.php"
|
|
|
|
|
|
|
|
// Client the dnsHome.de client.
|
|
|
|
type Client struct {
|
|
|
|
baseURL string
|
2023-05-05 07:49:38 +00:00
|
|
|
HTTPClient *http.Client
|
2023-01-27 09:28:38 +00:00
|
|
|
|
|
|
|
credentials map[string]string
|
|
|
|
credMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient Creates a new Client.
|
|
|
|
func NewClient(credentials map[string]string) *Client {
|
|
|
|
return &Client{
|
|
|
|
HTTPClient: &http.Client{Timeout: 10 * time.Second},
|
|
|
|
baseURL: defaultBaseURL,
|
|
|
|
credentials: credentials,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a TXT record.
|
|
|
|
// only one TXT record for ACME is allowed, so it will update the "current" TXT record.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) Add(ctx context.Context, hostname, value string) error {
|
2023-01-27 09:28:38 +00:00
|
|
|
domain := strings.TrimPrefix(hostname, "_acme-challenge.")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.doAction(ctx, domain, addAction, value)
|
2023-01-27 09:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes a TXT record.
|
|
|
|
// only one TXT record for ACME is allowed, so it will remove "all" the TXT records.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) Remove(ctx context.Context, hostname, value string) error {
|
2023-01-27 09:28:38 +00:00
|
|
|
domain := strings.TrimPrefix(hostname, "_acme-challenge.")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.doAction(ctx, domain, removeAction, value)
|
2023-01-27 09:28:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) doAction(ctx context.Context, domain, action, value string) error {
|
|
|
|
endpoint, err := c.createEndpoint(domain, action, value)
|
2023-01-27 09:28:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), http.NoBody)
|
2023-01-27 09:28:38 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("unable to create request: %w", err)
|
2023-01-27 09:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2023-01-27 09:28:38 +00:00
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
|
2023-01-27 09:28:38 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2023-01-27 09:28:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2023-01-27 09:28:38 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2023-01-27 09:28:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
output := string(raw)
|
2023-01-27 09:28:38 +00:00
|
|
|
|
|
|
|
if !strings.HasPrefix(output, successCode) {
|
|
|
|
return errors.New(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
func (c *Client) createEndpoint(domain, action, value string) (*url.URL, error) {
|
|
|
|
if len(value) < 12 {
|
|
|
|
return nil, fmt.Errorf("the TXT value must have more than 12 characters: %s", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint, err := url.Parse(c.baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.credMu.Lock()
|
|
|
|
password, ok := c.credentials[domain]
|
|
|
|
c.credMu.Unlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("domain %s not found in credentials, check your credentials map", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint.User = url.UserPassword(domain, password)
|
|
|
|
|
|
|
|
query := endpoint.Query()
|
|
|
|
query.Set("acme", action)
|
|
|
|
query.Set("txt", value)
|
|
|
|
endpoint.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
return endpoint, nil
|
|
|
|
}
|