Skip to content

Noise finalmask: Support Lua script as well (on main branch: inline scripts were disabled for now; sandbox-only for now) - #7124

Merged
RPRX merged 4 commits into
XTLS:mainfrom
CluvexStudio:finalmask-lua
Oct 16, 2026
Merged

RPRX merged 4 commits into
XTLS:mainfrom
CluvexStudio:finalmask-lua

Conversation

@CluvexStudio

@CluvexStudio CluvexStudio commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

builds on the lua engine from #6823, docs in XTLS/Xray-docs-next#909

-- noise.script is a lua script defining HandleNoise(plan, address). it submits each packet with plan:Add(packet) or plan:Add(packet, delay) for a delay in ms after that packet, plan:AddRandom(size) for random bytes that never become a lua string, and only returns something when it wants to report an error
-- xray.finalmask.noise gives the script Random(size) for crypto random bytes and RandomBetween(from, to), inclusive. xray.log works too. RegisterLua exports just those two, so another module's engine can offer them
-- script is a lua file and the core resolves the path. it takes whatever lua.Compile accepts, so inline scripts work again once #7136 brings them back. can't be set together with noise
-- this is what exp can't do: conditionals, loops, state that survives between calls, and noise that depends on the peer

it follows what the lifecycle page documents for the other pooled hooks: each noise layer gets its own pool so two layers naming the same file don't share state, and a refused return value keeps the instance while only a lua exception or a timeout drops it. the engine is created the first time a layer needs noise, never in infra, which only compiles the script so a typo is caught at startup. the call timeout is 1s rather than 6s because this hook runs while the first packet to a peer waits, the init timeout stays 120s

no LTable on the hook path, one plan per instance that the hook is handed, and the packet buffer is reused per connection

the script only runs when a peer needs noise, never per packet. noise stays best effort: a script that fails only logs a warning and the real packet still goes out, which is how a failed noise write already behaves. sizes, delays and random bounds are capped at 65535 so a script can't ask for a huge allocation or a sleep that never ends

@LjhAUMEM you said a lua pluggable mask would be cool, this is the smallest piece of that

@LjhAUMEM

Copy link
Copy Markdown
Contributor

只有 noise 吗,整个 finalmask 其他人也可以放开手去搞,然后我就可以退休了

上次加的 exp 就挺好的,如果有需要可以把 rand packet 配置项也 break 了,一个 exp 似乎就可以完成,然后搭配 lua 做一些 exp 做不到的基本就全了

我这边没什么意见,不过开放的接口文档里一定要写全

@CluvexStudio

Copy link
Copy Markdown
Contributor Author

我这边没什么意见,不过开放的接口文档里一定要写全

@LjhAUMEM thanks. docs are already up for this one in zh en and ru XTLS/Xray-docs-next#909 hook page module page a guide and the entry added to index.md and lifecycle.md

about dropping rand and packet, you're right that exp covers both, but it breaks every noise config people already have so i'd rather do it as its own pr with a deprecation note in the docs. can do it if you want

and yeah i'm happy to take more of finalmask than noise. custom header is the next obvious one for lua, Meo597 called it the big calculator and it's the part that actually needs branches and loops

@Meo597

Meo597 commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

关于此 pr 的 fm 部分我没什么意见

只要不是 streamSettings.finalmask.lua 就行
因为我对它的期望很高
几天前我 vibe 了一个 fm 实现让 lua 接管连接,而不是目前这种无状态的池化 hook,那可以做更多事情
但为了能控制 payload 以及需要双向无阻塞,不得不抠很多细节
最终 ai 写出来的一坨过于丑陋就让我给扔了
或许我步子不应该一开始就迈那么大,应该慢慢演进

回到这个 pr 上面
lua 部分应该导出 xray.finalmask.noise
一些函数应该移至 lua.go 并且 RegisterLua 来导出一些函数以供未来其它模块使用
仅供自己使用的才用 registerLua 具体示例可见 dns 那块
dns 脚本引擎自己 require("xray.dns") 不应有 dns.Query 因此有 registerLua 和 RegisterLua

其它的我没有逐行 review
lua 一不小心就会导致高内存开销,建议加个 benchmark

@CluvexStudio

Copy link
Copy Markdown
Contributor Author

lua 部分应该导出 xray.finalmask.noise

@Meo597 all four done. module is xray.finalmask.noise now, the lua glue moved into lua.go with a public RegisterLua so another module's engine can offer it later, and script.go is just the engine and the lifecycle like app/dns/script.go does it. no private registerLua, both functions are plain helpers so there's nothing to hide the way dns.Query is from a dns script

benchmark is in lua_benchmark_test.go, ran it on a github runner. the bridging on its own is 145ns and 16B, a call that returns two packets is about 2us and 1.4KB against 412ns and 128B for the same thing built from static items, and the write path after a peer already has its noise stays at 318ns and 64B. so a script is a few times the static items but only once per peer and nothing is paid per packet

and yeah lua taking over a connection is the interesting one, agree. this was meant to be the small step, a pooled hook that stays off the write path, so the bigger stateful thing is still free to take finalmask.lua

