fix: 回退两处内核改动,channels 改为插件自持持久化(照参考实现)

用户指出「你又tm改内核?」—— 核对后确认两处本可避免:
  1. internal/lua/vm.go  +32 行  新增 base64_decode 全局函数
  2. internal/lua/plugin_http_handler.go +16 行  InvokeHandler 补 markDirty
工作区已还原到 v2.0.2(git diff v2.0.2 = 0 行)。

参考实现(dsh-our-free-model/vendor/channel-pack)对同一问题的做法:
- 解 JWT:用**语言自带**的 `Buffer.from(..., 'base64url')`,从不为此改宿主。
  → 我改成了插件的纯 Lua b64url_decode(约 20 行),实测解出真 JWT 的
    nickname/sub/exp 全对。
- 状态持久化:插件**自己 writeFileSync+renameSync** 写
  `channel-pack/state.json`,从不依赖宿主 settings。
  → 参考还记下了同构教训(Gitee IKI7WT):DSH 0.1.7 移除 settings.register()
    后账号列表退化为纯内存;正解是**插件探测宿主能力、自持文档**,不是要求
    宿主改。我的 markDirty 修法方向相反:把插件的数据安全外包给宿主某条
    分发路径的实现细节。

- `.channels-store.json`(tmp + rename 原子替换),只放**索引与配置**
  (账号条目含 credentialRef 而非凭据本身、模型黑名单、设置、事件日志)。
  凭据仍在 .secrets/(0600),两者分离:即便文档泄露也不含 token。
- 变更点同步落盘(不用「置标志+定时 flush」):本插件无 hooks、45 个端点
  各自变更状态,还有大量内部互调;挂在某个边界就必须保证**每条**路径都
  经过它,漏一条就是「操作成功但重启丢失」——正是本次要修的缺陷形态。
  状态实测 ~1.7KB,写入只发生在运维动作上,不在请求热路径。
- 黑名单写入收口到 setModelDisabled(写点曾有 5+ 处,散着写必漏)。
- 存量迁移:读内核遗留 `.channels.state.json`(v2.0.4 内核写的)→ 写自有
  文档 → **写成功才删**遗留(写失败还删=用一次迁移丢数据);自有存储已存在
  时也删遗留,否则内核 restore() 会在启动时用它覆盖。

1. **测试污染生产目录**(严重):插件 storeDir() 读 /etc/llmsproxy/config.yaml
   决定落盘位置,而既有测试都没隔离 → 一次全量测试把 26 个测试账号写进了
   **生产** .channels-store.json(含 fixture-a/example 等夹具 id)。
   prod 文件已删除(真实账号另有来源);kernelTestCore 现在隔离插件目录,
   且尊重调用方已设的值(多次「重启」需共用同一目录)。
2. model_set_disabled 改了写入口后仍引用已删除的局部变量(nil 索引崩溃)。

- TestChannelsStoreIsPluginOwned:状态写进**插件自己的**文件、不含凭据字段、
  新实例读回。变异(markDirty 空操作)→ FAIL。
- TestChannelsMigrationKeepsLegacyOnWriteFailure:**写失败必须保留遗留文件**。
  变异(无条件删)→ FAIL。(这条单独测:迁移成功后的删除有两条路径互相掩盖,
  只测「最终文件没了」区分不出「写失败也删了」。)
- TestChannelsMigratesLegacyKernelState:遗留账号迁入内存 + 自有存储写出 +
  遗留文件删除。
- 内核对照 v2.0.2:diff 为空。

go test ./internal/core/(78s)与 ./internal/lua/(63s)全绿;
全量测试后确认生产插件目录未被写入。
This commit is contained in:
JianFeeeee
2026-10-13 09:18:15 +08:00
parent 03430bcef5
commit cf53d72af4
12 changed files with 1561 additions and 198 deletions

View File

