feat: add kernel plugin network timers - #18149
Conversation
|
请举例使用场景 |
主要用于局域网同步,比如桌面跟docker |
There was a problem hiding this comment.
Pull request overview
This PR extends the Go kernel-side plugin runtime with (1) guarded outbound WebSocket dialing for peer-to-peer/event-driven communication, (2) browser-compatible timer APIs in the goja sandbox, and (3) sync-idle coordination primitives so plugins can avoid racing with SiYuan’s official sync while performing workspace writes.
Changes:
- Added
siyuan.net(trusted-peer outboundws) andsiyuan.sync(sync state + idle lock token) namespaces to the kernel plugin sandbox. - Introduced shared WebSocket client object builder and refactored
siyuan.client.socketto reuse it. - Added
setTimeout/setInterval(and clear variants) to the plugin global runtime, plus normalization tests for trusted peer hosts.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| kernel/plugin/websocket_client.go | New shared builder for browser-like WebSocket JS objects used by both siyuan.client.socket and siyuan.net.ws. |
| kernel/plugin/timers.go | Adds timer APIs to the goja plugin sandbox and ties timer lifecycle to plugin context. |
| kernel/plugin/sandbox.go | Wires new net, sync, and timer injections into the plugin runtime bootstrap. |
| kernel/plugin/api_sync.go | Exposes siyuan.sync.isSyncing() and siyuan.sync.tryLockIdle(ttlMs) to plugins. |
| kernel/plugin/api_net.go | Adds siyuan.net.setTrustedPeers() and guarded outbound siyuan.net.ws(). |
| kernel/plugin/api_net_test.go | Tests peer host normalization used by the trusted-peer guard. |
| kernel/plugin/api_client.go | Refactors siyuan.client.socket to reuse the shared WebSocket builder. |
| kernel/model/sync.go | Adds IsSyncing() and a TTL-based TryLockSyncIdle() lock token for coordination. |
| kernel/api/sync.go | Exposes syncing in /api/sync/getSyncInfo response for frontend/plugin coordination. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| }, func(rt *goja.Runtime, result any, err error) { | ||
| if lo.IsNil(err) { | ||
| if opening, ok := result.(bool); !ok || !opening { | ||
| if resolveErr := openResolve(nil); resolveErr != nil { | ||
| logging.LogErrorf("[plugin:%s] ws.open resolve: %v", p.Name, resolveErr) | ||
| } | ||
| } | ||
| } else { |
| closeRunErr := p.worker.Run(func(rt *goja.Runtime) (result any, err error) { | ||
| if c := gwsConn.Load(); c != nil { | ||
| setReadyState(rt, WebSocketReadyStateClosing) | ||
| err = c.WriteClose(code, reason) | ||
| } | ||
| return | ||
| }, func(rt *goja.Runtime, result any, err error) { |
| delay := time.Duration(call.Argument(1).ToInteger()) * time.Millisecond | ||
| if delay < 0 { | ||
| delay = 0 | ||
| } |
| delay := time.Duration(call.Argument(1).ToInteger()) * time.Millisecond | ||
| if delay < 0 { | ||
| delay = 0 | ||
| } |
|
审查结论 (不包含 Copilot 已报告的问题)
已验证
|
|
SSRF 检查已经在安全政策中放弃了 https://github.com/siyuan-note/siyuan/security/policy 所以不用考虑 |
是要移除现有的对于正向代理的 SSRF 检查吗? |
|
已有的不用移除,只是在安全政策中说明 ssrf 不要报告了,后续代码也不用考虑解决 ssrf。 |
该 PR 受现存 SSRF 策略影响引入了大量冗余代码 (新增了绕过 SSRF 策略的 API), 同时导致现有的 SSRF 策略形同虚设, 不如直接将现有策略更改为日志审计 |
|
我看现在也有个安全模式, 可以在安全模式下使用 SSRF 阻止策略, 普通模式下使用日志审计策略 |
|
安全模式是自动关插件,不是有个专门的模式状态 |
安全模式可以使用命令行参数 |
|
感觉价值好像不大 |
|
新增的代码不用考虑 ssrf 阻止,不然好麻烦。 |
目前看没有其他比较好的方案了 |
|
我觉得没有必要,完全放弃 SSRF 防御吧 |
我这个方便比直接移除 SSRF 变更与影响更小😏 |
|
直接移掉吧 |
|
#18220 已经合并 |
|
已经重构大半了,不过是在有SSRF的情况下hhh,后面我再改改 |
930277d to
b732a0f
Compare
|
新增两万五千行代码看起来好夸张 |
|
用ai粗暴合并了下,好多还是之前的东西 |
858d182 to
930277d
Compare
还是重新从 |
930277d to
68b9c29
Compare
Description / 描述
Adds kernel plugin runtime capabilities for event-driven peer communication and safe coordination with SiYuan's built-in sync. Kernel plugins can now open guarded outbound WebSocket connections, use browser-compatible timers, and query or hold the official sync idle state before performing workspace writes.
Refs #17445
Related to #17487, #17670
Type of change / 变更类型
缺陷修复
代码重构
新功能
修改文案或增加新语言
Changes
siyuan.net.ws(...)for kernel plugins, with trusted peer validation before dialing outbound WebSocket connections.setTimeout,clearTimeout,setInterval, andclearIntervalto the kernel plugin sandbox.siyuan.sync.isSyncing()andsiyuan.sync.tryLockIdle(ttlMs)so kernel plugins can avoid conflicting with official sync while performing workspace writes./api/sync/getSyncInfoassyncingfor frontend/plugin coordination.siyuan.client.socketpath into shared WebSocket helper code.Verification
cd kernel && go test -vet=off -run '^$' ./plugin ./api ./modelgit diff --check origin/dev...origin/pr/18149git merge-tree --write-tree origin/dev origin/pr/18149Checklist / 检查清单
devbranch and has no merge conflicts