mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
用户钉死了内核的职责边界,本提交把越界部分全部拆除:
内核只做:加载适配器 · auto 调度 · 加载插件 · 调度插件 · 维护源与 key · 签发密钥
## 架构纠正(用户指出的根本错误)
我此前把信任模型弄反了。实测:插件 VM 的 os.execute/io.popen/package 全部可达
(true,true,true) —— 插件从来不是 OS 级沙箱。正确的边界是:
插件 可信(运维安装),非沙箱,有完整 OS 能力 → 平台知识/登录/浏览器归它
适配器 不可信(可被动态安装),请求级 → 沙箱归它(12 个内置适配器实测均不碰
os/io/package,后续沙箱化零风险,属独立一步)
## 删除(内核越界部分)
- plugin_browser.go 内核浏览器管理器(Xvfb/chromium/x11vnc/websockify
进程栈、回环监听器)—— 388 行全删,浏览器由插件用
os.execute 自己拉起(实测 spawn+reap 闭环可用)
- plugin_login.go 登录会话状态机 + LoginFlow 枚举 + VNC 全局门禁
—— 457 行全删。"登录"从内核概念中消失
- gateway/browser_proxy.go viewer 反代
- handlePluginLogin API 登录会话列表端点
- vnc_enabled 全局开关 改为 per-plugin 授权(见下)
## 新增(内核职责内的通用机制,无任何登录/浏览器语义)
- plugin_outbound.go 通用额外出站主机授权:插件声明 plugin.hosts(请求)
→ 运维在设置页逐个批准(授权)。与 kernel actions
同一"声明≠授权"模型。内核只知道白名单加减,不知道
host 被用来干什么
- on_unload 生命周期钩子 卸载是"调度插件"的一部分:内核在关闭插件状态前调用
插件导出的 on_unload,让它自清理外部资源。内核不知道
资源是什么(进程/句柄/远端会话都行)。带进程回收回归
测试 TestOnUnloadReapsPluginProcesses
## channels.lua 全量接管
- 登录会话自持:plugin.state.sessions(nonce 只在 begin 响应出现一次,
惰性过期清理)
- 服务端浏览器栈自持:spawnBrowser 用 io.popen 拉起 Xvfb/chromium/x11vnc/
websockify 并记 pid,reapBrowser 回收;on_unload 兜底
- 优先级阶梯(用户指定):① 本机浏览器(给 URL)→ ② 自动化(轮询/nonce 回调)
→ ③ VNC 无头浏览器兜底(无浏览器系统/自动化无法完成的场景,如回环重定向
必须落在服务端)
- plugin.auth_hosts → plugin.hosts(语义泛化:出站主机授权与登录无关)
## 验收
- 全仓旧符号零残留(grep 权威确认,代码+注释+lua+测试)
- 内核非测试代码零厂商名
- build + vet 全绿;lua/core/gateway/types/scheduler/provider 全量测试通过
- channels 端到端(登录→凭据→add_source→模型可见)继续通过
265 lines
7.4 KiB
Go
265 lines
7.4 KiB
Go
package lua
|
||
|
||
import (
|
||
"fmt"
|
||
"sort"
|
||
"strings"
|
||
|
||
golua "github.com/aarzilli/golua/lua"
|
||
)
|
||
|
||
// Package lua — Lua 绑定:凭据保险库 + 内核动作面
|
||
//
|
||
// 这两个全局把"插件自己主导链接认证"所需的两块补齐:
|
||
//
|
||
// __llmsproxy_secrets —— 安全存放 OAuth token / session / 设备指纹
|
||
// __llmsproxy_kernel —— 触发内核动作(注册源、探测、清冷却、改链路…)
|
||
//
|
||
// 注册点与 __llmsproxy_http 相同:只在 PLUGIN 的 bootPluginState 里装,
|
||
// 因为适配器共用 setupGlobals 而不该拿到这些能力。
|
||
//
|
||
// 两张表都用 buildJSONTable 的既有写法(NewTable + PushGoClosure + SetField),
|
||
// 该写法在 json 表上长期稳定,也是内核自己的用法。
|
||
|
||
// secretGlobal 是凭据保险库的 Lua 名字。
|
||
const secretGlobal = "__llmsproxy_secrets"
|
||
|
||
// kernelGlobal 是内核动作面的 Lua 名字。
|
||
const kernelGlobal = "__llmsproxy_kernel"
|
||
|
||
// registerSecretBridge installs the credential vault onto a plugin's state.
|
||
//
|
||
// The plugin name is bound HERE, at boot, rather than read from the Lua state at
|
||
// call time. That is what makes "a plugin can only read its OWN credentials" a
|
||
// mechanism instead of a convention: a shard or any other holder of the compiled
|
||
// script cannot pass a different name.
|
||
func registerSecretBridge(L *golua.State, ps *Plugins, name string) {
|
||
L.NewTable()
|
||
L.PushGoClosure(luaSecretSet(ps, name))
|
||
L.SetField(-2, "set")
|
||
L.PushGoClosure(luaSecretGet(ps, name))
|
||
L.SetField(-2, "get")
|
||
L.PushGoClosure(luaSecretDelete(ps, name))
|
||
L.SetField(-2, "delete")
|
||
L.PushGoClosure(luaSecretKeys(ps, name))
|
||
L.SetField(-2, "keys")
|
||
L.SetGlobal(secretGlobal)
|
||
}
|
||
|
||
// luaSecretSet: set(key, value) -> (true, nil) | (nil, err)
|
||
func luaSecretSet(ps *Plugins, name string) golua.LuaGoFunction {
|
||
return func(L *golua.State) int {
|
||
key := L.ToString(1)
|
||
val := L.ToString(2)
|
||
if err := ps.SetSecret(name, key, val); err != nil {
|
||
L.PushNil()
|
||
L.PushString(err.Error())
|
||
return 2
|
||
}
|
||
L.PushBoolean(true)
|
||
L.PushNil()
|
||
return 2
|
||
}
|
||
}
|
||
|
||
// luaSecretGet: get(key) -> (value, nil) | (nil, err). A missing key yields
|
||
// (nil, nil) so a plugin can test `if tok == nil then login() end` — the "no
|
||
// credential stored yet" case is the common one for a fresh install, and making
|
||
// it an error would force every caller to string-match on a message.
|
||
func luaSecretGet(ps *Plugins, name string) golua.LuaGoFunction {
|
||
return func(L *golua.State) int {
|
||
key := L.ToString(1)
|
||
v, ok, err := ps.GetSecret(name, key)
|
||
if err != nil {
|
||
L.PushNil()
|
||
L.PushString(err.Error())
|
||
return 2
|
||
}
|
||
if !ok {
|
||
L.PushNil()
|
||
L.PushNil()
|
||
return 2
|
||
}
|
||
L.PushString(v)
|
||
L.PushNil()
|
||
return 2
|
||
}
|
||
}
|
||
|
||
// luaSecretDelete: delete(key) -> (true, nil) | (nil, err)
|
||
func luaSecretDelete(ps *Plugins, name string) golua.LuaGoFunction {
|
||
return func(L *golua.State) int {
|
||
key := L.ToString(1)
|
||
if err := ps.DeleteSecret(name, key); err != nil {
|
||
L.PushNil()
|
||
L.PushString(err.Error())
|
||
return 2
|
||
}
|
||
L.PushBoolean(true)
|
||
L.PushNil()
|
||
return 2
|
||
}
|
||
}
|
||
|
||
// luaSecretKeys: keys() -> (array of key names, nil). Names only — a plugin can
|
||
// render "which accounts are configured" without ever echoing token bytes.
|
||
func luaSecretKeys(ps *Plugins, name string) golua.LuaGoFunction {
|
||
return func(L *golua.State) int {
|
||
keys, err := ps.SecretKeys(name)
|
||
if err != nil {
|
||
L.PushNil()
|
||
L.PushString(err.Error())
|
||
return 2
|
||
}
|
||
L.NewTable()
|
||
for i, k := range keys {
|
||
L.PushString(k)
|
||
L.RawSeti(-2, i+1)
|
||
}
|
||
L.PushNil()
|
||
return 2
|
||
}
|
||
}
|
||
|
||
// registerKernelBridge installs the kernel action surface onto a plugin's state.
|
||
func registerKernelBridge(L *golua.State, ps *Plugins, name string) {
|
||
L.NewTable()
|
||
L.PushGoClosure(luaKernelActions(ps, name))
|
||
L.SetField(-2, "actions")
|
||
L.PushGoClosure(luaKernelCall(ps, name))
|
||
L.SetField(-2, "call")
|
||
L.SetGlobal(kernelGlobal)
|
||
}
|
||
|
||
// luaKernelActions: actions() -> (array of {name,title,danger,description,params,
|
||
// granted}, nil)
|
||
//
|
||
// It reports GRANTED alongside each action so a plugin can degrade gracefully
|
||
// (skip the part that needs a permission it does not have) instead of
|
||
// discovering it by hitting an error mid-operation.
|
||
func luaKernelActions(ps *Plugins, name string) golua.LuaGoFunction {
|
||
return func(L *golua.State) int {
|
||
perm := ps.PluginKernelPermission(name)
|
||
L.NewTable()
|
||
for i, a := range KernelActions() {
|
||
L.NewTable()
|
||
L.PushString(a.Name)
|
||
L.SetField(-2, "name")
|
||
L.PushString(a.Title)
|
||
L.SetField(-2, "title")
|
||
L.PushString(a.Danger)
|
||
L.SetField(-2, "danger")
|
||
L.PushString(a.Description)
|
||
L.SetField(-2, "description")
|
||
L.PushBoolean(perm.allows(a))
|
||
L.SetField(-2, "granted")
|
||
L.NewTable()
|
||
for j, p := range a.Params {
|
||
L.PushString(p)
|
||
L.RawSeti(-2, j+1)
|
||
}
|
||
L.SetField(-2, "params")
|
||
L.RawSeti(-2, i+1)
|
||
}
|
||
L.PushNil()
|
||
return 2
|
||
}
|
||
}
|
||
|
||
// luaKernelCall: call(name, args) -> (result, nil) | (nil, err)
|
||
//
|
||
// One entry point rather than a function per action: the action set will change,
|
||
// and a table field per action would mean editing bootPluginState — the exact
|
||
// place that produced a cgo crash during earlier work.
|
||
func luaKernelCall(ps *Plugins, name string) golua.LuaGoFunction {
|
||
return func(L *golua.State) int {
|
||
action := L.ToString(1)
|
||
args := map[string]interface{}{}
|
||
if L.Type(2) == golua.LUA_TTABLE {
|
||
var raw interface{}
|
||
if err := luaToJSON(L, 2, &raw); err == nil {
|
||
if m, ok := raw.(map[string]interface{}); ok {
|
||
args = m
|
||
}
|
||
}
|
||
}
|
||
v, err := ps.callKernel(name, action, args)
|
||
if err != nil {
|
||
L.PushNil()
|
||
L.PushString(err.Error())
|
||
return 2
|
||
}
|
||
if v == nil {
|
||
L.PushNil()
|
||
} else {
|
||
pushGoValue(L, v)
|
||
}
|
||
L.PushNil()
|
||
return 2
|
||
}
|
||
}
|
||
|
||
// readPluginKernelDeclared reads `plugin.kernel`, the actions a plugin asks for.
|
||
//
|
||
// The shape is a list of names:
|
||
//
|
||
// plugin.kernel = { "status", "reset_health" }
|
||
//
|
||
// Unknown names are kept rather than dropped so the settings page can show a
|
||
// typo as "requested but not a real action" — silently discarding it would make
|
||
// a plugin's intent invisible at exactly the moment the operator is deciding
|
||
// what to grant.
|
||
func readPluginKernelDeclared(L *golua.State) []string {
|
||
raw, ok := pluginFieldValue(L, "kernel")
|
||
if !ok {
|
||
return nil
|
||
}
|
||
items, isList := raw.([]interface{})
|
||
if !isList {
|
||
if one, isTable := raw.(map[string]interface{}); isTable {
|
||
// A single string field or a set-style table; accept both spellings.
|
||
if v, ok := one["actions"].([]interface{}); ok {
|
||
items = v
|
||
} else {
|
||
return nil
|
||
}
|
||
} else {
|
||
return nil
|
||
}
|
||
}
|
||
out := make([]string, 0, len(items))
|
||
seen := map[string]bool{}
|
||
for _, it := range items {
|
||
name, ok := it.(string)
|
||
if !ok {
|
||
continue
|
||
}
|
||
name = strings.TrimSpace(name)
|
||
if name == "" || seen[name] {
|
||
continue
|
||
}
|
||
seen[name] = true
|
||
out = append(out, name)
|
||
}
|
||
sort.Strings(out)
|
||
return out
|
||
}
|
||
|
||
// luaTableArg decodes a Lua table argument into a Go map, reporting a clear error
|
||
// when the caller passed something else (a common mistake that would otherwise
|
||
// surface as a silent zero value).
|
||
func luaTableArg(L *golua.State, idx int) (map[string]interface{}, error) {
|
||
if L.Type(idx) != golua.LUA_TTABLE {
|
||
return nil, fmt.Errorf("expected a table argument, got %s", normalizeString(L, idx))
|
||
}
|
||
var raw interface{}
|
||
if err := luaToJSON(L, idx, &raw); err != nil {
|
||
return nil, err
|
||
}
|
||
m, ok := raw.(map[string]interface{})
|
||
if !ok {
|
||
return nil, fmt.Errorf("argument must be a keyed table")
|
||
}
|
||
return m, nil
|
||
}
|