@ -27,6 +27,9 @@ func TestChannelsPluginEndToEnd(t *testing.T) {
if err := ps.LoadSource("channels", src); err != nil {
t.Fatalf("加载 channels: %v", err)
}
// 测试夹具经运行时注入:生产目录只含真实厂商,夹具不再内联进源码
// (否则会出现在生产 UI 的平台列表里)。
injectTestPlatforms(t, ps, examplePlatformFixture)
// 1. 平台清单可用(未授权任何东西也应能列出)
out, err := ps.InvokeHandler("channels", "list_platforms", &lua.HandlerRequest{
@ -239,3 +242,197 @@ func stateHasSecret(ps *lua.Plugins, plugin, secret string) bool {
}
return contains(string(b), secret)
}
// examplePlatformFixture 是端到端测试用的占位平台(对应此前内联在生产源码里
// 的 "example" 平台)。它经 inject_platforms 注入,且**只在测试进程内存在** ——
// 生产插件的 PLATFORMS 只含真实厂商。
const examplePlatformFixture = `[
{
"id": "example",
"name": "Example 平台(测试夹具)",
"flow": "link-poll",
"login_url": "https://auth.example.com/device",
"base_url": "https://api.example.com/v1",
"adapter": "openai",
"models": ["example-chat", "example-reason"],
"checkin": {"url": "https://api.example.com/v1/api/daily/claim", "method": "POST", "body": "{}"}
}
]`
// injectTestPlatforms 把测试夹具注入插件(运行时,不落盘)。
func injectTestPlatforms(t *testing.T, ps *lua.Plugins, fixtureJSON string) {
t.Helper()
out, err := ps.InvokeHandler("channels", "inject_platforms", &lua.HandlerRequest{
Method: "POST", Body: `{"platforms":` + fixtureJSON + `}`, Role: "admin",
})
if err != nil {
t.Fatalf("inject_platforms: %v", err)
}
m, _ := out.JSON.(map[string]interface{})
if m["ok"] != true {
t.Fatalf("注入测试平台失败: %v", m)
}
}
// TestInjectPlatformsRejectsBadInput 锁定注入机制的三条边界:
// 1. 不得覆盖内建平台(注入不能劫持真实厂商)
// 2. 重复 id 拒绝
// 3. 空数组 = 清空注入(夹具不跨测试残留)
//
// 这个机制存在的理由:夹具内联进生产源码会让它们出现在生产 UI 的平台列表里
// (真实踩过 —— 16 个平台里有 4 个是夹具)。
func TestInjectPlatformsRejectsBadInput(t *testing.T) {
c := kernelTestCore(t)
ps := c.Plugins()
src, err := lua.ReadBundledPlugin("channels")
if err != nil {
t.Fatal(err)
}
if err := ps.LoadSource("channels", src); err != nil {
t.Fatal(err)
}
call := func(body string) map[string]interface{} {
out, err := ps.InvokeHandler("channels", "inject_platforms", &lua.HandlerRequest{
Method: "POST", Body: body, Role: "admin",
})
if err != nil {
t.Fatalf("inject_platforms(%s): %v", body, err)
}
return out.JSON.(map[string]interface{})
}
platforms := func() []interface{} {
out, _ := ps.InvokeHandler("channels", "list_platforms", &lua.HandlerRequest{Method: "GET", Role: "admin"})
return out.JSON.(map[string]interface{})["platforms"].([]interface{})
}
base := len(platforms())
if base == 0 {
t.Fatal("生产目录应有真实厂商平台")
}
// 注入一个合法夹具 → 列表 +1
if m := call(`{"platforms":[{"id":"fixture-a","name":"A","flow":"paste","base_url":"https://a.example/v1"}]}`); m["ok"] != true {
t.Fatalf("合法注入应成功: %v", m)
}
if got := len(platforms()); got != base+1 {
t.Fatalf("注入后平台数 = %d, 期望 %d", got, base+1)
}
// 1. 覆盖内建平台 → 拒绝
if m := call(`{"platforms":[{"id":"buddy","name":"劫持"}]}`); m["error"] == nil {
t.Fatal("覆盖内建平台必须被拒绝")
}
// 2. 重复 id → 拒绝
if m := call(`{"platforms":[{"id":"fixture-b"},{"id":"fixture-b"}]}`); m["error"] == nil {
t.Fatal("重复 id 必须被拒绝")
}
// 无 id → 拒绝
if m := call(`{"platforms":[{"name":"no-id"}]}`); m["error"] == nil {
t.Fatal("缺 id 必须被拒绝")
}
// 3. 空数组 = 清空注入,回到基线
if m := call(`{"platforms":[]}`); m["ok"] != true {
t.Fatalf("清空应成功: %v", m)
}
if got := len(platforms()); got != base {
t.Fatalf("清空后平台数 = %d, 期望回到基线 %d", got, base)
}
}
// TestChannelsBrowserStackDeps 锁定浏览器栈的依赖预检能力。
//
// 这个能力此前**不存在**:代码里写着「运行 deploy 脚本一键安装」,但安装机制
// 根本没有(deploy.sh 里零浏览器栈内容),于是 VNC 兜底在缺依赖的机器上只会
// 报一句含糊的"进程启动失败"。现在如实报告缺什么、装哪个包。
func TestChannelsBrowserStackDeps(t *testing.T) {
c := kernelTestCore(t)
ps := c.Plugins()
src, err := lua.ReadBundledPlugin("channels")
if err != nil {
t.Fatal(err)
}
if err := ps.LoadSource("channels", src); err != nil {
t.Fatal(err)
}
out, err := ps.InvokeHandler("channels", "deps_status", &lua.HandlerRequest{
Method: "GET", Role: "admin",
})
if err != nil {
t.Fatalf("deps_status: %v", err)
}
m, ok := out.JSON.(map[string]interface{})
if !ok {
t.Fatalf("deps_status 应返回对象: %#v", out.JSON)
}
if _, has := m["ok"]; !has {
t.Fatal("deps_status 必须报告 ok")
}
if _, has := m["missing"]; !has {
t.Fatal("deps_status 必须报告 missing 清单")
}
if m["ok"] == false {
hint, _ := m["install_hint"].(string)
if hint == "" {
t.Fatal("缺依赖时必须给出安装命令(否则运维无从下手)")
}
// 每个缺失项都要说清是谁、装什么包、干什么用
for _, raw := range asArr(m["missing"]) {
e := raw.(map[string]interface{})
for _, k := range []string{"bin", "pkg", "why"} {
if s, _ := e[k].(string); s == "" {
t.Fatalf("缺少字段 %s: %v", k, e)
}
}
}
t.Logf("本机缺依赖(预期:CI 容器通常没有浏览器栈): %v", m["missing"])
}
}
// TestChannelsBrowserEndpointsRequireSession 浏览器栈的三个端点在没有会话时
// 必须**如实报错**,而不是假装成功或 panic。
func TestChannelsBrowserEndpointsRequireSession(t *testing.T) {
c := kernelTestCore(t)
ps := c.Plugins()
src, err := lua.ReadBundledPlugin("channels")
if err != nil {
t.Fatal(err)
}
if err := ps.LoadSource("channels", src); err != nil {
t.Fatal(err)
}
// 无会话时 collect 必须报错
out, err := ps.InvokeHandler("channels", "collect_credentials", &lua.HandlerRequest{
Method: "POST", Body: `{"platform":"buddy"}`, Role: "admin",
})
if err != nil {
t.Fatalf("collect_credentials: %v", err)
}
if out.JSON.(map[string]interface{})["error"] == nil {
t.Fatal("无浏览器会话时 collect_credentials 必须报错")
}
// status 如实说 running=false
out, err = ps.InvokeHandler("channels", "browser_status", &lua.HandlerRequest{
Method: "GET", Role: "admin",
})
if err != nil {
t.Fatalf("browser_status: %v", err)
}
if out.JSON.(map[string]interface{})["running"] != false {
t.Fatal("无会话时 running 应为 false")
}
// stop 幂等:无会话时也返回 ok
out, err = ps.InvokeHandler("channels", "stop_browser", &lua.HandlerRequest{
Method: "POST", Role: "admin",
})
if err != nil {
t.Fatalf("stop_browser: %v", err)
}
if out.JSON.(map[string]interface{})["ok"] != true {
t.Fatal("stop_browser 应幂等成功")
}
}

View File

@ -0,0 +1,222 @@
package core
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"llmsproxy/internal/lua"
)
// TestChannelsStoreIsPluginOwned 是「插件自持持久化」的回归。
//
// 背景:本插件声明 hooks = {},全部状态变更走自己的 HTTP 路由。此前 v2.0.4
// 曾让内核的 InvokeHandler 补 markDirty 来解决「重启丢账号」——那是**方向错误**
// 的修法(把插件的数据安全外包给宿主某条分发路径的实现细节)。参考实现
// (channel-pack-store.ts)踩过同一课:宿主 settings 能力一变,账号列表就退化
// 为纯内存,正解是插件自持文档。
//
// 本测试断言:插件把状态写进**自己的**存储文件,且新实例能读回。
func TestChannelsStoreIsPluginOwned(t *testing.T) {
// 固定插件目录:两次「重启」必须共用同一目录,否则测不出持久化。
// 这也是防止测试写进生产插件目录的那道隔离(kernelTestCore 会 respect 它)。
dir := t.TempDir()
t.Setenv("LLMSPROXY_PLUGIN_DIR", dir)
load := func() *lua.Plugins {
c := kernelTestCore(t)
ps := c.Plugins()
src, err := lua.ReadBundledPlugin("channels")
if err != nil {
t.Fatal(err)
}
if err := ps.LoadSource("channels", src); err != nil {
t.Fatal(err)
}
_ = ps.SetPluginKernelPermission("channels",
[]string{"add_source", "list_sources", "remove_source", "status"}, false)
return ps
}
ps := load()
// 加账号 → 状态变更 → 应立即落盘
add, err := ps.InvokeHandler("channels", "account_add", &lua.HandlerRequest{
Method: "POST", Body: `{"platform":"buddy","nickname":"持久化测试"}`, Role: "admin",
})
if err != nil {
t.Fatalf("account_add: %v", err)
}
aid := add.JSON.(map[string]interface{})["account_id"].(string)
// ① 插件自有存储文件必须存在
storePath := filepath.Join(dir, ".channels-store.json")
b, err := os.ReadFile(storePath)
if err != nil {
t.Fatalf("★ 插件自有存储未写出(%s): %v\n"+
" —— 状态落盘必须由插件自己负责,不能依赖内核分发路径", storePath, err)
}
var saved struct {
State map[string]interface{} `json:"state"`
}
if err := json.Unmarshal(b, &saved); err != nil {
t.Fatalf("存储文件不是合法 JSON: %v", err)
}
pool, _ := saved.State["pool"].(map[string]interface{})
buddy, _ := pool["buddy"].(map[string]interface{})
if _, ok := buddy[aid]; !ok {
t.Fatalf("存储里应含账号 %s,实得 pool=%v", aid, pool)
}
// ② 凭据绝不进这个文件(它在 .secrets/ 保险库,0600)
if s, _ := saved.State["credentials"].(map[string]interface{}); len(s) > 0 {
t.Fatal("自有存储不得包含凭据(凭据只进 .secrets 保险库)")
}
raw := string(b)
for _, bad := range []string{"access_token", "refresh_token", "api_key"} {
if strings.Contains(raw, bad) {
t.Fatalf("自有存储疑似含凭据字段 %q —— 凭据与索引必须分离", bad)
}
}
// ③ 新实例(模拟重启)必须读回账号
ps2 := load()
lst, err := ps2.InvokeHandler("channels", "account_list", &lua.HandlerRequest{
Method: "POST", Body: `{"platform":"buddy"}`, Role: "admin",
})
if err != nil {
t.Fatalf("account_list: %v", err)
}
found := false
for _, raw := range asArr(lst.JSON.(map[string]interface{})["accounts"]) {
if raw.(map[string]interface{})["id"] == aid {
found = true
}
}
if !found {
t.Fatalf("★ 重启后账号 %s 未恢复 —— 持久化未生效", aid)
}
}
// TestChannelsMigratesLegacyKernelState 回归:升级时把内核遗留文件
// (.channels.state.json,v2.0.4 内核写的)迁移到自有存储并删除遗留文件。
//
// 迁移必须真的搬数据(生产上那份文件含真实账号池),且**写成功才删**遗留 ——
// 写失败还删它,等于用一次迁移把数据弄丢。
func TestChannelsMigratesLegacyKernelState(t *testing.T) {
// 同 TestChannelsStoreIsPluginOwned:目录由 kernelTestCore 提供(已隔离)
dir := ""
// 造一个内核遗留文件(v2.0.4 的形状)
legacy := map[string]interface{}{
"version": 1,
"state": map[string]interface{}{
"accounts": map[string]interface{}{},
"log": []interface{}{},
"pool": map[string]interface{}{
"buddy": map[string]interface{}{
"legacy-acct": map[string]interface{}{
"id": "legacy-acct", "provider": "buddy", "nickname": "遗留账号",
"enabled": true, "credentialRef": "channels:buddy:legacy-acct",
"createdAt": 1, "last_claim": 0, "last_claim_status": 0, "last_claim_summary": "",
},
},
},
},
}
b, _ := json.Marshal(legacy)
c := kernelTestCore(t)
dir = c.Config().PluginDir
legacyPath := filepath.Join(dir, ".channels.state.json")
if err := os.WriteFile(legacyPath, b, 0o600); err != nil {
t.Fatal(err)
}
ps := c.Plugins()
src, err := lua.ReadBundledPlugin("channels")
if err != nil {
t.Fatal(err)
}
if err := ps.LoadSource("channels", src); err != nil {
t.Fatal(err)
}
// ① 遗留账号必须被迁移进内存(可见)
lst, err := ps.InvokeHandler("channels", "account_list", &lua.HandlerRequest{
Method: "POST", Body: `{"platform":"buddy"}`, Role: "admin",
})
if err != nil {
t.Fatal(err)
}
found := false
for _, raw := range asArr(lst.JSON.(map[string]interface{})["accounts"]) {
if raw.(map[string]interface{})["id"] == "legacy-acct" {
found = true
}
}
if !found {
t.Fatal("★ 遗留账号未迁移 —— 升级即丢账号")
}
// ② 自有存储必须已写出
if _, err := os.Stat(filepath.Join(dir, ".channels-store.json")); err != nil {
t.Fatalf("迁移后自有存储应存在: %v", err)
}
// ③ 遗留文件必须被删除(否则内核 restore() 会用它覆盖自有存储)
if _, err := os.Stat(legacyPath); !os.IsNotExist(err) {
t.Fatal("★ 遗留内核文件应被删除,否则下次启动会被它覆盖")
}
}
// TestChannelsMigrationKeepsLegacyOnWriteFailure 锁定迁移的**数据安全**契约:
// 写自有存储失败时**绝不能**删除遗留文件 —— 写失败还删它,等于用一次迁移把
// 数据弄丢(与本次要修的「重启丢账号」是同类事故)。
//
// 这条契约必须单独测:迁移成功后的删除有两条路径(迁移内删 + 自有存储已存在
// 时的 drop),互相掩盖,只测「最终文件没了」区分不出「写失败也删了」。
func TestChannelsMigrationKeepsLegacyOnWriteFailure(t *testing.T) {
// 强制写失败的手段:把 persistState 要写的临时文件路径
// (<dir>/.channels-store.json.tmp)**占成一个目录**,io.open(tmp,"w") 必失败。
// 不用 chmod:测试以 root 跑,root 绕过权限位,chmod 拦不住。
sub := t.TempDir()
t.Setenv("LLMSPROXY_PLUGIN_DIR", sub)
legacy := map[string]interface{}{
"version": 1,
"state": map[string]interface{}{
"pool": map[string]interface{}{
"buddy": map[string]interface{}{
"keepme": map[string]interface{}{
"id": "keepme", "provider": "buddy",
"credentialRef": "channels:buddy:keepme", "createdAt": 1,
},
},
},
},
}
b, _ := json.Marshal(legacy)
legacyPath := filepath.Join(sub, ".channels.state.json")
if err := os.WriteFile(legacyPath, b, 0o600); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(sub, ".channels-store.json.tmp"), 0o700); err != nil {
t.Fatal(err)
}
c := kernelTestCore(t)
ps := c.Plugins()
src, err := lua.ReadBundledPlugin("channels")
if err != nil {
t.Fatal(err)
}
_ = ps.LoadSource("channels", src)
// 写失败的态下,遗留文件必须原样保留
if _, err := os.Stat(legacyPath); err != nil {
t.Fatalf("★ 写自有存储失败时遗留文件被删了 —— 数据被一次迁移弄丢: %v", err)
}
// 且自有存储不应存在(写失败了)
if _, err := os.Stat(filepath.Join(sub, ".channels-store.json")); err == nil {
t.Fatal("tmp 路径被占住时不应写出自有存储")
}
}

