client.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. "bufio"
  7. "bytes"
  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. // NewClient creates a new client connection using the given net connection.
  22. // The URL u specifies the host and request URI. Use requestHeader to specify
  23. // the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
  24. // (Cookie). Use the response.Header to get the selected subprotocol
  25. // (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
  26. //
  27. // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
  28. // non-nil *http.Response so that callers can handle redirects, authentication,
  29. // etc.
  30. //
  31. // Deprecated: Use Dialer instead.
  32. func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
  33. d := Dialer{
  34. ReadBufferSize: readBufSize,
  35. WriteBufferSize: writeBufSize,
  36. NetDial: func(net, addr string) (net.Conn, error) {
  37. return netConn, nil
  38. },
  39. }
  40. return d.Dial(u.String(), requestHeader)
  41. }
  42. // A Dialer contains options for connecting to WebSocket server.
  43. type Dialer struct {
  44. // NetDial specifies the dial function for creating TCP connections. If
  45. // NetDial is nil, net.Dial is used.
  46. NetDial func(network, addr string) (net.Conn, error)
  47. // Proxy specifies a function to return a proxy for a given
  48. // Request. If the function returns a non-nil error, the
  49. // request is aborted with the provided error.
  50. // If Proxy is nil or returns a nil *URL, no proxy is used.
  51. Proxy func(*http.Request) (*url.URL, error)
  52. // TLSClientConfig specifies the TLS configuration to use with tls.Client.
  53. // If nil, the default configuration is used.
  54. TLSClientConfig *tls.Config
  55. // HandshakeTimeout specifies the duration for the handshake to complete.
  56. HandshakeTimeout time.Duration
  57. // Input and output buffer sizes. If the buffer size is zero, then a
  58. // default value of 4096 is used.
  59. ReadBufferSize, WriteBufferSize int
  60. // Subprotocols specifies the client's requested subprotocols.
  61. Subprotocols []string
  62. }
  63. var errMalformedURL = errors.New("malformed ws or wss URL")
  64. // parseURL parses the URL.
  65. //
  66. // This function is a replacement for the standard library url.Parse function.
  67. // In Go 1.4 and earlier, url.Parse loses information from the path.
  68. func parseURL(s string) (*url.URL, error) {
  69. // From the RFC:
  70. //
  71. // ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
  72. // wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
  73. var u url.URL
  74. switch {
  75. case strings.HasPrefix(s, "ws://"):
  76. u.Scheme = "ws"
  77. s = s[len("ws://"):]
  78. case strings.HasPrefix(s, "wss://"):
  79. u.Scheme = "wss"
  80. s = s[len("wss://"):]
  81. default:
  82. return nil, errMalformedURL
  83. }
  84. if i := strings.Index(s, "?"); i >= 0 {
  85. u.RawQuery = s[i+1:]
  86. s = s[:i]
  87. }
  88. if i := strings.Index(s, "/"); i >= 0 {
  89. u.Opaque = s[i:]
  90. s = s[:i]
  91. } else {
  92. u.Opaque = "/"
  93. }
  94. u.Host = s
  95. if strings.Contains(u.Host, "@") {
  96. // Don't bother parsing user information because user information is
  97. // not allowed in websocket URIs.
  98. return nil, errMalformedURL
  99. }
  100. return &u, nil
  101. }
  102. func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
  103. hostPort = u.Host
  104. hostNoPort = u.Host
  105. if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
  106. hostNoPort = hostNoPort[:i]
  107. } else {
  108. switch u.Scheme {
  109. case "wss":
  110. hostPort += ":443"
  111. case "https":
  112. hostPort += ":443"
  113. default:
  114. hostPort += ":80"
  115. }
  116. }
  117. return hostPort, hostNoPort
  118. }
  119. // DefaultDialer is a dialer with all fields set to the default zero values.
  120. var DefaultDialer = &Dialer{
  121. Proxy: http.ProxyFromEnvironment,
  122. }
  123. // Dial creates a new client connection. Use requestHeader to specify the
  124. // origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
  125. // Use the response.Header to get the selected subprotocol
  126. // (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
  127. //
  128. // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
  129. // non-nil *http.Response so that callers can handle redirects, authentication,
  130. // etcetera. The response body may not contain the entire response and does not
  131. // need to be closed by the application.
  132. func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
  133. if d == nil {
  134. d = &Dialer{
  135. Proxy: http.ProxyFromEnvironment,
  136. }
  137. }
  138. challengeKey, err := generateChallengeKey()
  139. if err != nil {
  140. return nil, nil, err
  141. }
  142. u, err := parseURL(urlStr)
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. switch u.Scheme {
  147. case "ws":
  148. u.Scheme = "http"
  149. case "wss":
  150. u.Scheme = "https"
  151. default:
  152. return nil, nil, errMalformedURL
  153. }
  154. if u.User != nil {
  155. // User name and password are not allowed in websocket URIs.
  156. return nil, nil, errMalformedURL
  157. }
  158. req := &http.Request{
  159. Method: "GET",
  160. URL: u,
  161. Proto: "HTTP/1.1",
  162. ProtoMajor: 1,
  163. ProtoMinor: 1,
  164. Header: make(http.Header),
  165. Host: u.Host,
  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-Protocol" && len(d.Subprotocols) > 0):
  189. return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
  190. default:
  191. req.Header[k] = vs
  192. }
  193. }
  194. hostPort, hostNoPort := hostPortNoPort(u)
  195. var proxyURL *url.URL
  196. // Check wether the proxy method has been configured
  197. if d.Proxy != nil {
  198. proxyURL, err = d.Proxy(req)
  199. }
  200. if err != nil {
  201. return nil, nil, err
  202. }
  203. var targetHostPort string
  204. if proxyURL != nil {
  205. targetHostPort, _ = hostPortNoPort(proxyURL)
  206. } else {
  207. targetHostPort = hostPort
  208. }
  209. var deadline time.Time
  210. if d.HandshakeTimeout != 0 {
  211. deadline = time.Now().Add(d.HandshakeTimeout)
  212. }
  213. netDial := d.NetDial
  214. if netDial == nil {
  215. netDialer := &net.Dialer{Deadline: deadline}
  216. netDial = netDialer.Dial
  217. }
  218. netConn, err := netDial("tcp", targetHostPort)
  219. if err != nil {
  220. return nil, nil, err
  221. }
  222. defer func() {
  223. if netConn != nil {
  224. netConn.Close()
  225. }
  226. }()
  227. if err := netConn.SetDeadline(deadline); err != nil {
  228. return nil, nil, err
  229. }
  230. if proxyURL != nil {
  231. connectReq := &http.Request{
  232. Method: "CONNECT",
  233. URL: &url.URL{Opaque: hostPort},
  234. Host: hostPort,
  235. Header: make(http.Header),
  236. }
  237. connectReq.Write(netConn)
  238. // Read response.
  239. // Okay to use and discard buffered reader here, because
  240. // TLS server will not speak until spoken to.
  241. br := bufio.NewReader(netConn)
  242. resp, err := http.ReadResponse(br, connectReq)
  243. if err != nil {
  244. return nil, nil, err
  245. }
  246. if resp.StatusCode != 200 {
  247. f := strings.SplitN(resp.Status, " ", 2)
  248. return nil, nil, errors.New(f[1])
  249. }
  250. }
  251. if u.Scheme == "https" {
  252. cfg := d.TLSClientConfig
  253. if cfg == nil {
  254. cfg = &tls.Config{ServerName: hostNoPort}
  255. } else if cfg.ServerName == "" {
  256. shallowCopy := *cfg
  257. cfg = &shallowCopy
  258. cfg.ServerName = hostNoPort
  259. }
  260. tlsConn := tls.Client(netConn, cfg)
  261. netConn = tlsConn
  262. if err := tlsConn.Handshake(); err != nil {
  263. return nil, nil, err
  264. }
  265. if !cfg.InsecureSkipVerify {
  266. if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
  267. return nil, nil, err
  268. }
  269. }
  270. }
  271. conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize)
  272. if err := req.Write(netConn); err != nil {
  273. return nil, nil, err
  274. }
  275. resp, err := http.ReadResponse(conn.br, req)
  276. if err != nil {
  277. return nil, nil, err
  278. }
  279. if resp.StatusCode != 101 ||
  280. !strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") ||
  281. !strings.EqualFold(resp.Header.Get("Connection"), "upgrade") ||
  282. resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
  283. // Before closing the network connection on return from this
  284. // function, slurp up some of the response to aid application
  285. // debugging.
  286. buf := make([]byte, 1024)
  287. n, _ := io.ReadFull(resp.Body, buf)
  288. resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
  289. return nil, resp, ErrBadHandshake
  290. }
  291. resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
  292. conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
  293. netConn.SetDeadline(time.Time{})
  294. netConn = nil // to avoid close in defer.
  295. return conn, resp, nil
  296. }