This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
181 lines (161 loc) · 3.68 KB
/
client.go
File metadata and controls
181 lines (161 loc) · 3.68 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
package swifthttp
import (
"github.com/SyNdicateFoundation/fastrand"
"github.com/valyala/bytebufferpool"
"golang.org/x/net/http2"
"net/http"
"sync"
"time"
"github.com/SyNdicateFoundation/legitagent"
"github.com/SyNdicateFoundation/singproxy"
utls "github.com/refraction-networking/utls"
)
var defaultTimeout = HttpTimeout{
Dial: time.Second * 5,
Request: time.Second * 10,
}
func NewHttpClient(options ...OptionFunc) *Client {
hc := &Client{
timeout: defaultTimeout,
httpVersion: HttpVersion1_1,
tls: &HttpTLSConfig{
TLSMode: HttpTlsModeAutoTLS,
},
h2StreamPool: sync.Pool{
New: func() interface{} {
return &h2Stream{
header: make(http.Header),
body: bytebufferpool.Get(),
}
},
},
hpackEncoderBufPool: sync.Pool{
New: func() interface{} {
return new(bytebufferpool.ByteBuffer)
},
},
}
for _, option := range options {
option(hc)
}
return hc
}
func WithTimeout(timeout HttpTimeout) OptionFunc {
return func(hc *Client) {
hc.timeout = timeout
}
}
func WithDialTimeout(timeout time.Duration) OptionFunc {
return func(hc *Client) {
hc.timeout.Dial = timeout
}
}
func WithRequestTimeout(timeout time.Duration) OptionFunc {
return func(hc *Client) {
hc.timeout.Request = timeout
}
}
func WithAgentGenerator(gen *legitagent.Generator) OptionFunc {
return func(hc *Client) {
hc.legitAgentGenerator = gen
}
}
func WithIpSpoofer(perSession bool, useIpv6 bool) OptionFunc {
return func(hc *Client) {
if hc.ipSpoofing == nil {
hc.ipSpoofing = new(ipSpoofConfig)
}
hc.ipSpoofing.enabled = true
hc.ipSpoofing.perSessionIp = perSession
hc.ipSpoofing.useIpv6 = useIpv6
}
}
func WithProxy(proxy singproxy.Proxy) OptionFunc {
return func(hc *Client) {
if proxy == nil {
panic("WithProxy: proxy cannot be nil")
}
hc.proxy = proxy
}
}
func WithCustomTLSConfig(config *HttpTLSConfig) OptionFunc {
return func(hc *Client) {
if config == nil {
panic("WithCustomTLSConfig: tls config cannot be nil")
}
hc.tls = config
}
}
func WithTLS(tlsMode HttpTlsMode) OptionFunc {
return func(hc *Client) {
if hc.tls == nil {
hc.tls = new(HttpTLSConfig)
}
hc.tls.TLSMode = tlsMode
}
}
func WithTLSCustomConfig(config *utls.Config) OptionFunc {
return func(hc *Client) {
if config == nil {
panic("WithTLSCustomConfig: utls.Config cannot be nil")
}
if hc.tls == nil {
hc.tls = new(HttpTLSConfig)
}
hc.tls.UTLSConfig = config
}
}
func WithOptimizedTLS(optimized bool) OptionFunc {
return func(hc *Client) {
if hc.tls == nil {
hc.tls = new(HttpTLSConfig)
}
hc.tls.OptimizedConn = optimized
}
}
func WithVersion(httpVersion HttpVersion) OptionFunc {
return func(hc *Client) {
hc.httpVersion = httpVersion
if hc.httpVersion == HttpVersion3_0 || hc.httpVersion == HttpVersionSPDY31 {
if hc.tls == nil {
hc.tls = new(HttpTLSConfig)
}
hc.tls.TLSMode = HttpTlsModeForever
}
}
}
func WithRandomizer(randomizer fastrand.Engine) OptionFunc {
return func(hc *Client) {
hc.randomizer = randomizer
}
}
func WithRandomizedHeaderSort(randomize bool) OptionFunc {
return func(hc *Client) {
hc.randomizeHeaderSort = randomize
}
}
func WithEnableReader(enable bool) OptionFunc {
return func(hc *Client) {
hc.enableReaderLoop = enable
}
}
func WithCustomH2Settings(settings []http2.Setting) OptionFunc {
return func(client *Client) {
client.customH2Settings = settings
}
}
func WithoutPooling() OptionFunc {
return func(hc *Client) {
hc.h2StreamPool = sync.Pool{
New: func() any {
return &h2Stream{
header: make(http.Header),
body: bytebufferpool.Get(),
}
},
}
hc.hpackEncoderBufPool = sync.Pool{
New: func() any { return new(bytebufferpool.ByteBuffer) },
}
}
}