actions.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package goproxy
  2. import "net/http"
  3. // ReqHandler will "tamper" with the request coming to the proxy server
  4. // If Handle returns req,nil the proxy will send the returned request
  5. // to the destination server. If it returns nil,resp the proxy will
  6. // skip sending any requests, and will simply return the response `resp`
  7. // to the client.
  8. type ReqHandler interface {
  9. Handle(req *http.Request, ctx *ProxyCtx) (*http.Request, *http.Response)
  10. }
  11. // A wrapper that would convert a function to a ReqHandler interface type
  12. type FuncReqHandler func(req *http.Request, ctx *ProxyCtx) (*http.Request, *http.Response)
  13. // FuncReqHandler.Handle(req,ctx) <=> FuncReqHandler(req,ctx)
  14. func (f FuncReqHandler) Handle(req *http.Request, ctx *ProxyCtx) (*http.Request, *http.Response) {
  15. return f(req, ctx)
  16. }
  17. // after the proxy have sent the request to the destination server, it will
  18. // "filter" the response through the RespHandlers it has.
  19. // The proxy server will send to the client the response returned by the RespHandler.
  20. // In case of error, resp will be nil, and ctx.RoundTrip.Error will contain the error
  21. type RespHandler interface {
  22. Handle(resp *http.Response, ctx *ProxyCtx) *http.Response
  23. }
  24. // A wrapper that would convert a function to a RespHandler interface type
  25. type FuncRespHandler func(resp *http.Response, ctx *ProxyCtx) *http.Response
  26. // FuncRespHandler.Handle(req,ctx) <=> FuncRespHandler(req,ctx)
  27. func (f FuncRespHandler) Handle(resp *http.Response, ctx *ProxyCtx) *http.Response {
  28. return f(resp, ctx)
  29. }
  30. // When a client send a CONNECT request to a host, the request is filtered through
  31. // all the HttpsHandlers the proxy has, and if one returns true, the connection is
  32. // sniffed using Man in the Middle attack.
  33. // That is, the proxy will create a TLS connection with the client, another TLS
  34. // connection with the destination the client wished to connect to, and would
  35. // send back and forth all messages from the server to the client and vice versa.
  36. // The request and responses sent in this Man In the Middle channel are filtered
  37. // through the usual flow (request and response filtered through the ReqHandlers
  38. // and RespHandlers)
  39. type HttpsHandler interface {
  40. HandleConnect(req string, ctx *ProxyCtx) (*ConnectAction, string)
  41. }
  42. // A wrapper that would convert a function to a HttpsHandler interface type
  43. type FuncHttpsHandler func(host string, ctx *ProxyCtx) (*ConnectAction, string)
  44. // FuncHttpsHandler should implement the RespHandler interface
  45. func (f FuncHttpsHandler) HandleConnect(host string, ctx *ProxyCtx) (*ConnectAction, string) {
  46. return f(host, ctx)
  47. }