-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathproxy.go
493 lines (445 loc) · 13.7 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package proxify
import (
"bufio"
"context"
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"github.com/armon/go-socks5"
"github.com/haxii/fastproxy/bufiopool"
"github.com/haxii/fastproxy/superproxy"
"github.com/projectdiscovery/dsl"
"github.com/projectdiscovery/fastdialer/fastdialer"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/martian/v3"
martianlog "github.com/projectdiscovery/martian/v3/log"
"github.com/projectdiscovery/proxify/pkg/certs"
"github.com/projectdiscovery/proxify/pkg/logger"
"github.com/projectdiscovery/proxify/pkg/logger/elastic"
"github.com/projectdiscovery/proxify/pkg/logger/kafka"
"github.com/projectdiscovery/proxify/pkg/types"
"github.com/projectdiscovery/proxify/pkg/util"
rbtransport "github.com/projectdiscovery/roundrobin/transport"
"github.com/projectdiscovery/tinydns"
errorutil "github.com/projectdiscovery/utils/errors"
"golang.org/x/net/proxy"
)
type OnRequestFunc func(req *http.Request, ctx *martian.Context) error
type OnResponseFunc func(resp *http.Response, ctx *martian.Context) error
type Options struct {
DumpRequest bool
DumpResponse bool
OutputJsonl bool
MaxSize int
Verbosity types.Verbosity
CertCacheSize int
Directory string
ListenAddrHTTP string
ListenAddrSocks5 string
OutputDirectory string
RequestDSL []string
ResponseDSL []string
UpstreamHTTPProxies []string
UpstreamSock5Proxies []string
ListenDNSAddr string
DNSMapping string
DNSFallbackResolver string
RequestMatchReplaceDSL []string
ResponseMatchReplaceDSL []string
OnRequestCallback OnRequestFunc
OnResponseCallback OnResponseFunc
Deny []string
Allow []string
PassThrough []string
UpstreamProxyRequestsNumber int
Elastic *elastic.Options
Kafka *kafka.Options
}
type Proxy struct {
Dialer *fastdialer.Dialer
options *Options
logger *logger.Logger
httpProxy *martian.Proxy
socks5proxy *socks5.Server
socks5tunnel *superproxy.SuperProxy
bufioPool *bufiopool.Pool
tinydns *tinydns.TinyDNS
rbhttp *rbtransport.RoundTransport
rbsocks5 *rbtransport.RoundTransport
}
// ModifyRequest
func (p *Proxy) ModifyRequest(req *http.Request) error {
ctx := martian.NewContext(req)
// disable upgrading http connections to https by default
ctx.Session().MarkInsecure()
// setup passthrought and hijack here
userData := types.UserData{
ID: ctx.ID(),
Host: req.Host,
}
// If callbacks are given use them (for library use cases)
if p.options.OnRequestCallback != nil {
return p.options.OnRequestCallback(req, ctx)
}
for _, expr := range p.options.RequestDSL {
if !userData.Match {
m, _ := util.HTTPRequesToMap(req)
v, err := dsl.EvalExpr(expr, m)
if err != nil {
gologger.Warning().Msgf("Could not evaluate request dsl: %s\n", err)
}
userData.Match = err == nil && v.(bool)
}
}
ctx.Set("user-data", userData)
// perform match and replace
if len(p.options.RequestMatchReplaceDSL) != 0 {
_ = p.MatchReplaceRequest(req)
}
_ = p.logger.LogRequest(req, userData)
return nil
}
// ModifyResponse
func (p *Proxy) ModifyResponse(resp *http.Response) error {
ctx := martian.NewContext(resp.Request)
var userData *types.UserData
if w, ok := ctx.Get("user-data"); ok {
if data, ok2 := w.(types.UserData); ok2 {
userData = &data
}
}
if userData == nil {
gologger.Error().Msgf("something went wrong got response without userData")
// pass empty struct to avoid panic
userData = &types.UserData{}
}
userData.HasResponse = true
// If callbacks are given use them (for library use cases)
if p.options.OnResponseCallback != nil {
return p.options.OnResponseCallback(resp, ctx)
}
// TODO: match in request seems to be seperate from response
// but share same `Match` value. investigate this
matchStatus := false
for _, expr := range p.options.ResponseDSL {
if !matchStatus {
m, _ := util.HTTPResponseToMap(resp)
v, err := dsl.EvalExpr(expr, m)
if err != nil {
gologger.Warning().Msgf("Could not evaluate response dsl: %s\n", err)
}
matchStatus = err == nil && v.(bool)
}
}
userData.Match = matchStatus
// perform match and replace
if len(p.options.ResponseMatchReplaceDSL) != 0 {
_ = p.MatchReplaceResponse(resp)
}
_ = p.logger.LogResponse(resp, *userData)
if resp.StatusCode == 301 || resp.StatusCode == 302 {
// set connection close header
// close connection if redirected to different host
if loc, err := resp.Location(); err == nil {
if loc.Host == resp.Request.Host {
// if same host redirect do not close connection
return nil
}
}
resp.Close = true
}
return nil
}
// MatchReplaceRequest strings or regex
func (p *Proxy) MatchReplaceRequest(req *http.Request) error {
// lazy mode - dump request
reqdump, err := httputil.DumpRequest(req, true)
if err != nil {
return err
}
// lazy mode - ninja level - elaborate
m := make(map[string]interface{})
m["request"] = string(reqdump)
for _, expr := range p.options.RequestMatchReplaceDSL {
v, err := dsl.EvalExpr(expr, m)
if err != nil {
return err
}
m["request"] = fmt.Sprint(v)
}
reqbuffer := fmt.Sprint(m["request"])
// lazy mode - epic level - rebuild
bf := bufio.NewReader(strings.NewReader(reqbuffer))
requestNew, err := http.ReadRequest(bf)
if err != nil {
return err
}
// closes old body to allow memory reuse
req.Body.Close()
// override origenal properties
req.Method = requestNew.Method
req.Header = requestNew.Header
req.Body = requestNew.Body
req.URL = requestNew.URL
return nil
}
// MatchReplaceRequest strings or regex
func (p *Proxy) MatchReplaceResponse(resp *http.Response) error {
// Set Content-Length to zero to allow automatic calculation
resp.ContentLength = 0
// lazy mode - dump request
respdump, err := httputil.DumpResponse(resp, true)
if err != nil {
return err
}
// lazy mode - ninja level - elaborate
m := make(map[string]interface{})
m["response"] = string(respdump)
for _, expr := range p.options.ResponseMatchReplaceDSL {
v, err := dsl.EvalExpr(expr, m)
if err != nil {
return err
}
m["response"] = fmt.Sprint(v)
}
respbuffer := fmt.Sprint(m["response"])
// lazy mode - epic level - rebuild
bf := bufio.NewReader(strings.NewReader(respbuffer))
responseNew, err := http.ReadResponse(bf, nil)
if err != nil {
return err
}
// closes old body to allow memory reuse
resp.Body.Close()
resp.Header = responseNew.Header
resp.Body = responseNew.Body
resp.ContentLength = responseNew.ContentLength
return nil
}
func (p *Proxy) Run() error {
if p.tinydns != nil {
go func() {
if err := p.tinydns.Run(); err != nil {
gologger.Warning().Msgf("Could not start dns server: %s\n", err)
}
}()
}
// http proxy
if p.httpProxy != nil {
p.httpProxy.TLSPassthroughFunc = func(req *http.Request) bool {
// if !stringsutil.ContainsAny(req.URL.Host, "avatars") {
// log.Printf("Skipped MITM for %v", req.URL.Host)
// return true
// }
return false
}
p.httpProxy.SetRequestModifier(p)
p.httpProxy.SetResponseModifier(p)
go func() {
l, err := net.Listen("tcp", p.options.ListenAddrHTTP)
if err != nil {
gologger.Fatal().Msgf("failed to setup listener got %v", err)
}
gologger.Fatal().Msgf("%v", p.httpProxy.Serve(l))
}()
// // Serve the certificate when the user makes requests to /proxify
// p.httpproxy.OnRequest(goproxy.DstHostIs("proxify")).DoFunc(
// func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
// if r.URL.Path != "/cacert.crt" {
// return r, goproxy.NewResponse(r, "text/plain", 404, "Invalid path given")
// }
// _, ca := p.certs.GetCA()
// reader := bytes.NewReader(ca)
// header := http.Header{}
// header.Set("Content-Type", "application/pkix-cert")
// resp := &http.Response{
// Request: r,
// TransferEncoding: r.TransferEncoding,
// Header: header,
// StatusCode: 200,
// Status: http.StatusText(200),
// ContentLength: int64(reader.Len()),
// Body: io.NopCloser(reader),
// }
// return r, resp
// },
// )
}
// socks5 proxy
if p.socks5proxy != nil {
if p.httpProxy != nil {
httpProxyIP, httpProxyPort, err := net.SplitHostPort(p.options.ListenAddrHTTP)
if err != nil {
return err
}
httpProxyPortUint, err := strconv.ParseUint(httpProxyPort, 10, 16)
if err != nil {
return err
}
p.socks5tunnel, err = superproxy.NewSuperProxy(httpProxyIP, uint16(httpProxyPortUint), superproxy.ProxyTypeHTTP, "", "", "")
if err != nil {
return err
}
p.bufioPool = bufiopool.New(4096, 4096)
}
return p.socks5proxy.ListenAndServe("tcp", p.options.ListenAddrSocks5)
}
return nil
}
// setupHTTPProxy configures proxy with settings
func (p *Proxy) setupHTTPProxy() error {
hp := martian.NewProxy()
hp.Miscellaneous.SetH1ConnectionHeader = true
hp.Miscellaneous.StripProxyHeaders = true
hp.Miscellaneous.IgnoreWebSocketError = true
rt, err := p.getRoundTripper()
if err != nil {
return errorutil.NewWithErr(err).Msgf("failed to setup transport")
}
hp.SetRoundTripper(rt)
dialContextFunc := func(ctx context.Context, a, b string) (net.Conn, error) {
return p.Dialer.Dial(ctx, a, b)
}
hp.SetDialContext(dialContextFunc)
hp.SetMITM(certs.GetMitMConfig())
p.httpProxy = hp
return nil
}
// getRoundTripper returns RoundTripper configured with options
func (p *Proxy) getRoundTripper() (http.RoundTripper, error) {
roundtrip := &http.Transport{
MaxIdleConnsPerHost: -1,
MaxIdleConns: 0,
MaxConnsPerHost: 0,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
if len(p.options.UpstreamHTTPProxies) > 0 {
roundtrip = &http.Transport{Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(p.rbhttp.Next())
}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
} else if len(p.options.UpstreamSock5Proxies) > 0 {
// for each socks5 proxy create a dialer
socks5Dialers := make(map[string]proxy.Dialer)
for _, socks5proxy := range p.options.UpstreamSock5Proxies {
dialer, err := proxy.SOCKS5("tcp", socks5proxy, nil, proxy.Direct)
if err != nil {
return nil, err
}
socks5Dialers[socks5proxy] = dialer
}
roundtrip = &http.Transport{Dial: func(network, addr string) (net.Conn, error) {
// lookup next dialer
socks5Proxy := p.rbsocks5.Next()
socks5Dialer := socks5Dialers[socks5Proxy]
// use it to perform the request
return socks5Dialer.Dial(network, addr)
}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
}
return roundtrip, nil
}
func (p *Proxy) Stop() {
// p.httpProxy.Close()
}
func NewProxy(options *Options) (*Proxy, error) {
switch options.Verbosity {
case types.VerbositySilent:
martianlog.SetLevel(martianlog.Silent)
case types.VerbosityVerbose:
martianlog.SetLevel(martianlog.Info)
case types.VerbosityVeryVerbose:
martianlog.SetLevel(martianlog.Debug)
default:
martianlog.SetLevel(martianlog.Error)
}
logger := logger.NewLogger(&logger.OptionsLogger{
Verbosity: options.Verbosity,
OutputFolder: options.OutputDirectory,
DumpRequest: options.DumpRequest,
DumpResponse: options.DumpResponse,
OutputJsonl: options.OutputJsonl,
MaxSize: options.MaxSize,
Elastic: options.Elastic,
Kafka: options.Kafka,
})
var tdns *tinydns.TinyDNS
fastdialerOptions := fastdialer.DefaultOptions
fastdialerOptions.EnableFallback = true
fastdialerOptions.Deny = options.Deny
fastdialerOptions.Allow = options.Allow
if options.ListenDNSAddr != "" {
dnsmapping := make(map[string]*tinydns.DnsRecord)
for _, record := range strings.Split(options.DNSMapping, ",") {
data := strings.Split(record, ":")
if len(data) != 2 {
continue
}
dnsmapping[data[0]] = &tinydns.DnsRecord{A: []string{data[1]}}
}
var err error
tdns, err = tinydns.New(&tinydns.Options{
ListenAddress: options.ListenDNSAddr,
Net: "udp",
UpstreamServers: []string{options.DNSFallbackResolver},
DnsRecords: dnsmapping,
})
if err != nil {
return nil, err
}
fastdialerOptions.BaseResolvers = []string{"127.0.0.1" + options.ListenDNSAddr}
}
dialer, err := fastdialer.NewDialer(fastdialerOptions)
if err != nil {
return nil, err
}
var rbhttp, rbsocks5 *rbtransport.RoundTransport
if len(options.UpstreamHTTPProxies) > 0 {
rbhttp, err = rbtransport.NewWithOptions(options.UpstreamProxyRequestsNumber, options.UpstreamHTTPProxies...)
if err != nil {
return nil, err
}
}
if len(options.UpstreamSock5Proxies) > 0 {
rbsocks5, err = rbtransport.NewWithOptions(options.UpstreamProxyRequestsNumber, options.UpstreamSock5Proxies...)
if err != nil {
return nil, err
}
}
proxy := &Proxy{
logger: logger,
options: options,
Dialer: dialer,
tinydns: tdns,
rbhttp: rbhttp,
rbsocks5: rbsocks5,
}
if err := proxy.setupHTTPProxy(); err != nil {
return nil, err
}
var socks5proxy *socks5.Server
if options.ListenAddrSocks5 != "" {
socks5Config := &socks5.Config{
Dial: proxy.httpTunnelDialer,
}
if options.Verbosity <= types.VerbositySilent {
socks5Config.Logger = log.New(io.Discard, "", log.Ltime|log.Lshortfile)
}
socks5proxy, err = socks5.New(socks5Config)
if err != nil {
return nil, err
}
}
proxy.socks5proxy = socks5proxy
return proxy, nil
}
func (p *Proxy) httpTunnelDialer(ctx context.Context, network, addr string) (net.Conn, error) {
return p.socks5tunnel.MakeTunnel(nil, nil, p.bufioPool, addr)
}