Debian Codegraph @

dispatcher.go

raw symbols SCIP
1 package goproxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/"
5 "io/ioutilscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/"
6 "netscip-go gomod github.com/golang/go/src go1.18 net/"
7 "net/httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/"
8 "regexpscip-go gomod github.com/golang/go/src go1.18 regexp/"
9 "stringsscip-go gomod github.com/golang/go/src go1.18 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 ReqConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition# interface {
15 RespConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespCondition#
16 HandleReqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#HandleReq.(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/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 RespConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespCondition# interface {
23 HandleRespscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespCondition#HandleResp.(resp *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool
24 }
25
26 // ReqConditionFunc.HandleReq(req,ctx) <=> ReqConditionFunc(req,ctx)
27 type ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc# func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool
28
29 // RespConditionFunc.HandleResp(resp,ctx) <=> RespConditionFunc(resp,ctx)
30 type RespConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespConditionFunc# func(resp *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool
31
32 func (c ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc#) HandleReqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc#HandleReq().(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/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 ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc#) HandleRespscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc#HandleResp().(resp *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
39 return c(ctx.Reqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#Req., ctx)
40 }
41
42 func (c RespConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespConditionFunc#) HandleRespscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespConditionFunc#HandleResp().(resp *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/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 UrlHasPrefixscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/UrlHasPrefix().(prefix string) ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc# {
51 return func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
52 return stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Path., prefix) ||
53 stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host.+req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Path., prefix) ||
54 stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Schemescip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Scheme.+req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host.+req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.18 `net/url`/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 UrlIsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/UrlIs().(urls ...string) ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc# {
63 urlSet := make(map[string]bool)
64 for _, u := range urls {
65 urlSet[u] = true
66 }
67 return func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
68 _, pathOk := urlSet[req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Path.]
69 _, hostAndOk := urlSet[req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host.+req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.18 `net/url`/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 ReqHostMatchesscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqHostMatches().(regexps ...*regexpscip-go gomod github.com/golang/go/src go1.18 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.18 regexp/Regexp#) ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc# {
77 return func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
78 for _, re := range regexps {
79 if re.MatchStringscip-go gomod github.com/golang/go/src go1.18 regexp/Regexp#MatchString().(req.Hostscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#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 ReqHostIsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqHostIs().(hosts ...string) ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc# {
90 hostSet := make(map[string]bool)
91 for _, h := range hosts {
92 hostSet[h] = true
93 }
94 return func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
95 _, ok := hostSet[req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host.]
96 return ok
97 }
98 }
99
100 var localHostIpv4scip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/localHostIpv4. = regexpscip-go gomod github.com/golang/go/src go1.18 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.18 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 IsLocalHostscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/IsLocalHost. ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc# = func(reqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/IsLocalHost:req. *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/IsLocalHost:ctx. *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
105 return reqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/IsLocalHost:req..URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host. == "::1" ||
106 reqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/IsLocalHost:req..URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host. == "0:0:0:0:0:0:0:1" ||
107 localHostIpv4scip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/localHostIpv4..MatchStringscip-go gomod github.com/golang/go/src go1.18 regexp/Regexp#MatchString().(reqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/IsLocalHost:req..URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host.) ||
108 reqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/IsLocalHost:req..URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/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 UrlMatchesscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/UrlMatches().(re *regexpscip-go gomod github.com/golang/go/src go1.18 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.18 regexp/Regexp#) ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc# {
114 return func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
115 return re.MatchStringscip-go gomod github.com/golang/go/src go1.18 regexp/Regexp#MatchString().(req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Path.) ||
116 re.MatchStringscip-go gomod github.com/golang/go/src go1.18 regexp/Regexp#MatchString().(req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host.+req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Path.)
117 }
118 }
119
120 // DstHostIs returns a ReqCondition testing wether the host in the request url is the given string
121 func DstHostIsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/DstHostIs().(host string) ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc# {
122 return func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
123 return req.URLscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#URL..Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/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 SrcIpIsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/SrcIpIs().(ips ...string) ReqConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition# {
129 return ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc#(func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
130 for _, ip := range ips {
131 if stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(req.RemoteAddrscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#RemoteAddr., ip+":") {
132 return true
133 }
134 }
135 return false
136 })
137 }
138
139 // Not returns a ReqCondition negating the given ReqCondition
140 func Notscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/Not().(r ReqConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#) ReqConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqConditionFunc# {
141 return func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
142 return !r.HandleReqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#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 ContentTypeIsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ContentTypeIs().(typ string, types ...string) RespConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespCondition# {
149 types = append(types, typ)
150 return RespConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespConditionFunc#(func(resp *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
151 if resp == nil {
152 return false
153 }
154 contentType := resp.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#Header..Getscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Get().("Content-Type")
155 for _, typ := range types {
156 if contentType == typ || stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 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 StatusCodeIsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/StatusCodeIs().(codes ...int) RespConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespCondition# {
167 codeSet := make(map[int]bool)
168 for _, c := range codes {
169 codeSet[c] = true
170 }
171 return RespConditionFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespConditionFunc#(func(resp *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) bool {
172 if resp == nil {
173 return false
174 }
175 _, codeMatch := codeSet[resp.StatusCodescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#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 *ProxyHttpServerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#) OnRequestscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#OnRequest().(conds ...ReqConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#) *ReqProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds# {
186 return &ReqProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/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 ReqProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds# struct {
192 proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#proxy. *ProxyHttpServerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#
193 reqCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#reqConds. []ReqConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#
194 }
195
196 // DoFunc is equivalent to proxy.OnRequest().Do(FuncReqHandler(f))
197 func (pcond *ReqProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#) DoFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#DoFunc().(f func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) (*httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#)) {
198 pcond.Doscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#Do().(FuncReqHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/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 *ReqProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#) Doscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#Do().(h ReqHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqHandler#) {
209 pcond.proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#proxy..reqHandlersscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#reqHandlers. = append(pcond.proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#proxy..reqHandlersscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#reqHandlers.,
210 FuncReqHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/FuncReqHandler#(func(r *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) (*httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#) {
211 for _, cond := range pcond.reqCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#reqConds. {
212 if !cond.HandleReqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#HandleReq.(r, ctx) {
213 return r, nil
214 }
215 }
216 return h.Handlescip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqHandler#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 *ReqProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#) HandleConnectscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#HandleConnect().(h HttpsHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/HttpsHandler#) {
232 pcond.proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#proxy..httpsHandlersscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#httpsHandlers. = append(pcond.proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#proxy..httpsHandlersscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#httpsHandlers.,
233 FuncHttpsHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/FuncHttpsHandler#(func(host string, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) (*ConnectActionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ConnectAction#, string) {
234 for _, cond := range pcond.reqCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#reqConds. {
235 if !cond.HandleReqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#HandleReq.(ctx.Reqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#Req., ctx) {
236 return nil, ""
237 }
238 }
239 return h.HandleConnectscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/HttpsHandler#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 *ReqProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#) HandleConnectFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#HandleConnectFunc().(f func(host string, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) (*ConnectActionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ConnectAction#, string)) {
256 pcond.HandleConnectscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#HandleConnect().(FuncHttpsHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/FuncHttpsHandler#(f))
257 }
258
259 func (pcond *ReqProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#) HijackConnectscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#HijackConnect().(f func(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#, client netscip-go gomod github.com/golang/go/src go1.18 net/.Connscip-go gomod github.com/golang/go/src go1.18 net/Conn#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#)) {
260 pcond.proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#proxy..httpsHandlersscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#httpsHandlers. = append(pcond.proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#proxy..httpsHandlersscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#httpsHandlers.,
261 FuncHttpsHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/FuncHttpsHandler#(func(host string, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) (*ConnectActionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ConnectAction#, string) {
262 for _, cond := range pcond.reqCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqProxyConds#reqConds. {
263 if !cond.HandleReqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#HandleReq.(ctx.Reqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#Req., ctx) {
264 return nil, ""
265 }
266 }
267 return &ConnectActionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ConnectAction#{Actionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ConnectAction#Action.: ConnectHijackscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ConnectHijack., Hijackscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ConnectAction#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 ProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds# struct {
275 proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#proxy. *ProxyHttpServerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#
276 reqCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#reqConds. []ReqConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#
277 respCondscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#respCond. []RespConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespCondition#
278 }
279
280 // ProxyConds.DoFunc is equivalent to proxy.OnResponse().Do(FuncRespHandler(f))
281 func (pcond *ProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#) DoFuncscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#DoFunc().(f func(resp *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#) {
282 pcond.Doscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#Do().(FuncRespHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/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 *ProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#) Doscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#Do().(h RespHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespHandler#) {
288 pcond.proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#proxy..respHandlersscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#respHandlers. = append(pcond.proxyscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#proxy..respHandlersscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#respHandlers.,
289 FuncRespHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/FuncRespHandler#(func(resp *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response# {
290 for _, cond := range pcond.reqCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#reqConds. {
291 if !cond.HandleReqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ReqCondition#HandleReq.(ctx.Reqscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#Req., ctx) {
292 return resp
293 }
294 }
295 for _, cond := range pcond.respCondscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#respCond. {
296 if !cond.HandleRespscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespCondition#HandleResp.(resp, ctx) {
297 return resp
298 }
299 }
300 return h.Handlescip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespHandler#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 *ProxyHttpServerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#) OnResponsescip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyHttpServer#OnResponse().(conds ...RespConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespCondition#) *ProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds# {
308 return &ProxyCondsscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyConds#{proxy, make([]ReqConditionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/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 AlwaysMitmscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/AlwaysMitm. FuncHttpsHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/FuncHttpsHandler# = func(hostscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/AlwaysMitm:host. string, ctxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/AlwaysMitm:ctx. *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) (*ConnectActionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ConnectAction#, string) {
315 return MitmConnectscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/MitmConnect., hostscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/AlwaysMitm: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 AlwaysRejectscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/AlwaysReject. FuncHttpsHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/FuncHttpsHandler# = func(hostscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/AlwaysReject:host. string, ctxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/AlwaysReject:ctx. *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) (*ConnectActionscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ConnectAction#, string) {
323 return RejectConnectscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RejectConnect., hostscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/AlwaysReject: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 HandleBytesscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/HandleBytes().(f func(b []byte, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) []byte) RespHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/RespHandler# {
330 return FuncRespHandlerscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/FuncRespHandler#(func(resp *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#, ctx *ProxyCtxscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#) *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response# {
331 b, err := ioutilscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/.ReadAllscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/ReadAll().(resp.Bodyscip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#Body.)
332 if err != nil {
333 ctx.Warnfscip-go gomod github.com/elazarl/goproxy . `github.com/elazarl/goproxy`/ProxyCtx#Warnf().("Cannot read response %s", err)
334 return resp
335 }
336 resp.Bodyscip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src go1.18 io/Closer#Close().()
337
338 resp.Bodyscip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#Body. = ioutilscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/.NopCloserscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/NopCloser().(bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.NewBufferscip-go gomod github.com/golang/go/src go1.18 bytes/NewBuffer().(f(b, ctx)))
339 return resp
340 })
341 }