@Meo597

Meo597 commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

高开销的原因是 LTable 应尽力避免使用它

@CluvexStudio

Copy link
Copy Markdown
Contributor Author

高开销的原因是 LTable 应尽力避免使用它

@Meo597 you're right, the tables were most of it. there's no LTable left now, the script submits each packet with noise.Send(packet) or noise.Send(packet, delay) and the hook only returns an error if it wants to report one. the packet buffer is reused per connection so nothing is allocated between calls

same two packets on a github runner went from 1424B and 23 allocs to 432B and 9, about twice as fast too, so it's 2.7x the static items now instead of 5x

Send is only in the private registerLua, the public RegisterLua gives other modules just Random and RandomBetween since they can't send noise anyway, same split as dns.Query. the sink lives in the registry so a script can't reach it. docs are updated in all three languages

@Fangliding

Fangliding commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

应该提供一种路径之外的地方放Lua方便用户来回搬 比如把原代码base64一遍直接填在json里(考虑gzip之类的压缩?不知道一般压缩后是多大)
@Meo597

@Meo597

Meo597 commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

目的是为了方便 json 分享吗?
fm 有它自己的特殊性的确可以这么干
适合在 fm/infra 那边做,传给 core 的直接就是代码而不是文件路径
别的地方还准备做热重载不太适合

@Meo597

Meo597 commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

要不给 script 加个魔法头 base64+gzip: 之类的
然后统统丢给核心
默认是 file:

@CluvexStudio

Copy link
Copy Markdown
Contributor Author

要不给 script 加个魔法头 base64+gzip: 之类的

@Meo597 @Fangliding did it that way. one script, file: is the default so a plain path still works, plus base64: and base64+gzip: for the code itself. core gets the code either way, infra/conf is the only place that reads a file or unpacks anything

about how much gzip saves, the 537 byte script from the guide is 716 bytes as plain base64 and 404 with gzip, the small 113 byte one is 152 plain and 156 with gzip, so it only helps from a few hundred bytes up. that's in the docs

tests cover a plain path, file:, both inline forms, and a path with a colon in it so it doesn't get taken for a prefix. rejects too, unknown prefix, bad base64, something that isn't gzip, a cut gzip, no HandleNoise, and script with noise. docs updated in zh en and ru

@Fangliding

Copy link
Copy Markdown
Member

目的是为了方便 json 分享吗? fm 有它自己的特殊性的确可以这么干 适合在 fm/infra 那边做,传给 core 的直接就是代码而不是文件路径 别的地方还准备做热重载不太适合

传文本就不重载呗
你很喜欢搞重载但是这个不比证书和geofile无人值守更新 一更新希望它立刻生效别到时候又只能加个fswatch进来

@Meo597

Meo597 commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

fswatch 太重了,最多 rpc 重载

@CluvexStudio json 内联 lua 我会再开个 PR

@RPRX

RPRX commented Oct 12, 2026

Copy link
Copy Markdown
Member

先合了 #7132 以方便这里 rebase,虽然我觉得刚加的 exp 已经暂时够用了

@CluvexStudio

Copy link
Copy Markdown
Contributor Author

先合了 #7132 以方便这里 rebase

@RPRX rebased. script is just handed to the core now so it takes a path or an inline base64: / base64+gzip: script like dns and routing do, CompileSource is gone since Compile covers it, and the proto lost script_name because the name comes from there too. docs in #909 follow, zh en and ru

Comment thread infra/conf/transport_finalmask.go Resolved
@CluvexStudio

Copy link
Copy Markdown
Contributor Author

可以把 plan 直接传给 hook,用 plan:Send

@Meo597 all done, and the plan reads much better, thanks

-- HandleNoise(plan, address) now, packets go in with plan:Send(packet) or plan:Send(packet, delay). nothing hangs off the state any more, the hook is handed the plan it fills, and the module keeps only Random and RandomBetween so RegisterLua is a single public one. one plan per instance, reused, so a call still allocates nothing
-- InitScript is gone and infra never starts the engine, it only compiles the script so a typo still fails at startup. the pool is created the first time a layer needs noise. i took that fail fast option you mentioned, say so if you'd rather have nothing in infra
-- dropped the empty script guard in engine(), and left a TODO where the pool should be closed once the api removes an inbound or outbound
-- plan:Send, Random and RandomBetween check the number before converting it, so a huge float or a nan can't end up a negative int64
-- BenchmarkLuaNoiseWriteTo is gone, both benchmarks go through engine.packets now

about the name: i kept Send because that's what your example used and plan already says it is a plan, but if you meant another verb, plan:Add or plan:Queue is a one line change

docs follow in #909, zh en and ru: the hook page documents plan:Send, the module page lost Send, and the lifecycle page notes the noise layer only reads and compiles at startup and makes its instance on first use

@RPRX

RPRX commented Oct 13, 2026

Copy link
Copy Markdown
Member

#7136 (comment) fm 能被分享,所以得慎重对待这个 PR 了,我感觉 exp 已经暂时够用了

@CluvexStudio

