git » dnss » commit e62835a

dnstox: Take HTTP client proxy settings from the environment

author Alberto Bertogli
2017-07-05 20:51:40 UTC
committer Alberto Bertogli
2017-07-20 20:42:26 UTC
parent 2c5cfbc5d2a1b5137d17ee4531328a7520440aa6

dnstox: Take HTTP client proxy settings from the environment

This patch makes the HTTP resolver take the semi-standard proxy settings
from the environment.

To do so, it uses http.ProxyFromEnvironment, which looks at the
environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (and their
lowercase variants).

https://golang.org/pkg/net/http/#ProxyFromEnvironment

internal/dnstox/resolver.go +11 -4

diff --git a/internal/dnstox/resolver.go b/internal/dnstox/resolver.go
index 165a1be..f0e6f54 100644
--- a/internal/dnstox/resolver.go
+++ b/internal/dnstox/resolver.go
@@ -133,12 +133,21 @@ func NewHTTPSResolver(upstream, caFile string) *httpsResolver {
 }
 
 func (r *httpsResolver) Init() error {
+	transport := &http.Transport{
+		// Take the semi-standard proxy settings from the environment.
+		Proxy: http.ProxyFromEnvironment,
+	}
+
 	r.client = &http.Client{
 		// Give our HTTP requests 4 second timeouts: DNS usually doesn't wait
 		// that long anyway, but this helps with slow connections.
 		Timeout: 4 * time.Second,
+
+		Transport: transport,
 	}
 
+	// If CAFile is empty, we're ok with the defaults (use the system default
+	// CA database).
 	if r.CAFile == "" {
 		return nil
 	}
@@ -148,10 +157,8 @@ func (r *httpsResolver) Init() error {
 		return err
 	}
 
-	r.client.Transport = &http.Transport{
-		TLSClientConfig: &tls.Config{
-			ClientCAs: pool,
-		},
+	transport.TLSClientConfig = &tls.Config{
+		ClientCAs: pool,
 	}
 
 	return nil