feat(插件代理): 同源反向代理 —— VNC 不再暴露额外端口

问题:登录返回的 view_url 是 http://127.0.0.1:60100/vnc.html,WebUI 直接把它
嵌进 iframe。远程运维打开必白屏 —— 127.0.0.1 在客户端指向客户端自己,且
websockify/x11vnc 只绑回环,换服务器 IP 也连不上(实测 LAN IP 访问 000)。

★ 教训:只验证"服务端截图有画面"就宣布 VNC 可用是不充分的。四层链路里
①X 渲染 ②noVNC 客户端渲染 一直是好的,白屏真因在第④层「客户端如何拿到地址」。

内核新增通用「插件前缀代理」能力(不是为 channels 写的专用反向代理):
- plugin.http 支持 prefix = true:声明前缀路由,承接所有子路径
- handler 返回 { proxy = "http://127.0.0.1:PORT" },内核每次请求都问一遍
  ⇒ 会话停止/过期后插件不再给目标,旧地址自然失效(撤销点唯一)
- 鉴权与角色门禁复用现有链路,无旁路
- statusRecorder 增加 Unwrap(),让 Hijack 穿透访问日志中间件(否则 WebSocket 升级失败)
- 目标只允许带显式端口的**字面回环 IP**(用户拍板方案 A):拒绝 https、localhost
  主机名、无端口、带 userinfo/path/query;理由是主机名可能被 hosts/DNS 改写
- 拒绝跨站 Origin(WebSocket 带环境 Cookie,否则第三方网页可借管理员会话开隧道)
- 不向插件后端转发 Authorization/Cookie/api_key

channels 接入:
- 新增 channels/browser/proxy/:session(prefix=true)
- 会话代号 = seq-随机8位,防猜到活跃会话
- view_url 改为网关同源路径,noVNC 的 WebSocket 走同前缀 /websockify
- 会话过期即回收浏览器栈并返回 404

判据(每条咬真实外部效应):
- 静态资源转发
- 真实 HTTP 101 升级 + 双向数据回传(只断言静态转发不够 —— 升级是白屏真考点)
- 网关凭据不泄漏(在服务端断言请求头)
- 非法目标全拒(公网/localhost/https/无端口/带 path、query、userinfo)
- 跨站 Origin 403
- 未鉴权 401、过期会话 404、非子路径 404

回归:go test -tags luajit ./... 9 包全绿
This commit is contained in:
JianFeeeee
2026-10-15 14:57:13 +08:00
parent 25a525be84
commit 796b30f5b0
9 changed files with 276 additions and 9 deletions

View File

@ -4269,11 +4269,22 @@ local function spawnBrowser(start_url)
return nil, "浏览器 CDP 无应答(:" .. tostring(cdpPort) .. "):" .. tostring(why)
end
b.pids = pids
b.session = { display = disp, vnc = vport, ws = wport, cdp = cdpPort, pass = pass, seq = seq }
local host = (plugin.state.settings and plugin.state.settings.vnc_host) or "127.0.0.1"
-- 同源代理不再需要 vnc_host;后端仍只监听回环。
local sess = tostring(seq) .. "-" .. pass:sub(1, 8)
b.session = { display = disp, vnc = vport, ws = wport, cdp = cdpPort, pass = pass, seq = seq, sess = sess,
expires_at = (os.time and os.time() or 0) + 900 }
-- 代理入口:把 noVNC 视图路径经网关回传,而不是让客户端去连 VNC 端口。
-- 客户端可能来自另一台机器(127.0.0.1 指向客户端自己)且 websockify 只绑回环,
-- 所以只能由网关同端口转发;插件只提供回环目标与绝对路径,不处理传输。
local base = "/api/plugins/http/channels/browser/proxy/" .. sess
local proxyPath = base .. "/vnc.html?autoconnect=1&resize=scale&password=" .. pass
.. "&path=" .. urlencode(base:sub(2) .. "/websockify")
return {
view_url = ("http://%s:%d/vnc.html?autoconnect=1&host=%s&port=%d&password=%s"):format(
host, wport, host, wport, pass),
view_url = proxyPath,
proxy_path = proxyPath,
proxy_url = base .. "/vnc.html",
session_id = sess,
vnc_target = ("http://127.0.0.1:%d"):format(wport),
password = pass,
cdp_port = cdpPort,
display = disp,
@ -6251,6 +6262,9 @@ plugin.http = {
roles = { "admin" }, description = "浏览器栈依赖预检(缺什么、装哪个包)" },
{ path = "channels/browser/status", method = "GET", handler = "browser_status",
roles = { "admin" }, description = "服务端浏览器会话状态" },
{ path = "channels/browser/proxy/:session", method = "GET", prefix = true,
handler = "browser_proxy", roles = { "admin" },
description = "把会话的 noVNC 静态资源与 WebSocket 经网关同端口转发(不暴露 VNC 端口)" },
{ path = "channels/browser/collect", method = "POST", handler = "collect_credentials",
roles = { "admin" }, description = "从服务端浏览器取凭据(CDP)" },
{ path = "channels/browser/stop", method = "POST", handler = "stop_browser",
@ -7485,6 +7499,24 @@ function plugin.browser_status(req)
return { status = 200, json = { ok = true, running = s ~= nil, session = s } }
end
-- browser_proxy:前缀代理的每次请求入口。
-- 只回答「当前会话的回环目标」;回收后 session 为空,地址即失效(404),
-- 未运行的会话 id 也不会被转发(防目录穿越与跨会话探测)。
function plugin.browser_proxy(req)
sweepSessions()
local b = browserStack()
local s = b.session
local want = req and req.params and req.params.session or nil
if s and s.expires_at and s.expires_at <= os.time() then
reapBrowser()
s = nil
end
if not s or not s.ws or not s.sess or s.sess ~= want then
return { status = 404, json = { error = "没有这个浏览器会话" } }
end
return { proxy = ("http://127.0.0.1:%d"):format(s.ws) }
end
function plugin.inject_platforms(req)
local args = bodyOf(req)
if args.platforms == nil then args = req end