| 1 | package goproxy |
| 2 | |
| 3 | import ( |
| 4 | "crypto/tls" |
| 5 | "net/http" |
| 6 | "regexp" |
| 7 | ) |
| 8 | |
| 9 | // ProxyCtx is the Proxy context, contains useful information about every request. It is passed to |
| 10 | // every user function. Also used as a logger. |
| 11 | type ProxyCtx struct { |
| 12 | // Will contain the client request from the proxy |
| 13 | Req *http.Request |
| 14 | // Will contain the remote server's response (if available. nil if the request wasn't send yet) |
| 15 | Resp *http.Response |
| 16 | RoundTripper RoundTripper |
| 17 | // will contain the recent error that occurred while trying to send receive or parse traffic |
| 18 | Error error |
| 19 | // A handle for the user to keep data in the context, from the call of ReqHandler to the |
| 20 | // call of RespHandler |
| 21 | UserData interface{} |
| 22 | // Will connect a request to a response |
| 23 | Session int64 |
| 24 | certStore CertStorage |
| 25 | Proxy *ProxyHttpServer |
| 26 | } |
| 27 | |
| 28 | type RoundTripper interface { |
| 29 | RoundTrip(req *http.Request, ctx *ProxyCtx) (*http.Response, error) |
| 30 | } |
| 31 | |
| 32 | type CertStorage interface { |
| 33 | Fetch(hostname string, gen func() (*tls.Certificate, error)) (*tls.Certificate, error) |
| 34 | } |
| 35 | |
| 36 | type RoundTripperFunc func(req *http.Request, ctx *ProxyCtx) (*http.Response, error) |
| 37 | |
| 38 | func (f RoundTripperFunc) RoundTrip(req *http.Request, ctx *ProxyCtx) (*http.Response, error) { |
| 39 | return f(req, ctx) |
| 40 | } |
| 41 | |
| 42 | func (ctx *ProxyCtx) RoundTrip(req *http.Request) (*http.Response, error) { |
| 43 | if ctx.RoundTripper != nil { |
| 44 | return ctx.RoundTripper.RoundTrip(req, ctx) |
| 45 | } |
| 46 | return ctx.Proxy.Tr.RoundTrip(req) |
| 47 | } |
| 48 | |
| 49 | func (ctx *ProxyCtx) printf(msg string, argv ...interface{}) { |
| 50 | ctx.Proxy.Logger.Printf("[%03d] "+msg+"\n", append([]interface{}{ctx.Session & 0xFF}, argv...)...) |
| 51 | } |
| 52 | |
| 53 | // Logf prints a message to the proxy's log. Should be used in a ProxyHttpServer's filter |
| 54 | // This message will be printed only if the Verbose field of the ProxyHttpServer is set to true |
| 55 | // |
| 56 | // proxy.OnRequest().DoFunc(func(r *http.Request,ctx *goproxy.ProxyCtx) (*http.Request, *http.Response){ |
| 57 | // nr := atomic.AddInt32(&counter,1) |
| 58 | // ctx.Printf("So far %d requests",nr) |
| 59 | // return r, nil |
| 60 | // }) |
| 61 | func (ctx *ProxyCtx) Logf(msg string, argv ...interface{}) { |
| 62 | if ctx.Proxy.Verbose { |
| 63 | ctx.printf("INFO: "+msg, argv...) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Warnf prints a message to the proxy's log. Should be used in a ProxyHttpServer's filter |
| 68 | // This message will always be printed. |
| 69 | // |
| 70 | // proxy.OnRequest().DoFunc(func(r *http.Request,ctx *goproxy.ProxyCtx) (*http.Request, *http.Response){ |
| 71 | // f,err := os.OpenFile(cachedContent) |
| 72 | // if err != nil { |
| 73 | // ctx.Warnf("error open file %v: %v",cachedContent,err) |
| 74 | // return r, nil |
| 75 | // } |
| 76 | // return r, nil |
| 77 | // }) |
| 78 | func (ctx *ProxyCtx) Warnf(msg string, argv ...interface{}) { |
| 79 | ctx.printf("WARN: "+msg, argv...) |
| 80 | } |
| 81 | |
| 82 | var charsetFinder = regexp.MustCompile("charset=([^ ;]*)") |
| 83 | |
| 84 | // Will try to infer the character set of the request from the headers. |
| 85 | // Returns the empty string if we don't know which character set it used. |
| 86 | // Currently it will look for charset=<charset> in the Content-Type header of the request. |
| 87 | func (ctx *ProxyCtx) Charset() string { |
| 88 | charsets := charsetFinder.FindStringSubmatch(ctx.Resp.Header.Get("Content-Type")) |
| 89 | if charsets == nil { |
| 90 | return "" |
| 91 | } |
| 92 | return charsets[1] |
| 93 | } |