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