proxy.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2017 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. "encoding/base64"
  8. "errors"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "strings"
  13. )
  14. type netDialerFunc func(network, addr string) (net.Conn, error)
  15. func (fn netDialerFunc) Dial(network, addr string) (net.Conn, error) {
  16. return fn(network, addr)
  17. }
  18. func init() {
  19. proxy_RegisterDialerType("http", func(proxyURL *url.URL, forwardDialer proxy_Dialer) (proxy_Dialer, error) {
  20. return &httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDialer.Dial}, nil
  21. })
  22. }
  23. type httpProxyDialer struct {
  24. proxyURL *url.URL
  25. forwardDial func(network, addr string) (net.Conn, error)
  26. }
  27. func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error) {
  28. hostPort, _ := hostPortNoPort(hpd.proxyURL)
  29. conn, err := hpd.forwardDial(network, hostPort)
  30. if err != nil {
  31. return nil, err
  32. }
  33. connectHeader := make(http.Header)
  34. if user := hpd.proxyURL.User; user != nil {
  35. proxyUser := user.Username()
  36. if proxyPassword, passwordSet := user.Password(); passwordSet {
  37. credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
  38. connectHeader.Set("Proxy-Authorization", "Basic "+credential)
  39. }
  40. }
  41. connectReq := &http.Request{
  42. Method: "CONNECT",
  43. URL: &url.URL{Opaque: addr},
  44. Host: addr,
  45. Header: connectHeader,
  46. }
  47. if err := connectReq.Write(conn); err != nil {
  48. conn.Close()
  49. return nil, err
  50. }
  51. // Read response. It's OK to use and discard buffered reader here becaue
  52. // the remote server does not speak until spoken to.
  53. br := bufio.NewReader(conn)
  54. resp, err := http.ReadResponse(br, connectReq)
  55. if err != nil {
  56. conn.Close()
  57. return nil, err
  58. }
  59. if resp.StatusCode != 200 {
  60. conn.Close()
  61. f := strings.SplitN(resp.Status, " ", 2)
  62. return nil, errors.New(f[1])
  63. }
  64. return conn, nil
  65. }