mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
用户报障:「除了 codebuddy,其他的登陆都不太正常,尤其是有道那个」。逐平台审计
参考实现(vendor/channel-pack/src)后发现根因不是 12 个独立 bug,而是几个系统性偏差。
## ① callback 流的回调是回环地址 → 必须走服务端浏览器(核心认识)
Trae/Gemini/有道 的 redirect_uri 都是回环地址(127.0.0.1 / localhost)。回环地址由
「打开授权页的那台机器」解析:远程用户在自己电脑上打开,重定向就落在**用户机器**上,
回调永远到不了网关。CodeBuddy 一直是好的,因为它走 state-poll,**根本不需要回调**。
→ 插件自起 loopback 接收器(Python,绑回环、双栈,路径按各平台注册值),
由服务端浏览器(VNC)承载授权页,重定向落回本机。
## ② 三个平台的回调路径各是注册值(写错就是 redirect_uri_mismatch)
有道 /auth/callback、Trae /authorize、Gemini /oauth-callback(参考 GEMINI_CALLBACK_PATH)
且 Gemini 的 redirect_uri 主机是 **localhost** 而不是 127.0.0.1。
另:Trae 的回调**不带 state**(token 直接放 query),其余平台必须带 → 校验规则按形态分。
## ③ 有道版本号不能写死
exchange/续期/签到的 version 是必填。实测 2026-10-13 上游已是 2026.9.23,而参考的兜底
常量还停在 2026.9.4。新增 lobsteraiClientVersion:远端为主(data.value.version,**非统一
信封**)+ 失败用兜底但**不缓存**(照参考 resolver)。失败时把上游 message 带出来
(实测形如 code=40102「无效或已过期的授权码」)。
## ④ Gemini token 交换缺 client_secret(症状正是「授权成功但账号不出现」)
参考注释点名:token 端点强制校验客户端身份,只发 client_id 会 invalid_request。
同时补 access_type=offline / prompt=consent(缺了拿不到 refresh_token),
并去掉误标的 pkce(上游 Cloud Code 用的是最朴素的 authorization_code + client_secret)。
## ⑤ Trae 授权 URL 少了 5 个必需参数
参考列为「一个都不能少」:login_trace_id(回调**反查 pending 的唯一凭据**)、
machine_id/device_id(hex32,每账号必须不同,否则被「该设备已签到」拦)、
x_device_id/x_machine_id。此前**完全没造**设备身份 → 登录页停在「授权中」。
## ⑥ Trae 签到头与设备身份
授权方案是 `Cloud-IDE-JWT`(不是 Bearer);参考点明简化头**实测签到失败**,
需约 20 个客户端头,且 X-Device-Id 是**基于 user_id 确定性派生的 15 位数字**。
实现 seededStream(SHA256(utf8(salt:seed) ++ counterBE32) 串联)+ 三级派生,
并用参考实现(Node)生成期望值做**差分验证**逐字节对齐。
凭据改为 JSON 以保留 uid/device_id/machine_id(签到与轮换需要)。
## ⑦ 同段逻辑抽一个助手,防两条路漂移
lobsteraiExchange 被 oauth_callback 与 poll_login 两条路共用(原先各内联一份)。
## 判据(新增测试)
- TestLobsterLoopbackClosedLoop / TestLobsterExchangePreservesIdentity:loopback 闭环、
state 不匹配 400、重放 409、凭据含 uuid/first_keyfrom
- TestCallbackPlatformsLoopback:三个平台的路径与 state 规则、Trae 无 state 也能接住
- TestAuthorizeURLsHaveNoUnfilledPlaceholders:授权 URL 不留 {占位符}
- TestTraeDeriveMatchesReference:与参考实现**逐字节**差分(tools/trae_derive_expected.mjs)
- TestTraeCheckinHeaders:Cloud-IDE-JWT + 派生设备身份 + 客户端头齐全
- TestRemoteModelCatalogMergesWithFallback:远端为主、兜底补位、失败不清缓存
## 顺带
- set_endpoints 补 callback / client_version 键(否则测试无法把端点指到假上游)
- 删掉 spawnBrowser 里会把 redirect_uri 一律改写为插件挂载点的死代码(对有道致害)
- session 过期与插件卸载时回收 loopback 接收器进程(防进程泄漏)
- e2e/channels_flows_e2e_test.go:qoder 已从 manual 改为 device-poll,断言同步更新
202 lines
6.6 KiB
Go
202 lines
6.6 KiB
Go
package core
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"sync"
|
||
"testing"
|
||
|
||
"llmsproxy/internal/lua"
|
||
)
|
||
|
||
// 远端模型目录的判据:**远端为主 + 兜底补位**(照参考 reconcileWithFallback)。
|
||
//
|
||
// 参考明确:"正确做法是**合并**,而不是「远端失败就用兜底」的简单回退"
|
||
// (cline-models.ts)。故这里逐条锁定:
|
||
// - 远端有的以远端为准(顺序也照远端)
|
||
// - 远端没有的用兜底表补位
|
||
// - 拉取失败**不清空**既有缓存(宁可留旧目录也别让平台变空)
|
||
// - 平台声明的真实目录 key 必须在上游源里(占位名会让选模型调不通)
|
||
type modelsHarness struct {
|
||
t *testing.T
|
||
ps *lua.Plugins
|
||
ts *httptest.Server
|
||
mu sync.Mutex
|
||
body string
|
||
code int
|
||
hits int
|
||
}
|
||
|
||
func newModelsHarness(t *testing.T, platform, body string) *modelsHarness {
|
||
t.Helper()
|
||
h := &modelsHarness{t: t, body: body, code: 200}
|
||
h.ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
h.mu.Lock()
|
||
h.hits++
|
||
code, body := h.code, h.body
|
||
h.mu.Unlock()
|
||
w.Header().Set("Content-Type", "application/json")
|
||
w.WriteHeader(code)
|
||
fmt.Fprint(w, body)
|
||
}))
|
||
t.Cleanup(h.ts.Close)
|
||
|
||
c := kernelTestCore(t)
|
||
h.ps = c.Plugins()
|
||
src, err := readBundledChannels(t)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := h.ps.LoadSource("channels", src); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
_ = h.ps.SetPluginKernelPermission("channels", []string{"add_source", "remove_source", "list_sources", "status"}, false)
|
||
// 说明:测试上游是 127.0.0.1(loopback),出站白名单默认允许,
|
||
// 不需要额外授权 —— 也就不需要碰内核的授权 API。
|
||
// 把该平台的远端端点指到假上游
|
||
if _, err := h.ps.Call("channels", "set_endpoints", map[string]interface{}{
|
||
"platform": platform,
|
||
"models_remote": map[string]interface{}{
|
||
"endpoint0": h.ts.URL,
|
||
},
|
||
}); err != nil {
|
||
t.Logf("set_endpoints(models_remote) 未支持: %v", err)
|
||
}
|
||
return h
|
||
}
|
||
|
||
// modelIDs 从 model_list 的 Call 返回值({json:{models:[...]}})取 id 列表。
|
||
func modelIDs(t *testing.T, got interface{}) []string {
|
||
t.Helper()
|
||
j := got.(map[string]interface{})["json"].(map[string]interface{})
|
||
ms, _ := j["models"].([]interface{})
|
||
ids := make([]string, 0, len(ms))
|
||
for _, e := range ms {
|
||
ids = append(ids, e.(map[string]interface{})["id"].(string))
|
||
}
|
||
return ids
|
||
}
|
||
|
||
func TestRemoteModelCatalogMergesWithFallback(t *testing.T) {
|
||
// zcode:远端 builtinModels 是**对象**(键为序号),且远端/兜底有重叠
|
||
body := `{"data":{"builtinModels":{"0":{"modelId":"GLM-5.3"},"1":{"modelId":"GLM-5.3-Flash"},"2":{"modelId":"NEW-MODEL-X"}}}}`
|
||
h := newModelsHarness(t, "zcode", body)
|
||
|
||
t.Run("行1 远端拉到就用远端并保留兜底独有的", func(t *testing.T) {
|
||
got, err := h.ps.Call("channels", "model_refresh", map[string]interface{}{"platform": "zcode"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
m := got.(map[string]interface{})["json"].(map[string]interface{})
|
||
res := m["results"].([]interface{})[0].(map[string]interface{})
|
||
if res["ok"] != true {
|
||
t.Fatalf("刷新应成功, 实得 %v", res)
|
||
}
|
||
// NEW-MODEL-X 只由远端下发,必须出现(证明用的不是硬编码表)
|
||
ml, err := h.ps.Call("channels", "model_list", map[string]interface{}{"platform": "zcode"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
ids := modelIDs(t, ml)
|
||
joined := strings.Join(ids, ",")
|
||
if !strings.Contains(joined, "NEW-MODEL-X") {
|
||
t.Fatalf("只由远端下发的模型必须出现(否则是硬编码表): %v", ids)
|
||
}
|
||
if !strings.Contains(joined, "GLM-5.3") {
|
||
t.Fatalf("兜底/远端共有项应保留: %v", ids)
|
||
}
|
||
// 远端与兜底重叠项不得重复
|
||
seen := map[string]int{}
|
||
for _, id := range ids {
|
||
seen[id]++
|
||
if seen[id] > 1 {
|
||
t.Fatalf("模型 %s 重复出现(合并应去重): %v", id, ids)
|
||
}
|
||
}
|
||
})
|
||
|
||
t.Run("行2 拉取失败不清空既有缓存", func(t *testing.T) {
|
||
// 先成功一次建立缓存
|
||
if _, err := h.ps.Call("channels", "model_refresh", map[string]interface{}{"platform": "zcode"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
// 再让上游 500
|
||
h.mu.Lock()
|
||
h.code = 500
|
||
h.body = `{}`
|
||
h.mu.Unlock()
|
||
got, err := h.ps.Call("channels", "model_refresh", map[string]interface{}{"platform": "zcode"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
res := got.(map[string]interface{})["json"].(map[string]interface{})["results"].([]interface{})[0].(map[string]interface{})
|
||
if res["ok"] == true {
|
||
t.Fatalf("上游 500 时刷新应报失败, 实得 %v", res)
|
||
}
|
||
ml, _ := h.ps.Call("channels", "model_list", map[string]interface{}{"platform": "zcode"})
|
||
ids := modelIDs(t, ml)
|
||
if !strings.Contains(strings.Join(ids, ","), "NEW-MODEL-X") {
|
||
t.Fatalf("拉取失败必须保留既有目录(不能让平台变空): %v", ids)
|
||
}
|
||
})
|
||
}
|
||
|
||
// TestPlatformModelsAreRealCatalogKeys 锁定 12 平台的模型名**不是编的占位名**。
|
||
// 此前 qoder/zcode/loomy/raccoon 用的是 "*-default"(我编的),上游不认 ——
|
||
// 后果是这些平台登录成功后选任何模型都调不通,而测试全绿(它测的就是我编的名字)。
|
||
func TestPlatformModelsAreRealCatalogKeys(t *testing.T) {
|
||
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)
|
||
}
|
||
got, err := ps.Call("channels", "provider_status", map[string]interface{}{})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
plats := got.(map[string]interface{})["json"].(map[string]interface{})["statuses"].([]interface{})
|
||
if len(plats) != 12 {
|
||
t.Fatalf("应有 12 个真实平台, 实得 %d", len(plats))
|
||
}
|
||
realQoder := []string{"qfmodel", "qmodel_38max", "kmodel_latest", "dmodel"}
|
||
sawQoder := 0
|
||
for _, e := range plats {
|
||
p := e.(map[string]interface{})
|
||
id := p["id"].(string)
|
||
ml, err := ps.Call("channels", "model_list", map[string]interface{}{"platform": id})
|
||
if err != nil {
|
||
t.Fatalf("平台 %s model_list: %v", id, err)
|
||
}
|
||
ids := modelIDs(t, ml)
|
||
if len(ids) == 0 {
|
||
t.Fatalf("平台 %s 无模型", id)
|
||
}
|
||
for _, mid := range ids {
|
||
// 占位名特征:以 -default 结尾
|
||
if strings.HasSuffix(mid, "-default") {
|
||
t.Fatalf("平台 %s 的模型 %q 是编的占位名(上游不认,选了调不通)", id, mid)
|
||
}
|
||
}
|
||
if id == "qoder" {
|
||
j := map[string]bool{}
|
||
for _, mid := range ids {
|
||
j[mid] = true
|
||
}
|
||
for _, want := range realQoder {
|
||
if j[want] {
|
||
sawQoder++
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if sawQoder == 0 {
|
||
t.Fatal("qoder 应含参考的真实目录 key(如 qfmodel),而非编造的占位名")
|
||
}
|
||
}
|