forked from xqdoo00o/funcaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
375 lines (350 loc) · 10.3 KB
/
api.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
package funcaptcha
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
"github.com/bogdanfinn/tls-client/profiles"
"github.com/google/uuid"
)
const arkPreURL = "/fc/gt2/public_key/"
var arkURLIns, _ = url.Parse("https://openai.com")
var initVer, initHex string
type arkReq struct {
arkURL string
arkBx string
arkHeader http.Header
arkBody url.Values
arkCookies []*http.Cookie
userAgent string
}
var (
jar = tls_client.NewCookieJar()
options = []tls_client.HttpClientOption{
tls_client.WithTimeoutSeconds(360),
tls_client.WithClientProfile(profiles.Chrome_117),
tls_client.WithRandomTLSExtensionOrder(),
tls_client.WithNotFollowRedirects(),
tls_client.WithCookieJar(jar),
}
client *tls_client.HttpClient
proxy = os.Getenv("http_proxy")
authArks []*arkReq
chatArks []*arkReq
signupArks []*arkReq
)
type kvPair struct {
Name string `json:"name"`
Value string `json:"value"`
}
type cookie struct {
Name string `json:"name"`
Value string `json:"value"`
Expires string `json:"expires"`
}
type postBody struct {
Params []kvPair `json:"params"`
}
type request struct {
URL string `json:"url"`
Headers []kvPair `json:"headers,omitempty"`
PostData postBody `json:"postData,omitempty"`
Cookies []cookie `json:"cookies,omitempty"`
}
type entry struct {
StartedDateTime string `json:"startedDateTime"`
Request request `json:"request"`
}
type logData struct {
Entries []entry `json:"entries"`
}
type HARData struct {
Log logData `json:"log"`
}
type ChatArkoseResponse struct {
Token string `json:"token"`
ChallengeURL string `json:"challenge_url"`
ChallengeURLCDN string `json:"challenge_url_cdn"`
ChallengeURLCDNSRI string `json:"challenge_url_cdn_sri"`
NoScript string `json:"noscript"`
Mbio bool `json:"mbio"`
Tbio bool `json:"tbio"`
Kbio bool `json:"kbio"`
DisableDefaultStyling bool `json:"disable_default_styling"`
CompatibilityModeEnabled bool `json:"compatibility_mode_enabled"`
ForceStandardMode bool `json:"force_standard_mode"`
}
func readHAR() {
// 指定需要读取的文件夹路径
dirPath := "harPool"
var harPath []string
// 打开文件夹并读取其内容
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 判断是否为普通文件(非文件夹)
if !info.IsDir() {
// 获取文件后缀名
ext := filepath.Ext(info.Name())
if ext == ".har" {
harPath = append(harPath, path)
}
}
return nil
})
if err != nil {
println("Error: please put HAR files in harPool directory!")
}
for _, path := range harPath {
file, err := os.ReadFile(path)
if err != nil {
return
}
var harFile HARData
err = json.Unmarshal(file, &harFile)
if err != nil {
println("Error: not a HAR file!")
return
}
for _, v := range harFile.Log.Entries {
if strings.Contains(v.Request.URL, arkPreURL) {
var tmpArk arkReq
tmpArk.arkURL = v.Request.URL
if v.StartedDateTime == "" {
println("Error: no arkose request!")
continue
}
t, _ := time.Parse(time.RFC3339, v.StartedDateTime)
bw := getBw(t.Unix())
fallbackBw := getBw(t.Unix() - 21600)
tmpArk.arkHeader = make(http.Header)
for _, h := range v.Request.Headers {
// arkHeader except cookie & content-length
if !strings.EqualFold(h.Name, "content-length") && !strings.EqualFold(h.Name, "cookie") && !strings.HasPrefix(h.Name, ":") {
tmpArk.arkHeader.Set(h.Name, h.Value)
if strings.EqualFold(h.Name, "user-agent") {
tmpArk.userAgent = h.Value
}
}
}
tmpArk.arkCookies = []*http.Cookie{}
for _, cookie := range v.Request.Cookies {
expire, _ := time.Parse(time.RFC3339, cookie.Expires)
if expire.After(time.Now()) {
tmpArk.arkCookies = append(tmpArk.arkCookies, &http.Cookie{Name: cookie.Name, Value: cookie.Value, Expires: expire.UTC()})
}
}
var arkType string
tmpArk.arkBody = make(url.Values)
for _, p := range v.Request.PostData.Params {
// arkBody except bda & rnd
if p.Name == "bda" {
cipher, err := url.QueryUnescape(p.Value)
if err != nil {
panic(err)
}
tmpArk.arkBx = Decrypt(cipher, tmpArk.userAgent+bw, tmpArk.userAgent+fallbackBw)
} else if p.Name != "rnd" {
query, err := url.QueryUnescape(p.Value)
if err != nil {
panic(err)
}
tmpArk.arkBody.Set(p.Name, query)
if p.Name == "public_key" {
if query == "0A1D34FC-659D-4E23-B17B-694DCFCF6A6C" {
arkType = "auth"
authArks = append(authArks, &tmpArk)
} else if query == "35536E1E-65B4-4D96-9D97-6ADB7EFF8147" {
arkType = "chat"
chatArks = append(chatArks, &tmpArk)
} else if query == "0655BC92-82E1-43D9-B32E-9DF9B01AF50C" {
arkType = "signup"
signupArks = append(signupArks, &tmpArk)
}
}
}
}
if tmpArk.arkBx != "" {
println("success read " + arkType + " arkose")
} else {
println("failed to decrypt HAR file")
}
}
}
}
}
//goland:noinspection GoUnhandledErrorResult
func init() {
initVer = "1.5.4"
initHex = "cd12da708fe6cbe6e068918c38de2ad9" // should be fixed associated with version.
readHAR()
cli, _ := tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
client = &cli
if proxy != "" {
(*client).SetProxy(proxy)
}
}
//goland:noinspection GoUnusedExportedFunction
func SetTLSClient(cli *tls_client.HttpClient) {
client = cli
}
func GetOpenAIAuthToken(puid string, dx string, proxy string) (string, error) {
arkResp, err := sendRequest(0, "", puid, dx, proxy)
if err != nil {
return "", err
}
return arkResp.Token, nil
}
func GetOpenAIAuthTokenWithBx(bx string, puid string, dx string, proxy string) (string, error) {
arkResp, err := sendRequest(0, getBdaWitBx(bx), puid, dx, proxy)
if err != nil {
return "", err
}
return arkResp.Token, nil
}
func GetOpenAiSignupToken(datablobVal, proxy string) (*ChatArkoseResponse, error) {
arkResp, err := sendRequest(11, "", "", datablobVal, proxy)
return arkResp, err
}
func GetOpenAIToken(version int, puid string, dx string, proxy string) (*ChatArkoseResponse, error) {
arkResp, err := sendRequest(version, "", puid, dx, proxy)
return arkResp, err
}
func GetOpenAITokenWithBx(version int, bx string, puid string, dx string, proxy string) (*ChatArkoseResponse, error) {
arkResp, err := sendRequest(version, getBdaWitBx(bx), puid, dx, proxy)
return arkResp, err
}
//goland:noinspection SpellCheckingInspection,GoUnhandledErrorResult
func sendRequest(arkType int, unusebda string, puid string, dx string, proxy string) (*ChatArkoseResponse, error) {
var tmpArk *arkReq
if arkType == 0 {
// 登录
if len(authArks) == 0 {
return nil, errors.New("a valid HAR file which contains login arkose is required")
}
tmpArk = authArks[0]
authArks = append(authArks[1:], authArks[0])
} else if arkType == 3 || arkType == 4 {
// 聊天
if len(chatArks) == 0 {
return nil, errors.New("a valid HAR file which contains gpt-4 arkose is required")
}
tmpArk = chatArks[0]
chatArks = append(chatArks[1:], chatArks[0])
} else if arkType == 11 {
// 注册
if len(signupArks) == 0 {
return nil, errors.New("a valid HAR file which contains signup arkose is required")
}
tmpArk = signupArks[0]
signupArks = append(signupArks[1:], signupArks[0])
}
if tmpArk == nil || tmpArk.arkBx == "" || len(tmpArk.arkBody) == 0 || len(tmpArk.arkHeader) == 0 {
return nil, errors.New("a valid HAR file required")
}
if proxy != "" {
(*client).SetProxy(proxy)
}
bda, bw := getBDA(tmpArk)
tmpArk.arkBody.Set("bda", base64.StdEncoding.EncodeToString([]byte(bda)))
tmpArk.arkBody.Set("rnd", strconv.FormatFloat(rand.Float64(), 'f', -1, 64))
if dx != "" {
tmpArk.arkBody.Set("data[blob]", dx)
}
req, _ := http.NewRequest(http.MethodPost, tmpArk.arkURL, strings.NewReader(tmpArk.arkBody.Encode()))
req.Header = tmpArk.arkHeader.Clone()
req.Header.Set("X-Ark-Esync-Value", bw)
(*client).GetCookieJar().SetCookies(arkURLIns, tmpArk.arkCookies)
if puid != "" {
req.Header.Set("Cookie", "_puid="+puid+";")
}
resp, err := (*client).Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("status code " + resp.Status)
}
var arkoseResp ChatArkoseResponse
err = json.NewDecoder(resp.Body).Decode(&arkoseResp)
if err != nil {
return nil, err
}
// Check if rid is empty
if !strings.Contains(arkoseResp.Token, "sup=1|rid=") {
return &arkoseResp, errors.New("captcha required")
}
return &arkoseResp, nil
}
//goland:noinspection SpellCheckingInspection
func getBDA(arkReq *arkReq) (string, string) {
var bx string = arkReq.arkBx
var t = time.Now()
if bx == "" {
bx = fmt.Sprintf(bx_template,
getF(),
getN(t),
getWh(),
webglExtensions,
getWebglExtensionsHash(),
webglRenderer,
webglVendor,
webglVersion,
webglShadingLanguageVersion,
webglAliasedLineWidthRange,
webglAliasedPointSizeRange,
webglAntialiasing,
webglBits,
webglMaxParams,
webglMaxViewportDims,
webglUnmaskedVendor,
webglUnmaskedRenderer,
webglVsfParams,
webglVsiParams,
webglFsfParams,
webglFsiParams,
getWebglHashWebgl(),
initVer,
initHex,
getFe(),
getIfeHash(),
)
} else {
re := regexp.MustCompile(`"key"\:"n","value"\:"\S*?"`)
bx = re.ReplaceAllString(bx, `"key":"n","value":"`+getN(t)+`"`)
re = regexp.MustCompile(`"key"\:"1l2l5234ar2","value"\:"\S*?"`)
bx = re.ReplaceAllString(bx, `"key":"1l2l5234ar2","value":"`+fmt.Sprintf("%d", t.UnixMilli())+"\u2062\"")
re = regexp.MustCompile(`"key"\:"4b4b269e68","value"\:"(\S*?)"`)
oldUUID := re.FindStringSubmatch(bx)
if len(oldUUID) > 1 {
re = regexp.MustCompile(oldUUID[1])
newUUID := uuid.NewString()
bx = re.ReplaceAllString(bx, newUUID)
}
}
bw := getBw(t.Unix())
return Encrypt(bx, arkReq.userAgent+bw), bw
}
func getBt() int64 {
return time.Now().Unix()
}
func getBw(bt int64) string {
return strconv.FormatInt(bt-(bt%21600), 10)
}
func getBdaWitBx(bx string) string {
bt := getBt()
bw := getBw(bt)
return Encrypt(bx, bv+bw)
}