duckdns.go raw

   1  // Package duckdns implements a DNS provider for solving the DNS-01 challenge using DuckDNS.
   2  // See http://www.duckdns.org/spec.jsp for more info on updating TXT records.
   3  package duckdns
   4  
   5  import (
   6  	"context"
   7  	"errors"
   8  	"fmt"
   9  	"net/http"
  10  	"time"
  11  
  12  	"github.com/go-acme/lego/v4/challenge"
  13  	"github.com/go-acme/lego/v4/challenge/dns01"
  14  	"github.com/go-acme/lego/v4/platform/config/env"
  15  	"github.com/go-acme/lego/v4/providers/dns/duckdns/internal"
  16  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  17  )
  18  
  19  // Environment variables names.
  20  const (
  21  	envNamespace = "DUCKDNS_"
  22  
  23  	EnvToken = envNamespace + "TOKEN"
  24  
  25  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  26  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  27  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  28  	EnvSequenceInterval   = envNamespace + "SEQUENCE_INTERVAL"
  29  )
  30  
  31  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  32  
  33  // Config is used to configure the creation of the DNSProvider.
  34  type Config struct {
  35  	Token              string
  36  	PropagationTimeout time.Duration
  37  	PollingInterval    time.Duration
  38  	SequenceInterval   time.Duration
  39  	HTTPClient         *http.Client
  40  }
  41  
  42  // NewDefaultConfig returns a default configuration for the DNSProvider.
  43  func NewDefaultConfig() *Config {
  44  	return &Config{
  45  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  46  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  47  		SequenceInterval:   env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout),
  48  		HTTPClient: &http.Client{
  49  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  50  		},
  51  	}
  52  }
  53  
  54  // DNSProvider implements the challenge.Provider interface.
  55  type DNSProvider struct {
  56  	config *Config
  57  	client *internal.Client
  58  }
  59  
  60  // NewDNSProvider returns a new DNS provider using
  61  // environment variable DUCKDNS_TOKEN for adding and removing the DNS record.
  62  func NewDNSProvider() (*DNSProvider, error) {
  63  	values, err := env.Get(EnvToken)
  64  	if err != nil {
  65  		return nil, fmt.Errorf("duckdns: %w", err)
  66  	}
  67  
  68  	config := NewDefaultConfig()
  69  	config.Token = values[EnvToken]
  70  
  71  	return NewDNSProviderConfig(config)
  72  }
  73  
  74  // NewDNSProviderConfig return a DNSProvider instance configured for DuckDNS.
  75  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  76  	if config == nil {
  77  		return nil, errors.New("duckdns: the configuration of the DNS provider is nil")
  78  	}
  79  
  80  	if config.Token == "" {
  81  		return nil, errors.New("duckdns: credentials missing")
  82  	}
  83  
  84  	client := internal.NewClient(config.Token)
  85  
  86  	if config.HTTPClient != nil {
  87  		client.HTTPClient = config.HTTPClient
  88  	}
  89  
  90  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
  91  
  92  	return &DNSProvider{config: config, client: client}, nil
  93  }
  94  
  95  // Present creates a TXT record to fulfill the dns-01 challenge.
  96  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
  97  	info := dns01.GetChallengeInfo(domain, keyAuth)
  98  	return d.client.AddTXTRecord(context.Background(), dns01.UnFqdn(info.EffectiveFQDN), info.Value)
  99  }
 100  
 101  // CleanUp clears DuckDNS TXT record.
 102  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 103  	info := dns01.GetChallengeInfo(domain, keyAuth)
 104  	return d.client.RemoveTXTRecord(context.Background(), dns01.UnFqdn(info.EffectiveFQDN))
 105  }
 106  
 107  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 108  // Adjusting here to cope with spikes in propagation times.
 109  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 110  	return d.config.PropagationTimeout, d.config.PollingInterval
 111  }
 112  
 113  // Sequential All DNS challenges for this provider will be resolved sequentially.
 114  // Returns the interval between each iteration.
 115  func (d *DNSProvider) Sequential() time.Duration {
 116  	return d.config.SequenceInterval
 117  }
 118