com35.go raw
1 // Package com35 implements a DNS provider for solving the DNS-01 challenge using 35.com/三五互联.
2 package com35
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/platform/config/env"
12 "github.com/go-acme/lego/v4/providers/dns/internal/westcn"
13 )
14
15 // Environment variables names.
16 const (
17 envNamespace = "COM35_"
18
19 EnvUsername = envNamespace + "USERNAME"
20 EnvPassword = envNamespace + "PASSWORD"
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://api.35.cn/api/v2"
29
30 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
31
32 // Config is used to configure the creation of the DNSProvider.
33 type Config = westcn.Config
34
35 // NewDefaultConfig returns a default configuration for the DNSProvider.
36 func NewDefaultConfig() *Config {
37 return &Config{
38 TTL: env.GetOrDefaultInt(EnvTTL, 60),
39 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
40 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second),
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 35.com/三五互联.
53 func NewDNSProvider() (*DNSProvider, error) {
54 values, err := env.Get(EnvUsername, EnvPassword)
55 if err != nil {
56 return nil, fmt.Errorf("35com: %w", err)
57 }
58
59 config := NewDefaultConfig()
60 config.Username = values[EnvUsername]
61 config.Password = values[EnvPassword]
62
63 return NewDNSProviderConfig(config)
64 }
65
66 // NewDNSProviderConfig return a DNSProvider instance configured for 35.com/三五互联.
67 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
68 if config == nil {
69 return nil, errors.New("35com: the configuration of the DNS provider is nil")
70 }
71
72 provider, err := westcn.NewDNSProviderConfig(config, defaultBaseURL)
73 if err != nil {
74 return nil, fmt.Errorf("35com: %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("35com: %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("35com: %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