You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

435 lines
13 KiB

  1. // Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package websocket
  5. import (
  6. "bytes"
  7. "context"
  8. "crypto/tls"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "io/ioutil"
  13. "net"
  14. "net/http"
  15. "net/http/httptrace"
  16. "net/url"
  17. "strings"
  18. "time"
  19. )
  20. // ErrBadHandshake is returned when the server response to opening handshake is
  21. // invalid.
  22. var ErrBadHandshake = errors.New("websocket: bad handshake")
  23. var errInvalidCompression = errors.New("websocket: invalid compression negotiation")
  24. // NewClient creates a new client connection using the given net connection.
  25. // The URL u specifies the host and request URI. Use requestHeader to specify
  26. // the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
  27. // (Cookie). Use the response.Header to get the selected subprotocol
  28. // (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
  29. //
  30. // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
  31. // non-nil *http.Response so that callers can handle redirects, authentication,
  32. // etc.
  33. //
  34. // Deprecated: Use Dialer instead.
  35. func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
  36. d := Dialer{
  37. ReadBufferSize: readBufSize,
  38. WriteBufferSize: writeBufSize,
  39. NetDial: func(net, addr string) (net.Conn, error) {
  40. return netConn, nil
  41. },
  42. }
  43. return d.Dial(u.String(), requestHeader)
  44. }
  45. // A Dialer contains options for connecting to WebSocket server.
  46. //
  47. // It is safe to call Dialer's methods concurrently.
  48. type Dialer struct {
  49. // NetDial specifies the dial function for creating TCP connections. If
  50. // NetDial is nil, net.Dial is used.
  51. NetDial func(network, addr string) (net.Conn, error)
  52. // NetDialContext specifies the dial function for creating TCP connections. If
  53. // NetDialContext is nil, NetDial is used.
  54. NetDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
  55. // NetDialTLSContext specifies the dial function for creating TLS/TCP connections. If
  56. // NetDialTLSContext is nil, NetDialContext is used.
  57. // If NetDialTLSContext is set, Dial assumes the TLS handshake is done there and
  58. // TLSClientConfig is ignored.
  59. NetDialTLSContext func(ctx context.Context, network, addr string) (net.Conn, error)
  60. // Proxy specifies a function to return a proxy for a given
  61. // Request. If the function returns a non-nil error, the
  62. // request is aborted with the provided error.
  63. // If Proxy is nil or returns a nil *URL, no proxy is used.
  64. Proxy func(*http.Request) (*url.URL, error)
  65. // TLSClientConfig specifies the TLS configuration to use with tls.Client.
  66. // If nil, the default configuration is used.
  67. // If either NetDialTLS or NetDialTLSContext are set, Dial assumes the TLS handshake
  68. // is done there and TLSClientConfig is ignored.
  69. TLSClientConfig *tls.Config
  70. // HandshakeTimeout specifies the duration for the handshake to complete.
  71. HandshakeTimeout time.Duration
  72. // ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. If a buffer
  73. // size is zero, then a useful default size is used. The I/O buffer sizes
  74. // do not limit the size of the messages that can be sent or received.
  75. ReadBufferSize, WriteBufferSize int
  76. // WriteBufferPool is a pool of buffers for write operations. If the value
  77. // is not set, then write buffers are allocated to the connection for the
  78. // lifetime of the connection.
  79. //
  80. // A pool is most useful when the application has a modest volume of writes
  81. // across a large number of connections.
  82. //
  83. // Applications should use a single pool for each unique value of
  84. // WriteBufferSize.
  85. WriteBufferPool BufferPool
  86. // Subprotocols specifies the client's requested subprotocols.
  87. Subprotocols []string
  88. // EnableCompression specifies if the client should attempt to negotiate
  89. // per message compression (RFC 7692). Setting this value to true does not
  90. // guarantee that compression will be supported. Currently only "no context
  91. // takeover" modes are supported.
  92. EnableCompression bool
  93. // Jar specifies the cookie jar.
  94. // If Jar is nil, cookies are not sent in requests and ignored
  95. // in responses.
  96. Jar http.CookieJar
  97. }
  98. // Dial creates a new client connection by calling DialContext with a background context.
  99. func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
  100. return d.DialContext(context.Background(), urlStr, requestHeader)
  101. }
  102. var errMalformedURL = errors.New("malformed ws or wss URL")
  103. func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
  104. hostPort = u.Host
  105. hostNoPort = u.Host
  106. if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
  107. hostNoPort = hostNoPort[:i]
  108. } else {
  109. switch u.Scheme {
  110. case "wss":
  111. hostPort += ":443"
  112. case "https":
  113. hostPort += ":443"
  114. default:
  115. hostPort += ":80"
  116. }
  117. }
  118. return hostPort, hostNoPort
  119. }
  120. // DefaultDialer is a dialer with all fields set to the default values.
  121. var DefaultDialer = &Dialer{
  122. Proxy: http.ProxyFromEnvironment,
  123. HandshakeTimeout: 45 * time.Second,
  124. }
  125. // nilDialer is dialer to use when receiver is nil.
  126. var nilDialer = *DefaultDialer
  127. // DialContext creates a new client connection. Use requestHeader to specify the
  128. // origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
  129. // Use the response.Header to get the selected subprotocol
  130. // (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
  131. //
  132. // The context will be used in the request and in the Dialer.
  133. //
  134. // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
  135. // non-nil *http.Response so that callers can handle redirects, authentication,
  136. // etcetera. The response body may not contain the entire response and does not
  137. // need to be closed by the application.
  138. func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
  139. if d == nil {
  140. d = &nilDialer
  141. }
  142. challengeKey, err := generateChallengeKey()
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. u, err := url.Parse(urlStr)
  147. if err != nil {
  148. return nil, nil, err
  149. }
  150. switch u.Scheme {
  151. case "ws":
  152. u.Scheme = "http"
  153. case "wss":
  154. u.Scheme = "https"
  155. default:
  156. return nil, nil, errMalformedURL
  157. }
  158. if u.User != nil {
  159. // User name and password are not allowed in websocket URIs.
  160. return nil, nil, errMalformedURL
  161. }
  162. req := &http.Request{
  163. Method: http.MethodGet,
  164. URL: u,
  165. Proto: "HTTP/1.1",
  166. ProtoMajor: 1,
  167. ProtoMinor: 1,
  168. Header: make(http.Header),
  169. Host: u.Host,
  170. }
  171. req = req.WithContext(ctx)
  172. // Set the cookies present in the cookie jar of the dialer
  173. if d.Jar != nil {
  174. for _, cookie := range d.Jar.Cookies(u) {
  175. req.AddCookie(cookie)
  176. }
  177. }
  178. // Set the request headers using the capitalization for names and values in
  179. // RFC examples. Although the capitalization shouldn't matter, there are
  180. // servers that depend on it. The Header.Set method is not used because the
  181. // method canonicalizes the header names.
  182. req.Header["Upgrade"] = []string{"websocket"}
  183. req.Header["Connection"] = []string{"Upgrade"}
  184. req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
  185. req.Header["Sec-WebSocket-Version"] = []string{"13"}
  186. if len(d.Subprotocols) > 0 {
  187. req.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.Subprotocols, ", ")}
  188. }
  189. for k, vs := range requestHeader {
  190. switch {
  191. case k == "Host":
  192. if len(vs) > 0 {
  193. req.Host = vs[0]
  194. }
  195. case k == "Upgrade" ||
  196. k == "Connection" ||
  197. k == "Sec-Websocket-Key" ||
  198. k == "Sec-Websocket-Version" ||
  199. k == "Sec-Websocket-Extensions" ||
  200. (k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
  201. return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
  202. case k == "Sec-Websocket-Protocol":
  203. req.Header["Sec-WebSocket-Protocol"] = vs
  204. default:
  205. req.Header[k] = vs
  206. }
  207. }
  208. if d.EnableCompression {
  209. req.Header["Sec-WebSocket-Extensions"] = []string{"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}
  210. }
  211. if d.HandshakeTimeout != 0 {
  212. var cancel func()
  213. ctx, cancel = context.WithTimeout(ctx, d.HandshakeTimeout)
  214. defer cancel()
  215. }
  216. // Get network dial function.
  217. var netDial func(network, add string) (net.Conn, error)
  218. switch u.Scheme {
  219. case "http":
  220. if d.NetDialContext != nil {
  221. netDial = func(network, addr string) (net.Conn, error) {
  222. return d.NetDialContext(ctx, network, addr)
  223. }
  224. } else if d.NetDial != nil {
  225. netDial = d.NetDial
  226. }
  227. case "https":
  228. if d.NetDialTLSContext != nil {
  229. netDial = func(network, addr string) (net.Conn, error) {
  230. return d.NetDialTLSContext(ctx, network, addr)
  231. }
  232. } else if d.NetDialContext != nil {
  233. netDial = func(network, addr string) (net.Conn, error) {
  234. return d.NetDialContext(ctx, network, addr)
  235. }
  236. } else if d.NetDial != nil {
  237. netDial = d.NetDial
  238. }
  239. default:
  240. return nil, nil, errMalformedURL
  241. }
  242. if netDial == nil {
  243. netDialer := &net.Dialer{}
  244. netDial = func(network, addr string) (net.Conn, error) {
  245. return netDialer.DialContext(ctx, network, addr)
  246. }
  247. }
  248. // If needed, wrap the dial function to set the connection deadline.
  249. if deadline, ok := ctx.Deadline(); ok {
  250. forwardDial := netDial
  251. netDial = func(network, addr string) (net.Conn, error) {
  252. c, err := forwardDial(network, addr)
  253. if err != nil {
  254. return nil, err
  255. }
  256. err = c.SetDeadline(deadline)
  257. if err != nil {
  258. c.Close()
  259. return nil, err
  260. }
  261. return c, nil
  262. }
  263. }
  264. // If needed, wrap the dial function to connect through a proxy.
  265. if d.Proxy != nil {
  266. proxyURL, err := d.Proxy(req)
  267. if err != nil {
  268. return nil, nil, err
  269. }
  270. if proxyURL != nil {
  271. dialer, err := proxy_FromURL(proxyURL, netDialerFunc(netDial))
  272. if err != nil {
  273. return nil, nil, err
  274. }
  275. netDial = dialer.Dial
  276. }
  277. }
  278. hostPort, hostNoPort := hostPortNoPort(u)
  279. trace := httptrace.ContextClientTrace(ctx)
  280. if trace != nil && trace.GetConn != nil {
  281. trace.GetConn(hostPort)
  282. }
  283. netConn, err := netDial("tcp", hostPort)
  284. if err != nil {
  285. return nil, nil, err
  286. }
  287. if trace != nil && trace.GotConn != nil {
  288. trace.GotConn(httptrace.GotConnInfo{
  289. Conn: netConn,
  290. })
  291. }
  292. defer func() {
  293. if netConn != nil {
  294. netConn.Close()
  295. }
  296. }()
  297. if u.Scheme == "https" && d.NetDialTLSContext == nil {
  298. // If NetDialTLSContext is set, assume that the TLS handshake has already been done
  299. cfg := cloneTLSConfig(d.TLSClientConfig)
  300. if cfg.ServerName == "" {
  301. cfg.ServerName = hostNoPort
  302. }
  303. tlsConn := tls.Client(netConn, cfg)
  304. netConn = tlsConn
  305. if trace != nil && trace.TLSHandshakeStart != nil {
  306. trace.TLSHandshakeStart()
  307. }
  308. err := doHandshake(ctx, tlsConn, cfg)
  309. if trace != nil && trace.TLSHandshakeDone != nil {
  310. trace.TLSHandshakeDone(tlsConn.ConnectionState(), err)
  311. }
  312. if err != nil {
  313. return nil, nil, err
  314. }
  315. }
  316. conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize, d.WriteBufferPool, nil, nil)
  317. if err := req.Write(netConn); err != nil {
  318. return nil, nil, err
  319. }
  320. if trace != nil && trace.GotFirstResponseByte != nil {
  321. if peek, err := conn.br.Peek(1); err == nil && len(peek) == 1 {
  322. trace.GotFirstResponseByte()
  323. }
  324. }
  325. resp, err := http.ReadResponse(conn.br, req)
  326. if err != nil {
  327. if d.TLSClientConfig != nil {
  328. for _, proto := range d.TLSClientConfig.NextProtos {
  329. if proto != "http/1.1" {
  330. return nil, nil, fmt.Errorf(
  331. "websocket: protocol %q was given but is not supported;"+
  332. "sharing tls.Config with net/http Transport can cause this error: %w",
  333. proto, err,
  334. )
  335. }
  336. }
  337. }
  338. return nil, nil, err
  339. }
  340. if d.Jar != nil {
  341. if rc := resp.Cookies(); len(rc) > 0 {
  342. d.Jar.SetCookies(u, rc)
  343. }
  344. }
  345. if resp.StatusCode != 101 ||
  346. !tokenListContainsValue(resp.Header, "Upgrade", "websocket") ||
  347. !tokenListContainsValue(resp.Header, "Connection", "upgrade") ||
  348. resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
  349. // Before closing the network connection on return from this
  350. // function, slurp up some of the response to aid application
  351. // debugging.
  352. buf := make([]byte, 1024)
  353. n, _ := io.ReadFull(resp.Body, buf)
  354. resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
  355. return nil, resp, ErrBadHandshake
  356. }
  357. for _, ext := range parseExtensions(resp.Header) {
  358. if ext[""] != "permessage-deflate" {
  359. continue
  360. }
  361. _, snct := ext["server_no_context_takeover"]
  362. _, cnct := ext["client_no_context_takeover"]
  363. if !snct || !cnct {
  364. return nil, resp, errInvalidCompression
  365. }
  366. conn.newCompressionWriter = compressNoContextTakeover
  367. conn.newDecompressionReader = decompressNoContextTakeover
  368. break
  369. }
  370. resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
  371. conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
  372. netConn.SetDeadline(time.Time{})
  373. netConn = nil // to avoid close in defer.
  374. return conn, resp, nil
  375. }
  376. func cloneTLSConfig(cfg *tls.Config) *tls.Config {
  377. if cfg == nil {
  378. return &tls.Config{}
  379. }
  380. return cfg.Clone()
  381. }