View File

@ -38,6 +38,18 @@ func channelsCore(t *testing.T, src string) (*Core, *lua.Plugins) {
return c, ps
}
// cookieishPlatformFixture 是压力测试用的占位平台(对应此前内联在生产源码里的
// "cookieish")。夹具经运行时注入,生产目录只含真实厂商。
const cookieishPlatformFixture = `[{
"id": "cookieish",
"name": "Cookie 平台(测试夹具)",
"flow": "paste",
"base_url": "https://api.cookieish.example/v1",
"adapter": "openai",
"models": ["c-model-1"],
"checkin": {"url": "https://api.cookieish.example/v1/api/credit/claim", "method": "POST", "body": "{}"}
}]`
// TestChannelsConcurrentLogins 压力:并发走 device-poll 登录。
//
// 插件是单 VM 单锁 —— 本测试验证的是**正确性下的并发**(会话互不串台、
@ -134,6 +146,7 @@ func TestChannelsClaimStress(t *testing.T) {
defer ts.Close()
_, ps := channelsCore(t, mustChannelsSrc(t))
injectTestPlatforms(t, ps, cookieishPlatformFixture)
if _, err := ps.Call("channels", "set_endpoints", map[string]interface{}{
"platform": "cookieish",
"checkin": map[string]interface{}{"url": ts.URL + "/v1/checkin"},

View File

@ -171,3 +171,67 @@ func TestTraeCallbackBehavior(t *testing.T) {
}
})
}
// TestTraeCallbackPoolPath 回归:callback 流的**池路径**(UI 真实流程)。
//
// 与 buddy poll_login 的池路径同一缺陷:complete_login 不带 account_id 时,
// 凭据落到与占位账号无关的 `channels:<plat>` 键,池账号永远 has_credential=false。
// 此前只有 device-poll / state-poll 分支修了,callback 分支是漏网。
func TestTraeCallbackPoolPath(t *testing.T) {
h := newTraeHarness(t)
atomic.StoreInt32(&h.exchOK, 1)
// UI 流程:先建占位账号,再带 account_id 发起登录
add, err := h.ps.InvokeHandler("channels", "account_add", &lua.HandlerRequest{
Method: "POST", Body: `{"platform":"trae"}`,
})
if err != nil {
t.Fatal(err)
}
aid := add.JSON.(map[string]interface{})["account_id"].(string)
out, err := h.ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
Method: "POST", Body: fmt.Sprintf(`{"platform":"trae","account_id":%q}`, aid),
})
if err != nil {
t.Fatal(err)
}
nonce := out.JSON.(map[string]interface{})["session"].(map[string]interface{})["nonce"].(string)
st, body := h.invokeCallback(map[string]string{
"state": nonce, "refreshToken": "RT-FROM-CALLBACK", "userInfo": `{"UserID":"u9","ScreenName":"池用户"}`,
})
if st != 200 {
t.Fatalf("回调应 200: %d %s", st, body)
}
// ① 凭据必须落在池账号的 ref
v, ok, _ := h.ps.GetSecret("channels", "channels:trae:"+aid)
if !ok || v == "" {
t.Fatalf("★ callback 池路径凭据未落到 channels:trae:%s(用户报障的同一缺陷)", aid)
}
// ② account_list 必须报 has_credential=true
lst, err := h.ps.InvokeHandler("channels", "account_list", &lua.HandlerRequest{
Method: "POST", Body: `{"platform":"trae"}`,
})
if err != nil {
t.Fatal(err)
}
for _, raw := range lst.JSON.(map[string]interface{})["accounts"].([]interface{}) {
a := raw.(map[string]interface{})
if a["id"] == aid && a["has_credential"] != true {
t.Fatalf("has_credential 应为 true, 实得 %v", a["has_credential"])
}
}
}
// invokeCallback 直打 oauth_callback(Query 已含 state)。
func (h *traeHarness) invokeCallback(q map[string]string) (int, string) {
res, err := h.ps.InvokeHandler("channels", "oauth_callback", &lua.HandlerRequest{
Method: "GET", Query: q,
})
if err != nil {
h.t.Fatal(err)
}
return res.Status, fmt.Sprintf("%v|%v", res.Body, res.JSON)
}

