proxy.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package goproxy
  2. import (
  3. "bufio"
  4. "io"
  5. "log"
  6. "net"
  7. "net/http"
  8. "os"
  9. "regexp"
  10. "sync/atomic"
  11. )
  12. // The basic proxy type. Implements http.Handler.
  13. type ProxyHttpServer struct {
  14. // session variable must be aligned in i386
  15. // see http://golang.org/src/pkg/sync/atomic/doc.go#L41
  16. sess int64
  17. // KeepDestinationHeaders indicates the proxy should retain any headers present in the http.Response before proxying
  18. KeepDestinationHeaders bool
  19. // setting Verbose to true will log information on each request sent to the proxy
  20. Verbose bool
  21. Logger *log.Logger
  22. NonproxyHandler http.Handler
  23. reqHandlers []ReqHandler
  24. respHandlers []RespHandler
  25. httpsHandlers []HttpsHandler
  26. Tr *http.Transport
  27. // ConnectDial will be used to create TCP connections for CONNECT requests
  28. // if nil Tr.Dial will be used
  29. ConnectDial func(network string, addr string) (net.Conn, error)
  30. }
  31. var hasPort = regexp.MustCompile(`:\d+$`)
  32. func copyHeaders(dst, src http.Header, keepDestHeaders bool) {
  33. if !keepDestHeaders {
  34. for k := range dst {
  35. dst.Del(k)
  36. }
  37. }
  38. for k, vs := range src {
  39. for _, v := range vs {
  40. dst.Add(k, v)
  41. }
  42. }
  43. }
  44. func isEof(r *bufio.Reader) bool {
  45. _, err := r.Peek(1)
  46. if err == io.EOF {
  47. return true
  48. }
  49. return false
  50. }
  51. func (proxy *ProxyHttpServer) filterRequest(r *http.Request, ctx *ProxyCtx) (req *http.Request, resp *http.Response) {
  52. req = r
  53. for _, h := range proxy.reqHandlers {
  54. req, resp = h.Handle(r, ctx)
  55. // non-nil resp means the handler decided to skip sending the request
  56. // and return canned response instead.
  57. if resp != nil {
  58. break
  59. }
  60. }
  61. return
  62. }
  63. func (proxy *ProxyHttpServer) filterResponse(respOrig *http.Response, ctx *ProxyCtx) (resp *http.Response) {
  64. resp = respOrig
  65. for _, h := range proxy.respHandlers {
  66. ctx.Resp = resp
  67. resp = h.Handle(resp, ctx)
  68. }
  69. return
  70. }
  71. func removeProxyHeaders(ctx *ProxyCtx, r *http.Request) {
  72. r.RequestURI = "" // this must be reset when serving a request with the client
  73. ctx.Logf("Sending request %v %v", r.Method, r.URL.String())
  74. // If no Accept-Encoding header exists, Transport will add the headers it can accept
  75. // and would wrap the response body with the relevant reader.
  76. r.Header.Del("Accept-Encoding")
  77. // curl can add that, see
  78. // https://jdebp.eu./FGA/web-proxy-connection-header.html
  79. r.Header.Del("Proxy-Connection")
  80. r.Header.Del("Proxy-Authenticate")
  81. r.Header.Del("Proxy-Authorization")
  82. // Connection, Authenticate and Authorization are single hop Header:
  83. // http://www.w3.org/Protocols/rfc2616/rfc2616.txt
  84. // 14.10 Connection
  85. // The Connection general-header field allows the sender to specify
  86. // options that are desired for that particular connection and MUST NOT
  87. // be communicated by proxies over further connections.
  88. r.Header.Del("Connection")
  89. }
  90. // Standard net/http function. Shouldn't be used directly, http.Serve will use it.
  91. func (proxy *ProxyHttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  92. //r.Header["X-Forwarded-For"] = w.RemoteAddr()
  93. if r.Method == "CONNECT" {
  94. proxy.handleHttps(w, r)
  95. } else {
  96. ctx := &ProxyCtx{Req: r, Session: atomic.AddInt64(&proxy.sess, 1), proxy: proxy}
  97. var err error
  98. ctx.Logf("Got request %v %v %v %v", r.URL.Path, r.Host, r.Method, r.URL.String())
  99. if !r.URL.IsAbs() {
  100. proxy.NonproxyHandler.ServeHTTP(w, r)
  101. return
  102. }
  103. r, resp := proxy.filterRequest(r, ctx)
  104. if resp == nil {
  105. removeProxyHeaders(ctx, r)
  106. resp, err = ctx.RoundTrip(r)
  107. if err != nil {
  108. ctx.Error = err
  109. resp = proxy.filterResponse(nil, ctx)
  110. if resp == nil {
  111. ctx.Logf("error read response %v %v:", r.URL.Host, err.Error())
  112. http.Error(w, err.Error(), 500)
  113. return
  114. }
  115. }
  116. ctx.Logf("Received response %v", resp.Status)
  117. }
  118. origBody := resp.Body
  119. resp = proxy.filterResponse(resp, ctx)
  120. defer origBody.Close()
  121. ctx.Logf("Copying response to client %v [%d]", resp.Status, resp.StatusCode)
  122. // http.ResponseWriter will take care of filling the correct response length
  123. // Setting it now, might impose wrong value, contradicting the actual new
  124. // body the user returned.
  125. // We keep the original body to remove the header only if things changed.
  126. // This will prevent problems with HEAD requests where there's no body, yet,
  127. // the Content-Length header should be set.
  128. if origBody != resp.Body {
  129. resp.Header.Del("Content-Length")
  130. }
  131. copyHeaders(w.Header(), resp.Header, proxy.KeepDestinationHeaders)
  132. w.WriteHeader(resp.StatusCode)
  133. nr, err := io.Copy(w, resp.Body)
  134. if err := resp.Body.Close(); err != nil {
  135. ctx.Warnf("Can't close response body %v", err)
  136. }
  137. ctx.Logf("Copied %v bytes to client error=%v", nr, err)
  138. }
  139. }
  140. // NewProxyHttpServer creates and returns a proxy server, logging to stderr by default
  141. func NewProxyHttpServer() *ProxyHttpServer {
  142. proxy := ProxyHttpServer{
  143. Logger: log.New(os.Stderr, "", log.LstdFlags),
  144. reqHandlers: []ReqHandler{},
  145. respHandlers: []RespHandler{},
  146. httpsHandlers: []HttpsHandler{},
  147. NonproxyHandler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  148. http.Error(w, "This is a proxy server. Does not respond to non-proxy requests.", 500)
  149. }),
  150. Tr: &http.Transport{TLSClientConfig: tlsClientSkipVerify, Proxy: http.ProxyFromEnvironment},
  151. }
  152. proxy.ConnectDial = dialerFromEnv(&proxy)
  153. return &proxy
  154. }