Skip to content

feat(remote-access): 远程访问插件与自托管中继(关联 #116) - #192

Merged
freedomkk-qfeng merged 38 commits into
ECNU:mainfrom
JunieXD:feat/116-remote-access
Oct 22, 2026
Merged

freedomkk-qfeng merged 38 commits into
ECNU:mainfrom
JunieXD:feat/116-remote-access

Conversation

@JunieXD

@JunieXD JunieXD commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

问题与修改

#116 远程接入的首版:在手机或其他电脑的浏览器中使用本机的 DSH/EduWork,经可自托管的中继连接,端到端加密,由本机确认和撤销设备。

本 PR 在 @Paraso42 的原型 #180 基础上,按 #180 的 review 重新设计实现。沿用了 #180 的以下结论和做法:endpoint 白名单门禁及拒绝原因;设备的待确认、已信任、已撤销三种状态,以及“撤销即断开已有连接”;对 DSH 宿主接入点的梳理;未配对拒绝、断线重连不重复执行、撤销即断开等验收场景;中继部署和真机验收的经验。

新增包 packages/dsh-remote-access(@eduwork/dsh-remote-access 0.1.0,未发布),按目录分为:

  • protocol:浏览器、宿主、中继共用的协议与加密(Noise IK,纯 JS 实现);
  • host:宿主适配层,唯一调用 DSH 的地方。每条远程连接一个 owner Peer(interactions: 'answer'),撤销或断开时释放;
  • connections、devices、gate:连接管理、设备登记、门禁;
  • relay-link、relay:宿主到中继的连接,以及中继服务(eduwork-remote-relay,同时提供网页客户端);
  • client:本机设置页;在远程设备上,按手机宽度调整 DSH 自己的界面;
  • web:加载页、Service Worker 与预置脚本。

对 #180 review 的逐条对应:

review 要求 做法
接入官方 Peer 每条连接一个 owner Peer,RPC、事件、审批和回答都经它;连接结束或撤销时 dispose
中继验证宿主注册 中继下发挑战,宿主用 Ed25519 身份密钥签名,hostId 即公钥;覆盖无效签名、重放、错误密钥、同 hostId 并发注册的测试
响应绑定原连接 请求、流、取消都属于各自的连接对象,连接结束后迟到的结果直接丢弃,不存在“当前连接”
重试与去重 隧道层不缓存、不重发;业务去重沿用 DSH 的 requestId
不依赖官方内部实现、可复现 只用公开接口和 __DSH_TRANSPORT__ 挂钩;官方界面由宿主 renderIndex 现场提供,不做快照、不替换全局 WebSocket;端到端检查走真实的加载页、SW 和 iframe
架构收敛,单一接入路径 一个包内分模块,三端共用同一份协议代码;宿主适配层以外不引用 DSH
协议文档只写当前版本 docs/PROTOCOL.md:消息格式、状态转换、关闭码、版本兼容
握手完成条件 二维码中的宿主公钥认证宿主,设备对握手哈希签名,收到宿主 ready 后才发业务流量
首版收窄 门禁在宿主执行,默认拒绝。官方 DSH 0.2.1-rc.2 与 EduWork 0.4.6 的全部 endpoint 和 /api 路由均已归类,测试要求每一项都有归类

用户可见的变化:

  • 设置中新增“远程访问”:开关、中继服务器与接入密钥(密钥存为 DSH 凭据)、电脑名称、添加设备(二维码或链接)、核对配对码后允许、已配对设备列表与移除。
  • 远程设备可以:选择工作区、查看和新建会话、继续或停止任务、审批与回答提问、预览和下载成果。设置只能查看;桌面操作、账号、记忆修改、技能和插件管理、凭据仅限本机。
  • 手机上就是 DSH 自己的界面,只在窄屏下调整:侧栏改为全屏抽屉并在选择后收起,左上角放“打开侧边栏”“新建会话”,设置弹窗铺满屏幕。
  • 前端文件经隧道 gzip 传输(官方 DSH 首次加载 12.7 MB → 4.6 MB);RPC、流和下载不压缩。
  • 中继由 RELAY_URL 配置公开地址,可以独占域名,也可以放在共用域名的路径下(如 https://example.edu/eduwork-remote/)。连接、配对链接、网页文件、manifest、Service Worker 和健康检查都在这个路径下,Service Worker 只管 <路径>/h/;反向代理按原路径转发。宿主注册时签名覆盖完整地址,同一域名下不同路径的中继互不通用;各中继的已配对电脑列表分开保存。
  • 手机上的 DSH 界面由所连的电脑提供,与中继网页同源,所以它能使用浏览器里与其他电脑的配对;中继与其他网站共用域名时,还能以用户身份访问那些网站。配对页提示“请只与自己的电脑配对”,README 说明原因,并建议中继使用单独的域名,不与登录服务或需要登录的网站共用。

学校版开箱即用:

  • dsh-oidc:新增仅限宿主调用的 identityToken(profileID),返回当前已验证的 ID Token,快到期时先刷新。网关登录刷新时一并保存新的 ID Token。
  • 中继可凭机构登录的 ID Token 接纳电脑(RELAY_OIDC_ISSUER、RELAY_OIDC_CLIENT_IDS),只记录用户标识,不记录 Token。Token 的签发对象(有 azp 取 azp,否则取唯一的 aud)须是配置的 client ID 之一并在 aud 中,与 dsh-oidc 的规则一致。接入密钥方式保留,两者可同时启用。
  • Token 到期前电脑在新连接上重新注册,到期时间随之顺延,已连接的设备不断开;续期失败时保留原注册并退避重试。Token 到期仍未换新时,中继关闭这台电脑的注册和全部设备连接;控制连接短暂断线不影响已建立的设备连接。机构登录失效(退出登录、刷新令牌被拒,或刷新不再返回新 ID Token)时,电脑停止连接中继,设置页提示重新登录。
  • 中继的单条连接出错(例如收到格式错误的帧)时只断开这条连接,不影响整个中继。
  • 中继限制:每个账号最多 5 台电脑,每台电脑每分钟最多 60 个设备连接,同一地址一分钟内被拒 10 次后须等待。
  • EduWork 配置新增顶层 remoteAccess: { relay, oidcProfileId },可随签名配置更新下发。设置了它时,设置页不显示中继和密钥,只说明由哪个机构提供;用户开启“允许远程访问”后,登录机构账号期间自动连接中继,退出登录后停止。ID Token 只发给这个预置的中继,设置页无法更改。

计划中、本 PR 未做的:手机提醒(方案待定)、大文件流式下载、远程浏览目录和注册工作区(review 要求暂缓)、点对点直连。

验证

  • packages/dsh-remote-access:npm run check,含类型检查、构建与 71 个测试,全部通过。node scripts/check-web.ts:真实中继、加载页、SW、iframe,配对、RPC、流、下载、上传、断线重连、撤销;Chromium 与 WebKit 各在根路径和 /eduwork-remote/ 下运行,均通过,并检查 SW 的作用范围、没有请求落在中继路径之外。
  • packages/dsh-oidc:scripts/packages/manage.mjs check dsh-oidc,142 个测试通过、1 个跳过;覆盖刷新不返回新 ID Token、刷新暂时失败(503)、刷新令牌被拒(invalid_grant)和退出登录。
  • EduWork 配置相关:dsh-host 与相关桌面测试,172 个通过、17 个跳过。
  • 端到端闭环,用合成模型,Playwright 1.64.1 的 Chromium 与 WebKit,手机视口 390×844。步骤:设置中开启 → 打开链接配对、两边配对码一致、本机允许 → 远端审批,命令在宿主执行 → 远端回答提问 → 预览成果、下载会话 ZIP → 中继重启后自动重连 → 撤销。
    • 官方 DSH 0.2.1-rc.2 网页版:经公网中继两种内核通过;手机界面流程(本机中继)两种内核通过。
    • EduWork 0.4.6 macOS(从 main a758fb4 本机构建,装入本分支插件):经公网中继两种内核通过;用真实学校账号、只接受学校登录的中继,两种内核通过。
    • EduWork 0.4.6 Windows(main a758fb4 的 CI 开发包,装入本分支插件,Windows 11 ARM 虚拟机中以 x64 仿真运行):用真实学校账号、经公网中继,Chromium 3 次、WebKit 2 次通过;退出学校账号后远程访问自动停止,重新登录后自动恢复。
  • 界面文案提交(dd276f1)之后重跑了官方 DSH 两种内核的闭环与手机界面流程、EduWork macOS 的 Chromium 闭环与 WebKit 手机界面流程。
  • 子路径部署及之后的修改(0493067 起):公网中继改为部署在 /eduwork-remote/ 下(OpenResty 按原路径转发)后:
    • 官方 DSH 网页版闭环两种内核通过,并新增两项:手机上传 480 KB 附件,宿主收到的文件逐字节一致、模型收到文件引用;浏览器记录与中继站点访问日志中都没有落在 /eduwork-remote/ 之外的请求。手机界面流程(本机子路径中继)两种内核通过。
    • EduWork macOS 用真实学校账号、子路径中继,闭环两种内核通过。连续强制刷新 4 次,学校每次都返回新的 ID Token(有效期 10 小时,iat、exp 依次递增),电脑每次用新 Token 重新注册成功。
    • Windows 宿主与 Android 真机的结果在子路径改动之前,之后未重测。
  • 合并 main(04aa635)后重跑上述三组单元测试,结果不变。
  • Android 真机(Edge)经公网中继试用了上述功能。
  • 未验证:iOS 真机;ID Token 10 小时自然到期前的续期(单测用秒级 Token 覆盖,真实登录做了强制刷新);提交 prompt 途中断线;手机网络与校园网环境。

兼容性与后续事项

  • 远程访问默认关闭,需用户在设置中开启。
  • 本 PR 合并后产品中还没有远程访问。机构启用的顺序如下,每一步需另行授权:
    1. 部署中继:按包 README 配置 RELAY_URL、RELAY_OIDC_ISSUER、RELAY_OIDC_CLIENT_IDS。中继需要可从外网访问的 HTTPS 地址,建议使用单独的域名;反向代理须转发 WebSocket、保留路径,空闲超时不低于 60 秒。
    2. 发布 @eduwork/dsh-remote-access 与包含 identityToken 的 @eduwork/dsh-oidc,加入默认产品装配(默认装配使用已发布的 npm 锁),并在 scripts/settings-navigation-icons.mjs 中补上设置导航图标;随后发布客户端,机构发行版跟进到该版本。
    3. 用签名配置更新下发 remoteAccess: { relay, oidcProfileId },relay 与中继的 RELAY_URL 相同,oidcProfileId 填机构登录在 organizations 中的 id。旧版客户端会拒收含未知键的配置更新,因此须声明 package:@eduwork/dsh-remote-access 能力,并把 minClient 设为第 2 步发布的版本(已写入 docs/CONTENT_UPDATES.md)。
    4. 用户更新客户端、登录机构账号后,在“设置 → 远程访问”中开启,再添加设备。

合作贡献者

@Paraso42(#180)贡献了远程访问原型及本 PR 沿用的宿主接入点、接口门禁、设备生命周期与验收场景。插件中英文贡献记录和 Git Co-authored-by 署名已通过 #206 补齐。

Noise IK channel over a WebSocket with fragmentation and a backlog limit,
request and stream multiplexing that follows DSH's worker tunnel frames with
per-body credit, device authentication by a non-extractable WebCrypto key
signing the handshake hash, pairing links and codes, and relay registration
signatures. Unit tests cover connection-bound results, carrier failures and
flow control; scripts/check-browser.ts runs the device side in Chromium and
WebKit.
Hosts register with an operator-issued key and an Ed25519 signature over a
one-time nonce bound to the relay origin; a newer verified registration
replaces the old one. Every device connection is paired with a fresh Host
socket and forwarded without decryption, pausing the source while the
destination is backed up. Covers refused registrations, replacement,
offline and unanswered Hosts, per-Host limits, close-code forwarding and
heartbeats.
The device pings through the tunnel and the Host closes a channel that stays
silent for 90 seconds. The Host pings the relay on its control socket. The
relay now forwards a deliberate normal close so the device can tell a Host
that hung up from a lost path. Noise key pairs can be restored from a stored
secret.
- gate: the endpoints and fetch routes a remote device may use, checked
  against the full DSH 0.2.1-rc.2 web surface; everything else is refused
  with a reason. The mapping starts from Paraso42's work in ECNU#180.
- host: the adapter over the DSH seams (owner Peer, shared fetch handler,
  wire streams, rendered index and frontend files).
- connections: one Peer per connection, disposed when it closes; revocation
  ends every connection of the device.
- devices: the persisted Host identity and paired devices, and admission by
  single-use invite plus the owner's confirmation of the pairing code.
- relay-link: registration, liveness and reconnection with backoff.
- RemoteAccess ties them together; the tests drive a real relay and Host over
  loopback WebSockets.
The plugin runs remote access while it is switched on and the relay and
access key are set; any settings change restarts it. Its methods give the
settings page the status, the pairing invite, confirmation of waiting
devices and revocation; the gate refuses them to remote devices.

The Host plugin is bundled with the pure JavaScript sodium, so the package
installs with no dependencies and no native addon. The remote document is
served as the Host renders it; the web client adds its own transport hook,
which keeps the Host and the relay's web client coupled only through the
tunnel protocol version.
The loader page pairs this browser with a Host from an invite link and
keeps paired Hosts connected, reconnecting after losses. The DSH interface
runs in an iframe: a preamble hands DSH's client the carrier hooks it
already supports, so RPCs and streams use that page's own tunnel and Peer,
and a Service Worker answers the document, static files, downloads and
uploads through the loader. Values cross into the iframe's realm by
structured cloning.

The relay binary serves the web client with a strict CSP. scripts/check-web.ts
checks the whole flow in Chromium and WebKit; it replaces check-browser.ts.
…age product-neutral

Field hints are described-by text rather than part of the label, the page
intro no longer names a product, and the logger uses the plugin's entry id.
README in Chinese and English, the protocol of the current version, license
and third-party notices. The build collects the license of every bundled
package into lib/THIRD_PARTY_LICENSES.txt and fails on an unreviewed one.
The package joins the catalog, so CI checks and packs it like the others;
it is not assembled into the client before it is published.
…nowledge the preview notice

Endpoints of the EduWork 0.4.6 desktop composition are classified like the
official ones: Studio results, the calendar and read-only views are allowed;
desktop actions, accounts, memories, skills and data imports stay on the
computer. A desktop Host never shows DSH's preview notice, so a device must be
able to record it as read; settings/mutate is allowed for that one write.
Browsers start an <a download> link without asking the Service Worker, so the
request reached the relay. The preamble fetches such links through the Service
Worker and saves a local copy; check-web now downloads a file in both engines.
…t switched them on

Connections started from a settings change carried DSH's configuration
transaction into every later request, so DSH refused settings writes from
remote devices as nested transactions.
The settings page stores the relay access key as the DSH credential
EDUWORK_REMOTE_ACCESS_KEY instead of a plugin setting, so it no longer sits in
plain text in the profile's cordis.patch.yml. The plugin resolves it when
remote access starts and restarts when the credential changes.
A first load moved about 12.7 MB of script and style through the relay. The
Service Worker now asks for gzip and unpacks it; the Host compresses only the
frontend's public files, keeping a compressed copy of each file that never
changes at its URL, so the official frontend's first load drops to about
4.6 MB. RPC results, downloads and streams stay uncompressed, since their
compressed lengths could hint at their content.
Remote devices now use DSH's full interface, adjusted only where a phone
needs it. Below 768px the sidebar rail is hidden and the sidebar opens
over the whole width, with its workspace tree, search and new session
unchanged. It closes once a Session or a global panel is picked. The
sidebar's open and new-session controls sit at the top left, as DSH
places them while its sidebar is hidden on macOS. Conversations keep
DSH's own durations, retries, approvals and questions.

The preamble marks the remote document with data-eduwork-remote. The
client plugin applies the layout only there and no longer shows the
remote access settings, whose methods the gate refuses to remote devices.
At phone width DSH keeps the settings dialog at its desktop layout, with
the section list beside the section, which leaves a section a column a
few characters wide. On a remote device the dialog now fills the screen
and the section list becomes a row of tabs above the section, with the
header's actions and close button beside the title. Sections render
unchanged, and opening settings at a given section still shows it.
The gate refuses every settings change from a device, and DSH shows the
refusal only as the control springing back to its old value. On a remote
device the settings header now says the settings are view only and to
change them on the computer. The other header actions, which act on the
Host's desktop (such as opening the settings file), are hidden there.
- The open Session's row in the drawer shows its actions (menu, archive,
  pin) in place of its time: DSH reveals them on hover, and a touch opens
  the Session instead.
- On iOS, fields use at least 16px text, below which Safari zooms into
  the page as a field takes focus and stays zoomed.
- The loading page keeps the frame clear of the notch and the home
  indicator, which DSH cannot see from inside the frame, and pulling down
  at the top no longer reloads the page and reconnects.
Gateway sign-in in oidc identity mode keeps the ID Token it verified at
login and on refresh. The Host-only identityToken(profileID) returns it,
refreshing once first when the Access Token is due or the ID Token has
less than ten minutes left, so a service the organization runs can
confirm who is signed in.
The relay can admit a Host by the ID Token of an OpenID Connect issuer
it is configured for (RELAY_OIDC_ISSUER, RELAY_OIDC_CLIENT_IDS), beside
access keys. It verifies the token against the issuer's published keys,
keeps only the subject, and ends the registration when the token
expires; the Host registers again on a new control socket five minutes
before, so connected devices stay up.

The relay now also limits refused registrations per address (the
address can come from a proxy header, RELAY_CLIENT_IP_HEADER), Hosts per
member and connections per Host.

A distribution can set the plugin's organization: its relay and the
dsh-oidc profile whose sign-in admits the Host. The relay is then fixed
and not shown on the settings page, so the token goes nowhere else, and
remote access starts and stops with that sign-in.
A top-level remoteAccess section names the organization's relay and the
sign-in (an organizations[] id) that admits a computer to it. It is
validated with the rest of the file, may come from a signed
configuration update like organizations, features and media, and reaches
the installed remote access plugin as its fixed organization. Users
still switch remote access on themselves.
Say what the user can do rather than how the relay works: no relay
close reasons, error messages or Service Worker in the interface (the
Host logs them), no relay address for an organization's relay, a
version mismatch told apart from a refusal, and the per-account
computer limit named when an organization's relay refuses.
# Conflicts:
#	docs/PACKAGES.md
#	docs/PACKAGES_EN.md
#	source-receipt.json
#	tests/package-maintenance.test.mjs
@JunieXD
JunieXD marked this pull request as ready for review October 22, 2026 06:00
The relay is now configured by RELAY_URL, its public base URL, which may
carry a path such as https://example.edu/eduwork-remote/. Its endpoints,
pages, files and health check are all below that path, and anything
outside it is not the relay's, so a reverse proxy passes the path on
unchanged. A Host signs the full base URL when it registers, which keeps
relays on different paths of one domain apart, and pairing links start
with it.

The web client's own URLs are relative: the relay names its base in the
loader page's <base>, the manifest's start URL and scope are relative,
and the Service Worker is scoped to h/ below the base and finds the base
from its scope. remoteAccess.relay in the EduWork configuration accepts a
path and is kept in that canonical form.

check-web.ts runs each engine against a relay at the root and one below
a path, checking that the worker's scope stays within it and that no
request reaches the domain outside it. The READMEs note that a shared
domain also shares the browser's storage with the other sites.
A frame the WebSocket library cannot parse, such as an unmasked client
frame on a connection that has not registered, made it emit an error
with no listener, which took the whole relay down. Every socket now ends
on its own error, and its close handlers clean up as on any close. A
regression test sends such a frame and then uses another connection.
An expired ID Token closed the Host's control socket, but the device
connections already paired under it kept forwarding. The relay now
keeps, per Host, when the credential that admitted it runs out and the
connections paired under it, and at that moment ends them all. A Host
that registers again with a newer token moves the deadline; one that
only lost its control socket, as on a brief network loss, keeps its
connections until then.

On the Host, a renewal that fails, because no new token could be had or
its socket closed before registering, no longer drops the registration
it was to replace. That registration lasts until its credential
expires, and the renewal is tried again with backoff, so a slow issuer
does not disconnect devices early.
JunieXD and others added 9 commits October 22, 2026 16:26
…clients

The relay checked the authorized party only for a token with several
audiences. It now takes the client a token was issued to, its azp or
else its only audience, and requires it to be one of the configured
client IDs and in the audience, as dsh-oidc does on the desktop. A
single-audience token authorized to another party is refused. The
protocol document states this rule and what an expiring token ends.
When the ID Token is due for renewal and the refresh fails for a moment,
identityToken rejects without ending the sign-in and the next call
renews it; when the issuer rejects the refresh token, the sign-in ends
and identityToken asks for a new sign-in.
The deadline for registering on a Host socket, or for accepting on an
accept socket, was cleared only by the first message. A connection that
closed before sending one left its timer, and the socket the timer
holds, until the deadline passed. Both deadlines now end with their
connection.
…n lapses

When the sign-in had ended, or its issuer no longer renewed the ID
Token, the Host kept retrying with backoff for as long as it ran,
refreshing the sign-in on every attempt, while the settings page said it
could not reach the relay. The plugin now reports no credential in those
cases; the link then stops in a new lapsed state, and the settings page
asks the user to sign in again, after which remote access resumes. A
brief failure, such as an unreachable issuer, is still tried again.

A renewal that brings a token expiring no later than the one it replaces
is no longer repeated every 30 seconds until the expiry: the relay ends
the registration when it lapses, and the Host then finds no credential.
All relays of one domain shared one list of paired computers, so a
relay below one path listed computers paired through another, which it
cannot reach. Each relay's web client now keeps its own list, named
after its path.
…he browser

Browsers separate data only by domain, and the DSH interface a paired
computer provides runs on the relay's domain. It can therefore use the
browser's pairings with other computers, and on a shared domain it can
use the other sites there as the user. The pairing page now asks users
to pair only their own computers. The README explains why, recommends a
domain of the relay's own, never one shared with a sign-in service or
sites that need signing in to, and notes that a Service Worker covering
a shared domain must leave the relay's path alone.
# Conflicts:
#	docs/PACKAGES.md
#	docs/PACKAGES_EN.md
#	source-receipt.json

@freedomkk-qfeng freedomkk-qfeng left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复核通过。已检查远控协议、每连接独立 Peer、宿主接口门禁、配对与撤销、OIDC 接入和子路径支持。之前的畸形 WebSocket 帧导致中继退出、ID Token 过期后已有连接继续存活、azp 校验问题已修复。

本地远控类型检查、构建及 71 项测试通过,OIDC 相关 28 项测试通过,配置与包维护测试 63 项通过、1 项跳过。同步 #147 后仅重新生成 source-receipt.json,远控与 OIDC 业务代码保持原样;源码清单审计通过。安装包验收曾遇到一次目录重命名被拒绝,单独重跑后公版/学校版、正式/开发版四种合成包全部通过。

按维护者确认,本次先合入源码,继续复用 DSH 现有远控 UI。公版由用户配置中继和 Key,机构装配可预置中继及 OIDC。ChatECNU 的 iframe 嵌入配置在中继服务侧继续跟进,不将统一服务器前端作为本轮合并前提;现有同源信任边界保留文档说明。npm 发布、默认产品装配及学校部署留待后续执行。

@freedomkk-qfeng
freedomkk-qfeng merged commit 669b765 into ECNU:main Oct 22, 2026
13 checks passed
freedomkk-qfeng added a commit that referenced this pull request Oct 22, 2026
Record the integration mapping, endpoint gate, device lifecycle and validation
scenarios contributed in #180 and carried forward into #192. Keep both language
entries aligned and refresh the source receipt without changing runtime code.

Co-authored-by: Paraso42 <[email protected]>
@JunieXD
JunieXD deleted the feat/116-remote-access branch October 28, 2026 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants