| 1 | package goproxy |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io/ioutil" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // ReqCondition.HandleReq will decide whether or not to use the ReqHandler on an HTTP request |
| 13 | // before sending it to the remote server |
| 14 | type ReqCondition interface { |
| 15 | RespCondition |
| 16 | HandleReq(req *http.Request, ctx *ProxyCtx) bool |
| 17 | } |
| 18 | |
| 19 | // RespCondition.HandleReq will decide whether or not to use the RespHandler on an HTTP response |
| 20 | // before sending it to the proxy client. Note that resp might be nil, in case there was an |
| 21 | // error sending the request. |
| 22 | type RespCondition interface { |
| 23 | HandleResp(resp *http.Response, ctx *ProxyCtx) bool |
| 24 | } |
| 25 | |
| 26 | // ReqConditionFunc.HandleReq(req,ctx) <=> ReqConditionFunc(req,ctx) |
| 27 | type ReqConditionFunc func(req *http.Request, ctx *ProxyCtx) bool |
| 28 | |
| 29 | // RespConditionFunc.HandleResp(resp,ctx) <=> RespConditionFunc(resp,ctx) |
| 30 | type RespConditionFunc func(resp *http.Response, ctx *ProxyCtx) bool |
| 31 | |
| 32 | func (c ReqConditionFunc) HandleReq(req *http.Request, ctx *ProxyCtx) bool { |
| 33 | return c(req, ctx) |
| 34 | } |
| 35 | |
| 36 | // ReqConditionFunc cannot test responses. It only satisfies RespCondition interface so that |
| 37 | // to be usable as RespCondition. |
| 38 | func (c ReqConditionFunc) HandleResp(resp *http.Response, ctx *ProxyCtx) bool { |
| 39 | return c(ctx.Req, ctx) |
| 40 | } |
| 41 | |
| 42 | func (c RespConditionFunc) HandleResp(resp *http.Response, ctx *ProxyCtx) bool { |
| 43 | return c(resp, ctx) |
| 44 | } |
| 45 | |
| 46 | // UrlHasPrefix returns a ReqCondition checking wether the destination URL the proxy client has requested |
| 47 | // has the given prefix, with or without the host. |
| 48 | // For example UrlHasPrefix("host/x") will match requests of the form 'GET host/x', and will match |
| 49 | // requests to url 'http://host/x' |
| 50 | func UrlHasPrefix(prefix string) ReqConditionFunc { |
| 51 | return func(req *http.Request, ctx *ProxyCtx) bool { |
| 52 | return strings.HasPrefix(req.URL.Path, prefix) || |
| 53 | strings.HasPrefix(req.URL.Host+req.URL.Path, prefix) || |
| 54 | strings.HasPrefix(req.URL.Scheme+req.URL.Host+req.URL.Path, prefix) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // UrlIs returns a ReqCondition, testing whether or not the request URL is one of the given strings |
| 59 | // with or without the host prefix. |
| 60 | // UrlIs("google.com/","foo") will match requests 'GET /' to 'google.com', requests `'GET google.com/' to |
| 61 | // any host, and requests of the form 'GET foo'. |
| 62 | func UrlIs(urls ...string) ReqConditionFunc { |
| 63 | urlSet := make(map[string]bool) |
| 64 | for _, u := range urls { |
| 65 | urlSet[u] = true |
| 66 | } |
| 67 | return func(req *http.Request, ctx *ProxyCtx) bool { |
| 68 | _, pathOk := urlSet[req.URL.Path] |
| 69 | _, hostAndOk := urlSet[req.URL.Host+req.URL.Path] |
| 70 | return pathOk || hostAndOk |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // ReqHostMatches returns a ReqCondition, testing whether the host to which the request was directed to matches |
| 75 | // any of the given regular expressions. |
| 76 | func ReqHostMatches(regexps ...*regexp.Regexp) ReqConditionFunc { |
| 77 | return func(req *http.Request, ctx *ProxyCtx) bool { |
| 78 | for _, re := range regexps { |
| 79 | if re.MatchString(req.Host) { |
| 80 | return true |
| 81 | } |
| 82 | } |
| 83 | return false |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // ReqHostIs returns a ReqCondition, testing whether the host to which the request is directed to equal |
| 88 | // to one of the given strings |
| 89 | func ReqHostIs(hosts ...string) ReqConditionFunc { |
| 90 | hostSet := make(map[string]bool) |
| 91 | for _, h := range hosts { |
| 92 | hostSet[h] = true |
| 93 | } |
| 94 | return func(req *http.Request, ctx *ProxyCtx) bool { |
| 95 | _, ok := hostSet[req.URL.Host] |
| 96 | return ok |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | var localHostIpv4 = regexp.MustCompile(`127\.0\.0\.\d+`) |
| 101 | |
| 102 | // IsLocalHost checks whether the destination host is explicitly local host |
| 103 | // (buggy, there can be IPv6 addresses it doesn't catch) |
| 104 | var IsLocalHost ReqConditionFunc = func(req *http.Request, ctx *ProxyCtx) bool { |
| 105 | return req.URL.Host == "::1" || |
| 106 | req.URL.Host == "0:0:0:0:0:0:0:1" || |
| 107 | localHostIpv4.MatchString(req.URL.Host) || |
| 108 | req.URL.Host == "localhost" |
| 109 | } |
| 110 | |
| 111 | // UrlMatches returns a ReqCondition testing whether the destination URL |
| 112 | // of the request matches the given regexp, with or without prefix |
| 113 | func UrlMatches(re *regexp.Regexp) ReqConditionFunc { |
| 114 | return func(req *http.Request, ctx *ProxyCtx) bool { |
| 115 | return re.MatchString(req.URL.Path) || |
| 116 | re.MatchString(req.URL.Host+req.URL.Path) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // DstHostIs returns a ReqCondition testing wether the host in the request url is the given string |
| 121 | func DstHostIs(host string) ReqConditionFunc { |
| 122 | return func(req *http.Request, ctx *ProxyCtx) bool { |
| 123 | return req.URL.Host == host |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // SrcIpIs returns a ReqCondition testing whether the source IP of the request is one of the given strings |
| 128 | func SrcIpIs(ips ...string) ReqCondition { |
| 129 | return ReqConditionFunc(func(req *http.Request, ctx *ProxyCtx) bool { |
| 130 | for _, ip := range ips { |
| 131 | if strings.HasPrefix(req.RemoteAddr, ip+":") { |
| 132 | return true |
| 133 | } |
| 134 | } |
| 135 | return false |
| 136 | }) |
| 137 | } |
| 138 | |
| 139 | // Not returns a ReqCondition negating the given ReqCondition |
| 140 | func Not(r ReqCondition) ReqConditionFunc { |
| 141 | return func(req *http.Request, ctx *ProxyCtx) bool { |
| 142 | return !r.HandleReq(req, ctx) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // ContentTypeIs returns a RespCondition testing whether the HTTP response has Content-Type header equal |
| 147 | // to one of the given strings. |
| 148 | func ContentTypeIs(typ string, types ...string) RespCondition { |
| 149 | types = append(types, typ) |
| 150 | return RespConditionFunc(func(resp *http.Response, ctx *ProxyCtx) bool { |
| 151 | if resp == nil { |
| 152 | return false |
| 153 | } |
| 154 | contentType := resp.Header.Get("Content-Type") |
| 155 | for _, typ := range types { |
| 156 | if contentType == typ || strings.HasPrefix(contentType, typ+";") { |
| 157 | return true |
| 158 | } |
| 159 | } |
| 160 | return false |
| 161 | }) |
| 162 | } |
| 163 | |
| 164 | // StatusCodeIs returns a RespCondition, testing whether or not the HTTP status |
| 165 | // code is one of the given ints |
| 166 | func StatusCodeIs(codes ...int) RespCondition { |
| 167 | codeSet := make(map[int]bool) |
| 168 | for _, c := range codes { |
| 169 | codeSet[c] = true |
| 170 | } |
| 171 | return RespConditionFunc(func(resp *http.Response, ctx *ProxyCtx) bool { |
| 172 | if resp == nil { |
| 173 | return false |
| 174 | } |
| 175 | _, codeMatch := codeSet[resp.StatusCode] |
| 176 | return codeMatch |
| 177 | }) |
| 178 | } |
| 179 | |
| 180 | // ProxyHttpServer.OnRequest Will return a temporary ReqProxyConds struct, aggregating the given condtions. |
| 181 | // You will use the ReqProxyConds struct to register a ReqHandler, that would filter |
| 182 | // the request, only if all the given ReqCondition matched. |
| 183 | // Typical usage: |
| 184 | // proxy.OnRequest(UrlIs("example.com/foo"),UrlMatches(regexp.MustParse(`.*\.exampl.\com\./.*`)).Do(...) |
| 185 | func (proxy *ProxyHttpServer) OnRequest(conds ...ReqCondition) *ReqProxyConds { |
| 186 | return &ReqProxyConds{proxy, conds} |
| 187 | } |
| 188 | |
| 189 | // ReqProxyConds aggregate ReqConditions for a ProxyHttpServer. Upon calling Do, it will register a ReqHandler that would |
| 190 | // handle the request if all conditions on the HTTP request are met. |
| 191 | type ReqProxyConds struct { |
| 192 | proxy *ProxyHttpServer |
| 193 | reqConds []ReqCondition |
| 194 | } |
| 195 | |
| 196 | // DoFunc is equivalent to proxy.OnRequest().Do(FuncReqHandler(f)) |
| 197 | func (pcond *ReqProxyConds) DoFunc(f func(req *http.Request, ctx *ProxyCtx) (*http.Request, *http.Response)) { |
| 198 | pcond.Do(FuncReqHandler(f)) |
| 199 | } |
| 200 | |
| 201 | // ReqProxyConds.Do will register the ReqHandler on the proxy, |
| 202 | // the ReqHandler will handle the HTTP request if all the conditions |
| 203 | // aggregated in the ReqProxyConds are met. Typical usage: |
| 204 | // proxy.OnRequest().Do(handler) // will call handler.Handle(req,ctx) on every request to the proxy |
| 205 | // proxy.OnRequest(cond1,cond2).Do(handler) |
| 206 | // // given request to the proxy, will test if cond1.HandleReq(req,ctx) && cond2.HandleReq(req,ctx) are true |
| 207 | // // if they are, will call handler.Handle(req,ctx) |
| 208 | func (pcond *ReqProxyConds) Do(h ReqHandler) { |
| 209 | pcond.proxy.reqHandlers = append(pcond.proxy.reqHandlers, |
| 210 | FuncReqHandler(func(r *http.Request, ctx *ProxyCtx) (*http.Request, *http.Response) { |
| 211 | for _, cond := range pcond.reqConds { |
| 212 | if !cond.HandleReq(r, ctx) { |
| 213 | return r, nil |
| 214 | } |
| 215 | } |
| 216 | return h.Handle(r, ctx) |
| 217 | })) |
| 218 | } |
| 219 | |
| 220 | // HandleConnect is used when proxy receives an HTTP CONNECT request, |
| 221 | // it'll then use the HttpsHandler to determine what should it |
| 222 | // do with this request. The handler returns a ConnectAction struct, the Action field in the ConnectAction |
| 223 | // struct returned will determine what to do with this request. ConnectAccept will simply accept the request |
| 224 | // forwarding all bytes from the client to the remote host, ConnectReject will close the connection with the |
| 225 | // client, and ConnectMitm, will assume the underlying connection is an HTTPS connection, and will use Man |
| 226 | // in the Middle attack to eavesdrop the connection. All regular handler will be active on this eavesdropped |
| 227 | // connection. |
| 228 | // The ConnectAction struct contains possible tlsConfig that will be used for eavesdropping. If nil, the proxy |
| 229 | // will use the default tls configuration. |
| 230 | // proxy.OnRequest().HandleConnect(goproxy.AlwaysReject) // rejects all CONNECT requests |
| 231 | func (pcond *ReqProxyConds) HandleConnect(h HttpsHandler) { |
| 232 | pcond.proxy.httpsHandlers = append(pcond.proxy.httpsHandlers, |
| 233 | FuncHttpsHandler(func(host string, ctx *ProxyCtx) (*ConnectAction, string) { |
| 234 | for _, cond := range pcond.reqConds { |
| 235 | if !cond.HandleReq(ctx.Req, ctx) { |
| 236 | return nil, "" |
| 237 | } |
| 238 | } |
| 239 | return h.HandleConnect(host, ctx) |
| 240 | })) |
| 241 | } |
| 242 | |
| 243 | // HandleConnectFunc is equivalent to HandleConnect, |
| 244 | // for example, accepting CONNECT request if they contain a password in header |
| 245 | // io.WriteString(h,password) |
| 246 | // passHash := h.Sum(nil) |
| 247 | // proxy.OnRequest().HandleConnectFunc(func(host string, ctx *ProxyCtx) (*ConnectAction, string) { |
| 248 | // c := sha1.New() |
| 249 | // io.WriteString(c,ctx.Req.Header.Get("X-GoProxy-Auth")) |
| 250 | // if c.Sum(nil) == passHash { |
| 251 | // return OkConnect, host |
| 252 | // } |
| 253 | // return RejectConnect, host |
| 254 | // }) |
| 255 | func (pcond *ReqProxyConds) HandleConnectFunc(f func(host string, ctx *ProxyCtx) (*ConnectAction, string)) { |
| 256 | pcond.HandleConnect(FuncHttpsHandler(f)) |
| 257 | } |
| 258 | |
| 259 | func (pcond *ReqProxyConds) HijackConnect(f func(req *http.Request, client net.Conn, ctx *ProxyCtx)) { |
| 260 | pcond.proxy.httpsHandlers = append(pcond.proxy.httpsHandlers, |
| 261 | FuncHttpsHandler(func(host string, ctx *ProxyCtx) (*ConnectAction, string) { |
| 262 | for _, cond := range pcond.reqConds { |
| 263 | if !cond.HandleReq(ctx.Req, ctx) { |
| 264 | return nil, "" |
| 265 | } |
| 266 | } |
| 267 | return &ConnectAction{Action: ConnectHijack, Hijack: f}, host |
| 268 | })) |
| 269 | } |
| 270 | |
| 271 | // ProxyConds is used to aggregate RespConditions for a ProxyHttpServer. |
| 272 | // Upon calling ProxyConds.Do, it will register a RespHandler that would |
| 273 | // handle the HTTP response from remote server if all conditions on the HTTP response are met. |
| 274 | type ProxyConds struct { |
| 275 | proxy *ProxyHttpServer |
| 276 | reqConds []ReqCondition |
| 277 | respCond []RespCondition |
| 278 | } |
| 279 | |
| 280 | // ProxyConds.DoFunc is equivalent to proxy.OnResponse().Do(FuncRespHandler(f)) |
| 281 | func (pcond *ProxyConds) DoFunc(f func(resp *http.Response, ctx *ProxyCtx) *http.Response) { |
| 282 | pcond.Do(FuncRespHandler(f)) |
| 283 | } |
| 284 | |
| 285 | // ProxyConds.Do will register the RespHandler on the proxy, h.Handle(resp,ctx) will be called on every |
| 286 | // request that matches the conditions aggregated in pcond. |
| 287 | func (pcond *ProxyConds) Do(h RespHandler) { |
| 288 | pcond.proxy.respHandlers = append(pcond.proxy.respHandlers, |
| 289 | FuncRespHandler(func(resp *http.Response, ctx *ProxyCtx) *http.Response { |
| 290 | for _, cond := range pcond.reqConds { |
| 291 | if !cond.HandleReq(ctx.Req, ctx) { |
| 292 | return resp |
| 293 | } |
| 294 | } |
| 295 | for _, cond := range pcond.respCond { |
| 296 | if !cond.HandleResp(resp, ctx) { |
| 297 | return resp |
| 298 | } |
| 299 | } |
| 300 | return h.Handle(resp, ctx) |
| 301 | })) |
| 302 | } |
| 303 | |
| 304 | // OnResponse is used when adding a response-filter to the HTTP proxy, usual pattern is |
| 305 | // proxy.OnResponse(cond1,cond2).Do(handler) // handler.Handle(resp,ctx) will be used |
| 306 | // // if cond1.HandleResp(resp) && cond2.HandleResp(resp) |
| 307 | func (proxy *ProxyHttpServer) OnResponse(conds ...RespCondition) *ProxyConds { |
| 308 | return &ProxyConds{proxy, make([]ReqCondition, 0), conds} |
| 309 | } |
| 310 | |
| 311 | // AlwaysMitm is a HttpsHandler that always eavesdrop https connections, for example to |
| 312 | // eavesdrop all https connections to www.google.com, we can use |
| 313 | // proxy.OnRequest(goproxy.ReqHostIs("www.google.com")).HandleConnect(goproxy.AlwaysMitm) |
| 314 | var AlwaysMitm FuncHttpsHandler = func(host string, ctx *ProxyCtx) (*ConnectAction, string) { |
| 315 | return MitmConnect, host |
| 316 | } |
| 317 | |
| 318 | // AlwaysReject is a HttpsHandler that drops any CONNECT request, for example, this code will disallow |
| 319 | // connections to hosts on any other port than 443 |
| 320 | // proxy.OnRequest(goproxy.Not(goproxy.ReqHostMatches(regexp.MustCompile(":443$"))). |
| 321 | // HandleConnect(goproxy.AlwaysReject) |
| 322 | var AlwaysReject FuncHttpsHandler = func(host string, ctx *ProxyCtx) (*ConnectAction, string) { |
| 323 | return RejectConnect, host |
| 324 | } |
| 325 | |
| 326 | // HandleBytes will return a RespHandler that read the entire body of the request |
| 327 | // to a byte array in memory, would run the user supplied f function on the byte arra, |
| 328 | // and will replace the body of the original response with the resulting byte array. |
| 329 | func HandleBytes(f func(b []byte, ctx *ProxyCtx) []byte) RespHandler { |
| 330 | return FuncRespHandler(func(resp *http.Response, ctx *ProxyCtx) *http.Response { |
| 331 | b, err := ioutil.ReadAll(resp.Body) |
| 332 | if err != nil { |
| 333 | ctx.Warnf("Cannot read response %s", err) |
| 334 | return resp |
| 335 | } |
| 336 | resp.Body.Close() |
| 337 | |
| 338 | resp.Body = ioutil.NopCloser(bytes.NewBuffer(f(b, ctx))) |
| 339 | return resp |
| 340 | }) |
| 341 | } |