From a27032f09e572dc92fbc69690e519a98164d0634 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 21 Feb 2018 15:56:04 +0100 Subject: [PATCH] cmd/strelaysrv: Don't patch the default HTTP client (fixes #4745) --- cmd/strelaysrv/main.go | 18 ++++++++++++------ cmd/strelaysrv/pool.go | 3 +-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/strelaysrv/main.go b/cmd/strelaysrv/main.go index 8afc73c1f..3a666349c 100644 --- a/cmd/strelaysrv/main.go +++ b/cmd/strelaysrv/main.go @@ -86,6 +86,12 @@ var ( pprofEnabled bool ) +// httpClient is the HTTP client we use for outbound requests. It has a +// timeout and may get further options set during initialization. +var httpClient = &http.Client{ + Timeout: 30 * time.Second, +} + func main() { log.SetFlags(log.Lshortfile | log.LstdFlags) @@ -129,14 +135,14 @@ func main() { if err != nil { log.Fatal(err) } + if laddr.IP != nil && !laddr.IP.IsUnspecified() { + // We bind to a specific address. Our outgoing HTTP requests should + // also come from that address. laddr.Port = 0 - transport, ok := http.DefaultTransport.(*http.Transport) - if ok { - transport.Dial = (&net.Dialer{ - Timeout: 30 * time.Second, - LocalAddr: laddr, - }).Dial + boundDialer := &net.Dialer{LocalAddr: laddr} + httpClient.Transport = &http.Transport{ + DialContext: boundDialer.DialContext, } } diff --git a/cmd/strelaysrv/pool.go b/cmd/strelaysrv/pool.go index 4a92ef347..56f85e268 100644 --- a/cmd/strelaysrv/pool.go +++ b/cmd/strelaysrv/pool.go @@ -7,7 +7,6 @@ import ( "encoding/json" "io/ioutil" "log" - "net/http" "net/url" "time" ) @@ -27,7 +26,7 @@ func poolHandler(pool string, uri *url.URL, mapping mapping) { uriCopy.String(), }) - resp, err := http.Post(pool, "application/json", &b) + resp, err := httpClient.Post(pool, "application/json", &b) if err != nil { log.Println("Error joining pool", pool, err) } else if resp.StatusCode == 500 {