2015-05-01 22:20:22 +00:00
|
|
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2016-01-15 18:48:03 +00:00
|
|
|
"bytes"
|
2015-05-01 22:20:22 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2016-01-15 18:48:03 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-05-01 22:20:22 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrBadHandshake is returned when the server response to opening handshake is
|
|
|
|
// invalid.
|
|
|
|
var ErrBadHandshake = errors.New("websocket: bad handshake")
|
|
|
|
|
2017-04-18 01:02:51 +00:00
|
|
|
var errInvalidCompression = errors.New("websocket: invalid compression negotiation")
|
|
|
|
|
2015-05-01 22:20:22 +00:00
|
|
|
// NewClient creates a new client connection using the given net connection.
|
|
|
|
// The URL u specifies the host and request URI. Use requestHeader to specify
|
|
|
|
// the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
|
|
|
|
// (Cookie). Use the response.Header to get the selected subprotocol
|
|
|
|
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
|
|
|
|
//
|
|
|
|
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
|
|
|
|
// non-nil *http.Response so that callers can handle redirects, authentication,
|
|
|
|
// etc.
|
2016-01-15 18:48:03 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use Dialer instead.
|
2015-05-01 22:20:22 +00:00
|
|
|
func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
|
2016-01-15 18:48:03 +00:00
|
|
|
d := Dialer{
|
|
|
|
ReadBufferSize: readBufSize,
|
|
|
|
WriteBufferSize: writeBufSize,
|
|
|
|
NetDial: func(net, addr string) (net.Conn, error) {
|
|
|
|
return netConn, nil
|
|
|
|
},
|
2015-05-01 22:20:22 +00:00
|
|
|
}
|
2016-01-15 18:48:03 +00:00
|
|
|
return d.Dial(u.String(), requestHeader)
|
2015-05-01 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Dialer contains options for connecting to WebSocket server.
|
|
|
|
type Dialer struct {
|
|
|
|
// NetDial specifies the dial function for creating TCP connections. If
|
|
|
|
// NetDial is nil, net.Dial is used.
|
|
|
|
NetDial func(network, addr string) (net.Conn, error)
|
|
|
|
|
2016-01-15 18:48:03 +00:00
|
|
|
// Proxy specifies a function to return a proxy for a given
|
|
|
|
// Request. If the function returns a non-nil error, the
|
|
|
|
// request is aborted with the provided error.
|
|
|
|
// If Proxy is nil or returns a nil *URL, no proxy is used.
|
|
|
|
Proxy func(*http.Request) (*url.URL, error)
|
|
|
|
|
2015-05-01 22:20:22 +00:00
|
|
|
// TLSClientConfig specifies the TLS configuration to use with tls.Client.
|
|
|
|
// If nil, the default configuration is used.
|
|
|
|
TLSClientConfig *tls.Config
|
|
|
|
|
|
|
|
// HandshakeTimeout specifies the duration for the handshake to complete.
|
|
|
|
HandshakeTimeout time.Duration
|
|
|
|
|
2017-04-18 01:02:51 +00:00
|
|
|
// ReadBufferSize and WriteBufferSize specify I/O buffer sizes. If a buffer
|
|
|
|
// size is zero, then a useful default size is used. The I/O buffer sizes
|
|
|
|
// do not limit the size of the messages that can be sent or received.
|
2015-05-01 22:20:22 +00:00
|
|
|
ReadBufferSize, WriteBufferSize int
|
|
|
|
|
|
|
|
// Subprotocols specifies the client's requested subprotocols.
|
|
|
|
Subprotocols []string
|
2017-04-18 01:02:51 +00:00
|
|
|
|
|
|
|
// EnableCompression specifies if the client should attempt to negotiate
|
|
|
|
// per message compression (RFC 7692). Setting this value to true does not
|
|
|
|
// guarantee that compression will be supported. Currently only "no context
|
|
|
|
// takeover" modes are supported.
|
|
|
|
EnableCompression bool
|
|
|
|
|
|
|
|
// Jar specifies the cookie jar.
|
|
|
|
// If Jar is nil, cookies are not sent in requests and ignored
|
|
|
|
// in responses.
|
|
|
|
Jar http.CookieJar
|
2015-05-01 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errMalformedURL = errors.New("malformed ws or wss URL")
|
|
|
|
|
|
|
|
func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
|
|
|
|
hostPort = u.Host
|
|
|
|
hostNoPort = u.Host
|
|
|
|
if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
|
|
|
|
hostNoPort = hostNoPort[:i]
|
|
|
|
} else {
|
2016-01-15 18:48:03 +00:00
|
|
|
switch u.Scheme {
|
|
|
|
case "wss":
|
|
|
|
hostPort += ":443"
|
|
|
|
case "https":
|
2015-05-01 22:20:22 +00:00
|
|
|
hostPort += ":443"
|
2016-01-15 18:48:03 +00:00
|
|
|
default:
|
2015-05-01 22:20:22 +00:00
|
|
|
hostPort += ":80"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hostPort, hostNoPort
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:39:27 +00:00
|
|
|
// DefaultDialer is a dialer with all fields set to the default values.
|
2016-01-15 18:48:03 +00:00
|
|
|
var DefaultDialer = &Dialer{
|
2018-05-04 21:39:27 +00:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
HandshakeTimeout: 45 * time.Second,
|
2016-01-15 18:48:03 +00:00
|
|
|
}
|
2015-05-01 22:20:22 +00:00
|
|
|
|
2018-05-04 21:39:27 +00:00
|
|
|
// nilDialer is dialer to use when receiver is nil.
|
|
|
|
var nilDialer Dialer = *DefaultDialer
|
|
|
|
|
2015-05-01 22:20:22 +00:00
|
|
|
// Dial creates a new client connection. Use requestHeader to specify the
|
|
|
|
// origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
|
|
|
|
// Use the response.Header to get the selected subprotocol
|
|
|
|
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
|
|
|
|
//
|
|
|
|
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
|
|
|
|
// non-nil *http.Response so that callers can handle redirects, authentication,
|
2016-01-15 18:48:03 +00:00
|
|
|
// etcetera. The response body may not contain the entire response and does not
|
|
|
|
// need to be closed by the application.
|
2015-05-01 22:20:22 +00:00
|
|
|
func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
|
2016-01-15 18:48:03 +00:00
|
|
|
|
|
|
|
if d == nil {
|
2018-05-04 21:39:27 +00:00
|
|
|
d = &nilDialer
|
2016-01-15 18:48:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
challengeKey, err := generateChallengeKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:39:27 +00:00
|
|
|
u, err := url.Parse(urlStr)
|
2015-05-01 22:20:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:48:03 +00:00
|
|
|
switch u.Scheme {
|
|
|
|
case "ws":
|
|
|
|
u.Scheme = "http"
|
|
|
|
case "wss":
|
|
|
|
u.Scheme = "https"
|
|
|
|
default:
|
|
|
|
return nil, nil, errMalformedURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.User != nil {
|
|
|
|
// User name and password are not allowed in websocket URIs.
|
|
|
|
return nil, nil, errMalformedURL
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &http.Request{
|
|
|
|
Method: "GET",
|
|
|
|
URL: u,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: make(http.Header),
|
|
|
|
Host: u.Host,
|
|
|
|
}
|
|
|
|
|
2017-04-18 01:02:51 +00:00
|
|
|
// Set the cookies present in the cookie jar of the dialer
|
|
|
|
if d.Jar != nil {
|
|
|
|
for _, cookie := range d.Jar.Cookies(u) {
|
|
|
|
req.AddCookie(cookie)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:48:03 +00:00
|
|
|
// Set the request headers using the capitalization for names and values in
|
|
|
|
// RFC examples. Although the capitalization shouldn't matter, there are
|
|
|
|
// servers that depend on it. The Header.Set method is not used because the
|
|
|
|
// method canonicalizes the header names.
|
|
|
|
req.Header["Upgrade"] = []string{"websocket"}
|
|
|
|
req.Header["Connection"] = []string{"Upgrade"}
|
|
|
|
req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
|
|
|
|
req.Header["Sec-WebSocket-Version"] = []string{"13"}
|
|
|
|
if len(d.Subprotocols) > 0 {
|
|
|
|
req.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.Subprotocols, ", ")}
|
|
|
|
}
|
|
|
|
for k, vs := range requestHeader {
|
|
|
|
switch {
|
|
|
|
case k == "Host":
|
|
|
|
if len(vs) > 0 {
|
|
|
|
req.Host = vs[0]
|
|
|
|
}
|
|
|
|
case k == "Upgrade" ||
|
|
|
|
k == "Connection" ||
|
|
|
|
k == "Sec-Websocket-Key" ||
|
|
|
|
k == "Sec-Websocket-Version" ||
|
2017-04-18 01:02:51 +00:00
|
|
|
k == "Sec-Websocket-Extensions" ||
|
2016-01-15 18:48:03 +00:00
|
|
|
(k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
|
|
|
|
return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
|
2018-05-04 21:39:27 +00:00
|
|
|
case k == "Sec-Websocket-Protocol":
|
|
|
|
req.Header["Sec-WebSocket-Protocol"] = vs
|
2016-01-15 18:48:03 +00:00
|
|
|
default:
|
|
|
|
req.Header[k] = vs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 01:02:51 +00:00
|
|
|
if d.EnableCompression {
|
2018-05-04 21:39:27 +00:00
|
|
|
req.Header["Sec-WebSocket-Extensions"] = []string{"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}
|
2015-05-01 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var deadline time.Time
|
|
|
|
if d.HandshakeTimeout != 0 {
|
|
|
|
deadline = time.Now().Add(d.HandshakeTimeout)
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:39:27 +00:00
|
|
|
// Get network dial function.
|
2015-05-01 22:20:22 +00:00
|
|
|
netDial := d.NetDial
|
|
|
|
if netDial == nil {
|
|
|
|
netDialer := &net.Dialer{Deadline: deadline}
|
|
|
|
netDial = netDialer.Dial
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:39:27 +00:00
|
|
|
// If needed, wrap the dial function to set the connection deadline.
|
|
|
|
if !deadline.Equal(time.Time{}) {
|
|
|
|
forwardDial := netDial
|
|
|
|
netDial = func(network, addr string) (net.Conn, error) {
|
|
|
|
c, err := forwardDial(network, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = c.SetDeadline(deadline)
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If needed, wrap the dial function to connect through a proxy.
|
|
|
|
if d.Proxy != nil {
|
|
|
|
proxyURL, err := d.Proxy(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if proxyURL != nil {
|
|
|
|
dialer, err := proxy_FromURL(proxyURL, netDialerFunc(netDial))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
netDial = dialer.Dial
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hostPort, hostNoPort := hostPortNoPort(u)
|
|
|
|
netConn, err := netDial("tcp", hostPort)
|
2015-05-01 22:20:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if netConn != nil {
|
|
|
|
netConn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-01-15 18:48:03 +00:00
|
|
|
if u.Scheme == "https" {
|
2017-04-18 01:02:51 +00:00
|
|
|
cfg := cloneTLSConfig(d.TLSClientConfig)
|
|
|
|
if cfg.ServerName == "" {
|
2015-05-01 22:20:22 +00:00
|
|
|
cfg.ServerName = hostNoPort
|
|
|
|
}
|
|
|
|
tlsConn := tls.Client(netConn, cfg)
|
|
|
|
netConn = tlsConn
|
|
|
|
if err := tlsConn.Handshake(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if !cfg.InsecureSkipVerify {
|
|
|
|
if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:48:03 +00:00
|
|
|
conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize)
|
|
|
|
|
|
|
|
if err := req.Write(netConn); err != nil {
|
|
|
|
return nil, nil, err
|
2015-05-01 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 18:48:03 +00:00
|
|
|
resp, err := http.ReadResponse(conn.br, req)
|
2015-05-01 22:20:22 +00:00
|
|
|
if err != nil {
|
2016-01-15 18:48:03 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-04-18 01:02:51 +00:00
|
|
|
|
|
|
|
if d.Jar != nil {
|
|
|
|
if rc := resp.Cookies(); len(rc) > 0 {
|
|
|
|
d.Jar.SetCookies(u, rc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:48:03 +00:00
|
|
|
if resp.StatusCode != 101 ||
|
|
|
|
!strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") ||
|
|
|
|
!strings.EqualFold(resp.Header.Get("Connection"), "upgrade") ||
|
|
|
|
resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
|
|
|
|
// Before closing the network connection on return from this
|
|
|
|
// function, slurp up some of the response to aid application
|
|
|
|
// debugging.
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
n, _ := io.ReadFull(resp.Body, buf)
|
|
|
|
resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
|
|
|
|
return nil, resp, ErrBadHandshake
|
2015-05-01 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 01:02:51 +00:00
|
|
|
for _, ext := range parseExtensions(resp.Header) {
|
|
|
|
if ext[""] != "permessage-deflate" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, snct := ext["server_no_context_takeover"]
|
|
|
|
_, cnct := ext["client_no_context_takeover"]
|
|
|
|
if !snct || !cnct {
|
|
|
|
return nil, resp, errInvalidCompression
|
|
|
|
}
|
|
|
|
conn.newCompressionWriter = compressNoContextTakeover
|
|
|
|
conn.newDecompressionReader = decompressNoContextTakeover
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:48:03 +00:00
|
|
|
resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
|
|
|
|
conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
|
|
|
|
|
2015-05-01 22:20:22 +00:00
|
|
|
netConn.SetDeadline(time.Time{})
|
|
|
|
netConn = nil // to avoid close in defer.
|
|
|
|
return conn, resp, nil
|
|
|
|
}
|