From 86dc5c7241ca338ac30faf6d65e758ef861dba66 Mon Sep 17 00:00:00 2001 From: Cedric Bail Date: Mon, 24 Jan 2022 16:55:25 -0700 Subject: [PATCH] Add support for wasm by switching to nhooyr/websocket library. --- NOTICE.md | 9 ++-- README.md | 6 +-- go.mod | 2 +- go.sum | 65 +++++++++++++++++++++++++++- websocket.go | 104 --------------------------------------------- websocket_js.go | 45 ++++++++++++++++++++ websocket_notjs.go | 62 +++++++++++++++++++++++++++ 7 files changed, 179 insertions(+), 114 deletions(-) create mode 100644 websocket_js.go create mode 100644 websocket_notjs.go diff --git a/NOTICE.md b/NOTICE.md index 10c4a1c..f4c5b9f 100644 --- a/NOTICE.md +++ b/NOTICE.md @@ -62,10 +62,11 @@ Go Sync * License: BSD 3-Clause style license and patent grant. * Project: https://cs.opensource.google/go/x/sync/ -Gorilla Websockets v1.4.2 +Nhooyr Websocket v1.8.7 + +* License: MIT +* Project: https://github.com/nhooyr/websocket -* License: BSD 2-Clause "Simplified" License -* Project: https://github.com/gorilla/websocket ## Cryptography @@ -74,4 +75,4 @@ may have restrictions on the import, possession, and use, and/or re-export to another country, of encryption software. BEFORE using any encryption software, please check the country's laws, regulations and policies concerning the import, possession, or use, and re-export of encryption software, to see if this is -permitted. \ No newline at end of file +permitted. diff --git a/README.md b/README.md index 63a7f44..a5c17d2 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ go get github.com/eclipse/paho.mqtt.golang ``` The client depends on Google's [proxy](https://godoc.org/golang.org/x/net/proxy) package and the -[websockets](https://godoc.org/github.com/gorilla/websocket) package, also easily installed with the commands: +[websockets](https://godoc.org/github.com/nhooyr/websocket) package, also easily installed with the commands: ``` -go get github.com/gorilla/websocket +go get github.com/nhooyr/websocket go get golang.org/x/net/proxy ``` @@ -193,4 +193,4 @@ Discussion of the Paho clients takes place on the [Eclipse paho-dev mailing list General questions about the MQTT protocol are discussed in the [MQTT Google Group](https://groups.google.com/forum/?hl=en-US&fromgroups#!forum/mqtt). -There is much more information available via the [MQTT community site](http://mqtt.org). \ No newline at end of file +There is much more information available via the [MQTT community site](http://mqtt.org). diff --git a/go.mod b/go.mod index beb1991..eb1e1a8 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/eclipse/paho.mqtt.golang go 1.14 require ( - github.com/gorilla/websocket v1.4.2 golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c + nhooyr.io/websocket v1.8.7 ) diff --git a/go.sum b/go.sum index 4ce755b..c888d1b 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,71 @@ -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= +github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= +github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0= +github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= +github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8= +github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo= +github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls= +github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8= +github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U= golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= +nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= diff --git a/websocket.go b/websocket.go index e0f2583..bdfae41 100644 --- a/websocket.go +++ b/websocket.go @@ -14,16 +14,8 @@ package mqtt import ( - "crypto/tls" - "fmt" - "io" - "net" "net/http" "net/url" - "sync" - "time" - - "github.com/gorilla/websocket" ) // WebsocketOptions are config options for a websocket dialer @@ -34,99 +26,3 @@ type WebsocketOptions struct { } 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 { - timeout = 10 * time.Second - } - - if options == nil { - // Apply default options - options = &WebsocketOptions{} - } - if options.Proxy == nil { - options.Proxy = http.ProxyFromEnvironment - } - dialer := &websocket.Dialer{ - Proxy: options.Proxy, - HandshakeTimeout: timeout, - EnableCompression: false, - TLSClientConfig: tlsc, - Subprotocols: []string{"mqtt"}, - ReadBufferSize: options.ReadBufferSize, - WriteBufferSize: options.WriteBufferSize, - } - - ws, resp, err := dialer.Dial(host, requestHeader) - - 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 - } - - wrapper := &websocketConnector{ - Conn: ws, - } - return wrapper, err -} - -// websocketConnector is a websocket wrapper so it satisfies the net.Conn interface so it is a -// drop in replacement of the golang.org/x/net/websocket package. -// Implementation guide taken from https://github.com/gorilla/websocket/issues/282 -type websocketConnector struct { - *websocket.Conn - r io.Reader - rio sync.Mutex - wio sync.Mutex -} - -// SetDeadline sets both the read and write deadlines -func (c *websocketConnector) SetDeadline(t time.Time) error { - if err := c.SetReadDeadline(t); err != nil { - return err - } - err := c.SetWriteDeadline(t) - return err -} - -// Write writes data to the websocket -func (c *websocketConnector) Write(p []byte) (int, error) { - c.wio.Lock() - defer c.wio.Unlock() - - err := c.WriteMessage(websocket.BinaryMessage, p) - if err != nil { - return 0, err - } - return len(p), nil -} - -// Read reads the current websocket frame -func (c *websocketConnector) Read(p []byte) (int, error) { - c.rio.Lock() - defer c.rio.Unlock() - for { - if c.r == nil { - // Advance to next message. - var err error - _, c.r, err = c.NextReader() - if err != nil { - return 0, err - } - } - n, err := c.r.Read(p) - if err == io.EOF { - // At end of message. - c.r = nil - if n > 0 { - return n, nil - } - // No data read, continue to next message. - continue - } - return n, err - } -} diff --git a/websocket_js.go b/websocket_js.go new file mode 100644 index 0000000..0eac338 --- /dev/null +++ b/websocket_js.go @@ -0,0 +1,45 @@ +/* + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * https://www.eclipse.org/legal/epl-2.0/ + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + */ + +package mqtt + +import ( + "context" + "crypto/tls" + "fmt" + "net" + "net/http" + "time" + + "nhooyr.io/websocket" +) + +// NewWebsocket returns a new websocket and returns a net.Conn compatible interface using the gorilla/websocket package +func NewWebsocket(host string, _ *tls.Config, _ time.Duration, _ http.Header, _ *WebsocketOptions) (net.Conn, error) { + dialOptions := websocket.DialOptions{ + Subprotocols: []string{"mqtt"}, + } + + ctx := context.Background() + + ws, resp, err := websocket.Dial(ctx, host, &dialOptions) + + 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 + } + + return websocket.NetConn(ctx, ws, websocket.MessageBinary), nil +} diff --git a/websocket_notjs.go b/websocket_notjs.go new file mode 100644 index 0000000..afc8375 --- /dev/null +++ b/websocket_notjs.go @@ -0,0 +1,62 @@ +//go:build !js +// +build !js + +/* + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * https://www.eclipse.org/legal/epl-2.0/ + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + */ + +package mqtt + +import ( + "context" + "crypto/tls" + "fmt" + "net" + "net/http" + "time" + + "nhooyr.io/websocket" +) + +// 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 { + timeout = 10 * time.Second + } + + if options == nil { + // Apply default options + options = &WebsocketOptions{} + } + + httpTransport := &http.Transport{TLSClientConfig: tlsc, Proxy: options.Proxy} + httpClient := &http.Client{Transport: httpTransport, Timeout: timeout} + + dialOptions := websocket.DialOptions{ + HTTPClient: httpClient, + HTTPHeader: requestHeader, + Subprotocols: []string{"mqtt"}, + } + + ctx := context.Background() + + ws, resp, err := websocket.Dial(ctx, host, &dialOptions) + + 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 + } + + return websocket.NetConn(ctx, ws, websocket.MessageBinary), nil +}