From 4cdb789918656f98bf1b2278fef840022f3c49d6 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Thu, 21 Sep 2023 23:49:45 +0800 Subject: [PATCH] chore: limit tuicv5's maxUdpRelayPacketSize up to 1200-PacketOverHead --- adapter/outbound/tuic.go | 6 +++++- transport/tuic/server.go | 4 ++++ transport/tuic/tuic.go | 1 + transport/tuic/v5/frag.go | 5 +++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/adapter/outbound/tuic.go b/adapter/outbound/tuic.go index 4d826912..b1032e8c 100644 --- a/adapter/outbound/tuic.go +++ b/adapter/outbound/tuic.go @@ -323,6 +323,10 @@ func NewTuic(option TuicOption) (*Tuic, error) { t.client = tuic.NewPoolClientV4(clientOption) } else { + maxUdpRelayPacketSize := option.MaxUdpRelayPacketSize + if maxUdpRelayPacketSize > tuic.MaxFragSizeV5 { + maxUdpRelayPacketSize = tuic.MaxFragSizeV5 + } clientOption := &tuic.ClientOptionV5{ TlsConfig: tlsConfig, QuicConfig: quicConfig, @@ -331,7 +335,7 @@ func NewTuic(option TuicOption) (*Tuic, error) { UdpRelayMode: udpRelayMode, CongestionController: option.CongestionController, ReduceRtt: option.ReduceRtt, - MaxUdpRelayPacketSize: option.MaxUdpRelayPacketSize, + MaxUdpRelayPacketSize: maxUdpRelayPacketSize, MaxOpenStreams: clientMaxOpenStreams, CWND: option.CWND, } diff --git a/transport/tuic/server.go b/transport/tuic/server.go index a6f91b88..cabc04e0 100644 --- a/transport/tuic/server.go +++ b/transport/tuic/server.go @@ -223,6 +223,10 @@ func NewServer(option *ServerOption, pc net.PacketConn) (*Server, error) { } } if len(option.Users) > 0 { + maxUdpRelayPacketSize := option.MaxUdpRelayPacketSize + if maxUdpRelayPacketSize > MaxFragSizeV5 { + maxUdpRelayPacketSize = MaxFragSizeV5 + } server.optionV5 = &v5.ServerOption{ HandleTcpFn: option.HandleTcpFn, HandleUdpFn: option.HandleUdpFn, diff --git a/transport/tuic/tuic.go b/transport/tuic/tuic.go index 8832ef91..387a152c 100644 --- a/transport/tuic/tuic.go +++ b/transport/tuic/tuic.go @@ -30,6 +30,7 @@ const DefaultConnectionReceiveWindow = common.DefaultConnectionReceiveWindow var GenTKN = v4.GenTKN var PacketOverHeadV4 = v4.PacketOverHead var PacketOverHeadV5 = v5.PacketOverHead +var MaxFragSizeV5 = v5.MaxFragSize type UdpRelayMode = common.UdpRelayMode diff --git a/transport/tuic/v5/frag.go b/transport/tuic/v5/frag.go index ae9dbf10..8df9f785 100644 --- a/transport/tuic/v5/frag.go +++ b/transport/tuic/v5/frag.go @@ -9,6 +9,11 @@ import ( "github.com/metacubex/quic-go" ) +// MaxFragSize is a safe udp relay packet size +// because tuicv5 support udp fragment so we unneeded to do a magic modify for quic-go to increase MaxDatagramFrameSize +// it may not work fine in some platform +var MaxFragSize = 1200 - PacketOverHead + func fragWriteNative(quicConn quic.Connection, packet Packet, buf *bytes.Buffer, fragSize int) (err error) { fullPayload := packet.DATA off := 0