Skip to content

Finalmask header-custom: Support Lua script as well - #7174

Closed
CluvexStudio wants to merge 2 commits into
XTLS:mainfrom
CluvexStudio:finalmask-header-lua
Closed

CluvexStudio wants to merge 2 commits into
XTLS:mainfrom
CluvexStudio:finalmask-header-lua

Conversation

@CluvexStudio

Copy link
Copy Markdown
Contributor

builds on the lua engine from #6823 and the noise hook from #7124, docs will follow once #909 is merged

-- header-custom takes a script, which can't be set together with clients, servers, errors or the udp client, server. the script's path resolves like the other lua ones and infra only compiles it, so a typo is caught at startup while the pool is created the first time a layer needs a header
-- tcp: HandleHeader(stream, address, role) runs the whole handshake, stream:Write(bytes), stream:Read(size) and stream:Sleep(ms). role is client or server, so one file serves both ends like the static config does. returning an error fails the connection, which is what header auth failed already does, and a server can stream:Write a fake answer before it returns, the way the static errors sequences work
-- udp: HandleHeaderWrite(header, address, role) builds the prefix with header:Add(bytes) and header:AddRandom(size), HandleHeaderRead(packet, address, role) returns how many bytes to strip, or nil to drop the packet, and reads it through packet:Len(), packet:Byte(at), packet:Match(at, bytes) and packet:Slice(from, to)
-- no new module: the engine registers the public RegisterLua of xray.finalmask.noise, so a header script gets Random and RandomBetween from there, plus xray.log

everything a static header does can be written in lua: fixed bytes, rand, randRange byte by byte, the delays, the server's error answer, and capture/reuse become plain locals because a handshake happens in one call. "mode": "standalone" still refuses a script, the udp handshake there would want its own hook

no LTable on either path, one stream, one builder and one packet view per instance, reused, and the numbers are checked: a handshake takes at most 65535 bytes in and 65535 out, a udp header at most 1024, and every size, index and delay is validated before it is converted so a huge float or a nan can't slip through. a shared config can't make the hooks allocate without bound

the tcp hook holds its pooled instance for the handshake, since stream:Read waits for the peer. that is the half duplex part of @Meo597's idea, the payload never goes through lua, so this needs no event framework. if you'd rather wait for one, say so and i'll park the tcp half

BenchmarkLuaHeader is in the package: per packet a udp header costs about ten times the static one, 424 bytes in eight allocations against 24 in three, so the static lists stay the better choice at high packet rates. the tcp hook runs once per connection

timeouts: 30s for a handshake because it waits for the peer, 1s per udp packet, and the instance init stays 120s

@Fangliding

Copy link
Copy Markdown
Member

我的意思是 lua mask 应该有能力在自己的地方完全实现 header custom 的功能 而不是再加一个lua

@LjhAUMEM

Copy link
Copy Markdown
Contributor

noise 的 rand packet 应该也可以在这里删了

@CluvexStudio

Copy link
Copy Markdown
Contributor Author

我的意思是 lua mask 应该有能力在自己的地方完全实现 header custom 的功能 而不是再加一个lua

@Fangliding got it, one lua mask then. i'll move these hooks into a mask of their own, {"type": "lua", "settings": {"script": "..."}} as both a tcp and a udp mask, and move HandleNoise there too so noise.script goes away again, it isn't in a release yet. then one lua layer does noise and headers, and the static lists can go later like @LjhAUMEM says

closing this one, the new pr will have the same hooks

@RPRX

RPRX commented Oct 16, 2026

Copy link
Copy Markdown
Member

你们别这么着急上 lua,沙盒模式至少得测到明年才能开放 inline 吧,不过 header-custom 太复杂了用 lua 换掉这点我倒没意见

@Meo597

Meo597 commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

全弄到 fm.lua 就行了,这样原有的配置方式不变,双话事人
只是被迫提前设计它,有点烧脑

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