From ad86f963be26c1c78154303f8eec86864bc6f32b Mon Sep 17 00:00:00 2001 From: fulder Date: Thu, 18 Jun 2020 13:27:59 +0200 Subject: [PATCH 1/3] Add Proxy to WebsocketOptions Signed-off-by: Michal Sadowski --- websocket.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/websocket.go b/websocket.go index 5370d2f0..dd2aaf2b 100644 --- a/websocket.go +++ b/websocket.go @@ -5,6 +5,7 @@ import ( "io" "net" "net/http" + "net/url" "sync" "time" @@ -15,8 +16,11 @@ import ( type WebsocketOptions struct { ReadBufferSize int WriteBufferSize int + Proxy ProxyFunction } +type ProxyFunction func(req *http.Request) (*url.URL, error) + // NewWebsocket returns a new websocket and returns a net.Conn compatible interface using the gorilla/websocket package func NewWebsocket(host string, tlsc *tls.Config, timeout time.Duration, requestHeader http.Header, options *WebsocketOptions) (net.Conn, error) { if timeout == 0 { @@ -27,9 +31,11 @@ func NewWebsocket(host string, tlsc *tls.Config, timeout time.Duration, requestH // Apply default options options = &WebsocketOptions{} } - + if options.Proxy == nil { + options.Proxy = http.ProxyFromEnvironment + } dialer := &websocket.Dialer{ - Proxy: http.ProxyFromEnvironment, + Proxy: options.Proxy, HandshakeTimeout: timeout, EnableCompression: false, TLSClientConfig: tlsc, From 39c2d721455ec40482ab9201a13ced434cd5e783 Mon Sep 17 00:00:00 2001 From: fulder Date: Thu, 18 Jun 2020 13:28:36 +0200 Subject: [PATCH 2/3] Log response info duriung websocket handshake failure Signed-off-by: Michal Sadowski --- websocket.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/websocket.go b/websocket.go index dd2aaf2b..bdae2a7a 100644 --- a/websocket.go +++ b/websocket.go @@ -2,6 +2,7 @@ package mqtt import ( "crypto/tls" + "fmt" "io" "net" "net/http" @@ -44,8 +45,11 @@ func NewWebsocket(host string, tlsc *tls.Config, timeout time.Duration, requestH WriteBufferSize: options.WriteBufferSize, } - ws, _, err := dialer.Dial(host, requestHeader) + ws, resp, err := dialer.Dial(host, requestHeader) + if resp != nil { + WARN.Println(CLI, fmt.Sprintf("Websocket handshake failure. StatusCode: %d. Body: %s", resp.StatusCode, resp.Body)) + } if err != nil { return nil, err } From 4c64ea82d6ee01f8dd7359006a769f9051ad04b0 Mon Sep 17 00:00:00 2001 From: fulder Date: Thu, 18 Jun 2020 14:55:32 +0200 Subject: [PATCH 3/3] Log websocket handshake failure only during error Signed-off-by: Michal Sadowski --- websocket.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/websocket.go b/websocket.go index bdae2a7a..9fa41ce3 100644 --- a/websocket.go +++ b/websocket.go @@ -47,10 +47,10 @@ func NewWebsocket(host string, tlsc *tls.Config, timeout time.Duration, requestH ws, resp, err := dialer.Dial(host, requestHeader) - if resp != nil { - WARN.Println(CLI, fmt.Sprintf("Websocket handshake failure. StatusCode: %d. Body: %s", resp.StatusCode, resp.Body)) - } if err != nil { + if resp != nil { + WARN.Println(CLI, fmt.Sprintf("Websocket handshake failure. StatusCode: %d. Body: %s", resp.StatusCode, resp.Body)) + } return nil, err }