client.go 11 KB

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