magpie-gemini-web 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +11 -9
- package/README.md +11 -9
- package/docs/backend-release.md +1 -1
- package/docs/studio-release.md +33 -11
- package/lib/backend-release.json +15 -15
- package/lib/backend.mjs +2 -1
- package/lib/native.mjs +7 -7
- package/lib/studio-native.mjs +5 -2
- package/lib/studio-release.json +21 -20
- package/lib/studio.mjs +3 -3
- package/lib/usage.mjs +8 -5
- package/package.json +1 -1
- package/patches/argon.patch +150 -120
- package/patches/studio-0.2.19-managed-config.patch +110 -0
- package/scripts/backend-argon.mjs +2 -2
- package/scripts/studio-managed-smoke.mjs +25 -3
- package/scripts/studio-release.mjs +1 -1
package/patches/argon.patch
CHANGED
|
@@ -19,10 +19,10 @@ index c6a75e2..db2c8f5 100644
|
|
|
19
19
|
golang.org/x/text v0.42.0 // indirect
|
|
20
20
|
modernc.org/libc v1.72.3 // indirect
|
|
21
21
|
diff --git a/internal/app/admin_cookies.go b/internal/app/admin_cookies.go
|
|
22
|
-
index
|
|
22
|
+
index 78236c6..a5109b0 100644
|
|
23
23
|
--- a/internal/app/admin_cookies.go
|
|
24
24
|
+++ b/internal/app/admin_cookies.go
|
|
25
|
-
@@ -
|
|
25
|
+
@@ -87,9 +87,10 @@ func handleAdminCookies(w http.ResponseWriter, r *http.Request) {
|
|
26
26
|
})
|
|
27
27
|
case http.MethodPost:
|
|
28
28
|
var p struct {
|
|
@@ -33,34 +33,28 @@ index f17a675..c90f08a 100644
|
|
|
33
33
|
+ Cookie string `json:"cookie"`
|
|
34
34
|
+ Note string `json:"note"`
|
|
35
35
|
+ ProxyID *int64 `json:"proxy_id"`
|
|
36
|
+
// 浏览器多号登录时的账号序号(/u/N),不传 = 0
|
|
37
|
+
AuthUser int `json:"authuser"`
|
|
36
38
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
writeJSON(w, 400, map[string]string{"error": err.Error()})
|
|
41
|
-
return
|
|
39
|
+
@@ -114,6 +115,9 @@ func handleAdminCookies(w http.ResponseWriter, r *http.Request) {
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
42
|
}
|
|
43
43
|
+ if p.ProxyID != nil && *p.ProxyID >= 0 {
|
|
44
44
|
+ bindAccountProxy(id, *p.ProxyID)
|
|
45
45
|
+ }
|
|
46
|
-
logf("[cookies] 新增账号 #%d label=%q", id, strings.TrimSpace(p.Label))
|
|
46
|
+
logf("[cookies] 新增账号 #%d label=%q authuser=%d", id, strings.TrimSpace(p.Label), p.AuthUser)
|
|
47
47
|
writeJSON(w, 200, map[string]interface{}{"id": id})
|
|
48
48
|
default:
|
|
49
|
-
@@ -
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
- Status *string `json:"status"`
|
|
56
|
-
+ Label *string `json:"label"`
|
|
57
|
-
+ Note *string `json:"note"`
|
|
58
|
-
+ Status *string `json:"status"`
|
|
59
|
-
+ ProxyID *int64 `json:"proxy_id"`
|
|
49
|
+
@@ -207,6 +211,7 @@ func handleAdminCookieItem(w http.ResponseWriter, r *http.Request) {
|
|
50
|
+
Label *string `json:"label"`
|
|
51
|
+
Note *string `json:"note"`
|
|
52
|
+
Status *string `json:"status"`
|
|
53
|
+
+ ProxyID *int64 `json:"proxy_id"`
|
|
54
|
+
AuthUser *int `json:"authuser"`
|
|
60
55
|
}
|
|
61
56
|
body, _ := io.ReadAll(r.Body)
|
|
62
|
-
|
|
63
|
-
@@ -204,6 +209,9 @@ func handleAdminCookieItem(w http.ResponseWriter, r *http.Request) {
|
|
57
|
+
@@ -226,6 +231,9 @@ func handleAdminCookieItem(w http.ResponseWriter, r *http.Request) {
|
|
64
58
|
return
|
|
65
59
|
}
|
|
66
60
|
}
|
|
@@ -71,7 +65,7 @@ index f17a675..c90f08a 100644
|
|
|
71
65
|
// 只改传了的字段:没传的沿用当前值。
|
|
72
66
|
var curLabel, curNote string
|
|
73
67
|
diff --git a/internal/app/admin_ui/index.html b/internal/app/admin_ui/index.html
|
|
74
|
-
index
|
|
68
|
+
index 4aedd27..2364e2b 100644
|
|
75
69
|
--- a/internal/app/admin_ui/index.html
|
|
76
70
|
+++ b/internal/app/admin_ui/index.html
|
|
77
71
|
@@ -1020,7 +1020,8 @@ const RT_GROUPS = [
|
|
@@ -84,6 +78,15 @@ index a765bb1..eb073b6 100644
|
|
|
84
78
|
],
|
|
85
79
|
},
|
|
86
80
|
{
|
|
81
|
+
@@ -1038,8 +1039,6 @@ const RT_GROUPS = [
|
|
82
|
+
hint: '上游前端版本号。请求大面积失败时先看这里是不是过期了' },
|
|
83
|
+
{ k: 'gemini_bl_auto', label: '自动跟随 bl 版本', type: 'bool',
|
|
84
|
+
hint: '每 6 小时从 /app 页面抓一次最新版本号,抓到就用抓到的。抓不到或格式不对就继续用上面钉的值' },
|
|
85
|
+
- { k: 'web_channel', label: '新协议(processSession)', type: 'bool',
|
|
86
|
+
- hint: '对话改走网页新版用的 processSession(WebChannel)接口,而不是老的 StreamGenerate。后端是同一个,只换传输方式;老接口目前仍正常,老接口出问题时再打开。多轮续接仍走老接口;扩展思考在新协议上暂时不生效(回报普通档)。默认关' },
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
{
|
|
87
90
|
diff --git a/internal/app/argon.go b/internal/app/argon.go
|
|
88
91
|
new file mode 100644
|
|
89
92
|
index 0000000..a2a5e77
|
|
@@ -264,7 +267,7 @@ index 0000000..a2a5e77
|
|
|
264
267
|
+}
|
|
265
268
|
diff --git a/internal/app/argon_probe_test.go b/internal/app/argon_probe_test.go
|
|
266
269
|
new file mode 100644
|
|
267
|
-
index 0000000..
|
|
270
|
+
index 0000000..bbc0041
|
|
268
271
|
--- /dev/null
|
|
269
272
|
+++ b/internal/app/argon_probe_test.go
|
|
270
273
|
@@ -0,0 +1,53 @@
|
|
@@ -289,7 +292,7 @@ index 0000000..dd9e687
|
|
|
289
292
|
+ dbPath := cfg.DBPath
|
|
290
293
|
+ cfg = defaultConfig(); cfg.DBPath = dbPath; cfg.RequestTimeout = 65
|
|
291
294
|
+ initRuntimeConfig()
|
|
292
|
-
+ page, err := fetchAppPage(cookie, ""); if err != nil { t.Fatal("page fetch failed") }
|
|
295
|
+
+ page, err := fetchAppPage(cookie, 0, ""); if err != nil { t.Fatal("page fetch failed") }
|
|
293
296
|
+ tok := snlm0eRe.FindSubmatch(page); if len(tok)<2 {t.Fatal("login token unavailable")}
|
|
294
297
|
+ bl := regexp.MustCompile(`"cfb2h":"([^"]+)"`).FindSubmatch(page)
|
|
295
298
|
+ if len(bl)<2 {t.Fatal("build version unavailable")}
|
|
@@ -782,7 +785,7 @@ index 0000000..2f0b8cd
|
|
|
782
785
|
+}
|
|
783
786
|
diff --git a/internal/app/argon_timing_probe_test.go b/internal/app/argon_timing_probe_test.go
|
|
784
787
|
new file mode 100644
|
|
785
|
-
index 0000000..
|
|
788
|
+
index 0000000..fb1ccec
|
|
786
789
|
--- /dev/null
|
|
787
790
|
+++ b/internal/app/argon_timing_probe_test.go
|
|
788
791
|
@@ -0,0 +1,74 @@
|
|
@@ -808,7 +811,7 @@ index 0000000..ae43ef4
|
|
|
808
811
|
+ dbPath := cfg.DBPath
|
|
809
812
|
+ cfg = defaultConfig(); cfg.DBPath = dbPath; cfg.RequestTimeout = 65
|
|
810
813
|
+ initRuntimeConfig()
|
|
811
|
-
+ page, err := fetchAppPage(cookie, ""); if err != nil { t.Fatal("page fetch failed") }
|
|
814
|
+
+ page, err := fetchAppPage(cookie, 0, ""); if err != nil { t.Fatal("page fetch failed") }
|
|
812
815
|
+ tok := snlm0eRe.FindSubmatch(page); if len(tok)<2 {t.Fatal("login token unavailable")}
|
|
813
816
|
+ bl := regexp.MustCompile(`"cfb2h":"([^"]+)"`).FindSubmatch(page)
|
|
814
817
|
+ if len(bl)<2 {t.Fatal("build version unavailable")}
|
|
@@ -1306,7 +1309,7 @@ index 94c0b70..2a4b697 100644
|
|
|
1306
1309
|
+ }
|
|
1307
1310
|
+}
|
|
1308
1311
|
diff --git a/internal/app/context_file.go b/internal/app/context_file.go
|
|
1309
|
-
index
|
|
1312
|
+
index 97995a2..ba03140 100644
|
|
1310
1313
|
--- a/internal/app/context_file.go
|
|
1311
1314
|
+++ b/internal/app/context_file.go
|
|
1312
1315
|
@@ -1,6 +1,7 @@
|
|
@@ -1321,22 +1324,22 @@ index bfc577b..cb60661 100644
|
|
|
1321
1324
|
//
|
|
1322
1325
|
// 上传失败**不静默回退到超长内联**:那样上游会把最新提问截掉,客户端拿到一个
|
|
1323
1326
|
// 答非所问的 200 却看不出问题。
|
|
1324
|
-
-func prepareContextFile(prompt, latest string, budget int, cookie, proxyURL string) (
|
|
1327
|
+
-func prepareContextFile(prompt, latest string, budget int, cookie string, authuser int, proxyURL string) (
|
|
1325
1328
|
+//
|
|
1326
1329
|
+// 附件里只放对话历史,工具协议(pc.Head / pc.Tail)留在正文:agentic 客户端的
|
|
1327
1330
|
+// 请求动辄十几万字节,几乎每轮都走这条路,工具说明和末尾锚点一旦进了附件,模型
|
|
1328
1331
|
+// 就不再稳定地吐 ```tool_call```。工具说明本身就超预算时才退回整段进附件。
|
|
1329
|
-
+func prepareContextFile(ctx context.Context, prompt string, pc promptCtx, budget int, cookie, proxyURL string) (
|
|
1332
|
+
+func prepareContextFile(ctx context.Context, prompt string, pc promptCtx, budget int, cookie string, authuser int, proxyURL string) (
|
|
1330
1333
|
string, []fileRef, bool, error) {
|
|
1331
1334
|
if budget <= 0 || len(prompt) <= budget {
|
|
1332
1335
|
return prompt, nil, false, nil
|
|
1333
|
-
@@ -84,11 +89,50 @@ func prepareContextFile(prompt, latest string, budget int, cookie,
|
|
1336
|
+
@@ -84,11 +89,50 @@ func prepareContextFile(prompt, latest string, budget int, cookie string, authus
|
|
1334
1337
|
if cookie == "" {
|
|
1335
1338
|
return prompt, nil, false, nil // 匿名:引用会被回 1100,转了也没用
|
|
1336
1339
|
}
|
|
1337
|
-
- ref, err := uploadBytes(cookie, proxyURL, []byte(prompt), contextFileName)
|
|
1340
|
+
- ref, err := uploadBytes(cookie, authuser, proxyURL, []byte(prompt), contextFileName)
|
|
1338
1341
|
+ history, inline := planContextFile(prompt, pc, budget)
|
|
1339
|
-
+ ref, err := uploadBytesCtx(ctx, cookie, proxyURL, []byte(history), contextFileName)
|
|
1342
|
+
+ ref, err := uploadBytesCtx(ctx, cookie, authuser, proxyURL, []byte(history), contextFileName)
|
|
1340
1343
|
if err != nil {
|
|
1341
1344
|
return prompt, nil, false, fmt.Errorf("超长对话转附件失败: %w", err)
|
|
1342
1345
|
}
|
|
@@ -1385,7 +1388,7 @@ index bfc577b..cb60661 100644
|
|
|
1385
1388
|
+ return promptParts{body: parts}.join()
|
|
1386
1389
|
}
|
|
1387
1390
|
diff --git a/internal/app/conversation.go b/internal/app/conversation.go
|
|
1388
|
-
index
|
|
1391
|
+
index 529cfad..c48b692 100644
|
|
1389
1392
|
--- a/internal/app/conversation.go
|
|
1390
1393
|
+++ b/internal/app/conversation.go
|
|
1391
1394
|
@@ -1,6 +1,7 @@
|
|
@@ -1396,7 +1399,7 @@ index 74df391..401482e 100644
|
|
|
1396
1399
|
"crypto/sha256"
|
|
1397
1400
|
"encoding/hex"
|
|
1398
1401
|
"encoding/json"
|
|
1399
|
-
@@ -
|
|
1402
|
+
@@ -207,7 +208,7 @@ func getAnonSession(proxyURL string) (string, error) {
|
|
1400
1403
|
if err != nil {
|
|
1401
1404
|
return nil, nil, nil, err
|
|
1402
1405
|
}
|
|
@@ -1405,7 +1408,7 @@ index 74df391..401482e 100644
|
|
|
1405
1408
|
resp, err := getTLSClient().Do(r)
|
|
1406
1409
|
if err != nil {
|
|
1407
1410
|
return nil, nil, nil, err
|
|
1408
|
-
@@ -
|
|
1411
|
+
@@ -239,15 +240,14 @@ func getAnonSession(proxyURL string) (string, error) {
|
|
1409
1412
|
return strings.Join(pairs, "; "), nil
|
|
1410
1413
|
}
|
|
1411
1414
|
|
|
@@ -1425,7 +1428,7 @@ index 74df391..401482e 100644
|
|
|
1425
1428
|
p, slotOK, slotErr := acquireSlot(conv.proxyID)
|
|
1426
1429
|
if !slotOK {
|
|
1427
1430
|
return &StreamResult{}, slotErr
|
|
1428
|
-
@@ -
|
|
1431
|
+
@@ -371,12 +371,19 @@ func streamGenerateConv(prompt string, mc ModelConfig, conv *convState,
|
|
1429
1432
|
t0 := time.Now()
|
|
1430
1433
|
var lastErr error
|
|
1431
1434
|
for attempt := 0; attempt < rtCfg().RetryAttempts; attempt++ {
|
|
@@ -1446,7 +1449,7 @@ index 74df391..401482e 100644
|
|
|
1446
1449
|
// 降级:同一个号重试还是降级,直接交给上层(见 gemini.go 同一处)。
|
|
1447
1450
|
var mde *ModelDowngradedError
|
|
1448
1451
|
if errors.As(err, &mde) {
|
|
1449
|
-
@@ -
|
|
1452
|
+
@@ -385,6 +392,9 @@ func streamGenerateConv(prompt string, mc ModelConfig, conv *convState,
|
|
1450
1453
|
Emitted: tracker.emitted, EmittedReasoning: rtracker.emitted}, err
|
|
1451
1454
|
}
|
|
1452
1455
|
if err != nil || statusCode != 200 || !hasContentFrame(string(raw)) {
|
|
@@ -1456,7 +1459,7 @@ index 74df391..401482e 100644
|
|
|
1456
1459
|
if err == nil && statusCode == 200 {
|
|
1457
1460
|
lastErr = fmt.Errorf("续接无内容帧(会话可能已失效,raw %d 字节)", len(raw))
|
|
1458
1461
|
} else if err != nil {
|
|
1459
|
-
@@ -
|
|
1462
|
+
@@ -399,7 +409,9 @@ func streamGenerateConv(prompt string, mc ModelConfig, conv *convState,
|
|
1460
1463
|
break
|
|
1461
1464
|
}
|
|
1462
1465
|
if attempt < rtCfg().RetryAttempts-1 {
|
|
@@ -1467,7 +1470,7 @@ index 74df391..401482e 100644
|
|
|
1467
1470
|
}
|
|
1468
1471
|
continue
|
|
1469
1472
|
}
|
|
1470
|
-
@@ -
|
|
1473
|
+
@@ -440,7 +452,7 @@ func streamGenerateConv(prompt string, mc ModelConfig, conv *convState,
|
|
1471
1474
|
// - 没命中 → 新建会话,首轮发全量拼接的历史(含 tools 指令,把当前上下文交给服务端)。
|
|
1472
1475
|
//
|
|
1473
1476
|
// 支持 tools(续接里也解析 ```tool_call``` 围栏);图片走原单轮路径(callGemini)。
|
|
@@ -1476,7 +1479,7 @@ index 74df391..401482e 100644
|
|
|
1476
1479
|
tools []map[string]interface{}, toolChoice interface{},
|
|
1477
1480
|
onDelta, onReasoning func(string)) (string, []ToolCall, *StreamResult, error) {
|
|
1478
1481
|
parentKey := convParentKey(messages)
|
|
1479
|
-
@@ -
|
|
1482
|
+
@@ -491,7 +503,7 @@ func callGeminiConv(messages []map[string]interface{}, mc ModelConfig,
|
|
1480
1483
|
}
|
|
1481
1484
|
}
|
|
1482
1485
|
}
|
|
@@ -1486,10 +1489,10 @@ index 74df391..401482e 100644
|
|
|
1486
1489
|
break
|
|
1487
1490
|
}
|
|
1488
1491
|
diff --git a/internal/app/cookie_pool.go b/internal/app/cookie_pool.go
|
|
1489
|
-
index
|
|
1492
|
+
index 60fe35f..14e5a44 100644
|
|
1490
1493
|
--- a/internal/app/cookie_pool.go
|
|
1491
1494
|
+++ b/internal/app/cookie_pool.go
|
|
1492
|
-
@@ -
|
|
1495
|
+
@@ -565,6 +565,9 @@ func checkAccountCookie(a CookieAccount) CookieCheck {
|
|
1493
1496
|
return CookieCheck{Detail: explainCookieFailure(err), ProxyName: name, TookMs: took}
|
|
1494
1497
|
}
|
|
1495
1498
|
markAccountResult(a.ID, true, "")
|
|
@@ -1500,7 +1503,7 @@ index 6a88ac2..dc828d6 100644
|
|
|
1500
1503
|
if extractSAPISID(a.Cookie) == "" {
|
|
1501
1504
|
detail += ",但缺 SAPISID(算不出授权头,部分接口会被当匿名)"
|
|
1502
1505
|
diff --git a/internal/app/gemini.go b/internal/app/gemini.go
|
|
1503
|
-
index
|
|
1506
|
+
index 7572da2..55ab2ee 100644
|
|
1504
1507
|
--- a/internal/app/gemini.go
|
|
1505
1508
|
+++ b/internal/app/gemini.go
|
|
1506
1509
|
@@ -3,6 +3,7 @@ package app
|
|
@@ -1511,15 +1514,15 @@ index 7c9186f..c04b69c 100644
|
|
|
1511
1514
|
"encoding/json"
|
|
1512
1515
|
"errors"
|
|
1513
1516
|
"fmt"
|
|
1514
|
-
@@ -
|
|
1515
|
-
"net/url"
|
|
1517
|
+
@@ -12,6 +13,7 @@ import (
|
|
1516
1518
|
"regexp"
|
|
1519
|
+
"strconv"
|
|
1517
1520
|
"strings"
|
|
1518
1521
|
+ "sync/atomic"
|
|
1519
1522
|
"time"
|
|
1520
1523
|
|
|
1521
1524
|
fhttp "github.com/bogdanfinn/fhttp"
|
|
1522
|
-
@@ -
|
|
1525
|
+
@@ -376,7 +378,7 @@ func (e *pinnedAccountError) Unwrap() error { return e.Err }
|
|
1523
1526
|
// 自己做。一旦已经吐过内容就不再重试——重试会让客户端收到重复文本。
|
|
1524
1527
|
func streamGenerate(prompt, latest string, mc ModelConfig,
|
|
1525
1528
|
onDelta, onReasoning func(string)) (*StreamResult, error) {
|
|
@@ -1528,7 +1531,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1528
1531
|
}
|
|
1529
1532
|
|
|
1530
1533
|
// fileRef 是一个已上传附件的引用。
|
|
1531
|
-
@@ -
|
|
1534
|
+
@@ -391,15 +393,23 @@ type fileRef struct {
|
|
1532
1535
|
//
|
|
1533
1536
|
// 附件填 inner[0][3],形状 [[[ref, 1], "文件名"], …]。附件只在登录态可用:
|
|
1534
1537
|
// 匿名能把文件传上去,但对话里一引用就被服务端回 1100。
|
|
@@ -1555,7 +1558,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1555
1558
|
var exhausted *ModelDowngradedError
|
|
1556
1559
|
if try > 0 && errors.As(nextErr, &exhausted) && exhausted.Reported == "" {
|
|
1557
1560
|
// 换号时已经没号可挑:报上一个号真实的降级(带自报模型名),补上解冻时间
|
|
1558
|
-
@@ -
|
|
1561
|
+
@@ -417,8 +427,8 @@ func streamGenerateWithFiles(prompt, latest string, mc ModelConfig, pending []pe
|
|
1559
1562
|
return res, err
|
|
1560
1563
|
}
|
|
1561
1564
|
|
|
@@ -1566,7 +1569,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1566
1569
|
var files []fileRef
|
|
1567
1570
|
|
|
1568
1571
|
// 先挑号,再按它上次绑的出口挑代理 —— 同一个账号要尽量固定从同一个 IP 出去,
|
|
1569
|
-
@@ -
|
|
1572
|
+
@@ -428,6 +438,9 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1570
1573
|
// 不发请求,真正发请求的是下面的 getXSRF,它在拿到 slot 之后。
|
|
1571
1574
|
// #20 匿名优先:不需要登录态能力时不占用 cookie 账号,走匿名省额度(见 anonFirstEligible)。
|
|
1572
1575
|
// 带附件(图/视频)时不能走匿名——下面 uploadBytes 那段会因 cookieStr=="" 直接报错。
|
|
@@ -1576,7 +1579,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1576
1579
|
var acct *CookieAccount
|
|
1577
1580
|
skip := exclude
|
|
1578
1581
|
if mc.PinAccountID > 0 {
|
|
1579
|
-
@@ -
|
|
1582
|
+
@@ -485,6 +498,9 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1580
1583
|
// 每个号只给一次「轮转后重试」的机会,避免在一个请求里反复打 accounts.google.com
|
|
1581
1584
|
rotatedOnce := map[int64]bool{}
|
|
1582
1585
|
for acct != nil && len(tried) < maxCookieTries {
|
|
@@ -1586,31 +1589,31 @@ index 7c9186f..c04b69c 100644
|
|
|
1586
1589
|
tried[acct.ID] = true
|
|
1587
1590
|
// 归属先记上:这一轮失败了也留痕,面板上看得出是哪个号在坏
|
|
1588
1591
|
cookieID, cookieLabel = acct.ID, accountDisplayName(acct)
|
|
1589
|
-
@@ -
|
|
1592
|
+
@@ -581,7 +597,10 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1590
1593
|
}
|
|
1591
1594
|
nImg, nVid := 0, 0
|
|
1592
1595
|
for _, u := range pending {
|
|
1593
|
-
- ref, uerr := uploadBytes(cookieStr, proxyURL, u.Data, u.Name)
|
|
1594
|
-
+ ref, uerr := uploadBytesCtx(ctx, cookieStr, proxyURL, u.Data, u.Name)
|
|
1596
|
+
- ref, uerr := uploadBytes(cookieStr, authUser, proxyURL, u.Data, u.Name)
|
|
1597
|
+
+ ref, uerr := uploadBytesCtx(ctx, cookieStr, authUser, proxyURL, u.Data, u.Name)
|
|
1595
1598
|
+ if err := ctx.Err(); err != nil {
|
|
1596
1599
|
+ return attrib(err)
|
|
1597
1600
|
+ }
|
|
1598
1601
|
if uerr != nil {
|
|
1599
1602
|
return attrib(fmt.Errorf("上传附件 %s 失败: %w", u.Name, uerr))
|
|
1600
1603
|
}
|
|
1601
|
-
@@ -
|
|
1604
|
+
@@ -598,7 +617,10 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1602
1605
|
// prompt 超长时转成文本附件。要等挑完号和出口才能做:上传要 cookie,
|
|
1603
1606
|
// 而且必须走跟正式请求同一个出口。
|
|
1604
1607
|
budget := rtCfg().MaxPromptBytes
|
|
1605
|
-
- if p, f, used, ferr := prepareContextFile(prompt, latest, budget, cookieStr, proxyURL); ferr != nil {
|
|
1606
|
-
+ if p, f, used, ferr := prepareContextFile(ctx, prompt, pc, budget, cookieStr, proxyURL); ferr != nil {
|
|
1608
|
+
- if p, f, used, ferr := prepareContextFile(prompt, latest, budget, cookieStr, authUser, proxyURL); ferr != nil {
|
|
1609
|
+
+ if p, f, used, ferr := prepareContextFile(ctx, prompt, pc, budget, cookieStr, authUser, proxyURL); ferr != nil {
|
|
1607
1610
|
+ if err := ctx.Err(); err != nil {
|
|
1608
1611
|
+ return attrib(err)
|
|
1609
1612
|
+ }
|
|
1610
1613
|
return attrib(ferr)
|
|
1611
1614
|
} else if used {
|
|
1612
1615
|
prompt = p
|
|
1613
|
-
@@ -
|
|
1616
|
+
@@ -726,45 +748,29 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1614
1617
|
xsrfRetried := false // XSRF 自愈只做一次,避免死循环
|
|
1615
1618
|
t0 := time.Now()
|
|
1616
1619
|
|
|
@@ -1650,15 +1653,39 @@ index 7c9186f..c04b69c 100644
|
|
|
1650
1653
|
+ return sleepCtx(ctx, time.Duration(rtCfg().RetryDelaySec)*time.Second)
|
|
1651
1654
|
}
|
|
1652
1655
|
|
|
1656
|
+
- // 新协议(#43):同一个 inner,只换传输层;返回的 raw 已包成老格式,下面的判断原样适用。
|
|
1657
|
+
- useWC := rtCfg().WebChannel
|
|
1658
|
+
+ // 新协议(#43)尚未接入按沉默计时(#33)、请求取消传播、工具提前收尾与扩展思考,
|
|
1659
|
+
+ // 当前版本锁死走 StreamGenerate。
|
|
1660
|
+
+ if rtCfg().WebChannel {
|
|
1661
|
+
+ logf("[gemini] web_channel 已在配置中开启,但当前版本固定走 StreamGenerate(保留按沉默计时、请求取消、工具提前收尾与扩展思考)")
|
|
1662
|
+
+ }
|
|
1663
|
+
+ useWC := false
|
|
1664
|
+
wcReq := wcRequest{
|
|
1665
|
+
Inner: wcInnerWithoutPrompt(inner), Prompt: prompt,
|
|
1666
|
+
HexID: mc.HexID, Mode: mc.Mode, Think: mc.thinkWire(),
|
|
1667
|
+
Cookie: cookieStr, SAPISID: sapisid, AuthUser: authUser,
|
|
1668
|
+
}
|
|
1653
1669
|
for attempt := 0; attempt < rtCfg().RetryAttempts; attempt++ {
|
|
1654
|
-
- statusCode, raw, ttfb, setCookie, err := doGeminiRequest(endpoint, body, geminiHeaders, proxyURL, lineCB)
|
|
1655
1670
|
+ if err := ctx.Err(); err != nil {
|
|
1656
1671
|
+ return attrib(err)
|
|
1657
1672
|
+ }
|
|
1658
|
-
|
|
1673
|
+
var statusCode int
|
|
1674
|
+
var raw []byte
|
|
1675
|
+
var ttfb int64
|
|
1676
|
+
@@ -772,9 +778,9 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1677
|
+
var err error
|
|
1678
|
+
if useWC {
|
|
1679
|
+
wcReq.Cookie = cookieStr
|
|
1680
|
+
- statusCode, raw, ttfb, setCookie, err = doWebChannelRequest(wcReq, proxyURL, lineCB)
|
|
1681
|
+
+ statusCode, raw, ttfb, setCookie, err = doWebChannelRequest(wcReq, proxyURL, lh.cb)
|
|
1682
|
+
} else {
|
|
1683
|
+
- statusCode, raw, ttfb, setCookie, err = doGeminiRequest(endpoint, body, geminiHeaders, proxyURL, lineCB)
|
|
1684
|
+
+ statusCode, raw, ttfb, setCookie, err = doGeminiRequestStop(ctx, endpoint, body, geminiHeaders, proxyURL, lh.cb, &lh.stop)
|
|
1685
|
+
}
|
|
1659
1686
|
if len(setCookie) > 0 && cookieID > 0 {
|
|
1660
1687
|
if merged := mergeSetCookie(cookieStr, setCookie); merged != cookieStr {
|
|
1661
|
-
|
|
1688
|
+
@@ -782,6 +788,10 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1662
1689
|
updateAccountCookie(cookieID, merged)
|
|
1663
1690
|
}
|
|
1664
1691
|
}
|
|
@@ -1669,7 +1696,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1669
1696
|
// 降级不是出口或 cookie 的错,也不该重试:同一个号再发一次还是降级。
|
|
1670
1697
|
var mde *ModelDowngradedError
|
|
1671
1698
|
if errors.As(err, &mde) {
|
|
1672
|
-
@@ -
|
|
1699
|
+
@@ -793,6 +803,9 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1673
1700
|
return res, err
|
|
1674
1701
|
}
|
|
1675
1702
|
if err != nil {
|
|
@@ -1679,7 +1706,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1679
1706
|
lastErr = err
|
|
1680
1707
|
if pickedOK {
|
|
1681
1708
|
recordProxyResult(picked.ID, false, err.Error())
|
|
1682
|
-
@@ -
|
|
1709
|
+
@@ -803,7 +816,9 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1683
1710
|
}
|
|
1684
1711
|
if attempt < rtCfg().RetryAttempts-1 {
|
|
1685
1712
|
logf("retry %d/%d: %v", attempt+1, rtCfg().RetryAttempts, err)
|
|
@@ -1690,7 +1717,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1690
1717
|
}
|
|
1691
1718
|
continue
|
|
1692
1719
|
}
|
|
1693
|
-
@@ -
|
|
1720
|
+
@@ -832,7 +847,9 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1694
1721
|
break
|
|
1695
1722
|
}
|
|
1696
1723
|
if attempt < rtCfg().RetryAttempts-1 {
|
|
@@ -1701,7 +1728,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1701
1728
|
}
|
|
1702
1729
|
continue
|
|
1703
1730
|
}
|
|
1704
|
-
@@ -
|
|
1731
|
+
@@ -860,7 +877,9 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1705
1732
|
}
|
|
1706
1733
|
if attempt < rtCfg().RetryAttempts-1 {
|
|
1707
1734
|
logf("retry %d/%d: 空响应(无内容帧,%d 字节)", attempt+1, rtCfg().RetryAttempts, len(raw))
|
|
@@ -1712,7 +1739,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1712
1739
|
}
|
|
1713
1740
|
continue
|
|
1714
1741
|
}
|
|
1715
|
-
@@ -
|
|
1742
|
+
@@ -926,6 +945,60 @@ func streamGenerateOnce(prompt, latest string, mc ModelConfig, pending []pending
|
|
1716
1743
|
return attrib(lastErr)
|
|
1717
1744
|
}
|
|
1718
1745
|
|
|
@@ -1773,7 +1800,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1773
1800
|
// upstreamModelRe 匹配响应帧里服务端自报的模型显示名(帧的 [42] 位)。
|
|
1774
1801
|
// 形如 ...,"fbb127bbb056c959",null,null,"3.6 Flash",true,...
|
|
1775
1802
|
var upstreamModelRe = regexp.MustCompile(`\\"[0-9a-f]{16}\\",null,null,\\"([^"\\]{1,40})\\"`)
|
|
1776
|
-
@@ -
|
|
1803
|
+
@@ -1006,10 +1079,122 @@ func setAuthUserHeaders(h map[string]string, authuser int) {
|
|
1777
1804
|
// 实测号活一两小时就失效 —— 所以这些必须收下来并写回账号。
|
|
1778
1805
|
func doGeminiRequest(endpoint, body string, headers map[string]string, proxyURL string,
|
|
1779
1806
|
onLine func(string) error) (int, []byte, int64, []string, error) {
|
|
@@ -1897,7 +1924,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1897
1924
|
if err != nil {
|
|
1898
1925
|
return 0, nil, 0, nil, err
|
|
1899
1926
|
}
|
|
1900
|
-
@@ -
|
|
1927
|
+
@@ -1017,36 +1202,37 @@ func doGeminiRequest(endpoint, body string, headers map[string]string, proxyURL
|
|
1901
1928
|
for k, v := range headers {
|
|
1902
1929
|
req.Header.Set(k, v)
|
|
1903
1930
|
}
|
|
@@ -1944,7 +1971,7 @@ index 7c9186f..c04b69c 100644
|
|
|
1944
1971
|
}
|
|
1945
1972
|
return resp.StatusCode, raw, ttfb, resp.Header.Values("Set-Cookie"), nil
|
|
1946
1973
|
}
|
|
1947
|
-
@@ -
|
|
1974
|
+
@@ -1355,6 +1541,7 @@ func probeGemini(prompt, proxyURL string) ProbeResult {
|
|
1948
1975
|
res.Diagnostic = "构建请求失败: " + e.Error()
|
|
1949
1976
|
return res
|
|
1950
1977
|
}
|
|
@@ -2122,10 +2149,10 @@ index 0000000..ef9b476
|
|
|
2122
2149
|
+}
|
|
2123
2150
|
diff --git a/internal/app/latency_test.go b/internal/app/latency_test.go
|
|
2124
2151
|
new file mode 100644
|
|
2125
|
-
index 0000000..
|
|
2152
|
+
index 0000000..fbbae69
|
|
2126
2153
|
--- /dev/null
|
|
2127
2154
|
+++ b/internal/app/latency_test.go
|
|
2128
|
-
@@ -0,0 +1,
|
|
2155
|
+
@@ -0,0 +1,312 @@
|
|
2129
2156
|
+package app
|
|
2130
2157
|
+
|
|
2131
2158
|
+import (
|
|
@@ -2156,14 +2183,14 @@ index 0000000..236a51d
|
|
|
2156
2183
|
+ t.Fatal("没有身份项时应退回按整串区分")
|
|
2157
2184
|
+ }
|
|
2158
2185
|
+
|
|
2159
|
-
+ key :=
|
|
2186
|
+
+ key := xsrfKey(cookie, 0)
|
|
2160
2187
|
+ xsrfMu.Lock()
|
|
2161
2188
|
+ xsrfCache[key] = xsrfEntry{token: "tok", fetched: time.Now()}
|
|
2162
2189
|
+ xsrfMu.Unlock()
|
|
2163
2190
|
+ t.Cleanup(func() { invalidateXSRF(cookie) })
|
|
2164
2191
|
+
|
|
2165
2192
|
+ // 命中缓存时不发网络请求;这里 proxyURL 指向不可达地址,真去抓会报错。
|
|
2166
|
-
+ tok, err := getXSRF(refreshed, "http://127.0.0.1:1")
|
|
2193
|
+
+ tok, err := getXSRF(refreshed, 0, "http://127.0.0.1:1")
|
|
2167
2194
|
+ if err != nil || tok != "tok" {
|
|
2168
2195
|
+ t.Fatalf("刷新后的 cookie 没命中缓存: tok=%q err=%v", tok, err)
|
|
2169
2196
|
+ }
|
|
@@ -2252,7 +2279,8 @@ index 0000000..236a51d
|
|
|
2252
2279
|
+ {"```tool_call\n{\"name\": \"bash\", \"arguments\": {\"command\": \"ls\"}}\n```", true},
|
|
2253
2280
|
+ {"好的\n```tool_call {\"name\":\"bash\",\"arguments\":{}}```", true},
|
|
2254
2281
|
+ {"```tool_call\n{\"name\": \"rm_rf\", \"arguments\": {}}\n```", false}, // 不认识的函数
|
|
2255
|
-
+ {"```tool_call\n{\"name\": \"bash\", \"arguments\": {\"command\": \"echo ```\"}}\n```",
|
|
2282
|
+
+ {"```tool_call\n{\"name\": \"bash\", \"arguments\": {\"command\": \"echo ```\"}}\n```", true}, // 参数里有围栏,外层已闭合
|
|
2283
|
+
+ {"```tool_call\n{\"name\": \"bash\", \"arguments\": {\"command\": \"echo ```\"}}", false}, // 参数里有围栏,外层未闭合
|
|
2256
2284
|
+ }
|
|
2257
2285
|
+ for _, c := range cases {
|
|
2258
2286
|
+ if got := done(c.text); got != c.want {
|
|
@@ -2364,14 +2392,15 @@ index 0000000..236a51d
|
|
|
2364
2392
|
+// 后台重取失败时必须作废缓存,否则失效的号会一直拿着旧 token 被当成可用。
|
|
2365
2393
|
+func TestRefreshAppTokensFailureDropsCache(t *testing.T) {
|
|
2366
2394
|
+ cookie := "SAPISID=sap2; __Secure-1PSID=psid2"
|
|
2395
|
+
+ key := xsrfKey(cookie, 0)
|
|
2367
2396
|
+ xsrfMu.Lock()
|
|
2368
|
-
+ xsrfCache[
|
|
2397
|
+
+ xsrfCache[key] = xsrfEntry{token: "stale", fetched: time.Now()}
|
|
2369
2398
|
+ xsrfMu.Unlock()
|
|
2370
2399
|
+ t.Cleanup(func() { invalidateXSRF(cookie) })
|
|
2371
2400
|
+
|
|
2372
|
-
+ refreshAppTokens(
|
|
2401
|
+
+ refreshAppTokens(key, cookie, 0, "http://127.0.0.1:1")
|
|
2373
2402
|
+ xsrfMu.Lock()
|
|
2374
|
-
+ _, ok := xsrfCache[
|
|
2403
|
+
+ _, ok := xsrfCache[key]
|
|
2375
2404
|
+ xsrfMu.Unlock()
|
|
2376
2405
|
+ if ok {
|
|
2377
2406
|
+ t.Fatal("重取失败后缓存仍在")
|
|
@@ -2517,10 +2546,10 @@ index acb1131..a8bab02 100644
|
|
|
2517
2546
|
return nil, err
|
|
2518
2547
|
}
|
|
2519
2548
|
diff --git a/internal/app/media.go b/internal/app/media.go
|
|
2520
|
-
index
|
|
2549
|
+
index ec3b6e4..07f3164 100644
|
|
2521
2550
|
--- a/internal/app/media.go
|
|
2522
2551
|
+++ b/internal/app/media.go
|
|
2523
|
-
@@ -
|
|
2552
|
+
@@ -762,6 +762,7 @@ func mediaGetOnce(rawURL string, headers map[string]string, proxyURL string) (in
|
|
2524
2553
|
if err != nil {
|
|
2525
2554
|
return 0, nil, nil, err
|
|
2526
2555
|
}
|
|
@@ -2529,10 +2558,10 @@ index 23d09b8..060df77 100644
|
|
|
2529
2558
|
req.Header.Set(k, v)
|
|
2530
2559
|
}
|
|
2531
2560
|
diff --git a/internal/app/messages.go b/internal/app/messages.go
|
|
2532
|
-
index
|
|
2561
|
+
index 81b8900..9d1702d 100644
|
|
2533
2562
|
--- a/internal/app/messages.go
|
|
2534
2563
|
+++ b/internal/app/messages.go
|
|
2535
|
-
@@ -
|
|
2564
|
+
@@ -48,8 +48,50 @@ type ToolCallFunction struct {
|
|
2536
2565
|
// 出口才知道能不能做,所以判断放在 streamGenerate 里。最新那条消息单独返回,
|
|
2537
2566
|
// 转附件时用来内联,好让模型不必去文件里找问题。
|
|
2538
2567
|
func messagesToPrompt(messages []map[string]interface{}, tools []map[string]interface{},
|
|
@@ -2585,7 +2614,7 @@ index 376171f..87b91fe 100644
|
|
|
2585
2614
|
}
|
|
2586
2615
|
|
|
2587
2616
|
// latestUserMessage 取最后一条 user 消息的正文,没有则返回空串。
|
|
2588
|
-
@@ -
|
|
2617
|
+
@@ -67,6 +109,12 @@ func latestUserMessage(messages []map[string]interface{}) string {
|
|
2589
2618
|
|
|
2590
2619
|
func buildPrompt(messages []map[string]interface{}, tools []map[string]interface{},
|
|
2591
2620
|
toolChoice interface{}) string {
|
|
@@ -2598,7 +2627,7 @@ index 376171f..87b91fe 100644
|
|
|
2598
2627
|
var parts []string
|
|
2599
2628
|
|
|
2600
2629
|
toolsInjected := false
|
|
2601
|
-
@@ -
|
|
2630
|
+
@@ -91,7 +139,7 @@ func buildPrompt(messages []map[string]interface{}, tools []map[string]interface
|
|
2602
2631
|
})
|
|
2603
2632
|
}
|
|
2604
2633
|
if len(defs) > 0 {
|
|
@@ -2607,7 +2636,7 @@ index 376171f..87b91fe 100644
|
|
|
2607
2636
|
rule := "Only use tool_call blocks when needed."
|
|
2608
2637
|
switch {
|
|
2609
2638
|
case forced != "":
|
|
2610
|
-
@@ -
|
|
2639
|
+
@@ -104,13 +152,13 @@ func buildPrompt(messages []map[string]interface{}, tools []map[string]interface
|
|
2611
2640
|
"and nothing else — do not answer the question yourself, even if you " +
|
|
2612
2641
|
"know the answer."
|
|
2613
2642
|
}
|
|
@@ -2624,7 +2653,7 @@ index 376171f..87b91fe 100644
|
|
|
2624
2653
|
toolsInjected = true
|
|
2625
2654
|
}
|
|
2626
2655
|
}
|
|
2627
|
-
@@ -
|
|
2656
|
+
@@ -167,33 +215,27 @@ func buildPrompt(messages []map[string]interface{}, tools []map[string]interface
|
|
2628
2657
|
// 工具名(shell_command)0/4 抄假名 —— 示例教的是「动作」不是名字,故不必按客户端
|
|
2629
2658
|
// 动态生成 schema。
|
|
2630
2659
|
if toolsInjected {
|
|
@@ -2677,8 +2706,8 @@ index 376171f..87b91fe 100644
|
|
|
2677
2706
|
}
|
|
2678
2707
|
|
|
2679
2708
|
func contentToString(content interface{}) string {
|
|
2680
|
-
@@ -
|
|
2681
|
-
return strings.TrimSpace(clean), toolCalls
|
|
2709
|
+
@@ -440,6 +482,48 @@ func parseToolCalls(text string) (string, []ToolCall) {
|
|
2710
|
+
return strings.TrimSpace(clean.String()), toolCalls
|
|
2682
2711
|
}
|
|
2683
2712
|
|
|
2684
2713
|
+// toolCallComplete 返回一个判定函数:累积正文里出现了**闭合且能解析**、函数名在
|
|
@@ -2707,15 +2736,16 @@ index 376171f..87b91fe 100644
|
|
|
2707
2736
|
+ if i < 0 {
|
|
2708
2737
|
+ return false
|
|
2709
2738
|
+ }
|
|
2710
|
-
+
|
|
2711
|
-
+
|
|
2739
|
+
+ after := text[i+len(toolFenceOpen):]
|
|
2740
|
+
+ body, _, ok := toolFenceEnd(after, false)
|
|
2741
|
+
+ if !ok {
|
|
2712
2742
|
+ return false
|
|
2713
2743
|
+ }
|
|
2714
2744
|
+ var data struct {
|
|
2715
2745
|
+ Name string `json:"name"`
|
|
2716
2746
|
+ Arguments map[string]interface{} `json:"arguments"`
|
|
2717
2747
|
+ }
|
|
2718
|
-
+ if json.Unmarshal([]byte(strings.TrimSpace(
|
|
2748
|
+
+ if json.Unmarshal([]byte(strings.TrimSpace(after[:body])), &data) != nil {
|
|
2719
2749
|
+ return false
|
|
2720
2750
|
+ }
|
|
2721
2751
|
+ return names[data.Name]
|
|
@@ -2726,7 +2756,7 @@ index 376171f..87b91fe 100644
|
|
|
2726
2756
|
// 返回 (mode, forcedName):mode ∈ {"auto","none","required"};
|
|
2727
2757
|
// forcedName 非空表示客户端点名了某个函数。
|
|
2728
2758
|
diff --git a/internal/app/server.go b/internal/app/server.go
|
|
2729
|
-
index
|
|
2759
|
+
index 830fa6a..93963d7 100644
|
|
2730
2760
|
--- a/internal/app/server.go
|
|
2731
2761
|
+++ b/internal/app/server.go
|
|
2732
2762
|
@@ -1,6 +1,7 @@
|
|
@@ -2741,8 +2771,8 @@ index b451d12..5d3cf5b 100644
|
|
|
2741
2771
|
"time"
|
|
2742
2772
|
)
|
|
2743
2773
|
|
|
2744
|
-
-const Version = "4.
|
|
2745
|
-
+const Version = "4.
|
|
2774
|
+
-const Version = "4.27.0"
|
|
2775
|
+
+const Version = "4.27.0-magpie.1"
|
|
2746
2776
|
|
|
2747
2777
|
func writeJSON(w http.ResponseWriter, status int, data interface{}) {
|
|
2748
2778
|
body, _ := json.Marshal(data)
|
|
@@ -3195,7 +3225,7 @@ index 634f3b7..399057e 100644
|
|
|
3195
3225
|
if err != nil {
|
|
3196
3226
|
fmt.Fprintf(os.Stderr, "[tokenizer] init failed (will fall back to chars/4): %v\n", err)
|
|
3197
3227
|
diff --git a/internal/app/upload.go b/internal/app/upload.go
|
|
3198
|
-
index
|
|
3228
|
+
index 4feb5d9..a21e8d1 100644
|
|
3199
3229
|
--- a/internal/app/upload.go
|
|
3200
3230
|
+++ b/internal/app/upload.go
|
|
3201
3231
|
@@ -2,6 +2,7 @@ package app
|
|
@@ -3209,25 +3239,25 @@ index 7747a72..2753f93 100644
|
|
|
3209
3239
|
@@ -20,6 +21,11 @@ const uploadHost = "https://push.clients6.google.com/upload/"
|
|
3210
3240
|
// 匿名也能传成功,但传上去的文件在对话里引用会被回 1100,所以调用方要自己确保有 cookie。
|
|
3211
3241
|
// proxyURL 必须跟正式请求同一出口,否则在 Google 眼里是两个会话共用文件。
|
|
3212
|
-
func uploadBytes(cookie, proxyURL string, data []byte, filename string) (string, error) {
|
|
3213
|
-
+ return uploadBytesCtx(context.Background(), cookie, proxyURL, data, filename)
|
|
3242
|
+
func uploadBytes(cookie string, authuser int, proxyURL string, data []byte, filename string) (string, error) {
|
|
3243
|
+
+ return uploadBytesCtx(context.Background(), cookie, authuser, proxyURL, data, filename)
|
|
3214
3244
|
+}
|
|
3215
3245
|
+
|
|
3216
3246
|
+// uploadBytesCtx 同 uploadBytes,ctx 取消(客户端断开)时中止上传。
|
|
3217
|
-
+func uploadBytesCtx(ctx context.Context, cookie, proxyURL string, data []byte, filename string) (string, error) {
|
|
3218
|
-
pushID, pctx, err := getUploadTokens(cookie, proxyURL)
|
|
3247
|
+
+func uploadBytesCtx(ctx context.Context, cookie string, authuser int, proxyURL string, data []byte, filename string) (string, error) {
|
|
3248
|
+
pushID, pctx, err := getUploadTokens(cookie, authuser, proxyURL)
|
|
3219
3249
|
if err != nil {
|
|
3220
3250
|
return "", fmt.Errorf("取上传页面参数失败: %w", err)
|
|
3221
|
-
@@ -63,7 +69,7 @@ func uploadBytes(cookie, proxyURL string, data []byte,
|
|
3251
|
+
@@ -63,7 +69,7 @@ func uploadBytes(cookie string, authuser int, proxyURL string, data []byte, file
|
|
3222
3252
|
"X-Goog-Upload-Protocol": "resumable",
|
|
3223
3253
|
"X-Goog-Upload-Header-Content-Length": strconv.Itoa(len(data)),
|
|
3224
3254
|
})
|
|
3225
|
-
- status, respHead, body, err := uploadPost(
|
|
3226
|
-
+ status, respHead, body, err := uploadPostCtx(ctx,
|
|
3255
|
+
- status, respHead, body, err := uploadPost(uploadStartURL(authuser), startHeaders,
|
|
3256
|
+
+ status, respHead, body, err := uploadPostCtx(ctx, uploadStartURL(authuser), startHeaders,
|
|
3227
3257
|
[]byte("File name: "+sanitizeUploadName(filename)), proxyURL)
|
|
3228
3258
|
if err != nil {
|
|
3229
3259
|
return "", fmt.Errorf("上传 start 失败: %w", err)
|
|
3230
|
-
@@ -82,7 +88,7 @@ func uploadBytes(cookie, proxyURL string, data []byte,
|
|
3260
|
+
@@ -82,7 +88,7 @@ func uploadBytes(cookie string, authuser int, proxyURL string, data []byte, file
|
|
3231
3261
|
"X-Goog-Upload-Command": "upload, finalize",
|
|
3232
3262
|
"X-Goog-Upload-Offset": "0",
|
|
3233
3263
|
})
|
|
@@ -3329,10 +3359,10 @@ index b0c9724..b1ca7cb 100644
|
|
|
3329
3359
|
if err != nil {
|
|
3330
3360
|
return pendingUpload{}, fmt.Errorf("image %d: fetch failed: %w", idx, err)
|
|
3331
3361
|
diff --git a/internal/app/xsrf.go b/internal/app/xsrf.go
|
|
3332
|
-
index
|
|
3362
|
+
index 254d686..e008abb 100644
|
|
3333
3363
|
--- a/internal/app/xsrf.go
|
|
3334
3364
|
+++ b/internal/app/xsrf.go
|
|
3335
|
-
@@ -
|
|
3365
|
+
@@ -29,15 +29,23 @@ var (
|
|
3336
3366
|
)
|
|
3337
3367
|
|
|
3338
3368
|
type xsrfEntry struct {
|
|
@@ -3360,7 +3390,7 @@ index c43b104..d3d7145 100644
|
|
|
3360
3390
|
var snlm0eRe = regexp.MustCompile(`"SNlM0e":"([^"]{10,200})"`)
|
|
3361
3391
|
|
|
3362
3392
|
// 上传要的两个页面参数,跟 XSRF token 同页取,省一次页面请求。
|
|
3363
|
-
@@ -
|
|
3393
|
+
@@ -46,8 +54,18 @@ var pushIDRe = regexp.MustCompile(`"qKIAYe":"([^"]{4,400})"`)
|
|
3364
3394
|
var pctxRe = regexp.MustCompile(`"Ylro7b":"([^"]{4,400})"`)
|
|
3365
3395
|
|
|
3366
3396
|
// cookieKey 用 cookie 的短摘要当缓存键,避免把整串凭证塞进 map key。
|
|
@@ -3380,26 +3410,26 @@ index c43b104..d3d7145 100644
|
|
|
3380
3410
|
return hex.EncodeToString(sum[:8])
|
|
3381
3411
|
}
|
|
3382
3412
|
|
|
3383
|
-
@@ -
|
|
3413
|
+
@@ -91,12 +109,9 @@ func getXSRF(cookie string, authuser int, proxyURL string) (string, error) {
|
|
3384
3414
|
}
|
|
3385
|
-
key :=
|
|
3415
|
+
key := xsrfKey(cookie, authuser)
|
|
3386
3416
|
|
|
3387
3417
|
- xsrfMu.Lock()
|
|
3388
3418
|
- if e, ok := xsrfCache[key]; ok && time.Since(e.fetched) < xsrfTTL {
|
|
3389
3419
|
- xsrfMu.Unlock()
|
|
3390
|
-
+ if e, ok := cachedAppTokens(key, cookie, proxyURL); ok {
|
|
3420
|
+
+ if e, ok := cachedAppTokens(key, cookie, authuser, proxyURL); ok {
|
|
3391
3421
|
return e.token, nil
|
|
3392
3422
|
}
|
|
3393
3423
|
- xsrfMu.Unlock()
|
|
3394
3424
|
|
|
3395
|
-
e, err := fetchAppTokens(cookie, proxyURL)
|
|
3425
|
+
e, err := fetchAppTokens(cookie, authuser, proxyURL)
|
|
3396
3426
|
if err != nil {
|
|
3397
|
-
@@ -
|
|
3427
|
+
@@ -108,16 +123,48 @@ func getXSRF(cookie string, authuser int, proxyURL string) (string, error) {
|
|
3398
3428
|
return e.token, nil
|
|
3399
3429
|
}
|
|
3400
3430
|
|
|
3401
3431
|
+// cachedAppTokens 返回未过期的缓存项;过了 xsrfRefreshAfter 时顺带踢一次后台刷新。
|
|
3402
|
-
+func cachedAppTokens(key, cookie, proxyURL string) (xsrfEntry, bool) {
|
|
3432
|
+
+func cachedAppTokens(key, cookie string, authuser int, proxyURL string) (xsrfEntry, bool) {
|
|
3403
3433
|
+ xsrfMu.Lock()
|
|
3404
3434
|
+ defer xsrfMu.Unlock()
|
|
3405
3435
|
+ e, ok := xsrfCache[key]
|
|
@@ -3409,20 +3439,20 @@ index c43b104..d3d7145 100644
|
|
|
3409
3439
|
+ if !e.refreshing && time.Since(e.fetched) >= xsrfRefreshAfter {
|
|
3410
3440
|
+ e.refreshing = true
|
|
3411
3441
|
+ xsrfCache[key] = e
|
|
3412
|
-
+ go refreshAppTokens(key, cookie, proxyURL)
|
|
3442
|
+
+ go refreshAppTokens(key, cookie, authuser, proxyURL)
|
|
3413
3443
|
+ }
|
|
3414
3444
|
+ return e, true
|
|
3415
3445
|
+}
|
|
3416
3446
|
+
|
|
3417
3447
|
+// refreshAppTokens 后台重抓页面。失败就丢掉缓存,让下一个请求同步重取并报出失效。
|
|
3418
|
-
+func refreshAppTokens(key, cookie, proxyURL string) {
|
|
3419
|
-
+ if err := storeAppTokens(key, cookie, proxyURL); err != nil {
|
|
3448
|
+
+func refreshAppTokens(key, cookie string, authuser int, proxyURL string) {
|
|
3449
|
+
+ if err := storeAppTokens(key, cookie, authuser, proxyURL); err != nil {
|
|
3420
3450
|
+ logf("[xsrf] 后台刷新失败,已作废缓存: %v", err)
|
|
3421
3451
|
+ }
|
|
3422
3452
|
+}
|
|
3423
3453
|
+
|
|
3424
|
-
+func storeAppTokens(key, cookie, proxyURL string) error {
|
|
3425
|
-
+ e, err := fetchAppTokens(cookie, proxyURL)
|
|
3454
|
+
+func storeAppTokens(key, cookie string, authuser int, proxyURL string) error {
|
|
3455
|
+
+ e, err := fetchAppTokens(cookie, authuser, proxyURL)
|
|
3426
3456
|
+ xsrfMu.Lock()
|
|
3427
3457
|
+ defer xsrfMu.Unlock()
|
|
3428
3458
|
+ if err != nil {
|
|
@@ -3434,20 +3464,20 @@ index c43b104..d3d7145 100644
|
|
|
3434
3464
|
+}
|
|
3435
3465
|
+
|
|
3436
3466
|
// getUploadTokens 取上传要用的 Push-ID / X-Client-Pctx,跟 XSRF token 同一份缓存。
|
|
3437
|
-
func getUploadTokens(cookie, proxyURL string) (pushID, pctx string, err error) {
|
|
3438
|
-
key :=
|
|
3467
|
+
func getUploadTokens(cookie string, authuser int, proxyURL string) (pushID, pctx string, err error) {
|
|
3468
|
+
key := xsrfKey(cookie, authuser)
|
|
3439
3469
|
|
|
3440
3470
|
- xsrfMu.Lock()
|
|
3441
3471
|
- if e, ok := xsrfCache[key]; ok && time.Since(e.fetched) < xsrfTTL {
|
|
3442
3472
|
- xsrfMu.Unlock()
|
|
3443
|
-
+ if e, ok := cachedAppTokens(key, cookie, proxyURL); ok {
|
|
3473
|
+
+ if e, ok := cachedAppTokens(key, cookie, authuser, proxyURL); ok {
|
|
3444
3474
|
return e.pushID, e.pctx, nil
|
|
3445
3475
|
}
|
|
3446
3476
|
- xsrfMu.Unlock()
|
|
3447
3477
|
|
|
3448
|
-
e, err := fetchAppTokens(cookie, proxyURL)
|
|
3478
|
+
e, err := fetchAppTokens(cookie, authuser, proxyURL)
|
|
3449
3479
|
if err != nil {
|
|
3450
|
-
@@ -
|
|
3480
|
+
@@ -171,6 +218,7 @@ func fetchAppPage(cookie string, authuser int, proxyURL string) ([]byte, error)
|
|
3451
3481
|
if err != nil {
|
|
3452
3482
|
return nil, err
|
|
3453
3483
|
}
|