-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathworker.js
819 lines (778 loc) · 28.6 KB
/
worker.js
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/**
* 改进后的 Cloudflare Workers 脚本
*
* 功能:
* - API 接口:
* - GET /v1/models 返回模型列表
* - POST /v1/chat/completions 发送消息(需 API 密钥验证)
* - POST /v1/rate-limits 检查调用频率
* - 配置管理页面:
* - /config 系列接口,通过环境变量 CONFIG_PASSWORD 控制访问
* - 加载页面时,对每个 cookie 验证所有模型状态,
* 并在页面中显示每个 cookie 的状态以及各模型状态,同时对过长的 Cookie 进行截断显示
*
* 使用 D1 SQL 数据库存储配置(表名:config,操作 id=1 的记录)
*/
const TARGET_URL = "https://grok.com/rest/app-chat/conversations/new";
const CHECK_URL = "https://grok.com/rest/rate-limits";
const MODELS = ["grok-2", "grok-3", "grok-3-thinking"];
const MODELS_TO_CHECK = ["grok-2", "grok-3", "grok-3-thinking"];
const USER_AGENTS = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.2420.81",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 OPR/109.0.0.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0.1 Safari/605.1.15",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14.7; rv:132.0) Gecko/20100101 Firefox/132.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_4_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 OPR/109.0.0.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14.4; rv:124.0) Gecko/20100101 Firefox/124.0",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux i686; rv:124.0) Gecko/20100101 Firefox/124.0",
];
/* ========== 辅助函数:请求超时 ========== */
async function fetchWithTimeout(url, options, timeout = 5000) {
return Promise.race([
fetch(url, options),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("请求超时")), timeout)
),
]);
}
/* ========== 辅助函数:截断过长的 Cookie ========== */
function truncateCookie(cookie) {
const maxLen = 30;
if (cookie.length > maxLen) {
return cookie.slice(0, 10) + "..." + cookie.slice(-10);
}
return cookie;
}
/* ========== 数据库操作封装 ========== */
async function getConfig(env) {
await env.D1_DB.prepare(
`CREATE TABLE IF NOT EXISTS config (
id INTEGER PRIMARY KEY,
data TEXT NOT NULL
)`
).run();
let row = await env.D1_DB.prepare("SELECT data FROM config WHERE id = 1").first();
if (row && row.data) {
try {
return JSON.parse(row.data);
} catch (e) {
console.error("配置 JSON 解析错误:", e);
}
}
const defaultConfig = {
cookies: [],
last_cookie_index: { "grok-2": 0, "grok-3": 0, "grok-3-thinking": 0 },
temporary_mode: true,
};
await setConfig(defaultConfig, env);
return defaultConfig;
}
async function setConfig(config, env) {
const jsonStr = JSON.stringify(config);
await env.D1_DB.prepare("REPLACE INTO config (id, data) VALUES (1, ?)")
.bind(jsonStr)
.run();
}
/* ========== Cookie 轮询 ========== */
async function getNextAccount(model, env) {
let config = await getConfig(env);
if (!config.cookies || config.cookies.length === 0) {
throw new Error("没有可用的 cookie,请先通过配置页面添加。");
}
const num = config.cookies.length;
const current = ((config.last_cookie_index[model] || 0) + 1) % num;
config.last_cookie_index[model] = current;
await setConfig(config, env);
return config.cookies[current];
}
/* ========== 请求头封装 ========== */
function getCommonHeaders(cookie) {
return {
"Accept": "*/*",
"Content-Type": "application/json",
"Origin": "https://grok.com",
"Referer": "https://grok.com/",
"Cookie": cookie,
"User-Agent": USER_AGENTS[Math.floor(Math.random() * USER_AGENTS.length)],
};
}
/* ========== 检查调用频率 ========== */
/**
* 使用指定 cookie 调用 CHECK_URL 接口,返回 JSON 数据(带超时保护)
*/
async function checkRateLimitWithCookie(model, cookie, isReasoning) {
const headers = getCommonHeaders(cookie);
const payload = {
requestKind: isReasoning ? "REASONING" : "DEFAULT",
modelName: model,
};
const response = await fetchWithTimeout(CHECK_URL, {
method: "POST",
headers,
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error(`Rate limit check failed for model ${model}, status: ${response.status}`);
}
return await response.json();
}
/**
* 检查单个 cookie 的状态:
* - expired:如果用模型 "grok-2" 测试失败,则认为该 cookie 已过期
* - 对 MODELS_TO_CHECK 中的模型检查剩余查询次数,返回数组 rateLimitDetails
*
* 优化:对 "grok-2" 的调用只做一次,作为过期检测及剩余次数检测
*/
async function checkCookieStatus(cookie) {
let rateLimitDetails = [];
try {
// 先测试 grok-2
const dataGrok2 = await checkRateLimitWithCookie("grok-2", cookie, false);
rateLimitDetails.push({ model: "grok-2", remainingQueries: dataGrok2.remainingQueries });
} catch (e) {
return { expired: true, rateLimited: false, rateLimitDetails: [] };
}
// 再检查 grok-3
try {
const dataGrok3 = await checkRateLimitWithCookie("grok-3", cookie, false);
rateLimitDetails.push({ model: "grok-3", remainingQueries: dataGrok3.remainingQueries });
} catch (e) {
rateLimitDetails.push({ model: "grok-3", error: e.toString(), remainingQueries: 0 });
}
// 检查 grok-3-thinking
try {
const dataGrok3Thinking = await checkRateLimitWithCookie("grok-3", cookie, true);
rateLimitDetails.push({ model: "grok-3-thinking", remainingQueries: dataGrok3Thinking.remainingQueries });
} catch (e) {
rateLimitDetails.push({ model: "grok-3-thinking", error: e.toString(), remainingQueries: 0 });
}
const rateLimited = rateLimitDetails.every(detail => detail.remainingQueries === 0);
return { expired: false, rateLimited, rateLimitDetails };
}
/* ========== 消息预处理 ========== */
function magic(messages) {
let disableSearch = false;
let forceConcise = false;
if (messages && messages.length > 0) {
let first = messages[0].content;
if (first.includes("<|disableSearch|>")) {
disableSearch = true;
first = first.replace(/<\|disableSearch\|>/g, "");
}
if (first.includes("<|forceConcise|>")) {
forceConcise = true;
first = first.replace(/<\|forceConcise\|>/g, "");
}
messages[0].content = first;
}
return { disableSearch, forceConcise, messages };
}
function formatMessage(messages) {
let roleMap = { user: "Human", assistant: "Assistant", system: "System" };
const roleInfoPattern = /<roleInfo>\s*user:\s*([^\n]*)\s*assistant:\s*([^\n]*)\s*system:\s*([^\n]*)\s*prefix:\s*([^\n]*)\s*<\/roleInfo>\n/;
let prefix = false;
let firstContent = messages[0].content;
let match = firstContent.match(roleInfoPattern);
if (match) {
roleMap = {
user: match[1],
assistant: match[2],
system: match[3],
};
prefix = match[4] === "1";
messages[0].content = firstContent.replace(roleInfoPattern, "");
}
let formatted = "";
for (const msg of messages) {
let role = prefix ? "\b" + roleMap[msg.role] : roleMap[msg.role];
formatted += `${role}: ${msg.content}\n`;
}
return formatted;
}
/* ========== API 接口 ========== */
async function handleModels() {
const data = MODELS.map((model) => ({
id: model,
object: "model",
created: Math.floor(Date.now() / 1000),
owned_by: "Elbert",
name: model,
}));
return new Response(JSON.stringify({ object: "list", data }), {
headers: { "Content-Type": "application/json" },
});
}
async function handleChatCompletions(request, env) {
const authHeader = request.headers.get("Authorization");
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return new Response(JSON.stringify({ error: "Missing or invalid Authorization header" }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
const token = authHeader.split(" ")[1];
if (token !== env.CONFIG_PASSWORD) {
return new Response(JSON.stringify({ error: "Invalid API key" }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
try {
const reqJson = await request.json();
const streamFlag = reqJson.stream || false;
const messages = reqJson.messages;
let model = reqJson.model;
if (!MODELS.includes(model)) {
return new Response(JSON.stringify({ error: "模型不可用" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
if (!messages) {
return new Response(JSON.stringify({ error: "必须提供消息" }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}
const { disableSearch, forceConcise, messages: newMessages } = magic(messages);
const formattedMessage = formatMessage(newMessages);
const isReasoning = model.length > 6;
model = model.substring(0, 6);
if (streamFlag) {
return await sendMessageStream(formattedMessage, model, disableSearch, forceConcise, isReasoning, env);
} else {
return await sendMessageNonStream(formattedMessage, model, disableSearch, forceConcise, isReasoning, env);
}
} catch (e) {
console.error("处理 chat completions 出错:", e);
return new Response(JSON.stringify({ error: e.toString() }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
}
/* ========== 检查调用频率 ========== */
async function handleRateLimits(request, env) {
try {
const reqJson = await request.json();
const model = reqJson.model;
const isReasoning = !!reqJson.isReasoning;
if (!MODELS.includes(model)) {
return new Response(JSON.stringify({ error: "模型不可用" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
return await checkRateLimit(model, isReasoning, env);
} catch (e) {
console.error("检查调用频率出错:", e);
return new Response(JSON.stringify({ error: e.toString() }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
}
async function checkRateLimit(model, isReasoning, env) {
let cookie;
try {
cookie = await getNextAccount(model, env);
} catch (e) {
return new Response(JSON.stringify({ error: e.toString() }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
const headers = getCommonHeaders(cookie);
const payload = {
requestKind: isReasoning ? "REASONING" : "DEFAULT",
modelName: model,
};
try {
const response = await fetchWithTimeout(CHECK_URL, {
method: "POST",
headers,
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error("调用频率检查失败");
}
const data = await response.json();
return new Response(JSON.stringify(data), {
headers: { "Content-Type": "application/json" },
});
} catch (e) {
console.error("调用频率检查异常:", e);
return new Response(JSON.stringify({ error: e.toString() }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
}
/* ========== 流式消息转发 ========== */
async function sendMessageStream(message, model, disableSearch, forceConcise, isReasoning, env) {
let cookie;
try {
cookie = await getNextAccount(model, env);
} catch (e) {
return new Response(JSON.stringify({ error: e.toString() }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
const headers = getCommonHeaders(cookie);
const config = await getConfig(env);
const payload = {
temporary: config.temporary_mode,
modelName: model,
message: message,
fileAttachments: [],
imageAttachments: [],
disableSearch: disableSearch,
enableImageGeneration: false,
returnImageBytes: false,
returnRawGrokInXaiRequest: false,
enableImageStreaming: true,
imageGenerationCount: 2,
forceConcise: forceConcise,
toolOverrides: {},
enableSideBySide: true,
isPreset: false,
sendFinalMetadata: true,
customInstructions: "",
deepsearchPreset: "",
isReasoning: isReasoning,
};
const init = {
method: "POST",
headers,
body: JSON.stringify(payload),
};
const response = await fetchWithTimeout(TARGET_URL, init);
if (!response.ok) {
return new Response(JSON.stringify({ error: "发送消息失败" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
// 使用 ReadableStream 优化流数据处理
const stream = new ReadableStream({
async start(controller) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
const encoder = new TextEncoder();
let buffer = "";
let thinking = 2;
let batchSize = 0;
let batchContent = "";
const MAX_BATCH_SIZE = 5;
const BATCH_INTERVAL = 50;
let lastBatchTime = Date.now();
const processBatch = async () => {
if (batchContent) {
const chunkData = {
id: "chatcmpl-" + crypto.randomUUID(),
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: model,
choices: [
{ index: 0, delta: { content: batchContent }, finish_reason: null },
],
};
controller.enqueue(encoder.encode("data: " + JSON.stringify(chunkData) + "\n\n"));
batchContent = "";
batchSize = 0;
}
};
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop();
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) continue;
try {
const data = JSON.parse(trimmed);
if (!data?.result?.response || typeof data.result.response.token !== "string") {
continue;
}
let token = data.result.response.token;
let content = token;
if (isReasoning) {
if (thinking === 2) {
thinking = 1;
content = `<Thinking>\n${token}`;
} else if (thinking === 1 && !data.result.response.isThinking) {
thinking = 0;
content = `\n</Thinking>\n${token}`;
}
}
batchContent += content;
batchSize++;
// 当达到批处理阈值或距离上次发送已经过了足够时间时,发送数据
const now = Date.now();
if (batchSize >= MAX_BATCH_SIZE || (now - lastBatchTime >= BATCH_INTERVAL && batchContent)) {
await processBatch();
lastBatchTime = now;
// 添加微小延迟,让出 CPU
await new Promise(resolve => setTimeout(resolve, 1));
}
if (data.result.response.isSoftStop) {
await processBatch(); // 处理剩余的批次
const finalChunk = {
id: "chatcmpl-" + crypto.randomUUID(),
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: model,
choices: [
{ index: 0, delta: { content: "" }, finish_reason: "completed" },
],
};
controller.enqueue(encoder.encode("data: " + JSON.stringify(finalChunk) + "\n\n"));
controller.close();
return;
}
} catch (e) {
console.error("JSON 解析错误:", e, "行内容:", trimmed);
}
}
}
// 处理剩余的缓冲区数据
if (buffer.trim() !== "") {
try {
const data = JSON.parse(buffer.trim());
if (data?.result?.response && typeof data.result.response.token === "string") {
let token = data.result.response.token;
let content = token;
if (isReasoning) {
if (thinking === 2) {
thinking = 1;
content = `<Thinking>\n${token}`;
} else if (thinking === 1 && !data.result.response.isThinking) {
thinking = 0;
content = `\n</Thinking>\n${token}`;
}
}
batchContent += content;
}
} catch (e) {
console.error("Final JSON parse error:", e, "in buffer:", buffer);
}
}
// 处理最后的批次
await processBatch();
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
controller.close();
}
});
return new Response(stream, {
headers: { "Content-Type": "text/event-stream" },
});
}
/* ========== 非流式消息转发 ========== */
async function sendMessageNonStream(message, model, disableSearch, forceConcise, isReasoning, env) {
let cookie;
try {
cookie = await getNextAccount(model, env);
} catch (e) {
return new Response(JSON.stringify({ error: e.toString() }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
const headers = getCommonHeaders(cookie);
const config = await getConfig(env);
const payload = {
temporary: config.temporary_mode,
modelName: model,
message: message,
fileAttachments: [],
imageAttachments: [],
disableSearch: disableSearch,
enableImageGeneration: false,
returnImageBytes: false,
returnRawGrokInXaiRequest: false,
enableImageStreaming: true,
imageGenerationCount: 2,
forceConcise: forceConcise,
toolOverrides: {},
enableSideBySide: true,
isPreset: false,
sendFinalMetadata: true,
customInstructions: "",
deepsearchPreset: "",
isReasoning: isReasoning,
};
const init = {
method: "POST",
headers,
body: JSON.stringify(payload),
};
const response = await fetchWithTimeout(TARGET_URL, init);
if (!response.ok) {
return new Response(JSON.stringify({ error: "发送消息失败" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
const fullText = await response.text();
let finalMessage = "";
const lines = fullText.split("\n").filter(line => line.trim() !== "");
for (const line of lines) {
try {
const data = JSON.parse(line);
if (data?.result?.response) {
if (data.result.response.modelResponse && data.result.response.modelResponse.message) {
finalMessage = data.result.response.modelResponse.message;
break;
} else if (typeof data.result.response.token === "string") {
finalMessage += data.result.response.token;
}
}
} catch (e) {
console.error("JSON 解析错误:", e, "行内容:", line);
}
}
const openai_response = {
id: "chatcmpl-" + crypto.randomUUID(),
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model: model,
choices: [
{ index: 0, message: { role: "assistant", content: finalMessage }, finish_reason: "completed" },
],
};
return new Response(JSON.stringify(openai_response), {
headers: { "Content-Type": "application/json" },
});
}
/* ========== 登录与认证 ========== */
async function requireAuth(request, env) {
const cookieHeader = request.headers.get("Cookie") || "";
const match = cookieHeader.match(/config_auth=([^;]+)/);
if (match && match[1] === env.CONFIG_PASSWORD) {
return true;
}
return false;
}
function loginPage() {
const html =
`<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录配置管理</title>
<style>
body { font-family: Arial, sans-serif; background: #f0f2f5; display: flex; align-items: center; justify-content: center; height: 100vh; }
.login-container { background: #fff; padding: 20px 30px; border-radius: 8px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); }
h2 { margin-bottom: 20px; }
input[type="password"] { width: 100%; padding: 8px; margin: 10px 0; border: 1px solid #ccc; border-radius: 4px; }
button { background: #007BFF; color: #fff; border: none; padding: 10px; border-radius: 4px; cursor: pointer; width: 100%; }
button:hover { background: #0056b3; }
</style>
</head>
<body>
<div class="login-container">
<h2>请输入密码</h2>
<form method="POST" action="/config/login">
<input type="password" name="password" placeholder="密码" required>
<button type="submit">登录</button>
</form>
</div>
</body>
</html>`;
return new Response(html, { headers: { "Content-Type": "text/html" } });
}
async function handleLogin(request, env) {
const formData = await request.formData();
const password = formData.get("password") || "";
if (password === env.CONFIG_PASSWORD) {
const redirectURL = new URL("/config", request.url).toString();
const urlObj = new URL(request.url);
const isHttps = urlObj.protocol === "https:";
const cookieHeader = `config_auth=${env.CONFIG_PASSWORD}; Path=/; HttpOnly; ${isHttps ? "Secure; " : ""}SameSite=Strict`;
return new Response("", {
status: 302,
headers: {
"Set-Cookie": cookieHeader,
"Location": redirectURL,
},
});
} else {
return new Response("密码错误", { status: 401 });
}
}
/* ========== 配置管理页面 ========== */
async function configPage(request, env) {
const config = await getConfig(env);
let cookieStatuses = [];
try {
cookieStatuses = await Promise.all(
config.cookies.map(cookie =>
checkCookieStatus(cookie).catch(e => ({ expired: true, rateLimited: false, rateLimitDetails: [] }))
)
);
} catch (e) {
console.error("Error checking cookie statuses:", e);
}
const tableRows = config.cookies.map((cookie, index) => {
const status = cookieStatuses[index] || { expired: true, rateLimited: false, rateLimitDetails: [] };
const cookieStateHtml = status.expired
? '<span style="color:red;">已过期</span>'
: '<span style="color:green;">有效</span>';
const rateLimitHtml = status.expired
? '--'
: status.rateLimitDetails.map(detail => {
if (detail.error) {
return `${detail.model}: <span style="color:red;">错误(${detail.error})</span>`;
} else {
return detail.remainingQueries > 0
? `${detail.model}: <span style="color:green;">有效 (剩余: ${detail.remainingQueries})</span>`
: `${detail.model}: <span style="color:red;">限额已达</span>`;
}
}).join(" | ");
return `<tr>
<td>${index + 1}</td>
<td>${truncateCookie(cookie)}</td>
<td>${cookieStateHtml}</td>
<td>${rateLimitHtml}</td>
<td>
<form method="POST" action="/config" class="form-inline">
<input type="hidden" name="action" value="delete_one">
<input type="hidden" name="index" value="${index}">
<button type="submit" class="btn-danger">删除</button>
</form>
</td>
</tr>`;
}).join('');
const html =
`<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>配置管理</title>
<style>
body { font-family: Arial, sans-serif; background: #f9f9f9; margin: 0; padding: 20px; }
.container { max-width: 900px; margin: auto; background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); }
h1 { text-align: center; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; word-break: break-all; }
th { background: #f2f2f2; }
form { margin: 0; }
.actions { display: flex; gap: 8px; margin-top: 20px; }
button { background: #28a745; color: #fff; border: none; padding: 6px 12px; border-radius: 4px; cursor: pointer; }
button:hover { background: #218838; }
.btn-danger { background: #dc3545; }
.btn-danger:hover { background: #c82333; }
.btn-toggle { background: #17a2b8; }
.btn-toggle:hover { background: #138496; }
.form-inline { display: flex; align-items: center; gap: 10px; }
input[type="text"] { flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 4px; }
</style>
</head>
<body>
<div class="container">
<h1>配置管理</h1>
<p><strong>API Key:</strong> 与配置密码相同</p>
<h2>当前 Cookies</h2>
<table>
<thead>
<tr>
<th>#</th>
<th>Cookie</th>
<th>Cookie状态</th>
<th>模型状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
${tableRows}
</tbody>
</table>
<p>Temporary Mode: <strong>${config.temporary_mode ? "开启" : "关闭"}</strong></p>
<hr>
<h2>添加 Cookie</h2>
<form method="POST" action="/config" class="form-inline">
<input type="hidden" name="action" value="add">
<input type="text" name="cookie" placeholder="请输入 Cookie" required>
<button type="submit">添加</button>
</form>
<hr>
<h2>全局操作</h2>
<div class="actions">
<form method="POST" action="/config">
<input type="hidden" name="action" value="delete">
<button type="submit" class="btn-danger">删除所有 Cookies</button>
</form>
<form method="POST" action="/config">
<input type="hidden" name="action" value="toggle">
<button type="submit" class="btn-toggle">切换 Temporary Mode</button>
</form>
</div>
</div>
</body>
</html>`;
return new Response(html, { headers: { "Content-Type": "text/html" } });
}
async function updateConfig(request, env) {
const formData = await request.formData();
const action = formData.get("action");
const config = await getConfig(env);
if (action === "add") {
const newCookie = formData.get("cookie");
if (newCookie && newCookie.trim() !== "") {
config.cookies.push(newCookie.trim());
}
} else if (action === "delete") {
config.cookies = [];
} else if (action === "toggle") {
config.temporary_mode = !config.temporary_mode;
} else if (action === "delete_one") {
const index = parseInt(formData.get("index"), 10);
if (!isNaN(index) && index >= 0 && index < config.cookies.length) {
config.cookies.splice(index, 1);
}
}
await setConfig(config, env);
return Response.redirect(new URL("/config", request.url).toString(), 302);
}
/* ========== 主调度函数 ========== */
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === "/" || url.pathname === "") {
return Response.redirect(new URL("/config", request.url).toString(), 302);
}
if (url.pathname.startsWith("/config")) {
if (url.pathname === "/config/login") {
if (request.method === "GET") {
return loginPage();
} else if (request.method === "POST") {
return handleLogin(request, env);
}
}
if (!(await requireAuth(request, env))) {
return Response.redirect(new URL("/config/login", request.url).toString(), 302);
}
if (request.method === "GET") {
return configPage(request, env);
} else if (request.method === "POST") {
return updateConfig(request, env);
}
} else if (url.pathname.startsWith("/v1/models")) {
return handleModels();
} else if (url.pathname.startsWith("/v1/rate-limits")) {
return handleRateLimits(request, env);
} else if (url.pathname.startsWith("/v1/chat/completions")) {
return handleChatCompletions(request, env);
}
return new Response("Not Found", { status: 404 });
}
};