-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
283 lines (248 loc) · 6.92 KB
/
Copy pathclient.go
File metadata and controls
283 lines (248 loc) · 6.92 KB
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
package tlsclient
import (
"errors"
"io"
"net"
"net/url"
"strings"
"sync/atomic"
"time"
"github.com/nukilabs/http"
"github.com/nukilabs/http/cookiejar"
"github.com/nukilabs/quic-go"
"github.com/nukilabs/tlsclient/bandwidth"
"github.com/nukilabs/tlsclient/profiles"
"github.com/nukilabs/tlsclient/proxy"
tls "github.com/nukilabs/utls"
"golang.org/x/net/publicsuffix"
)
type Client struct {
http.Client
profile profiles.ClientProfile
pinner *Pinner
tracker bandwidth.Tracker
proxyVal any
preHooks []PreHook
postHooks []PostHook
inHook atomic.Bool
redirect func(req *http.Request, via []*http.Request) error
tlsConf *tls.Config
quicConf *quic.Config
opts *TransportOptions
AutoDecompress bool
}
type PreHook func(*Client, *http.Request) (*http.Request, error)
type PostHook func(*Client, *http.Request, *http.Response) (*http.Response, error)
func New(profile profiles.ClientProfile, options ...Option) *Client {
jar, _ := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
client := &Client{
Client: http.Client{
Timeout: 30 * time.Second,
Jar: jar,
CheckRedirect: nil,
},
profile: profile,
tlsConf: &tls.Config{},
quicConf: &quic.Config{
KeepAlivePeriod: 30 * time.Second,
EnableDatagrams: true,
},
AutoDecompress: true,
tracker: bandwidth.NewNoopTracker(),
}
for _, option := range options {
option(client)
}
if client.pinner == nil {
client.pinner = NewPinner(false)
}
dialer := proxy.Direct(nil, client.Timeout)
client.Transport = NewRoundTripper(profile, dialer, client.pinner, client.tracker, client.tlsConf, client.quicConf, client.opts)
return client
}
func (c *Client) Clone() *Client {
jar, _ := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
clone := &Client{
Client: http.Client{
Timeout: c.Client.Timeout,
Jar: jar,
CheckRedirect: c.Client.CheckRedirect,
},
profile: c.profile,
pinner: c.pinner,
tracker: c.tracker,
redirect: c.redirect,
tlsConf: c.tlsConf.Clone(),
quicConf: c.quicConf.Clone(),
opts: c.opts,
AutoDecompress: c.AutoDecompress,
}
dialer := proxy.Direct(nil, clone.Timeout)
clone.Transport = NewRoundTripper(clone.profile, dialer, clone.pinner, clone.tracker, clone.tlsConf, clone.quicConf, clone.opts)
return clone
}
// GetProxy returns the current proxy value. The type depends on what was passed to SetProxy:
// *url.URL for proxy URLs, net.IP for single direct IPs, or [2]net.IP for dual-stack.
func (c *Client) GetProxy() any {
return c.proxyVal
}
// SetProxy configures the proxy/dialer for the client. Accepted types:
// - *url.URL: proxy URL (http, https, socks5) or bare IP (no scheme) for direct binding
// - net.IP: direct connection bound to a single local IP
// - [2]net.IP: dual-stack direct connection bound to IPv4 ([0]) and IPv6 ([1])
// - nil: direct connection with no local address binding
func (c *Client) SetProxy(v any) error {
var dialer proxy.ContextDialer
var err error
switch v := v.(type) {
case *url.URL:
dialer, err = proxy.New(v, c.Timeout, c.tlsConf)
case net.IP:
dialer = proxy.Direct(v, c.Timeout)
case [2]net.IP:
dialer = proxy.DirectDualStack(v[0], v[1], c.Timeout)
case nil:
dialer = proxy.Direct(nil, c.Timeout)
default:
return errors.New("unsupported proxy type")
}
if err != nil {
return err
}
c.proxyVal = v
c.Transport = NewRoundTripper(c.profile, dialer, c.pinner, c.tracker, c.tlsConf, c.quicConf, c.opts)
return nil
}
func (c *Client) SetPreHooks(hooks ...PreHook) {
c.preHooks = hooks
}
func (c *Client) AddPreHooks(hooks ...PreHook) {
c.preHooks = append(c.preHooks, hooks...)
}
func (c *Client) DeletePreHooks() {
c.preHooks = nil
}
func (c *Client) SetPostHooks(hooks ...PostHook) {
c.postHooks = hooks
}
func (c *Client) AddPostHooks(hooks ...PostHook) {
c.postHooks = append(c.postHooks, hooks...)
}
func (c *Client) DeletePostHooks() {
c.postHooks = nil
}
func (c *Client) SetCookieJar(jar http.CookieJar) {
c.Client.Jar = jar
}
func (c *Client) GetCookieJar() http.CookieJar {
return c.Client.Jar
}
func (c *Client) GetCookies(u *url.URL) []*http.Cookie {
return c.Client.Jar.Cookies(u)
}
func (c *Client) GetSiteCookies(u *url.URL) []*http.Cookie {
jar, ok := c.Client.Jar.(http.SiteCookieJar)
if !ok {
return nil
}
return jar.SiteCookies(u)
}
func (c *Client) SetCookies(u *url.URL, cookies []*http.Cookie) {
c.Client.Jar.SetCookies(u, cookies)
}
func (c *Client) CloseIdleConnections() {
c.Client.CloseIdleConnections()
}
func (c *Client) SetRedirectFunc(f func(req *http.Request, via []*http.Request) error) {
g := func(req *http.Request, via []*http.Request) error {
err := f(req, via)
if err != nil {
return err
}
for _, hook := range c.preHooks {
req, err = hook(c, req)
if err != nil {
return err
}
}
return nil
}
c.Client.CheckRedirect = g
c.redirect = g
}
func (c *Client) SetFollowRedirects(follow bool) {
if follow && c.redirect != nil {
c.Client.CheckRedirect = c.redirect
} else if follow {
c.Client.CheckRedirect = nil
} else {
c.Client.CheckRedirect = defaultRedirectFunc
}
}
func (c *Client) SetInHook(b bool) {
c.inHook.Store(b)
}
func (c *Client) IsInHook() bool {
return c.inHook.Load()
}
func (c *Client) ResetInHook() {
c.inHook.Store(false)
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
var err error
for _, hook := range c.preHooks {
if c.inHook.CompareAndSwap(false, true) {
req, err = hook(c, req)
c.inHook.Store(false)
if err != nil {
return nil, err
}
}
}
res, err := c.Client.Do(req)
if err != nil {
return nil, err
}
if c.AutoDecompress {
DecompressBody(res)
}
for _, hook := range c.postHooks {
if c.inHook.CompareAndSwap(false, true) {
res, err = hook(c, req, res)
c.inHook.Store(false)
if err != nil {
return nil, err
}
}
}
return res, nil
}
func (c *Client) Get(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
func (c *Client) Head(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
func (c *Client) Post(url, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return c.Do(req)
}
func (c *Client) PostForm(url string, data url.Values) (*http.Response, error) {
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}