zonomi.go raw

   1  // Package zonomi implements a DNS provider for solving the DNS-01 challenge using Zonomi DNS.
   2  package zonomi
   3  
   4  import (
   5  	"errors"
   6  	"fmt"
   7  	"net/http"
   8  	"time"
   9  
  10  	"github.com/go-acme/lego/v4/challenge"
  11  	"github.com/go-acme/lego/v4/challenge/dns01"
  12  	"github.com/go-acme/lego/v4/platform/config/env"
  13  	"github.com/go-acme/lego/v4/providers/dns/internal/rimuhosting"
  14  )
  15  
  16  // Environment variables names.
  17  const (
  18  	envNamespace = "ZONOMI_"
  19  
  20  	EnvAPIKey = envNamespace + "API_KEY"
  21  
  22  	EnvTTL                = envNamespace + "TTL"
  23  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  24  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  25  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  26  )
  27  
  28  const defaultBaseURL = "https://zonomi.com/app/dns/dyndns.jsp"
  29  
  30  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  31  
  32  // Config is used to configure the creation of the DNSProvider.
  33  type Config = rimuhosting.Config
  34  
  35  // NewDefaultConfig returns a default configuration for the DNSProvider.
  36  func NewDefaultConfig() *Config {
  37  	return &Config{
  38  		TTL:                env.GetOrDefaultInt(EnvTTL, rimuhosting.DefaultTTL),
  39  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  40  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  41  		HTTPClient: &http.Client{
  42  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  43  		},
  44  	}
  45  }
  46  
  47  // DNSProvider implements the challenge.Provider interface.
  48  type DNSProvider struct {
  49  	prv challenge.ProviderTimeout
  50  }
  51  
  52  // NewDNSProvider returns a DNSProvider instance configured for Zonomi.
  53  // Credentials must be passed in the environment variables.
  54  func NewDNSProvider() (*DNSProvider, error) {
  55  	values, err := env.Get(EnvAPIKey)
  56  	if err != nil {
  57  		return nil, fmt.Errorf("zonomi: %w", err)
  58  	}
  59  
  60  	config := NewDefaultConfig()
  61  	config.APIKey = values[EnvAPIKey]
  62  
  63  	return NewDNSProviderConfig(config)
  64  }
  65  
  66  // NewDNSProviderConfig return a DNSProvider instance configured for Zonomi.
  67  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  68  	if config == nil {
  69  		return nil, errors.New("zonomi: the configuration of the DNS provider is nil")
  70  	}
  71  
  72  	provider, err := rimuhosting.NewDNSProviderConfig(config, defaultBaseURL)
  73  	if err != nil {
  74  		return nil, fmt.Errorf("zonomi: %w", err)
  75  	}
  76  
  77  	return &DNSProvider{prv: provider}, nil
  78  }
  79  
  80  // Present creates a TXT record using the specified parameters.
  81  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
  82  	err := d.prv.Present(domain, token, keyAuth)
  83  	if err != nil {
  84  		return fmt.Errorf("zonomi: %w", err)
  85  	}
  86  
  87  	return nil
  88  }
  89  
  90  // CleanUp removes the TXT record matching the specified parameters.
  91  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
  92  	err := d.prv.CleanUp(domain, token, keyAuth)
  93  	if err != nil {
  94  		return fmt.Errorf("zonomi: %w", err)
  95  	}
  96  
  97  	return nil
  98  }
  99  
 100  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 101  // Adjusting here to cope with spikes in propagation times.
 102  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 103  	return d.prv.Timeout()
 104  }
 105