1
|
|
|
package client |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"net" |
6
|
|
|
"net/http" |
7
|
|
|
"net/http/cookiejar" |
8
|
|
|
"net/url" |
9
|
|
|
"time" |
10
|
|
|
|
11
|
|
|
"github.com/pkg/errors" |
12
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan/client/cookie" |
13
|
|
|
"golang.org/x/net/proxy" |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
func NewClientFromConfig( |
|
|
|
|
17
|
|
|
timeoutInMilliseconds int, |
18
|
|
|
socks5Url *url.URL, |
19
|
|
|
userAgent string, |
20
|
|
|
useCookieJar bool, |
21
|
|
|
cookies []*http.Cookie, |
22
|
|
|
u *url.URL, |
23
|
|
|
) (*http.Client, error) { |
24
|
|
|
transport := http.Transport{ |
25
|
|
|
MaxIdleConns: 100, |
26
|
|
|
IdleConnTimeout: 90 * time.Second, |
27
|
|
|
TLSHandshakeTimeout: 10 * time.Second, |
28
|
|
|
ExpectContinueTimeout: 1 * time.Second, |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
c := &http.Client{ |
32
|
|
|
Timeout: time.Millisecond * time.Duration(timeoutInMilliseconds), |
33
|
|
|
Transport: &transport, |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if useCookieJar { |
37
|
|
|
jar, err := cookiejar.New(nil) |
38
|
|
|
if err != nil { |
39
|
|
|
return nil, errors.Wrap(err, "NewClientFromConfig: failed to create cookie jar") |
40
|
|
|
} |
41
|
|
|
c.Jar = jar |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if c.Jar != nil { |
45
|
|
|
c.Jar.SetCookies(u, cookies) |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if len(cookies) > 0 && c.Jar == nil { |
49
|
|
|
c.Jar = cookie.NewStatelessJar(cookies) |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if socks5Url != nil { |
53
|
|
|
tbDialer, err := proxy.FromURL(socks5Url, proxy.Direct) |
54
|
|
|
if err != nil { |
55
|
|
|
return nil, errors.Wrap(err, "NewClientFromConfig: failed to create socks5 proxy") |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
transport.DialContext = func(ctx context.Context, network, addr string) (conn net.Conn, e error) { |
59
|
|
|
return tbDialer.Dial(network, addr) |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
var err error |
64
|
|
|
c.Transport, err = decorateTransportWithUserAgentDecorator(c.Transport, userAgent) |
65
|
|
|
if err != nil { |
66
|
|
|
return nil, errors.Wrap(err, "NewClientFromConfig: failed to decorate transport") |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return c, nil |
70
|
|
|
} |
71
|
|
|
|