-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgoclient.go
233 lines (228 loc) · 7.53 KB
/
goclient.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
package gorequest
import (
"context"
"errors"
"net"
"strconv"
"strings"
"time"
http "github.com/kawacode/fhttp"
http2 "github.com/kawacode/fhttp/http2"
goproxy "github.com/kawacode/goproxy"
gostruct "github.com/kawacode/gostruct"
gotools "github.com/kawacode/gotools"
tls "github.com/kawacode/utls"
)
// Create a client based on the protocol version
func CreateClient(bot *gostruct.BotData) (*http.Client, error) {
var client *http.Client
if bot.HttpRequest.Request.Protocol == "1" {
var err error
client, err = CreateHttp1Client(bot)
if err != nil {
return nil, err
}
} else {
var err error
client, err = CreateHttp2Client(bot)
if err != nil {
return nil, err
}
}
return client, nil
}
// It creates an HTTP/1.1 client with the ability to use a proxy, disable redirects, and set a timeout, that uses a custom TLS dialer that uses a custom JA3 fingerprint
func CreateHttp1Client(bot *gostruct.BotData) (*http.Client, error) {
http1transport := http.Transport{
DisableCompression: bot.HttpRequest.Request.DisableCompression,
DisableKeepAlives: true,
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
tls.EnableWeakCiphers()
var conn net.Conn
if len(bot.HttpRequest.Request.Proxy) > 0 {
dialer, err := goproxy.CreateProxyDialer(bot.HttpRequest.Request.Proxy)
if err != nil {
return nil, err
}
con, err := dialer.Dial(network, addr)
if err != nil {
return nil, err
}
conn = con
} else {
var err error
conn, err = net.Dial(network, addr)
if err != nil {
return nil, err
}
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
config := &tls.Config{ServerName: host, InsecureSkipVerify: bot.HttpRequest.Request.InsecureSkipVerify}
var uconn *tls.UConn
if bot.HttpRequest.Request.HelloClient.Str() != "-" {
uconn = tls.UClient(conn, config, bot.HttpRequest.Request.HelloClient)
if strings.Contains(bot.HttpRequest.Request.HelloClient.Str(), "CustomInternal") {
if bot.HttpRequest.Request.Ja3 == "" {
return nil, errors.New("missing clientspec/Ja3")
}
if bot.HttpRequest.Request.Protocol != "2" && bot.HttpRequest.Request.Protocol != "1" {
bot.HttpRequest.Request.Protocol = "2"
}
tlsspec, err := gotools.ParseJA3(bot.HttpRequest.Request.Ja3, bot.HttpRequest.Request.Protocol)
if err != nil {
return nil, err
}
if err := uconn.ApplyPreset(tlsspec); err != nil {
return nil, err
}
if err := uconn.Handshake(); err != nil {
return nil, err
}
}
} else {
uconn = tls.UClient(conn, config, tls.HelloChrome_Auto)
}
return uconn, nil
},
}
timeout := gotools.IsInt(bot.HttpRequest.Request.Timeout)
var client *http.Client
if timeout {
timeoutsec, _ := strconv.ParseInt(bot.HttpRequest.Request.Timeout, 0, 64)
client = &http.Client{
Transport: &http1transport,
Timeout: time.Duration(time.Duration(timeoutsec) * time.Second),
}
} else {
client = &http.Client{
Transport: &http1transport,
Timeout: time.Duration(time.Duration(30) * time.Second),
}
}
if gotools.IsInt(bot.HttpRequest.Request.MaxRedirects) {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
maxredirects, _ := strconv.ParseInt(bot.HttpRequest.Request.MaxRedirects, 0, 16)
if len(via) >= int(maxredirects) {
return http.ErrUseLastResponse
}
return nil
}
} else if bot.HttpRequest.Request.MaxRedirects == "false" {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
} else {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
maxredirects := 10
if len(via) >= maxredirects {
return http.ErrUseLastResponse
}
return nil
}
}
return client, nil
}
// It creates an HTTP2 client with the ability to use a proxy, disable redirects, and set a timeout, that uses a custom TLS dialer that uses a custom JA3 fingerprint
func CreateHttp2Client(bot *gostruct.BotData) (*http.Client, error) {
http2transport := http2.Transport{
AllowHTTP: bot.HttpRequest.Request.InsecureSkipVerify,
StrictMaxConcurrentStreams: false,
DisableCompression: bot.HttpRequest.Request.DisableCompression,
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
tls.EnableWeakCiphers()
var conn net.Conn
if len(bot.HttpRequest.Request.Proxy) > 0 {
dialer, err := goproxy.CreateProxyDialer(bot.HttpRequest.Request.Proxy)
if err != nil {
return nil, err
}
con, err := dialer.Dial(network, addr)
if err != nil {
return nil, err
}
conn = con
} else {
var err error
conn, err = net.Dial(network, addr)
if err != nil {
return nil, err
}
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
config := &tls.Config{ServerName: host, InsecureSkipVerify: bot.HttpRequest.Request.InsecureSkipVerify}
var uconn *tls.UConn
if bot.HttpRequest.Request.HelloClient.Str() != "-" {
uconn = tls.UClient(conn, config, bot.HttpRequest.Request.HelloClient)
if strings.Contains(bot.HttpRequest.Request.HelloClient.Str(), "CustomInternal") {
if bot.HttpRequest.Request.Ja3 == "" {
return nil, errors.New("missing clientspec/Ja3")
}
if bot.HttpRequest.Request.Protocol != "2" && bot.HttpRequest.Request.Protocol != "1" {
bot.HttpRequest.Request.Protocol = "2"
}
tlsspec, err := gotools.ParseJA3(bot.HttpRequest.Request.Ja3, bot.HttpRequest.Request.Protocol)
if err != nil {
return nil, err
}
if err := uconn.ApplyPreset(tlsspec); err != nil {
return nil, err
}
if err := uconn.Handshake(); err != nil {
return nil, err
}
}
} else {
uconn = tls.UClient(conn, config, tls.HelloChrome_Auto)
}
return uconn, nil
},
}
Settings, SettingsOrder, Priorities, windowupdate := gotools.GetFrameSettingsStringList(bot)
http2transport.SettingsOrder = SettingsOrder
http2transport.Settings = Settings
http2transport.Priorities = Priorities
http2transport.ConnectionFlow = uint32(windowupdate)
http2transport.PushHandler = &http2.DefaultPushHandler{}
timeout := gotools.IsInt(bot.HttpRequest.Request.Timeout)
var client *http.Client
if timeout {
timeoutsec, _ := strconv.ParseInt(bot.HttpRequest.Request.Timeout, 0, 64)
client = &http.Client{
Transport: &http2transport,
Timeout: time.Duration(time.Duration(timeoutsec) * time.Second),
}
} else {
client = &http.Client{
Transport: &http2transport,
Timeout: time.Duration(time.Duration(30) * time.Second),
}
}
if gotools.IsInt(bot.HttpRequest.Request.MaxRedirects) {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
maxredirects, _ := strconv.ParseInt(bot.HttpRequest.Request.MaxRedirects, 0, 16)
if len(via) >= int(maxredirects) {
return http.ErrUseLastResponse
}
return nil
}
} else if bot.HttpRequest.Request.MaxRedirects == "false" {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
} else {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
maxredirects := 10
if len(via) >= maxredirects {
return http.ErrUseLastResponse
}
return nil
}
}
return client, nil
}