Skip to content

Transport: Refactor to be based on Finalmask's dialer & listener - #6754

Merged
RPRX merged 22 commits into
XTLS:mainfrom
LjhAUMEM:fm
Sep 20, 2026
Merged

RPRX merged 22 commits into
XTLS:mainfrom
LjhAUMEM:fm

Conversation

@LjhAUMEM

@LjhAUMEM LjhAUMEM commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

人性化的 fm 使用方式

  • DialTCP(ctx context.Context, dest net.Destination) (net.Conn, error)
  • Listen(ctx context.Context, addr net.Addr) (net.Listener, error)
  • DialUDP(ctx context.Context, dest net.Destination) (net.Conn, error)
  • ListenPacket(ctx context.Context, addr net.Addr) (net.PacketConn, error)

mask wrapclient

  • WrapConnClient(net.Conn, *net.Destination, *Dialer) (net.Conn, error)
  • WrapPacketConnClient(net.PacketConn, *net.Destination, *Dialer) (net.PacketConn, error)

如果是 HandleDial 属性的 mask 应该不再相信上层传入的 addr,可从 wrap 中获取到 frist dest
HandleDial 属性与 HeaderConn 属性不可共存

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

link #6327 (comment)

@Fangliding

Copy link
Copy Markdown
Member

这样接口是不是太怪了 finalmask从一个可选组件变成了强制和finalmask交互然后再被透明转发到原本的系统组件 应该是 if streamSettings.FinalMask != nil {} 这样 然后再是本来是listen/dialsystem 比较好

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

为了 HandlerDial 只能替换成 fm Dial 或者修改原本 listen/dialsystem 里的方法,后者到里面没有 fm 的参数,且容易无限套娃

现在仍然是可选的,通过类似

	if len(fm.tcpMasks) == 0 {
		return fm.dialTCP(ctx, dest)
	}

@Fangliding

Fangliding commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

我的意思是类似这样不行吗

var pconn net.Conn
var err error
if streamSettings.FinalMask != nil {
	pconn, err = streamSettings.FinalMask.DialTCP(ctx, dest)
} else {
	pconn, err = internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
}
if err != nil {
	errors.LogErrorInner(ctx, err, "failed to dial to "+addr)
	return nil, err
}

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author
var pconn net.Conn
var err error
if streamSettings.FinalMask != nil {
	pconn, err = streamSettings.FinalMask.DialTCP(ctx, dest)
} else {
	pconn, err = internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
}
if err != nil {
	errors.LogErrorInner(ctx, err, "failed to dial to "+addr)
	return nil, err
}

这个和单独使用 streamSettings.FinalMask.DialTCP 是一样的,streamSettings.FinalMask 会在所有出入站的 mss 里完成初始化,改 DialSystem 还是不建议,有个共同最底层的应用 sockopt 的 dialer 还是方便,而且还有传参问题,要么多加个参数要么把参数移到 socketopt 里

@Fangliding

Fangliding commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

我的意思就是本来干净的dialer现在必须要一个初始化一个FinalMask的闭包才能用不太行

DialSystem我之前想了下确实不太行所以后面编辑掉了

@fatyzzz

fatyzzz commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

nice to see finalmask getting pushed deeper into the stack, i’m all for it

@LjhAUMEM
LjhAUMEM marked this pull request as draft September 13, 2026 06:56
@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

我的意思就是本来干净的dialer现在必须要一个初始化一个FinalMask的闭包才能用不太行

DialSystem我之前想了下确实不太行所以后面编辑掉了

那待讨论吧,转为 draft,对于 HandleDial 的 mask 现在其实是多创建了一个未使用的 socket 的

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

其实接口也可以改成和 internet.DialSystem(ctx, dest, streamSettings.SocketSettings) 一样,现在的接口是为了方便移植做的取舍,再改下去几乎就是只有 ray 能用 fm

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

想了个骚操作解决了第二点
FinalMask 改为内部持有 func(context.Context, net.Destination) (net.PacketConn, net.Addr, error)
对外开放改为 DialUDP(ctx context.Context, dest net.Destination) (net.Conn, error)

@Raymond520888

Copy link
Copy Markdown

@LjhAUMEM 操作再骚这个happ软件也难适配到FinalMask吧

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

但这样如果是 HandleDial 属性的 mask 就不应该再相信上层传进的 addr,不能单独处理解析 ip 就成了必填项,又或许 first dest 也要传进去

@LjhAUMEM 操作再骚这个happ软件也难适配到FinalMask吧

这个我不清楚

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

为什么 draft 也是 40 个 test 全部跑完

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

