doc.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Package goproxy provides a customizable HTTP proxy,
  3. supporting hijacking HTTPS connection.
  4. The intent of the proxy, is to be usable with reasonable amount of traffic
  5. yet, customizable and programable.
  6. The proxy itself is simply an `net/http` handler.
  7. Typical usage is
  8. proxy := goproxy.NewProxyHttpServer()
  9. proxy.OnRequest(..conditions..).Do(..requesthandler..)
  10. proxy.OnRequest(..conditions..).DoFunc(..requesthandlerFunction..)
  11. proxy.OnResponse(..conditions..).Do(..responesHandler..)
  12. proxy.OnResponse(..conditions..).DoFunc(..responesHandlerFunction..)
  13. http.ListenAndServe(":8080", proxy)
  14. Adding a header to each request
  15. proxy.OnRequest().DoFunc(func(r *http.Request,ctx *goproxy.ProxyCtx) (*http.Request, *http.Response){
  16. r.Header.Set("X-GoProxy","1")
  17. return r, nil
  18. })
  19. Note that the function is called before the proxy sends the request to the server
  20. For printing the content type of all incoming responses
  21. proxy.OnResponse().DoFunc(func(r *http.Response, ctx *goproxy.ProxyCtx)*http.Response{
  22. println(ctx.Req.Host,"->",r.Header.Get("Content-Type"))
  23. return r
  24. })
  25. note that we used the ProxyCtx context variable here. It contains the request
  26. and the response (Req and Resp, Resp is nil if unavailable) of this specific client
  27. interaction with the proxy.
  28. To print the content type of all responses from a certain url, we'll add a
  29. ReqCondition to the OnResponse function:
  30. proxy.OnResponse(goproxy.UrlIs("golang.org/pkg")).DoFunc(func(r *http.Response, ctx *goproxy.ProxyCtx)*http.Response{
  31. println(ctx.Req.Host,"->",r.Header.Get("Content-Type"))
  32. return r
  33. })
  34. We can write the condition ourselves, conditions can be set on request and on response
  35. var random = ReqConditionFunc(func(r *http.Request) bool {
  36. return rand.Intn(1) == 0
  37. })
  38. var hasGoProxyHeader = RespConditionFunc(func(resp *http.Response,req *http.Request)bool {
  39. return resp.Header.Get("X-GoProxy") != ""
  40. })
  41. Caution! If you give a RespCondition to the OnRequest function, you'll get a run time panic! It doesn't
  42. make sense to read the response, if you still haven't got it!
  43. Finally, we have convenience function to throw a quick response
  44. proxy.OnResponse(hasGoProxyHeader).DoFunc(func(r*http.Response,ctx *goproxy.ProxyCtx)*http.Response {
  45. r.Body.Close()
  46. return goproxy.ForbiddenTextResponse(ctx.Req,"Can't see response with X-GoProxy header!")
  47. })
  48. we close the body of the original repsonse, and return a new 403 response with a short message.
  49. Example use cases:
  50. 1. https://github.com/elazarl/goproxy/tree/master/examples/goproxy-avgsize
  51. To measure the average size of an Html served in your site. One can ask
  52. all the QA team to access the website by a proxy, and the proxy will
  53. measure the average size of all text/html responses from your host.
  54. 2. [not yet implemented]
  55. All requests to your web servers should be directed through the proxy,
  56. when the proxy will detect html pieces sent as a response to AJAX
  57. request, it'll send a warning email.
  58. 3. https://github.com/elazarl/goproxy/blob/master/examples/goproxy-httpdump/
  59. Generate a real traffic to your website by real users using through
  60. proxy. Record the traffic, and try it again for more real load testing.
  61. 4. https://github.com/elazarl/goproxy/tree/master/examples/goproxy-no-reddit-at-worktime
  62. Will allow browsing to reddit.com between 8:00am and 17:00pm
  63. 5. https://github.com/elazarl/goproxy/tree/master/examples/goproxy-jquery-version
  64. Will warn if multiple versions of jquery are used in the same domain.
  65. 6. https://github.com/elazarl/goproxy/blob/master/examples/goproxy-upside-down-ternet/
  66. Modifies image files in an HTTP response via goproxy's image extension found in ext/.
  67. */
  68. package goproxy