Copy link
Copy Markdown
Contributor Author

fm 能被分享,所以得慎重对待这个 PR 了

@RPRX the hook gives a script Random, RandomBetween, plan:Send and xray.log, no dns and no geodata. everything else comes from the lua state, so #7136 covers it, and the noise hook gets that for free since it goes through lua.Compile and NewStateFactory, where an inline script is never privileged

one thing was open on my side: a plan took any number of packets, so a shared script could loop plan:Send(noise.Random(65535)) and eat gigabytes inside the 1s call. a plan now takes 64 packets and 65535 bytes at most per call, with a test for that loop

if you want nothing executable to travel in a shared config at all, i'll make fm take only a file path and refuse base64: and base64+gzip:

Comment thread infra/conf/transport_finalmask.go Resolved
@Meo597

Meo597 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

似乎不需要 rebase 只需要改工厂那边传入一个 init stack size 就行

@RPRX

RPRX commented Oct 16, 2026

Copy link
Copy Markdown
Member

那么这个 PR 的 test 通不过

@Meo597

Meo597 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

好吧,我去 clone 他

@RPRX

RPRX commented Oct 16, 2026

Copy link
Copy Markdown
Member

@Meo597 tests

@Meo597

Meo597 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

漏了 infra 马上
ai 总喜欢做多余的测试,ai 写的果然得是 ai 改才行

@RPRX

RPRX commented Oct 16, 2026

Copy link
Copy Markdown
Member

过于没必要的 tests 可以删了

@Meo597

Meo597 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

好了

@Fangliding

Copy link
Copy Markdown
Member

能不能验证一下这个逻辑下的全能性 能自行完成多少已有的noise?
顺便那个header custom理论上是不是能删了

@Meo597

Meo597 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

对 fm 没有足够的了解
所以之前光 review 的 lua 部分

他现在还是接收 core 的调度,只能发给指定地址
一个小改动可以对任意目标发包
自己安排计划的话不行,需要我另外提供一个 event 框架

@Meo597

Meo597 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

我看了一下 header custom 应该可以完全由 lua 接管

@RPRX

RPRX commented Oct 16, 2026

Copy link
Copy Markdown
Member

不过目前由于 #7136 把 #7132 刚加的 inline scripts 给禁用了所以 fm 用到的 lua 难以分享,先看看沙盒模式有没有可攻击面吧

@Meo597

Meo597 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

所以 noise 看五元组还是三元组?
要不要我把往任意 dest 发包的 push 上来

@RPRX

RPRX commented Oct 16, 2026

Copy link
Copy Markdown
Member

要不要我把往任意 dest 发包的 push 上来

暂时不了吧,听起来有点那啥

@Meo597

Meo597 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

xray ddos 攻击器
xray 压缩工具
😁

@RPRX RPRX changed the title Finalmask noise: Build packets from a Lua script Noise finalmask: Support lua script as well Oct 16, 2026
@RPRX RPRX changed the title Noise finalmask: Support lua script as well Noise finalmask: Support lua script as well (inline scripts were disabled) Oct 16, 2026
@RPRX
RPRX merged commit 0309950 into XTLS:main Oct 16, 2026
@CluvexStudio

Copy link
Copy Markdown
Contributor Author

能不能验证一下这个逻辑下的全能性 能自行完成多少已有的noise?

@Fangliding verified it with a script that rebuilds every static item: packet of any type, rand, randRange, exp's <b> <r> <rc> <rd> <n> <t> <c>, delay, and reset already applies to both. so all of it, with two differences: randRange and the random letters and digits cost one RandomBetween call per byte, and a counter kept in a script is per pooled instance while <c> is per connection

<t> works because os.time is allowed in the sandbox, os.getenv, os.execute, io and debug are not

顺便那个header custom理论上是不是能删了

a header needs its own hook, this one only builds packets for the address the core gives it. i can write that mask in a separate pr, then custom has a lua replacement before anything gets deleted

@RPRX RPRX changed the title Noise finalmask: Support lua script as well (inline scripts were disabled) Noise finalmask: Support lua script as well (on main branch: inline scripts were disabled for now; sandbox-only for now) Oct 16, 2026
@RPRX RPRX changed the title Noise finalmask: Support lua script as well (on main branch: inline scripts were disabled for now; sandbox-only for now) Noise finalmask: Support Lua script as well (on main branch: inline scripts were disabled for now; sandbox-only for now) Oct 16, 2026
RPRX pushed a commit that referenced this pull request Oct 16, 2026
… scripts were disabled for now; sandbox-only for now) (#7124)

#7124 (comment)
#7124 (comment)
#7124 (comment)

---------

Co-authored-by: Meow <[email protected]>
@Meo597

Meo597 commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

沙盒模式有没有可攻击面吧

目前我能想到的就是 geodata 的实现不够严谨,规则集构建的时候没有做足够的校验
晚点我整一下
以前不需要,订阅下发那种零信任沙盒需要

@RPRX

RPRX commented Oct 16, 2026

Copy link
Copy Markdown
Member

#7174 (comment)

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.

5 participants