通过闭包捕获 socketopt 改到捕获 mss,这样外面修改 mss 里的 socketopt 也能实时更新,解决了一开始描述的第一点,两点全部解决,只剩下修复 test,有的 test 是凭空创造 mss 不走 ToMemoryStreamConfig,还在考虑 fang 说的改用两种 dial

@RPRX

RPRX commented Sep 14, 2026

Copy link
Copy Markdown
Member

@LjhAUMEM 该 PR 只要是符合 #6327 (comment) 的理念就可以接受,能解决很多潜在的问题

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

刚从医院回来
是符合的,不过也确实判断一下选择哪个 dial 修 test 方便些,毕竟很多 test 不是通过 ToMemoryStreamConfig 构造 mss

如果测试过了再小改下 udphop xicmp 大概今晚可以 ready

@LjhAUMEM
LjhAUMEM marked this pull request as ready for review September 14, 2026 14:53
@svlavr

svlavr commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

headerManagerConn.WriteTo drops a datagram without returning an error when the payload plus headers exceeds its 4096-byte buffer. This is still present in the current head (35302746), lines 348–355.

I reproduced it through NewFinalMask → DialUDP → conn.Write, using a one-byte HeaderConn test mask and a recording lower PacketConn:

Payload Header Returned n, err Lower writes Lower bytes
4095 1 4095, nil 1 4096
4096 1 0, nil 0 0

The boundary control also checks that the header and full payload reach the lower connection unchanged. The second case fails the assertion that a rejected write must return an error. The caller gets a short write with no error, and callers checking only err cannot detect the drop. A 4096-byte payload is valid UDP data; the restriction here comes from this wrapper's header capacity.

Could this rejection return a non-nil size/capacity error? Supporting larger payloads can be a separate decision; even with the current cap, the failure should be observable by the caller.

Tested with Go 1.27.0 on Windows/amd64, compiling the unchanged PR finalmask.go and the test below as a file-list package against the PR base (c412e77a) dependencies. This is a wrapper-level reproduction with test doubles, not a network/protocol test.

Minimal reproduction (package finalmask)
package finalmask

import (
	"bytes"
	"context"
	"testing"

	"github.com/xtls/xray-core/common/net"
)

type pr6754Sink struct {
	net.PacketConn
	writes int
	packet []byte
}

func (s *pr6754Sink) WriteTo(p []byte, _ net.Addr) (int, error) {
	s.writes++
	s.packet = bytes.Clone(p)
	return len(p), nil
}

type pr6754Header struct{ net.PacketConn }

func (*pr6754Header) Size() int { return 1 }
func (*pr6754Header) WriteTo(p []byte, _ net.Addr) (int, error) {
	p[0] = 0x42
	return len(p), nil
}

type pr6754Mask struct{}

func (pr6754Mask) HeaderConn() {}
func (pr6754Mask) WrapPacketConnClient(net.PacketConn, *net.Destination, *Dialer) (net.PacketConn, error) {
	return &pr6754Header{}, nil
}
func (pr6754Mask) WrapPacketConnServer(net.PacketConn, net.Addr, *ListenConfig) (net.PacketConn, error) {
	return &pr6754Header{}, nil
}

func TestPR6754OversizeWriteReportsError(t *testing.T) {
	for _, size := range []int{4095, 4096} {
		name := "fits"
		if size == 4096 {
			name = "oversize"
		}
		t.Run(name, func(t *testing.T) {
			sink := &pr6754Sink{}
			addr := &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 12345}
			fm := NewFinalMask(nil, []UDPMask{pr6754Mask{}}, nil, nil,
				func(context.Context, net.Destination) (net.PacketConn, net.Addr, error) {
					return sink, addr, nil
				}, nil)
			conn, err := fm.DialUDP(context.Background(), net.UDPDestination(net.LocalHostIP, 12345))
			if err != nil {
				t.Fatal(err)
			}
			payload := bytes.Repeat([]byte{0xa5}, size)
			n, err := conn.Write(payload)
			t.Logf("payload=%d header=1 n=%d err=%v lowerWrites=%d lowerBytes=%d", size, n, err, sink.writes, len(sink.packet))
			if size == 4095 {
				if err != nil || n != size || sink.writes != 1 || len(sink.packet) != 4096 || sink.packet[0] != 0x42 || !bytes.Equal(sink.packet[1:], payload) {
					t.Fatal("boundary control failed")
				}
			} else {
				if n != 0 || sink.writes != 0 {
					t.Fatal("unexpected oversized-packet path")
				}
				if err == nil {
					t.Fatal("oversized datagram was dropped without a returned error")
				}
			}
		})
	}
}

