client.go 13 KB

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