1
1
import { PassThrough } from "stream" ;
2
2
import _ from "lodash" ;
3
3
import axios , { AxiosResponse } from "axios" ;
4
- import { Worker } from 'worker_threads' ;
5
- import path from 'path' ;
6
4
7
5
import APIException from "@/lib/exceptions/APIException.ts" ;
8
6
import EX from "@/api/consts/exceptions.ts" ;
@@ -14,7 +12,7 @@ import util from "@/lib/util.ts";
14
12
// 模型名称
15
13
const MODEL_NAME = "deepseek-chat" ;
16
14
// 插冷鸡WASM文件路径
17
- const WASM_PATH = './35144cac553a7a2a .wasm' ;
15
+ const WASM_PATH = './sha3_wasm_bg.7b9ca65ddd .wasm' ;
18
16
// access_token有效期
19
17
const ACCESS_TOKEN_EXPIRES = 3600 ;
20
18
// 最大重试次数
@@ -39,7 +37,7 @@ const FAKE_HEADERS = {
39
37
"Sec-Fetch-Site" : "same-origin" ,
40
38
"User-Agent" :
41
39
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36" ,
42
- "X-App-Version" : "20241018.0 "
40
+ "X-App-Version" : "20241129.1 "
43
41
} ;
44
42
const EVENT_COMMIT_ID = '41e9c7b1' ;
45
43
// 当前IP地址
@@ -49,27 +47,6 @@ const accessTokenMap = new Map();
49
47
// access_token请求队列映射
50
48
const accessTokenRequestQueueMap : Record < string , Function [ ] > = { } ;
51
49
52
- // 添加 worker 池
53
- const workerPool : ( Worker & { inUse ?: boolean } ) [ ] = [ ] ;
54
- const MAX_WORKERS = 4 ; // 可以根据需要调整
55
-
56
- function getWorker ( ) {
57
- // 从池中获取空闲worker或创建新worker
58
- let worker = workerPool . find ( w => ! w . inUse ) ;
59
- if ( ! worker && workerPool . length < MAX_WORKERS ) {
60
- worker = new Worker ( path . join ( path . resolve ( ) , 'challenge-worker.js' ) ) ;
61
- workerPool . push ( worker ) ;
62
- }
63
- if ( worker ) {
64
- worker . inUse = true ;
65
- }
66
- return worker ;
67
- }
68
-
69
- function releaseWorker ( worker : ( Worker & { inUse ?: boolean } ) ) {
70
- worker . inUse = false ;
71
- }
72
-
73
50
async function getIPAddress ( ) {
74
51
if ( ipAddress ) return ipAddress ;
75
52
const result = await axios . get ( 'https://chat.deepseek.com/' , {
@@ -202,28 +179,31 @@ async function createSession(model: string, refreshToken: string): Promise<strin
202
179
* 相当于把计算量放在浏览器侧的话,用户分摊了这个计算量
203
180
* 但是如果逆向在服务器上算,那这个成本都在服务器集中,并发一高就GG
204
181
*/
205
- async function answerChallenge ( response : any ) : Promise < any > {
182
+ async function answerChallenge ( response : any , targetPath : string ) : Promise < any > {
206
183
const { algorithm, challenge, salt, difficulty, expire_at, signature } = response ;
207
184
const deepSeekHash = new DeepSeekHash ( ) ;
208
185
await deepSeekHash . init ( WASM_PATH ) ;
209
186
const answer = deepSeekHash . calculateHash ( algorithm , challenge , salt , difficulty , expire_at ) ;
210
- return {
187
+ return Buffer . from ( JSON . stringify ( {
211
188
algorithm,
212
189
challenge,
213
190
salt,
214
191
answer,
215
- signature
216
- }
192
+ signature,
193
+ target_path : targetPath
194
+ } ) ) . toString ( 'base64' ) ;
217
195
}
218
196
219
197
/**
220
198
* 获取challenge响应
221
199
*
222
200
* @param refreshToken 用于刷新access_token的refresh_token
223
201
*/
224
- async function getChallengeResponse ( refreshToken : string ) {
202
+ async function getChallengeResponse ( refreshToken : string , targetPath : string ) {
225
203
const token = await acquireToken ( refreshToken ) ;
226
- const result = await axios . get ( 'https://chat.deepseek.com/api/v0/chat/challenge' , {
204
+ const result = await axios . post ( 'https://chat.deepseek.com/api/v0/chat/create_pow_challenge' , {
205
+ target_path : targetPath
206
+ } , {
227
207
headers : {
228
208
Authorization : `Bearer ${ token } ` ,
229
209
...FAKE_HEADERS ,
@@ -283,19 +263,15 @@ async function createCompletion(
283
263
}
284
264
}
285
265
286
- let challenge ;
287
- if ( isSearchModel || isThinkingModel ) {
288
- const challengeResponse = await getChallengeResponse ( refreshToken ) ;
289
- challenge = await answerChallenge ( challengeResponse ) ;
290
- logger . info ( `插冷鸡: ${ JSON . stringify ( challenge ) } ` ) ;
291
- }
266
+ const challengeResponse = await getChallengeResponse ( refreshToken , '/api/v0/chat/completion' ) ;
267
+ const challenge = await answerChallenge ( challengeResponse , '/api/v0/chat/completion' ) ;
268
+ logger . info ( `插冷鸡: ${ challenge } ` ) ;
292
269
293
270
const result = await axios . post (
294
271
"https://chat.deepseek.com/api/v0/chat/completion" ,
295
272
{
296
273
chat_session_id : sessionId ,
297
274
parent_message_id : refParentMsgId || null ,
298
- challenge_response : challenge ,
299
275
prompt,
300
276
ref_file_ids : [ ] ,
301
277
search_enabled : isSearchModel ,
@@ -305,7 +281,8 @@ async function createCompletion(
305
281
headers : {
306
282
Authorization : `Bearer ${ token } ` ,
307
283
...FAKE_HEADERS ,
308
- Cookie : generateCookie ( )
284
+ Cookie : generateCookie ( ) ,
285
+ 'X-Ds-Pow-Response' : challenge
309
286
} ,
310
287
// 120秒超时
311
288
timeout : 120000 ,
@@ -394,12 +371,9 @@ async function createCompletionStream(
394
371
}
395
372
}
396
373
397
- let challenge ;
398
- if ( isSearchModel || isThinkingModel ) {
399
- const challengeResponse = await getChallengeResponse ( refreshToken ) ;
400
- challenge = await answerChallenge ( challengeResponse ) ;
401
- logger . info ( `插冷鸡: ${ JSON . stringify ( challenge ) } ` ) ;
402
- }
374
+ const challengeResponse = await getChallengeResponse ( refreshToken , '/api/v0/chat/completion' ) ;
375
+ const challenge = await answerChallenge ( challengeResponse , '/api/v0/chat/completion' ) ;
376
+ logger . info ( `插冷鸡: ${ challenge } ` ) ;
403
377
404
378
// 创建会话
405
379
const sessionId = refSessionId || await createSession ( model , refreshToken ) ;
@@ -412,7 +386,6 @@ async function createCompletionStream(
412
386
chat_session_id : sessionId ,
413
387
parent_message_id : refParentMsgId || null ,
414
388
prompt,
415
- challenge_response : challenge ,
416
389
ref_file_ids : [ ] ,
417
390
search_enabled : isSearchModel ,
418
391
thinking_enabled : isThinkingModel
@@ -421,7 +394,8 @@ async function createCompletionStream(
421
394
headers : {
422
395
Authorization : `Bearer ${ token } ` ,
423
396
...FAKE_HEADERS ,
424
- Cookie : generateCookie ( )
397
+ Cookie : generateCookie ( ) ,
398
+ 'X-Ds-Pow-Response' : challenge
425
399
} ,
426
400
// 120秒超时
427
401
timeout : 120000 ,
@@ -1254,11 +1228,6 @@ getIPAddress().then(() => {
1254
1228
logger . error ( '获取 IP 地址失败:' , err ) ;
1255
1229
} ) ;
1256
1230
1257
- // 在程序退出时清理workers
1258
- process . on ( 'exit' , ( ) => {
1259
- workerPool . forEach ( worker => worker . terminate ( ) ) ;
1260
- } ) ;
1261
-
1262
1231
export default {
1263
1232
createCompletion,
1264
1233
createCompletionStream,
0 commit comments