From e06278fead22272533a10d147621d323e8f4e08b Mon Sep 17 00:00:00 2001 From: xenolf Date: Sat, 19 Mar 2016 17:47:26 +0100 Subject: [PATCH] Make DO and namecheap providers use a HTTP client with a timeout. --- providers/dns/digitalocean/digitalocean.go | 7 +++++-- providers/dns/namecheap/namecheap.go | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/providers/dns/digitalocean/digitalocean.go b/providers/dns/digitalocean/digitalocean.go index fb3e00de..5aeaf11d 100644 --- a/providers/dns/digitalocean/digitalocean.go +++ b/providers/dns/digitalocean/digitalocean.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "sync" + "time" "github.com/xenolf/lego/acme" ) @@ -64,7 +65,8 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.apiAuthToken)) - resp, err := http.DefaultClient.Do(req) + client := http.Client{Timeout: 30 * time.Second} + resp, err := client.Do(req) if err != nil { return err } @@ -109,7 +111,8 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.apiAuthToken)) - resp, err := http.DefaultClient.Do(req) + client := http.Client{Timeout: 30 * time.Second} + resp, err := client.Do(req) if err != nil { return err } diff --git a/providers/dns/namecheap/namecheap.go b/providers/dns/namecheap/namecheap.go index 55f8a3d1..ab106598 100644 --- a/providers/dns/namecheap/namecheap.go +++ b/providers/dns/namecheap/namecheap.go @@ -32,6 +32,7 @@ var ( debug = false defaultBaseURL = "https://api.namecheap.com/xml.response" getIpURL = "https://dynamicdns.park-your-domain.com/getip" + httpClient = http.Client{Timeout: 60 * time.Second} ) // DNSProvider is an implementation of the ChallengeProviderTimeout interface @@ -93,7 +94,7 @@ type apierror struct { // getClientIP returns the client's public IP address. It uses namecheap's // IP discovery service to perform the lookup. func getClientIP() (addr string, err error) { - resp, err := http.Get(getIpURL) + resp, err := httpClient.Get(getIpURL) if err != nil { return "", err } @@ -179,7 +180,7 @@ func (d *DNSProvider) getTLDs() (tlds map[string]string, err error) { reqURL, _ := url.Parse(d.baseURL) reqURL.RawQuery = values.Encode() - resp, err := http.Get(reqURL.String()) + resp, err := httpClient.Get(reqURL.String()) if err != nil { return nil, err } @@ -228,7 +229,7 @@ func (d *DNSProvider) getHosts(ch *challenge) (hosts []host, err error) { reqURL, _ := url.Parse(d.baseURL) reqURL.RawQuery = values.Encode() - resp, err := http.Get(reqURL.String()) + resp, err := httpClient.Get(reqURL.String()) if err != nil { return nil, err } @@ -278,7 +279,7 @@ func (d *DNSProvider) setHosts(ch *challenge, hosts []host) error { values.Add("TTL"+ind, h.TTL) } - resp, err := http.PostForm(d.baseURL, values) + resp, err := httpClient.PostForm(d.baseURL, values) if err != nil { return err }