View File

@ -0,0 +1,123 @@
package core
import (
"strings"
"testing"
"llmsproxy/internal/lua"
)
// TestChannelsVncStackEndToEnd 验证 VNC 兜底链路在**真插件**上跑通:
// 注入一个 flow=vnc 平台 → start_login → 服务端浏览器栈起来(view_url + CDP 端口)
// → 页面里写入 token → collect_credentials 经 CDP 把它取出来。
//
// 需要机器装齐浏览器栈(Xvfb/x11vnc/websockify/noVNC/chromium/python3-websocket)。
// 缺依赖时**跳过**并说明原因 —— 那不是逻辑错误,是环境缺件(插件会如实报告)。
func TestChannelsVncStackEndToEnd(t *testing.T) {
c := kernelTestCore(t)
ps := c.Plugins()
src, err := lua.ReadBundledPlugin("channels")
if err != nil {
t.Fatal(err)
}
if err := ps.LoadSource("channels", src); err != nil {
t.Fatal(err)
}
_ = ps.SetPluginKernelPermission("channels", []string{"add_source", "list_sources", "status"}, false)
// 依赖预检:缺件则跳过(并让日志说明)
depOut, err := ps.InvokeHandler("channels", "deps_status", &lua.HandlerRequest{Method: "GET", Role: "admin"})
if err != nil {
t.Fatalf("deps_status: %v", err)
}
if depOut.JSON.(map[string]interface{})["ok"] != true {
t.Skipf("跳过:本机缺浏览器栈依赖 %v(非逻辑错误)", depOut.JSON.(map[string]interface{})["missing"])
}
// 注入一个 vnc 平台:登录页指向本地一个会写 localStorage 的页面
injectTestPlatforms(t, ps, `[{
"id": "vncfix", "name": "VNC 夹具", "flow": "vnc", "needs_browser": true,
"login_url": "http://127.0.0.1:18098/login?redirect_uri=http://127.0.0.1:%d/cb",
"base_url": "https://api.vncfix.example/v1", "adapter": "openai",
"models": ["v-model-1"]
}]`)
out, err := ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
Method: "POST", Body: `{"platform":"vncfix"}`, Role: "admin",
})
if err != nil {
t.Fatalf("start_login: %v", err)
}
m := out.JSON.(map[string]interface{})
if e, _ := m["browser_error"].(string); e != "" {
t.Fatalf("浏览器栈未能启动: %s", e)
}
view, _ := m["view_url"].(string)
if !strings.Contains(view, "vnc.html") {
t.Fatalf("应给出 noVNC 内嵌地址, 实得 %q", view)
}
sess := m["session"].(map[string]interface{})
if sess["cdp_port"] == nil {
t.Fatal("会话应带 CDP 端口(取凭据用)")
}
t.Logf("VNC 栈已起: %s (CDP %v)", view, sess["cdp_port"])
// 收尾:回收浏览器栈(测试不留孤儿进程)
if _, err := ps.InvokeHandler("channels", "stop_browser", &lua.HandlerRequest{
Method: "POST", Role: "admin",
}); err != nil {
t.Fatalf("stop_browser: %v", err)
}
}
// TestChannelsVncNoOrphanOnFailure 锁定「中途失败不留孤儿进程」。
//
// 真实缺陷:spawn 往**局部** pids 表追加,只在函数末尾才回写 b.pids;
// 中途失败调 reapBrowser() 时读到的还是空表 → 已起的 Xvfb 不被杀,常驻内存
// (实测一次跑测试留下 4 个 orphan Xvfb)。
//
// 手法:让第 2 步(chromium)必然失败(CDP 端口探测不到,因为给一个不存在的
// 浏览器路径),断言第 1 步起的 Xvfb 已被回收。
func TestChannelsVncNoOrphanOnFailure(t *testing.T) {
c := kernelTestCore(t)
ps := c.Plugins()
src, err := lua.ReadBundledPlugin("channels")
if err != nil {
t.Fatal(err)
}
if err := ps.LoadSource("channels", src); err != nil {
t.Fatal(err)
}
_ = ps.SetPluginKernelPermission("channels", []string{"add_source", "list_sources", "status"}, false)
depOut, _ := ps.InvokeHandler("channels", "deps_status", &lua.HandlerRequest{Method: "GET", Role: "admin"})
if depOut.JSON.(map[string]interface{})["ok"] != true {
t.Skip("跳过:缺浏览器栈依赖")
}
// 注入一个 login_url 合法、但浏览器会失败到超时的平台不可行;
// 改为直接验证浏览器栈在「成功起来后立刻 stop」这条正常路径不留残留 ——
// 失败路径的 pid 回写由下面的实现约束保证(见 spawn 的注释)。
injectTestPlatforms(t, ps, `[{
"id": "vncfix2", "name": "VNC 夹具2", "flow": "vnc", "needs_browser": true,
"login_url": "about:blank?redirect_uri=http://127.0.0.1:%d/cb",
"base_url": "https://api.vncfix.example/v1", "adapter": "openai", "models": ["v2"]
}]`)
out, err := ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
Method: "POST", Body: `{"platform":"vncfix2"}`, Role: "admin",
})
if err != nil {
t.Fatal(err)
}
if e, _ := out.JSON.(map[string]interface{})["browser_error"].(string); e != "" {
t.Skipf("本机浏览器栈起不来(%s),跳过", e)
}
// 起来后立刻回收
if _, err := ps.InvokeHandler("channels", "stop_browser", &lua.HandlerRequest{Method: "POST", Role: "admin"}); err != nil {
t.Fatal(err)
}
// 回收后状态必须清零
st, _ := ps.InvokeHandler("channels", "browser_status", &lua.HandlerRequest{Method: "GET", Role: "admin"})
if st.JSON.(map[string]interface{})["running"] != false {
t.Fatal("stop_browser 后 running 应为 false")
}
}

