proxy_http.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2014, Yawning Angel <yawning at torproject dot org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package main
  28. import (
  29. "bufio"
  30. "encoding/base64"
  31. "fmt"
  32. "net"
  33. "net/http"
  34. "net/http/httputil"
  35. "net/url"
  36. "time"
  37. "golang.org/x/net/proxy"
  38. )
  39. // httpProxy is a HTTP connect proxy.
  40. type httpProxy struct {
  41. hostPort string
  42. haveAuth bool
  43. username string
  44. password string
  45. forward proxy.Dialer
  46. }
  47. func newHTTP(uri *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
  48. s := new(httpProxy)
  49. s.hostPort = uri.Host
  50. s.forward = forward
  51. if uri.User != nil {
  52. s.haveAuth = true
  53. s.username = uri.User.Username()
  54. s.password, _ = uri.User.Password()
  55. }
  56. return s, nil
  57. }
  58. func (s *httpProxy) Dial(network, addr string) (net.Conn, error) {
  59. // Dial and create the http client connection.
  60. c, err := s.forward.Dial("tcp", s.hostPort)
  61. if err != nil {
  62. return nil, err
  63. }
  64. conn := new(httpConn)
  65. conn.httpConn = httputil.NewClientConn(c, nil) // nolint: staticcheck
  66. conn.remoteAddr, err = net.ResolveTCPAddr(network, addr)
  67. if err != nil {
  68. conn.httpConn.Close()
  69. return nil, err
  70. }
  71. // HACK HACK HACK HACK. http.ReadRequest also does this.
  72. reqURL, err := url.Parse("http://" + addr)
  73. if err != nil {
  74. conn.httpConn.Close()
  75. return nil, err
  76. }
  77. reqURL.Scheme = ""
  78. req, err := http.NewRequest("CONNECT", reqURL.String(), nil)
  79. if err != nil {
  80. conn.httpConn.Close()
  81. return nil, err
  82. }
  83. req.Close = false
  84. if s.haveAuth {
  85. // SetBasicAuth doesn't quite do what is appropriate, because
  86. // the correct header is `Proxy-Authorization`.
  87. req.Header.Set("Proxy-Authorization", "Basic " + base64.StdEncoding.EncodeToString([]byte(s.username+":"+s.password)))
  88. }
  89. req.Header.Set("User-Agent", "")
  90. resp, err := conn.httpConn.Do(req)
  91. if err != nil && err != httputil.ErrPersistEOF { // nolint: staticcheck
  92. conn.httpConn.Close()
  93. return nil, err
  94. }
  95. if resp.StatusCode != 200 {
  96. conn.httpConn.Close()
  97. return nil, fmt.Errorf("proxy error: %s", resp.Status)
  98. }
  99. conn.hijackedConn, conn.staleReader = conn.httpConn.Hijack()
  100. return conn, nil
  101. }
  102. type httpConn struct {
  103. remoteAddr *net.TCPAddr
  104. httpConn *httputil.ClientConn // nolint: staticcheck
  105. hijackedConn net.Conn
  106. staleReader *bufio.Reader
  107. }
  108. func (c *httpConn) Read(b []byte) (int, error) {
  109. if c.staleReader != nil {
  110. if c.staleReader.Buffered() > 0 {
  111. return c.staleReader.Read(b)
  112. }
  113. c.staleReader = nil
  114. }
  115. return c.hijackedConn.Read(b)
  116. }
  117. func (c *httpConn) Write(b []byte) (int, error) {
  118. return c.hijackedConn.Write(b)
  119. }
  120. func (c *httpConn) Close() error {
  121. return c.hijackedConn.Close()
  122. }
  123. func (c *httpConn) LocalAddr() net.Addr {
  124. return c.hijackedConn.LocalAddr()
  125. }
  126. func (c *httpConn) RemoteAddr() net.Addr {
  127. return c.remoteAddr
  128. }
  129. func (c *httpConn) SetDeadline(t time.Time) error {
  130. return c.hijackedConn.SetDeadline(t)
  131. }
  132. func (c *httpConn) SetReadDeadline(t time.Time) error {
  133. return c.hijackedConn.SetReadDeadline(t)
  134. }
  135. func (c *httpConn) SetWriteDeadline(t time.Time) error {
  136. return c.hijackedConn.SetWriteDeadline(t)
  137. }
  138. func init() {
  139. proxy.RegisterDialerType("http", newHTTP)
  140. }