On the PR checkout, save this as a new test file in transport/internet/finalmask and run:

go test ./transport/internet/finalmask -run '^TestPR6754OversizeWriteReportsError$' -count=1 -v

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

我不知道你写的这个 HeaderConn 是想做什么

func (*pr6754Header) Size() int { return 1 }

Size 代表前面预留分配的长度,不是让你直接覆盖前面 n 字节的数据

headerManagerConn.WriteTo drops a datagram without returning an error when the payload plus headers exceeds its 4096-byte buffer. This is still present in the current head (35302746), lines 348–355.

长度超过 4096 silent drop 是预期行为,超过这个长度的 udp 包也没意义

@svlavr

svlavr commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Thanks for clarifying, I understand now. I wanted to clarify the expected behavior of this code. The silent drop is intentional; I am not proposing a change.

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

准确来说也不是 silent,xray 里会有 errors.LogError,quic 包不会超过 1500,kcp 也有默认 mtu,这个场景下 4096 大小也够套几百层 headerconn 了,至于总长度超过 4096 要不要 err,因为套的数量是可选的,这个还需要考虑

@svlavr

svlavr commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

My question about the 4096-byte boundary was to understand how the neighboring UDP masks behave. For headerManagerConn, the behavior is now clear: an oversized packet is intentionally dropped and logged.

However, there is a separate header-custom UDP path that does not go through headerManagerConn. Its configuration accepts a 4097-byte custom header, but the first WriteTo then evaluates b[4097:] while the working buffer length is 4096, causing a panic. I reproduced this on the current head.

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

这边缘情况也太边缘了,4096 的 header 是要伪装成什么,还是纯浪费流量

而且这个触发路径,首先是将 fm 作为库使用自己写的 headerconn 且要预留 4K+1 的头部空间,我也不知道要说什么

@svlavr

svlavr commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

The point is not the marginal case, but the marginal user who can put any rand value in the JSON file.
Nowhere does it say what value it should have, and there is no upper-limit check, only a lower-limit check.
For randRange there is a check, but for rand I did not find one.
JSON rand → udpmaskLoader["header-custom"] → HeaderCustomUDP.Build() → custom.UDPConfig → UDPConfig.WrapPacketConnClient() → NewConnClientUDP() → udpCustomClientConn.WriteTo().

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

那你搞混了,udp custom 没有 headerconn 属性

@svlavr

svlavr commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Yes, exactly. I am not referring to HeaderConn here.
The path I posted is the separate header-custom UDP JSON path:
JSON rand → HeaderCustomUDP.Build() → custom.UDPConfig → NewConnClientUDP() → udpCustomClientConn.WriteTo().

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

哦,我明白了,@fatyzzz 你想给 udpCustom 的 packet 加个长度检查吗

@svlavr

svlavr commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

header size + payload size <= 4096

@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

done

@RPRX

RPRX commented Sep 20, 2026

Copy link
Copy Markdown
Member

@LjhAUMEM XDRIVE 也能走 h3 的然后吃到 noise,不过我不确定 @Risaro 有没有写,另外 TCP 应当默认 h2,你检查一下吧

@RPRX RPRX changed the title finalmask Transport: Refactor to be based on Finalmask Sep 20, 2026
@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

我看到 1.1 了,还有为什么会用上 reality

@RPRX

RPRX commented Sep 20, 2026

Copy link
Copy Markdown
Member

我也是之前似乎看到他说默认是 1.1 感觉有点怪,所以让你看看,REALITY 先去掉吧,你开个 PR,以及没支持 h3 的话加一下

@RPRX RPRX changed the title Transport: Refactor to be based on Finalmask Transport: Refactor to be based on Finalmask's dialer & listener Sep 20, 2026
@RPRX
RPRX merged commit 8267cf9 into XTLS:main Sep 20, 2026
40 checks passed
@LjhAUMEM

Copy link
Copy Markdown
Contributor Author

s3 没兴趣,xdns 结束后需要留时间写 rust

GFW-knocker pushed a commit to GFW-knocker/Xray-core that referenced this pull request Oct 2, 2026
…ault `interval` (XTLS#6808)

XTLS#6808 (review)

---------

Co-authored-by: 风扇滑翔翼 <[email protected]>
(cherry picked from commit 7780db9)

[GFW-knocker: hand-merged, config part only. The panic half fixes code from
the transport refactor (XTLS#6754), which this fork does not take: here the
finalmask managers return a plain nil and every caller closes its own raw conn.
The udpHop default interval (30) and its build-time check are kept.]
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.

6 participants