mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-12 20:27:23 +00:00
按发布纪律回流。**注意这是内容对齐而非 merge**,理由与依据:
1. 纪律禁止 merge 回主干,因为 merge 会把发布分支的**版本号变更**带进 main。
本项目版本号**只由 `git describe --tags` 推导**(Makefile VERSION),
仓库里没有任何文件存放版本号 ⇒ 回流不存在版本污染风险。
2. 逐项核对确认 **main 是 release 的内容严格子集**:
- `main` 独有的**文件**:0 个;
- `internal/lua/plugins.go` / `internal/core/kernel_actions.go` 等内核文件
main 独有行数:0;
- `deploy.sh` 那 14 行只是 emoji 清理(release 刻意去掉了 emoji);
- `channels.lua` 那 333 行是**旧版内容**(旧 exports 表、旧模型名单)。
- main 独有的 9 个提交按 patch-id **全部已在 release 中等效存在**
(`git cherry` 逐条列出均为 `-`,即已合入)。
3. 故回流 = 把 release 的内容带到 main,等价于"只捡拾修复、不带版本号",
且不会丢任何 main 独有的修复。
本次带回 v2.0.12 的全部内容(37 个提交):
- 源串行 + 按模型节流(照 model-gate)
- 已失效模型存储 + 远端目录闸门
- 余额分档选号
- 模型能力元数据(models.dev,isFree 权威判定)
- **凭据续期修复**(此前恒报 ok:true 却一个请求都不发)
- 通用件 TTL 缓存 + 串行锁;account_test/account_adopt/account_delete 修正
- 内核级:出站授权持久化重启后不生效、`__llmsproxy_plugin_dir` ground truth
校验:`git diff release/v2.0.x` = 0 行;`go build -tags luajit ./...` 通过。
112 lines
4.1 KiB
Go
112 lines
4.1 KiB
Go
package core
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"testing"
|
||
|
||
"llmsproxy/internal/lua"
|
||
)
|
||
|
||
// Trae 签到是最容易被"看起来对"骗过的地方:请求能发出去、上游可能也返回 200,
|
||
// 但头不对就只是一个失败/风控响应。参考实现明确记录:
|
||
//
|
||
// ① 授权方案是 Cloud-IDE-JWT,**不是** Bearer(用错前缀是 401 级失败);
|
||
// ② 简化头(只有 Authorization + 几个 Ug 头)**实测签到失败**,
|
||
// 真正成功的是约 20 个客户端头;
|
||
// ③ X-Device-Id 不是凭据里的 hex32,而是**基于 user_id 确定性派生的 15 位数字**
|
||
// (每账号天然不同设备,所以不再需要 9074 时换设备号重试)。
|
||
//
|
||
// 本测试按参考逐条锁定这些事实,并把派生的 device_id 钉死到与参考一致的值。
|
||
func TestTraeCheckinHeaders(t *testing.T) {
|
||
var got map[string]string
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
got = map[string]string{}
|
||
for k := range r.Header {
|
||
got[k] = r.Header.Get(k)
|
||
}
|
||
w.Header().Set("Content-Type", "application/json")
|
||
w.Write([]byte(`{"code":0}`))
|
||
}))
|
||
defer ts.Close()
|
||
|
||
c := kernelTestCore(t)
|
||
ps := c.Plugins()
|
||
src, err := readBundledChannels(t)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := ps.LoadSource("channels", src); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
// 把签到端点指到假上游(不覆盖 checkin.auth,验证声明真的在起作用)
|
||
if _, err := ps.Call("channels", "set_endpoints", map[string]interface{}{
|
||
"platform": "trae",
|
||
"checkin": map[string]interface{}{"url": ts.URL},
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
// uid 必须是真值:设备身份由它派生。用参考实现算出的期望值(见 tools/trae_derive_expected.mjs)
|
||
const uid = "12345"
|
||
// credential 传**字符串**(凭据是一段 JSON 文本),不是对象 —— complete_login
|
||
// 会原样存进保险库,取用时 credentialOf 再解出 access_token。
|
||
cred, _ := json.Marshal(map[string]interface{}{
|
||
"access_token": "ACCESS-1", "uid": uid, "device_id": "deadbeef",
|
||
})
|
||
body, _ := json.Marshal(map[string]string{
|
||
"platform": "trae", "credential": string(cred), "account": "t",
|
||
})
|
||
if _, err := ps.InvokeHandler("channels", "complete_login", &lua.HandlerRequest{
|
||
Method: "POST", Role: "admin", Body: string(body),
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
out, err := ps.InvokeHandler("channels", "claim_daily", &lua.HandlerRequest{
|
||
Method: "POST", Role: "admin", Body: `{"platform":"trae"}`,
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("claim_daily: %v", err)
|
||
}
|
||
if j, _ := out.JSON.(map[string]interface{}); j != nil && j["error"] != nil {
|
||
t.Fatalf("claim 失败: %v(完整响应 %v)", j["error"], j)
|
||
}
|
||
|
||
t.Run("行1 授权方案是 Cloud-IDE-JWT", func(t *testing.T) {
|
||
if v := got["Authorization"]; v != "Cloud-IDE-JWT ACCESS-1" {
|
||
t.Fatalf("Authorization 必须是 Cloud-IDE-JWT <token>(Bearer 会 401),实得 %q", v)
|
||
}
|
||
})
|
||
t.Run("行2 设备身份是按 user_id 派生的 15 位数字", func(t *testing.T) {
|
||
// 期望值来自参考实现(Node + node:crypto)的派生,不是本实现自己算的
|
||
if v := got["X-Device-Id"]; v != "707304999019651" {
|
||
t.Fatalf("X-Device-Id 应为按 uid 派生的 15 位数字 707304999019651,实得 %q", v)
|
||
}
|
||
})
|
||
t.Run("行3 客户端身份头齐全", func(t *testing.T) {
|
||
for k, want := range map[string]string{
|
||
"X-Market-Client-Id": "VSCode 1.107.1",
|
||
"X-User-Region": "CN",
|
||
"Package-Type": "stable_cn",
|
||
"X-Lgw-Req-Sdk-Type": "3",
|
||
"X-Lscbd-Platform": "windows",
|
||
"App-Version": "0.1.52",
|
||
} {
|
||
if v := got[k]; v != want {
|
||
t.Errorf("头 %s = %q,期望 %q", k, v, want)
|
||
}
|
||
}
|
||
if got["X-Market-User-Id"] == "" || len(got["X-Market-User-Id"]) != 36 {
|
||
t.Errorf("X-Market-User-Id 应是 UUID v4 形状,实得 %q", got["X-Market-User-Id"])
|
||
}
|
||
if len(got["Vscode-Sessionid"]) != 64 {
|
||
t.Errorf("Vscode-Sessionid 应是 64 位 hex,实得 %q", got["Vscode-Sessionid"])
|
||
}
|
||
if got["X-Tt-Trace-Id"] == "" || got["X-Request-Id"] == "" {
|
||
t.Error("每次请求都应有独立的 X-Tt-Trace-Id / X-Request-Id")
|
||
}
|
||
})
|
||
}
|