client.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. "encoding/base64"
  10. "errors"
  11. "io"
  12. "io/ioutil"
  13. "net"
  14. "net/http"
  15. "net/url"
  16. "strings"
  17. "time"
  18. )
  19. // ErrBadHandshake is returned when the server response to opening handshake is
  20. // invalid.
  21. var ErrBadHandshake = errors.New("websocket: bad handshake")
  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. // Proxy specifies a function to return a proxy for a given
  49. // Request. If the function returns a non-nil error, the
  50. // request is aborted with the provided error.
  51. // If Proxy is nil or returns a nil *URL, no proxy is used.
  52. Proxy func(*http.Request) (*url.URL, error)
  53. // TLSClientConfig specifies the TLS configuration to use with tls.Client.
  54. // If nil, the default configuration is used.
  55. TLSClientConfig *tls.Config
  56. // HandshakeTimeout specifies the duration for the handshake to complete.
  57. HandshakeTimeout time.Duration
  58. // Input and output buffer sizes. If the buffer size is zero, then a
  59. // default value of 4096 is used.
  60. ReadBufferSize, WriteBufferSize int
  61. // Subprotocols specifies the client's requested subprotocols.
  62. Subprotocols []string
  63. }
  64. var errMalformedURL = errors.New("malformed ws or wss URL")
  65. // parseURL parses the URL.
  66. //
  67. // This function is a replacement for the standard library url.Parse function.
  68. // In Go 1.4 and earlier, url.Parse loses information from the path.
  69. func parseURL(s string) (*url.URL, error) {
  70. // From the RFC:
  71. //
  72. // ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
  73. // wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
  74. var u url.URL
  75. switch {
  76. case strings.HasPrefix(s, "ws://"):
  77. u.Scheme = "ws"
  78. s = s[len("ws://"):]
  79. case strings.HasPrefix(s, "wss://"):
  80. u.Scheme = "wss"
  81. s = s[len("wss://"):]
  82. default:
  83. return nil, errMalformedURL
  84. }
  85. if i := strings.Index(s, "?"); i >= 0 {
  86. u.RawQuery = s[i+1:]
  87. s = s[:i]
  88. }
  89. if i := strings.Index(s, "/"); i >= 0 {
  90. u.Opaque = s[i:]
  91. s = s[:i]
  92. } else {
  93. u.Opaque = "/"
  94. }
  95. u.Host = s
  96. if strings.Contains(u.Host, "@") {
  97. // Don't bother parsing user information because user information is
  98. // not allowed in websocket URIs.
  99. return nil, errMalformedURL
  100. }
  101. return &u, nil
  102. }
  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 zero values.
  121. var DefaultDialer = &Dialer{
  122. Proxy: http.ProxyFromEnvironment,
  123. }
  124. // Dial creates a new client connection. Use requestHeader to specify the
  125. // origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
  126. // Use the response.Header to get the selected subprotocol
  127. // (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
  128. //
  129. // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
  130. // non-nil *http.Response so that callers can handle redirects, authentication,
  131. // etcetera. The response body may not contain the entire response and does not
  132. // need to be closed by the application.
  133. func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
  134. if d == nil {
  135. d = &Dialer{
  136. Proxy: http.ProxyFromEnvironment,
  137. }
  138. }
  139. challengeKey, err := generateChallengeKey()
  140. if err != nil {
  141. return nil, nil, err
  142. }
  143. u, err := parseURL(urlStr)
  144. if err != nil {
  145. return nil, nil, err
  146. }
  147. switch u.Scheme {
  148. case "ws":
  149. u.Scheme = "http"
  150. case "wss":
  151. u.Scheme = "https"
  152. default:
  153. return nil, nil, errMalformedURL
  154. }
  155. if u.User != nil {
  156. // User name and password are not allowed in websocket URIs.
  157. return nil, nil, errMalformedURL
  158. }
  159. req := &http.Request{
  160. Method: "GET",
  161. URL: u,
  162. Proto: "HTTP/1.1",
  163. ProtoMajor: 1,
  164. ProtoMinor: 1,
  165. Header: make(http.Header),
  166. Host: u.Host,
  167. }
  168. // Set the request headers using the capitalization for names and values in
  169. // RFC examples. Although the capitalization shouldn't matter, there are
  170. // servers that depend on it. The Header.Set method is not used because the
  171. // method canonicalizes the header names.
  172. req.Header["Upgrade"] = []string{"websocket"}
  173. req.Header["Connection"] = []string{"Upgrade"}
  174. req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
  175. req.Header["Sec-WebSocket-Version"] = []string{"13"}
  176. if len(d.Subprotocols) > 0 {
  177. req.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.Subprotocols, ", ")}
  178. }
  179. for k, vs := range requestHeader {
  180. switch {
  181. case k == "Host":
  182. if len(vs) > 0 {
  183. req.Host = vs[0]
  184. }
  185. case k == "Upgrade" ||
  186. k == "Connection" ||
  187. k == "Sec-Websocket-Key" ||
  188. k == "Sec-Websocket-Version" ||
  189. (k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
  190. return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
  191. default:
  192. req.Header[k] = vs
  193. }
  194. }
  195. hostPort, hostNoPort := hostPortNoPort(u)
  196. var proxyURL *url.URL
  197. // Check wether the proxy method has been configured
  198. if d.Proxy != nil {
  199. proxyURL, err = d.Proxy(req)
  200. }
  201. if err != nil {
  202. return nil, nil, err
  203. }
  204. var targetHostPort string
  205. if proxyURL != nil {
  206. targetHostPort, _ = hostPortNoPort(proxyURL)
  207. } else {
  208. targetHostPort = hostPort
  209. }
  210. var deadline time.Time
  211. if d.HandshakeTimeout != 0 {
  212. deadline = time.Now().Add(d.HandshakeTimeout)
  213. }
  214. netDial := d.NetDial
  215. if netDial == nil {
  216. netDialer := &net.Dialer{Deadline: deadline}
  217. netDial = netDialer.Dial
  218. }
  219. netConn, err := netDial("tcp", targetHostPort)
  220. if err != nil {
  221. return nil, nil, err
  222. }
  223. defer func() {
  224. if netConn != nil {
  225. netConn.Close()
  226. }
  227. }()
  228. if err := netConn.SetDeadline(deadline); err != nil {
  229. return nil, nil, err
  230. }
  231. if proxyURL != nil {
  232. connectHeader := make(http.Header)
  233. if user := proxyURL.User; user != nil {
  234. proxyUser := user.Username()
  235. if proxyPassword, passwordSet := user.Password(); passwordSet {
  236. credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
  237. connectHeader.Set("Proxy-Authorization", "Basic "+credential)
  238. }
  239. }
  240. connectReq := &http.Request{
  241. Method: "CONNECT",
  242. URL: &url.URL{Opaque: hostPort},
  243. Host: hostPort,
  244. Header: connectHeader,
  245. }
  246. connectReq.Write(netConn)
  247. // Read response.
  248. // Okay to use and discard buffered reader here, because
  249. // TLS server will not speak until spoken to.
  250. br := bufio.NewReader(netConn)
  251. resp, err := http.ReadResponse(br, connectReq)
  252. if err != nil {
  253. return nil, nil, err
  254. }
  255. if resp.StatusCode != 200 {
  256. f := strings.SplitN(resp.Status, " ", 2)
  257. return nil, nil, errors.New(f[1])
  258. }
  259. }
  260. if u.Scheme == "https" {
  261. cfg := cloneTLSConfig(d.TLSClientConfig)
  262. if cfg.ServerName == "" {
  263. cfg.ServerName = hostNoPort
  264. }
  265. tlsConn := tls.Client(netConn, cfg)
  266. netConn = tlsConn
  267. if err := tlsConn.Handshake(); err != nil {
  268. return nil, nil, err
  269. }
  270. if !cfg.InsecureSkipVerify {
  271. if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
  272. return nil, nil, err
  273. }
  274. }
  275. }
  276. conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize)
  277. if err := req.Write(netConn); err != nil {
  278. return nil, nil, err
  279. }
  280. resp, err := http.ReadResponse(conn.br, req)
  281. if err != nil {
  282. return nil, nil, err
  283. }
  284. if resp.StatusCode != 101 ||
  285. !strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") ||
  286. !strings.EqualFold(resp.Header.Get("Connection"), "upgrade") ||
  287. resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
  288. // Before closing the network connection on return from this
  289. // function, slurp up some of the response to aid application
  290. // debugging.
  291. buf := make([]byte, 1024)
  292. n, _ := io.ReadFull(resp.Body, buf)
  293. resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
  294. return nil, resp, ErrBadHandshake
  295. }
  296. resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
  297. conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
  298. netConn.SetDeadline(time.Time{})
  299. netConn = nil // to avoid close in defer.
  300. return conn, resp, nil
  301. }
  302. // cloneTLSConfig clones all public fields except the fields
  303. // SessionTicketsDisabled and SessionTicketKey. This avoids copying the
  304. // sync.Mutex in the sync.Once and makes it safe to call cloneTLSConfig on a
  305. // config in active use.
  306. func cloneTLSConfig(cfg *tls.Config) *tls.Config {
  307. if cfg == nil {
  308. return &tls.Config{}
  309. }
  310. return &tls.Config{
  311. Rand: cfg.Rand,
  312. Time: cfg.Time,
  313. Certificates: cfg.Certificates,
  314. NameToCertificate: cfg.NameToCertificate,
  315. GetCertificate: cfg.GetCertificate,
  316. RootCAs: cfg.RootCAs,
  317. NextProtos: cfg.NextProtos,
  318. ServerName: cfg.ServerName,
  319. ClientAuth: cfg.ClientAuth,
  320. ClientCAs: cfg.ClientCAs,
  321. InsecureSkipVerify: cfg.InsecureSkipVerify,
  322. CipherSuites: cfg.CipherSuites,
  323. PreferServerCipherSuites: cfg.PreferServerCipherSuites,
  324. ClientSessionCache: cfg.ClientSessionCache,
  325. MinVersion: cfg.MinVersion,
  326. MaxVersion: cfg.MaxVersion,
  327. CurvePreferences: cfg.CurvePreferences,
  328. }
  329. }