From 88b69792dac3116648bfb43ac21497f8991eaa7e Mon Sep 17 00:00:00 2001 From: Fangliding Date: Mon, 28 Sep 2026 17:49:47 +0800 Subject: [PATCH] Do not create Finalmask if not needed --- transport/internet/memory_settings.go | 61 +++++++++++++-------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/transport/internet/memory_settings.go b/transport/internet/memory_settings.go index fb72b6700..6277151c6 100644 --- a/transport/internet/memory_settings.go +++ b/transport/internet/memory_settings.go @@ -54,11 +54,10 @@ func ToMemoryStreamConfig(s *StreamConfig) (*MemoryStreamConfig, error) { mss.SecurityType = s.SecurityType mss.SecuritySettings = ess } + if s != nil && (len(s.Tcpmasks) != 0 || len(s.Udpmasks) != 0) { + var tcpMasks []finalmask.TCPMask + var udpMasks []finalmask.UDPMask - var tcpMasks []finalmask.TCPMask - var udpMasks []finalmask.UDPMask - - if s != nil { for i := range s.Tcpmasks { instance := common.Must2(s.Tcpmasks[i].GetInstance()) tcpMasks = append(tcpMasks, instance.(finalmask.TCPMask)) @@ -67,37 +66,37 @@ func ToMemoryStreamConfig(s *StreamConfig) (*MemoryStreamConfig, error) { instance := common.Must2(s.Udpmasks[i].GetInstance()) udpMasks = append(udpMasks, instance.(finalmask.UDPMask)) } - } - dialTCP := func(ctx context.Context, dest net.Destination) (net.Conn, error) { - return DialSystem(ctx, dest, mss.SocketSettings) - } - listen := func(ctx context.Context, addr net.Addr) (net.Listener, error) { - return ListenSystem(ctx, addr, mss.SocketSettings) - } - dialUDP := func(ctx context.Context, dest net.Destination) (net.PacketConn, net.Addr, error) { - conn, err := DialSystem(ctx, dest, mss.SocketSettings) - if err != nil { - return nil, nil, err + dialTCP := func(ctx context.Context, dest net.Destination) (net.Conn, error) { + return DialSystem(ctx, dest, mss.SocketSettings) } - var newConn net.PacketConn - var udpAddr net.Addr - switch c := conn.(type) { - case *net.PacketConnWrapper: - newConn = c.PacketConn - udpAddr = conn.RemoteAddr() - case *cnc.Connection: - newConn = &FakePacketConn{Conn: c} - udpAddr = &net.UDPAddr{IP: []byte{0, 0, 0, 0}, Port: 0} - default: - panic(reflect.TypeOf(c)) + listen := func(ctx context.Context, addr net.Addr) (net.Listener, error) { + return ListenSystem(ctx, addr, mss.SocketSettings) } - return newConn, udpAddr, nil + dialUDP := func(ctx context.Context, dest net.Destination) (net.PacketConn, net.Addr, error) { + conn, err := DialSystem(ctx, dest, mss.SocketSettings) + if err != nil { + return nil, nil, err + } + var newConn net.PacketConn + var udpAddr net.Addr + switch c := conn.(type) { + case *net.PacketConnWrapper: + newConn = c.PacketConn + udpAddr = conn.RemoteAddr() + case *cnc.Connection: + newConn = &FakePacketConn{Conn: c} + udpAddr = &net.UDPAddr{IP: []byte{0, 0, 0, 0}, Port: 0} + default: + panic(reflect.TypeOf(c)) + } + return newConn, udpAddr, nil + } + listenPacket := func(ctx context.Context, addr net.Addr) (net.PacketConn, error) { + return ListenSystemPacket(ctx, addr, mss.SocketSettings) + } + mss.FinalMask = finalmask.NewFinalMask(tcpMasks, udpMasks, dialTCP, listen, dialUDP, listenPacket) } - listenPacket := func(ctx context.Context, addr net.Addr) (net.PacketConn, error) { - return ListenSystemPacket(ctx, addr, mss.SocketSettings) - } - mss.FinalMask = finalmask.NewFinalMask(tcpMasks, udpMasks, dialTCP, listen, dialUDP, listenPacket) if s != nil && s.QuicParams != nil { mss.QuicParams = s.QuicParams