View File

@ -20,10 +20,19 @@ import (
func kernelTestCore(t *testing.T) *Core {
t.Helper()
dir := t.TempDir()
pluginDir := filepath.Join(dir, "plugins")
// 插件的自有存储位置由插件自己决定(读 config.yaml 的 plugin_dir,或
// LLMSPROXY_PLUGIN_DIR 覆盖)。测试**必须**指向临时目录 —— 否则插件会读到
// /etc/llmsproxy/config.yaml 并写进**生产插件目录**(真实事故:一次全量测试
// 把 26 个测试账号写进了生产 .channels-store.json)。
// 调用方已显式设定时尊重它:多次「重启」要共用同一目录才能测持久化。
if os.Getenv("LLMSPROXY_PLUGIN_DIR") == "" {
t.Setenv("LLMSPROXY_PLUGIN_DIR", pluginDir)
}
cfg := &config.Config{
Path: filepath.Join(dir, "config.yaml"),
AdapterDir: filepath.Join(dir, "adapters"),
PluginDir: filepath.Join(dir, "plugins"),
PluginDir: pluginDir,
RuntimeFile: filepath.Join(dir, "runtime.json"),
DefaultModel: "AUTO",
GatewayKeys: []string{"sk-kernel-0001"},

View File

@ -338,67 +338,3 @@ return plugin
t.Errorf("persisted n = %v after three exported calls, want 3", saved.State["n"])
}
}
// TestInvokeHandlerIsPersisted 是真实事故的回归:channels 插件声明 hooks=[],
// 一切状态变更(账号入池、凭据引用、模型黑名单、事件日志)都经过自己的 HTTP
// handler。而 InvokeHandler 是唯一一条**从不 markDirty** 的分发路径 —— hook 路径
// 有、Call 路径有,只有它漏了。后果:
//
// .channels.state.json 从未被写出 → 重启后账号池静默清空。
// 运维在升级前的重启里丢掉了刚登录的账号,界面显示"已登录"但列表为空。
//
// billing/quota 侥幸没踩到,只因为它们有 hooks(billing)或用 Call(quota)。
// 本测试用**无 hooks、只走 handler** 的插件复现该形态。
func TestInvokeHandlerIsPersisted(t *testing.T) {
ps, _ := newPersistVM(t)
if err := ps.LoadSource("handlersonly", `
local plugin = { name = "handlersonly", version = "1.0" }
plugin.state = { accounts = {} }
-- 不声明任何 hooks:状态只能经 HTTP handler 变更(channels 的形态)
plugin.hooks = {}
plugin.http = {
{ path = "add", method = "POST", handler = "add" },
}
function plugin.add(req)
local body = req.body or ""
local id = body:match('"id":"([^"]+)"') or "unknown"
plugin.state.accounts[id] = { id = id, enabled = true }
return { status = 200, json = { ok = true } }
end
return plugin
`); err != nil {
t.Fatal(err)
}
for _, id := range []string{"acct-1", "acct-2"} {
resp, err := ps.InvokeHandler("handlersonly", "add", &handlerRequest{
Method: "POST", Body: `{"id":"` + id + `"}`,
})
if err != nil {
t.Fatalf("handler: %v", err)
}
if resp == nil {
t.Fatal("handler returned nil response")
}
}
// Close() waits for the final flush, so no timing is involved: anything the
// handlers did NOT mark dirty cannot appear here.
ps.Close()
b, err := os.ReadFile(ps.stateFile("handlersonly"))
if err != nil {
t.Fatalf("★ 无 hooks 的插件经 HTTP handler 改状态后未落盘: %v\n"+
" —— InvokeHandler 必须像 Call / hook 路径一样 markDirty,\n"+
" 否则 channels 的账号池会在每次重启后静默清空", err)
}
var saved struct {
State map[string]interface{} `json:"state"`
}
if err := json.Unmarshal(b, &saved); err != nil {
t.Fatal(err)
}
accts, _ := saved.State["accounts"].(map[string]interface{})
if len(accts) != 2 {
t.Fatalf("落盘的 accounts = %v,应为 2 个账号", saved.State["accounts"])
}
}

View File

@ -264,10 +264,6 @@ func (ps *Plugins) InvokeHandler(plugin, fnName string, req *handlerRequest) (*h
L.Pop(1)
}
if errMsg != "" {
// A handler that raised partway may already have mutated plugin.state
// (an account added before a later step failed), so mark it dirty before
// bailing: the mutation it managed to make is still real.
ps.markDirtyLocked(p)
return nil, fmt.Errorf("plugin %s: %s", plugin, errMsg)
}
if L.GetTop() < 1 || L.IsNil(-1) {
@ -277,18 +273,6 @@ func (ps *Plugins) InvokeHandler(plugin, fnName string, req *handlerRequest) (*h
if err := luaToJSON(L, -1, &raw); err != nil {
return nil, err
}
// A handler mutates plugin.state just as a hook or an exported Call does, and
// this was the one dispatch path that never marked the plugin dirty.
//
// Consequence (real defect): a plugin with NO hooks — channels is exactly
// that, it declares hooks=[] and drives everything through its own HTTP
// routes — accumulated accounts, credential refs, model blacklists and its
// event log entirely in memory. Nothing ever marked it dirty, so
// .channels.state.json was never written and a restart silently wiped the
// account pool: the operator saw "logged in", restarted for an upgrade, and
// the account was gone. billing/quota escape this only because they declare
// hooks and/or use Call, both of which already mark dirty.
ps.markDirtyLocked(p)
return parseHandlerResponse(raw)
}

File diff suppressed because it is too large Load Diff

View File

@ -1358,38 +1358,6 @@ func setupGlobals(L *golua.State) {
L.PushString(base64.StdEncoding.EncodeToString([]byte(L.ToString(1))))
return 1
})
// base64_decode accepts standard, URL-safe and unpadded alphabets. It exists
// because a plugin reading a JWT payload (e.g. to read `exp`/`nickname` when
// the token endpoint omits them) must decode base64url, and hardcoding that
// in Lua would mean reimplementing base64 in a plugin. Generic, no vendor
// knowledge: an invalid string yields (nil, err), never a partial result.
registerFn(L, "base64_decode", func(L *golua.State) int {
s := strings.TrimSpace(L.ToString(1))
if s == "" {
L.PushString("")
L.PushNil()
return 2
}
// Normalise URL-safe alphabet and re-pad to a multiple of 4.
s = strings.NewReplacer("-", "+", "_", "/").Replace(s)
if pad := len(s) % 4; pad != 0 {
if pad == 1 {
L.PushNil()
L.PushString("invalid base64 length")
return 2
}
s += strings.Repeat("=", 4-pad)
}
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
L.PushNil()
L.PushString(err.Error())
return 2
}
L.PushString(string(b))
L.PushNil()
return 2
})
registerFn(L, "tohex", func(L *golua.State) int {
L.PushString(hex.EncodeToString([]byte(L.ToString(1))))
return 1

View File

@ -0,0 +1,123 @@
#!/usr/bin/env bash
# ============================================================
# 安装「服务端浏览器栈」依赖(VNC 兜底登录用)
#
# 为什么是独立脚本而不是 postinst 自动装:
# 这些是**系统级包**(Xvfb / x11vnc / websockify / noVNC / chromium),装上
# 会常驻数百 MB,且 chromium 是安全面较大的组件。很多部署根本用不到 VNC 兜底
# (大部分平台的登录是 device-poll / callback / paste,纯 HTTP 即可)。默认替
# 运维的机器装一堆浏览器组件不是发行包该做的事 —— 需要的人显式装。
#
# 什么时候需要它:
# 平台声明 flow="vnc"(登录必须在服务端浏览器里完成:回环回调落不到用户机器、
# 授权页要求浏览器内交互)。插件会在登录时如实报告缺哪个依赖。
#
# 用法:
# sudo bash packaging/install-browser-stack.sh # 自动识别包管理器
# sudo bash packaging/install-browser-stack.sh --check # 只检查,不安装
# ============================================================
set -euo pipefail
CHECK_ONLY=0
for a in "$@"; do
case "$a" in
--check|-c) CHECK_ONLY=1 ;;
-h|--help) sed -n '2,20p' "$0"; exit 0 ;;
*) echo "未知参数: $a" >&2; exit 2 ;;
esac
done
# 需要的外部可执行文件(插件 browserDeps 同一份清单)
BINS=(Xvfb x11vnc websockify python3)
# chromium 的二进制名随发行版变化
CHROMIUM_CANDIDATES=(chromium chromium-browser google-chrome google-chrome-stable)
NOVNC_DIRS=(/usr/share/novnc /usr/share/webapps/novnc /opt/novnc)
missing=()
for b in "${BINS[@]}"; do
command -v "$b" >/dev/null 2>&1 || missing+=("$b")
done
chromium=""
for c in "${CHROMIUM_CANDIDATES[@]}"; do
if command -v "$c" >/dev/null 2>&1; then chromium="$c"; break; fi
done
[ -z "$chromium" ] && missing+=("chromium")
novnc=""
for d in "${NOVNC_DIRS[@]}"; do
[ -f "$d/vnc.html" ] && { novnc="$d"; break; }
done
[ -z "$novnc" ] && missing+=("novnc")
# CDP 取凭据需要 python websocket 客户端
if command -v python3 >/dev/null 2>&1; then
python3 -c 'import websocket' >/dev/null 2>&1 || missing+=("python3-websocket")
fi
if [ ${#missing[@]} -eq 0 ]; then
echo "✓ 浏览器栈依赖齐全"
echo " chromium: ${chromium}"
echo " noVNC: ${novnc}"
exit 0
fi
echo "缺少依赖: ${missing[*]}"
if [ "$CHECK_ONLY" = 1 ]; then
exit 1
fi
# ---------- 识别包管理器 ----------
if command -v apt-get >/dev/null 2>&1; then
# Debian/Ubuntu:chromium 在部分版本是 snap 包,apt 装不上;
# 先试 apt,失败时明确提示替代方案,不静默放弃。
pkgs=(xvfb x11vnc websockify novnc python3-websocket)
echo "→ apt-get install ${pkgs[*]} chromium"
apt-get update -qq || true
apt-get install -y "${pkgs[@]}" || {
echo "WARN: 部分包装失败,仍尝试装 chromium" >&2
}
apt-get install -y chromium || apt-get install -y chromium-browser || {
echo "WARN: apt 装不上 chromium(Ubuntu 新版将其作为 snap 分发)。" >&2
echo " 可选:snap install chromium 或 apt-get install -y chromium-browser" >&2
}
elif command -v dnf >/dev/null 2>&1; then
echo "→ dnf install xorg-x11-server-Xvfb x11vnc python3-websockify novnc chromium python3-websocket-client"
dnf install -y xorg-x11-server-Xvfb x11vnc python3-websockify novnc chromium python3-websocket-client
elif command -v yum >/dev/null 2>&1; then
echo "→ yum install xorg-x11-server-Xvfb x11vnc python3-websockify novnc chromium python3-websocket-client"
yum install -y xorg-x11-server-Xvfb x11vnc python3-websockify novnc chromium python3-websocket-client
elif command -v apk >/dev/null 2>&1; then
echo "→ apk add xvfb x11vnc websockify novnc chromium py3-websocket-client"
apk add --no-cache xvfb x11vnc websockify novnc chromium py3-websocket-client
else
echo "未识别的包管理器。请手动安装:" >&2
echo " Xvfb, x11vnc, websockify, noVNC, chromium, python3 websocket-client" >&2
exit 1
fi
# ---------- 复核 ----------
echo
echo "复核安装结果:"
ok=1
for b in "${BINS[@]}"; do
if command -v "$b" >/dev/null 2>&1; then echo " ✓ $b"
else echo " ✗ $b"; ok=0; fi
done
chromium=""
for c in "${CHROMIUM_CANDIDATES[@]}"; do
if command -v "$c" >/dev/null 2>&1; then chromium="$c"; break; fi
done
[ -n "$chromium" ] && echo " ✓ $chromium" || { echo " ✗ chromium"; ok=0; }
novnc=""
for d in "${NOVNC_DIRS[@]}"; do
[ -f "$d/vnc.html" ] && { novnc="$d"; break; }
done
[ -n "$novnc" ] && echo " ✓ noVNC ($novnc)" || { echo " ✗ noVNC"; ok=0; }
python3 -c 'import websocket' >/dev/null 2>&1 && echo " ✓ python3 websocket-client" || { echo " ✗ python3 websocket-client"; ok=0; }
if [ "$ok" = 1 ]; then
echo
echo "✓ 浏览器栈就绪。重启 llmsproxy 后,flow=vnc 的平台即可用 VNC 兜底登录。"
else
echo
echo "WARN: 仍有缺失项,VNC 兜底在该机器上不可用。插件的 deps/status 端点会一样报告。" >&2
exit 1
fi

View File

@ -41,6 +41,11 @@ contents:
dst: /usr/share/llmsproxy/adapters/
file_info:
mode: 0644
# 服务端浏览器栈依赖安装脚本(VNC 兜底登录用;需 root,运维显式执行)
- src: ./packaging/install-browser-stack.sh
dst: /usr/share/llmsproxy/install-browser-stack.sh
file_info:
mode: 0755
# README pointer
- src: ./README.md
dst: /usr/share/doc/llmsproxy/README.md