| 1 | # Introduction |
| 2 | |
| 3 | [](https://godoc.org/github.com/elazarl/goproxy) |
| 4 | [](https://gitter.im/elazarl/goproxy?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
| 5 |  |
| 6 | |
| 7 | Package goproxy provides a customizable HTTP proxy library for Go (golang), |
| 8 | |
| 9 | It supports regular HTTP proxy, HTTPS through CONNECT, and "hijacking" HTTPS |
| 10 | connection using "Man in the Middle" style attack. |
| 11 | |
| 12 | The intent of the proxy is to be usable with reasonable amount of traffic, |
| 13 | yet customizable and programmable. |
| 14 | |
| 15 | The proxy itself is simply a `net/http` handler. |
| 16 | |
| 17 | In order to use goproxy, one should set their browser to use goproxy as an HTTP |
| 18 | proxy. Here is how you do that [in Chrome](https://support.google.com/chrome/answer/96815?hl=en) |
| 19 | and [in Firefox](http://www.wikihow.com/Enter-Proxy-Settings-in-Firefox). |
| 20 | |
| 21 | For example, the URL you should use as proxy when running `./bin/basic` is |
| 22 | `localhost:8080`, as this is the default binding for the basic proxy. |
| 23 | |
| 24 | ## Mailing List |
| 25 | |
| 26 | New features will be discussed on the [mailing list](https://groups.google.com/forum/#!forum/goproxy-dev) |
| 27 | before their development. |
| 28 | |
| 29 | ## Latest Stable Release |
| 30 | |
| 31 | Get the latest goproxy from `gopkg.in/elazarl/goproxy.v1`. |
| 32 | |
| 33 | # Why not Fiddler2? |
| 34 | |
| 35 | Fiddler is an excellent software with similar intent. However, Fiddler is not |
| 36 | as customizable as goproxy intends to be. The main difference is, Fiddler is not |
| 37 | intended to be used as a real proxy. |
| 38 | |
| 39 | A possible use case that suits goproxy but |
| 40 | not Fiddler, is gathering statistics on page load times for a certain website over a week. |
| 41 | With goproxy you could ask all your users to set their proxy to a dedicated machine running a |
| 42 | goproxy server. Fiddler is a GUI app not designed to be run like a server for multiple users. |
| 43 | |
| 44 | # A taste of goproxy |
| 45 | |
| 46 | To get a taste of `goproxy`, a basic HTTP/HTTPS transparent proxy |
| 47 | |
| 48 | ```go |
| 49 | package main |
| 50 | |
| 51 | import ( |
| 52 | "github.com/elazarl/goproxy" |
| 53 | "log" |
| 54 | "net/http" |
| 55 | ) |
| 56 | |
| 57 | func main() { |
| 58 | proxy := goproxy.NewProxyHttpServer() |
| 59 | proxy.Verbose = true |
| 60 | log.Fatal(http.ListenAndServe(":8080", proxy)) |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | This line will add `X-GoProxy: yxorPoG-X` header to all requests sent through the proxy |
| 65 | |
| 66 | ```go |
| 67 | proxy.OnRequest().DoFunc( |
| 68 | func(r *http.Request,ctx *goproxy.ProxyCtx)(*http.Request,*http.Response) { |
| 69 | r.Header.Set("X-GoProxy","yxorPoG-X") |
| 70 | return r,nil |
| 71 | }) |
| 72 | ``` |
| 73 | |
| 74 | `DoFunc` will process all incoming requests to the proxy. It will add a header to the request |
| 75 | and return it. The proxy will send the modified request. |
| 76 | |
| 77 | Note that we returned nil value as the response. Had we returned a response, goproxy would |
| 78 | have discarded the request and sent the new response to the client. |
| 79 | |
| 80 | In order to refuse connections to reddit at work time |
| 81 | |
| 82 | ```go |
| 83 | proxy.OnRequest(goproxy.DstHostIs("www.reddit.com")).DoFunc( |
| 84 | func(r *http.Request,ctx *goproxy.ProxyCtx)(*http.Request,*http.Response) { |
| 85 | if h,_,_ := time.Now().Clock(); h >= 8 && h <= 17 { |
| 86 | return r,goproxy.NewResponse(r, |
| 87 | goproxy.ContentTypeText,http.StatusForbidden, |
| 88 | "Don't waste your time!") |
| 89 | } |
| 90 | return r,nil |
| 91 | }) |
| 92 | ``` |
| 93 | |
| 94 | `DstHostIs` returns a `ReqCondition`, that is a function receiving a `Request` and returning a boolean. |
| 95 | We will only process requests that match the condition. `DstHostIs("www.reddit.com")` will return |
| 96 | a `ReqCondition` accepting only requests directed to "www.reddit.com". |
| 97 | |
| 98 | `DoFunc` will receive a function that will preprocess the request. We can change the request, or |
| 99 | return a response. If the time is between 8:00am and 17:00pm, we will reject the request, and |
| 100 | return a pre-canned text response saying "do not waste your time". |
| 101 | |
| 102 | See additional examples in the examples directory. |
| 103 | |
| 104 | |
| 105 | # Type of handlers for manipulating connect/req/resp behavior |
| 106 | |
| 107 | There are 3 kinds of useful handlers to manipulate the behavior, as follows: |
| 108 | |
| 109 | ```go |
| 110 | // handler called after receiving HTTP CONNECT from the client, and before proxy establish connection |
| 111 | // with destination host |
| 112 | httpsHandlers []HttpsHandler |
| 113 | |
| 114 | // handler called before proxy send HTTP request to destination host |
| 115 | reqHandlers []ReqHandler |
| 116 | |
| 117 | // handler called after proxy receives HTTP Response from destination host, and before proxy forward |
| 118 | // the Response to the client. |
| 119 | respHandlers []RespHandler |
| 120 | ``` |
| 121 | |
| 122 | Depending on what you want to manipulate, the ways to add handlers to each handler list are: |
| 123 | |
| 124 | ```go |
| 125 | // Add handlers to httpsHandlers |
| 126 | proxy.OnRequest(Some ReqConditions).HandleConnect(YourHandlerFunc()) |
| 127 | |
| 128 | // Add handlers to reqHandlers |
| 129 | proxy.OnRequest(Some ReqConditions).Do(YourReqHandlerFunc()) |
| 130 | |
| 131 | // Add handlers to respHandlers |
| 132 | proxy.OnResponse(Some RespConditions).Do(YourRespHandlerFunc()) |
| 133 | ``` |
| 134 | |
| 135 | For example: |
| 136 | |
| 137 | ```go |
| 138 | // This rejects the HTTPS request to *.reddit.com during HTTP CONNECT phase |
| 139 | proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("reddit.*:443$"))).HandleConnect(goproxy.AlwaysReject) |
| 140 | |
| 141 | // This will NOT reject the HTTPS request with URL ending with gif, due to the fact that proxy |
| 142 | // only got the URL.Hostname and URL.Port during the HTTP CONNECT phase if the scheme is HTTPS, which is |
| 143 | // quiet common these days. |
| 144 | proxy.OnRequest(goproxy.UrlMatches(regexp.MustCompile(`.*gif$`))).HandleConnect(goproxy.AlwaysReject) |
| 145 | |
| 146 | // The correct way to manipulate the HTTP request using URL.Path as condition is: |
| 147 | proxy.OnRequest(goproxy.UrlMatches(regexp.MustCompile(`.*gif$`))).Do(YourReqHandlerFunc()) |
| 148 | ``` |
| 149 | |
| 150 | # What's New |
| 151 | |
| 152 | 1. Ability to `Hijack` CONNECT requests. See |
| 153 | [the eavesdropper example](https://github.com/elazarl/goproxy/blob/master/examples/goproxy-eavesdropper/main.go#L27) |
| 154 | 2. Transparent proxy support for http/https including MITM certificate generation for TLS. See the [transparent example.](https://github.com/elazarl/goproxy/tree/master/examples/goproxy-transparent) |
| 155 | |
| 156 | # License |
| 157 | |
| 158 | I put the software temporarily under the Go-compatible BSD license. |
| 159 | If this prevents someone from using the software, do let me know and I'll consider changing it. |
| 160 | |
| 161 | At any rate, user feedback is very important for me, so I'll be delighted to know if you're using this package. |
| 162 | |
| 163 | # Beta Software |
| 164 | |
| 165 | I've received positive feedback from a few people who use goproxy in production settings. |
| 166 | I believe it is good enough for usage. |
| 167 | |
| 168 | I'll try to keep reasonable backwards compatibility. In case of a major API change, |
| 169 | I'll change the import path. |