Repository navigation
Noise finalmask: Support Lua script as well (on main branch: inline scripts were disabled for now; sandbox-only for now) - #7124
Conversation
|
只有 noise 吗,整个 finalmask 其他人也可以放开手去搞, 上次加的 exp 就挺好的,如果有需要可以把 我这边没什么意见,不过开放的接口文档里一定要写全 |
@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 |
|
关于此 pr 的 fm 部分我没什么意见
回到这个 pr 上面 其它的我没有逐行 review |
b97d42a to
14578bd
Compare
@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 |
|
高开销的原因是 LTable 应尽力避免使用它 |
14578bd to
cec4d2f
Compare
@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 |
|
应该提供一种路径之外的地方放Lua方便用户来回搬 比如把原代码base64一遍直接填在json里(考虑gzip之类的压缩?不知道一般压缩后是多大) |
|
目的是为了方便 json 分享吗? |
|
要不给 |
cec4d2f to
5a6fb9d
Compare
@Meo597 @Fangliding did it that way. one 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, |
传文本就不重载呗 |
|
fswatch 太重了,最多 rpc 重载 @CluvexStudio json 内联 lua 我会再开个 PR |
|
先合了 #7132 以方便这里 rebase, |
5a6fb9d to
fc2172e
Compare
@RPRX rebased. |
fc2172e to
c7bb715
Compare
@Meo597 all done, and the plan reads much better, thanks -- about the name: i kept docs follow in #909, zh en and ru: the hook page documents |
|
#7136 (comment) fm 能被分享,所以得慎重对待这个 PR 了, |
c7bb715 to
ae3b498
Compare
@RPRX the hook gives a script one thing was open on my side: a plan took any number of packets, so a shared script could 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 |
|
似乎不需要 rebase 只需要改工厂那边传入一个 init stack size 就行 |
|
|
|
好吧,我去 clone 他 |
|
@Meo597 tests |
|
漏了 infra 马上 |
|
|
|
好了 |
|
能不能验证一下这个逻辑下的全能性 能自行完成多少已有的noise? |
|
对 fm 没有足够的了解 他现在还是接收 core 的调度,只能发给指定地址 |
|
我看了一下 header custom 应该可以完全由 lua 接管 |
|
所以 noise 看五元组还是三元组? |
|
|
|
script as well
script as wellscript as well (inline scripts were disabled)
@Fangliding verified it with a script that rebuilds every static item:
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 |
script as well (inline scripts were disabled)script as well (on main branch: inline scripts were disabled for now; sandbox-only for now)
script as well (on main branch: inline scripts were disabled for now; sandbox-only for now)script as well (on main branch: inline scripts were disabled for now; sandbox-only for now)
… scripts were disabled for now; sandbox-only for now) (#7124) #7124 (comment) #7124 (comment) #7124 (comment) --------- Co-authored-by: Meow <[email protected]>
目前我能想到的就是 geodata 的实现不够严谨,规则集构建的时候没有做足够的校验 |
builds on the lua engine from #6823, docs in XTLS/Xray-docs-next#909
--
noise.scriptis a lua script definingHandleNoise(plan, address). it submits each packet withplan:Add(packet)orplan: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.noisegives the scriptRandom(size)for crypto random bytes andRandomBetween(from, to), inclusive.xray.logworks too.RegisterLuaexports just those two, so another module's engine can offer them--
scriptis a lua file and the core resolves the path. it takes whateverlua.Compileaccepts, so inline scripts work again once #7136 brings them back. can't be set together withnoise-- this is what
expcan't do: conditionals, loops, state that survives between calls, and noise that depends on the peerit 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