traefik/vendor/github.com/xenolf/lego/providers/dns/namecheap/namecheap.go

271 lines
7.3 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
// Package namecheap implements a DNS provider for solving the DNS-01
// challenge using namecheap DNS.
package namecheap
import (
2018-09-17 13:16:03 +00:00
"errors"
2017-02-07 21:33:23 +00:00
"fmt"
"io/ioutil"
"net/http"
2018-09-17 13:16:03 +00:00
"strconv"
2017-02-07 21:33:23 +00:00
"strings"
"time"
2018-05-31 07:30:04 +00:00
"github.com/xenolf/lego/acme"
2018-07-03 10:44:04 +00:00
"github.com/xenolf/lego/log"
"github.com/xenolf/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
)
// Notes about namecheap's tool API:
// 1. Using the API requires registration. Once registered, use your account
// name and API key to access the API.
// 2. There is no API to add or modify a single DNS record. Instead you must
// read the entire list of records, make modifications, and then write the
// entire updated list of records. (Yuck.)
// 3. Namecheap's DNS updates can be slow to propagate. I've seen them take
// as long as an hour.
// 4. Namecheap requires you to whitelist the IP address from which you call
// its APIs. It also requires all API calls to include the whitelisted IP
// address as a form or query string value. This code uses a namecheap
// service to query the client's IP address.
2018-09-17 13:16:03 +00:00
const (
2017-02-07 21:33:23 +00:00
defaultBaseURL = "https://api.namecheap.com/xml.response"
getIPURL = "https://dynamicdns.park-your-domain.com/getip"
)
2018-09-17 13:16:03 +00:00
// A challenge represents all the data needed to specify a dns-01 challenge
// to lets-encrypt.
type challenge struct {
domain string
key string
keyFqdn string
keyValue string
tld string
sld string
host string
}
// Config is used to configure the creation of the DNSProvider
type Config struct {
Debug bool
BaseURL string
APIUser string
APIKey string
ClientIP string
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
HTTPClient *http.Client
}
// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
return &Config{
BaseURL: defaultBaseURL,
Debug: env.GetOrDefaultBool("NAMECHEAP_DEBUG", false),
TTL: env.GetOrDefaultInt("NAMECHEAP_TTL", 120),
PropagationTimeout: env.GetOrDefaultSecond("NAMECHEAP_PROPAGATION_TIMEOUT", 60*time.Minute),
PollingInterval: env.GetOrDefaultSecond("NAMECHEAP_POLLING_INTERVAL", 15*time.Second),
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond("NAMECHEAP_HTTP_TIMEOUT", 60*time.Second),
},
}
}
2017-02-07 21:33:23 +00:00
// DNSProvider is an implementation of the ChallengeProviderTimeout interface
// that uses Namecheap's tool API to manage TXT records for a domain.
type DNSProvider struct {
2018-09-17 13:16:03 +00:00
config *Config
2017-02-07 21:33:23 +00:00
}
// NewDNSProvider returns a DNSProvider instance configured for namecheap.
2018-09-17 13:16:03 +00:00
// Credentials must be passed in the environment variables:
// NAMECHEAP_API_USER and NAMECHEAP_API_KEY.
2017-02-07 21:33:23 +00:00
func NewDNSProvider() (*DNSProvider, error) {
2018-07-03 10:44:04 +00:00
values, err := env.Get("NAMECHEAP_API_USER", "NAMECHEAP_API_KEY")
if err != nil {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("namecheap: %v", err)
2018-07-03 10:44:04 +00:00
}
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.APIUser = values["NAMECHEAP_API_USER"]
config.APIKey = values["NAMECHEAP_API_KEY"]
return NewDNSProviderConfig(config)
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
// NewDNSProviderCredentials uses the supplied credentials
// to return a DNSProvider instance configured for namecheap.
// Deprecated
2017-02-07 21:33:23 +00:00
func NewDNSProviderCredentials(apiUser, apiKey string) (*DNSProvider, error) {
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.APIUser = apiUser
config.APIKey = apiKey
return NewDNSProviderConfig(config)
}
// NewDNSProviderConfig return a DNSProvider instance configured for namecheap.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("namecheap: the configuration of the DNS provider is nil")
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
if config.APIUser == "" || config.APIKey == "" {
return nil, fmt.Errorf("namecheap: credentials missing")
}
2018-07-03 10:44:04 +00:00
2018-09-17 13:16:03 +00:00
if len(config.ClientIP) == 0 {
clientIP, err := getClientIP(config.HTTPClient, config.Debug)
if err != nil {
return nil, fmt.Errorf("namecheap: %v", err)
}
config.ClientIP = clientIP
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
return &DNSProvider{config: config}, nil
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
// Timeout returns the timeout and interval to use when checking for DNS propagation.
// Namecheap can sometimes take a long time to complete an update, so wait up to 60 minutes for the update to propagate.
2017-02-07 21:33:23 +00:00
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
2018-09-17 13:16:03 +00:00
return d.config.PropagationTimeout, d.config.PollingInterval
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
// Present installs a TXT record for the DNS challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
tlds, err := d.getTLDs()
if err != nil {
return fmt.Errorf("namecheap: %v", err)
}
ch, err := newChallenge(domain, keyAuth, tlds)
if err != nil {
return fmt.Errorf("namecheap: %v", err)
}
2018-10-10 14:28:04 +00:00
records, err := d.getHosts(ch.sld, ch.tld)
2018-09-17 13:16:03 +00:00
if err != nil {
return fmt.Errorf("namecheap: %v", err)
}
2018-10-10 14:28:04 +00:00
record := Record{
Name: ch.key,
Type: "TXT",
Address: ch.keyValue,
MXPref: "10",
TTL: strconv.Itoa(d.config.TTL),
}
records = append(records, record)
2018-09-17 13:16:03 +00:00
if d.config.Debug {
2018-10-10 14:28:04 +00:00
for _, h := range records {
log.Printf("%-5.5s %-30.30s %-6s %-70.70s", h.Type, h.Name, h.TTL, h.Address)
2018-09-17 13:16:03 +00:00
}
}
2018-10-10 14:28:04 +00:00
err = d.setHosts(ch.sld, ch.tld, records)
2018-09-17 13:16:03 +00:00
if err != nil {
return fmt.Errorf("namecheap: %v", err)
}
return nil
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
// CleanUp removes a TXT record used for a previous DNS challenge.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
tlds, err := d.getTLDs()
if err != nil {
return fmt.Errorf("namecheap: %v", err)
}
ch, err := newChallenge(domain, keyAuth, tlds)
if err != nil {
return fmt.Errorf("namecheap: %v", err)
}
2018-10-10 14:28:04 +00:00
records, err := d.getHosts(ch.sld, ch.tld)
2018-09-17 13:16:03 +00:00
if err != nil {
return fmt.Errorf("namecheap: %v", err)
}
2018-10-10 14:28:04 +00:00
// Find the challenge TXT record and remove it if found.
var found bool
for i, h := range records {
if h.Name == ch.key && h.Type == "TXT" {
records = append(records[:i], records[i+1:]...)
found = true
}
}
if !found {
2018-09-17 13:16:03 +00:00
return nil
}
2018-10-10 14:28:04 +00:00
err = d.setHosts(ch.sld, ch.tld, records)
2018-09-17 13:16:03 +00:00
if err != nil {
return fmt.Errorf("namecheap: %v", err)
}
return nil
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
// getClientIP returns the client's public IP address.
// It uses namecheap's IP discovery service to perform the lookup.
func getClientIP(client *http.Client, debug bool) (addr string, err error) {
2018-07-03 10:44:04 +00:00
resp, err := client.Get(getIPURL)
2017-02-07 21:33:23 +00:00
if err != nil {
return "", err
}
defer resp.Body.Close()
clientIP, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if debug {
2018-07-03 10:44:04 +00:00
log.Println("Client IP:", string(clientIP))
2017-02-07 21:33:23 +00:00
}
return string(clientIP), nil
}
// newChallenge builds a challenge record from a domain name, a challenge
// authentication key, and a map of available TLDs.
func newChallenge(domain, keyAuth string, tlds map[string]string) (*challenge, error) {
2018-05-31 07:30:04 +00:00
domain = acme.UnFqdn(domain)
2017-02-07 21:33:23 +00:00
parts := strings.Split(domain, ".")
// Find the longest matching TLD.
longest := -1
for i := len(parts); i > 0; i-- {
t := strings.Join(parts[i-1:], ".")
if _, found := tlds[t]; found {
longest = i - 1
}
}
if longest < 1 {
2018-05-31 07:30:04 +00:00
return nil, fmt.Errorf("invalid domain name %q", domain)
2017-02-07 21:33:23 +00:00
}
tld := strings.Join(parts[longest:], ".")
sld := parts[longest-1]
var host string
if longest >= 1 {
host = strings.Join(parts[:longest-1], ".")
}
2018-10-10 14:28:04 +00:00
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
2017-02-07 21:33:23 +00:00
return &challenge{
domain: domain,
key: "_acme-challenge." + host,
2018-10-10 14:28:04 +00:00
keyFqdn: fqdn,
keyValue: value,
2017-02-07 21:33:23 +00:00
tld: tld,
sld: sld,
host: host,
}, nil
}