freemyip.go raw
1 // Package freemyip implements a DNS provider for solving the DNS-01 challenge using freemyip.com.
2 package freemyip
3
4 import (
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "time"
10
11 "github.com/go-acme/lego/v4/challenge"
12 "github.com/go-acme/lego/v4/challenge/dns01"
13 "github.com/go-acme/lego/v4/platform/config/env"
14 "github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
15 "github.com/nrdcg/freemyip"
16 )
17
18 // Environment variables names.
19 const (
20 envNamespace = "FREEMYIP_"
21
22 EnvToken = envNamespace + "TOKEN"
23
24 EnvTTL = envNamespace + "TTL"
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 TTL int
40 HTTPClient *http.Client
41 }
42
43 // NewDefaultConfig returns a default configuration for the DNSProvider.
44 func NewDefaultConfig() *Config {
45 return &Config{
46 TTL: env.GetOrDefaultInt(EnvTTL, 3600),
47 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
48 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
49 SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout),
50 HTTPClient: &http.Client{
51 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
52 },
53 }
54 }
55
56 // DNSProvider implements the challenge.Provider interface.
57 type DNSProvider struct {
58 config *Config
59 client *freemyip.Client
60 }
61
62 // NewDNSProvider returns a DNSProvider instance configured for freemyip.com.
63 // Credentials must be passed in the environment variable: FREEMYIP_TOKEN.
64 func NewDNSProvider() (*DNSProvider, error) {
65 values, err := env.Get(EnvToken)
66 if err != nil {
67 return nil, fmt.Errorf("freemyip: %w", err)
68 }
69
70 config := NewDefaultConfig()
71 config.Token = values[EnvToken]
72
73 return NewDNSProviderConfig(config)
74 }
75
76 // NewDNSProviderConfig return a DNSProvider instance configured for freemyip.com.
77 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
78 if config == nil {
79 return nil, errors.New("freemyip: the configuration of the DNS provider is nil")
80 }
81
82 if config.Token == "" {
83 return nil, errors.New("freemyip: missing credentials")
84 }
85
86 client := freemyip.New(config.Token, true)
87
88 if config.HTTPClient != nil {
89 client.HTTPClient = config.HTTPClient
90 }
91
92 client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
93
94 return &DNSProvider{
95 config: config,
96 client: client,
97 }, 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.config.PropagationTimeout, d.config.PollingInterval
104 }
105
106 // Sequential All DNS challenges for this provider will be resolved sequentially.
107 // Returns the interval between each iteration.
108 func (d *DNSProvider) Sequential() time.Duration {
109 return d.config.SequenceInterval
110 }
111
112 // Present creates a TXT record using the specified parameters.
113 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
114 info := dns01.GetChallengeInfo(domain, keyAuth)
115
116 subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, freemyip.RootDomain)
117 if err != nil {
118 return fmt.Errorf("freemyip: %w", err)
119 }
120
121 _, err = d.client.EditTXTRecord(context.Background(), subDomain, info.Value)
122 if err != nil {
123 return fmt.Errorf("freemyip: %w", err)
124 }
125
126 return nil
127 }
128
129 // CleanUp removes the TXT record matching the specified parameters.
130 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
131 info := dns01.GetChallengeInfo(domain, keyAuth)
132
133 subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, freemyip.RootDomain)
134 if err != nil {
135 return fmt.Errorf("freemyip: %w", err)
136 }
137
138 _, err = d.client.DeleteTXTRecord(context.Background(), subDomain)
139 if err != nil {
140 return fmt.Errorf("freemyip: %w", err)
141 }
142
143 return nil
144 }
145