dns_config.go RawHistoryFolder
1package publictls
2
3import (
4 "fmt"
5 "net"
6 "strconv"
7 "strings"
8 "time"
9
10 "github.com/bnema/gordon/internal/domain"
11)
12
13const (
14 DefaultDNSPropagationTimeout = 5 * time.Minute
15 DefaultDNSPollingInterval = 5 * time.Second
16)
17
18var DefaultDNSResolvers = []string{"1.1.1.1:53", "8.8.8.8:53"}
19
20// DNSConfig controls public DNS visibility checks used by ACME DNS-01.
21// These are recursive resolvers, not authoritative DNS providers.
22type DNSConfig struct {
23 Resolvers []string
24 PropagationTimeout time.Duration
25 PollingInterval time.Duration
26}
27
28// DefaultDNSConfig returns Gordon's default public DNS check configuration.
29func DefaultDNSConfig() DNSConfig {
30 resolvers := append([]string(nil), DefaultDNSResolvers...)
31 return DNSConfig{
32 Resolvers: resolvers,
33 PropagationTimeout: DefaultDNSPropagationTimeout,
34 PollingInterval: DefaultDNSPollingInterval,
35 }
36}
37
38// Validate checks DNSConfig for ACME DNS-01 usage and normalizes resolver whitespace.
39func (c *DNSConfig) Validate() error {
40 if len(c.Resolvers) == 0 {
41 return fmt.Errorf("%w: dns.resolvers must contain at least one resolver", domain.ErrDNSConfigInvalid)
42 }
43 for i, resolver := range c.Resolvers {
44 trimmed := strings.TrimSpace(resolver)
45 if trimmed == "" {
46 return fmt.Errorf("%w: invalid dns.resolvers[%d]: empty resolver", domain.ErrDNSConfigInvalid, i)
47 }
48 host, port, err := net.SplitHostPort(trimmed)
49 if err != nil {
50 return fmt.Errorf("%w: invalid dns.resolvers[%d]: %q: expected host:port: %v", domain.ErrDNSConfigInvalid, i, trimmed, err)
51 }
52 if host == "" {
53 return fmt.Errorf("%w: invalid dns.resolvers[%d]: %q: host must not be empty", domain.ErrDNSConfigInvalid, i, trimmed)
54 }
55 portNumber, err := strconv.Atoi(port)
56 if err != nil || portNumber < 1 || portNumber > 65535 {
57 return fmt.Errorf("%w: invalid dns.resolvers[%d]: %q: port must be a number between 1 and 65535", domain.ErrDNSConfigInvalid, i, trimmed)
58 }
59 c.Resolvers[i] = trimmed
60 }
61 if c.PropagationTimeout <= 0 {
62 return fmt.Errorf("%w: dns.propagation_timeout must be positive", domain.ErrDNSConfigInvalid)
63 }
64 if c.PollingInterval <= 0 {
65 return fmt.Errorf("%w: dns.polling_interval must be positive", domain.ErrDNSConfigInvalid)
66 }
67 if c.PollingInterval >= c.PropagationTimeout {
68 return fmt.Errorf("%w: dns.polling_interval must be less than dns.propagation_timeout", domain.ErrDNSConfigInvalid)
69 }
70 return nil
71}