From bd48f62a7b65b7224ecc9e6ff89ed2efec44b18c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 23 Apr 2024 11:03:25 +0800 Subject: [PATCH] Fix initial MTU --- hysteria/packet.go | 2 +- hysteria2/packet.go | 2 +- tuic/packet.go | 20 +++----------------- 3 files changed, 5 insertions(+), 19 deletions(-) diff --git a/hysteria/packet.go b/hysteria/packet.go index 18f7a97..772d63b 100644 --- a/hysteria/packet.go +++ b/hysteria/packet.go @@ -139,7 +139,7 @@ func newUDPPacketConn(ctx context.Context, quicConn quic.Connection, onDestroy f cancel: cancel, quicConn: quicConn, data: make(chan *udpMessage, 64), - udpMTU: 1200, + udpMTU: 1200 - 3, defragger: newUDPDefragger(), onDestroy: onDestroy, } diff --git a/hysteria2/packet.go b/hysteria2/packet.go index ffe42cd..89a6409 100644 --- a/hysteria2/packet.go +++ b/hysteria2/packet.go @@ -135,7 +135,7 @@ func newUDPPacketConn(ctx context.Context, quicConn quic.Connection, onDestroy f cancel: cancel, quicConn: quicConn, data: make(chan *udpMessage, 64), - udpMTU: 1200, + udpMTU: 1200 - 3, defragger: newUDPDefragger(), onDestroy: onDestroy, } diff --git a/tuic/packet.go b/tuic/packet.go index fdc5192..ecb43e4 100644 --- a/tuic/packet.go +++ b/tuic/packet.go @@ -128,7 +128,6 @@ type udpPacketConn struct { data chan *udpMessage udpStream bool udpMTU int - udpMTUTime time.Time packetId atomic.Uint32 closeOnce sync.Once isServer bool @@ -148,6 +147,7 @@ func newUDPPacketConn(ctx context.Context, quicConn quic.Connection, udpStream b isServer: isServer, defragger: newUDPDefragger(), onDestroy: onDestroy, + udpMTU: 1200 - 3, } } @@ -179,18 +179,6 @@ func (c *udpPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { } } -func (c *udpPacketConn) needFragment() bool { - if c.udpMTU == 0 { - return false - } - nowTime := time.Now() - if nowTime.Sub(c.udpMTUTime) < 5*time.Second { - c.udpMTUTime = nowTime - return true - } - return false -} - func (c *udpPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { defer buffer.Release() select { @@ -215,7 +203,7 @@ func (c *udpPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) } defer message.releaseMessage() var err error - if !c.udpStream && c.needFragment() && buffer.Len() > c.udpMTU-message.headerSize() { + if !c.udpStream && buffer.Len() > c.udpMTU-message.headerSize() { err = c.writePackets(fragUDPMessage(message, c.udpMTU)) } else { err = c.writePacket(message) @@ -228,7 +216,6 @@ func (c *udpPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) return err } c.udpMTU = int(tooLargeErr) - 3 - c.udpMTUTime = time.Now() return c.writePackets(fragUDPMessage(message, c.udpMTU)) } @@ -254,7 +241,7 @@ func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { destination: destination, data: buf.As(p), } - if !c.udpStream && c.needFragment() && len(p) > c.udpMTU-message.headerSize() { + if !c.udpStream && len(p) > c.udpMTU-message.headerSize() { err = c.writePackets(fragUDPMessage(message, c.udpMTU)) if err == nil { return len(p), nil @@ -270,7 +257,6 @@ func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { return } c.udpMTU = int(tooLargeErr) - 3 - c.udpMTUTime = time.Now() err = c.writePackets(fragUDPMessage(message, c.udpMTU)) if err == nil { return len(p), nil