From 47f4627f1f432d2e77ba457cfe5c0f00dd13697a Mon Sep 17 00:00:00 2001 From: Patrick Pacher Date: Wed, 8 Apr 2020 20:48:49 +0200 Subject: [PATCH 1/2] Close HTTP response body before client reuse --- netenv/online-status.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/netenv/online-status.go b/netenv/online-status.go index a9880844..dd17c05f 100644 --- a/netenv/online-status.go +++ b/netenv/online-status.go @@ -326,6 +326,8 @@ func checkOnlineStatus(ctx context.Context) { // this might be a weird captive portal, just direct the user there updateOnlineStatus(StatusPortal, "detectportal.firefox.com", "http request succeeded, response content not as expected") } + // close the body now as we plan to re-uise the http.Client + response.Body.Close() // 3) try a https request @@ -339,7 +341,7 @@ func checkOnlineStatus(ctx context.Context) { response, err = client.Do(request) if err != nil { // if we fail, something is really weird - updateOnlineStatus(StatusSemiOnline, "", "http request failed") + updateOnlineStatus(StatusSemiOnline, "", "http request failed to "+parsedHTTPSTestURL.String()+" with error "+err.Error()) return } defer response.Body.Close() From ab233ae992ca0d3f63198044c86da4be31a8c0ea Mon Sep 17 00:00:00 2001 From: Patrick Pacher Date: Thu, 9 Apr 2020 10:58:42 +0200 Subject: [PATCH 2/2] Fix online-check reusing the local address --- netenv/online-status.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/netenv/online-status.go b/netenv/online-status.go index dd17c05f..0af161cc 100644 --- a/netenv/online-status.go +++ b/netenv/online-status.go @@ -204,6 +204,7 @@ func monitorOnlineStatus(ctx context.Context) error { timeout := time.Minute if GetOnlineStatus() != StatusOnline { timeout = time.Second + log.Debugf("checking online status again in %s because current status is %s", timeout, GetOnlineStatus()) } // wait for trigger select { @@ -271,13 +272,15 @@ func checkOnlineStatus(ctx context.Context) { // TODO: find (array of) alternatives to detectportal.firefox.com // TODO: find something about usage terms of detectportal.firefox.com + dialer := &net.Dialer{ + Timeout: 5 * time.Second, + LocalAddr: getLocalAddr("tcp"), + DualStack: true, + } + client := &http.Client{ Transport: &http.Transport{ - DialContext: (&net.Dialer{ - Timeout: 5 * time.Second, - LocalAddr: getLocalAddr("tcp"), - DualStack: true, - }).DialContext, + DialContext: dialer.DialContext, DisableKeepAlives: true, DisableCompression: true, WriteBufferSize: 1024, @@ -330,6 +333,7 @@ func checkOnlineStatus(ctx context.Context) { response.Body.Close() // 3) try a https request + dialer.LocalAddr = getLocalAddr("tcp") request = (&http.Request{ Method: "HEAD",