From 7aae781569b9cb1fd3b7048bc5704021e381582a Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 28 May 2023 15:22:08 +0800 Subject: [PATCH] chore: add WaitReadFrom support in quicStreamPacketConn --- adapter/outbound/hysteria.go | 3 ++- common/net/packet.go | 2 ++ common/net/packet/packet.go | 2 ++ transport/tuic/conn.go | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/adapter/outbound/hysteria.go b/adapter/outbound/hysteria.go index 2d5b90ac..161a4546 100644 --- a/adapter/outbound/hysteria.go +++ b/adapter/outbound/hysteria.go @@ -19,6 +19,7 @@ import ( "github.com/metacubex/quic-go/congestion" M "github.com/sagernet/sing/common/metadata" + N "github.com/Dreamacro/clash/common/net" "github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/proxydialer" tlsC "github.com/Dreamacro/clash/component/tls" @@ -324,7 +325,7 @@ func (c *hyPacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, e return } data = b - put = func() {} + put = N.NilPut addr = M.ParseSocksaddr(addrStr).UDPAddr() return } diff --git a/common/net/packet.go b/common/net/packet.go index 9afe86b0..e949ecf2 100644 --- a/common/net/packet.go +++ b/common/net/packet.go @@ -6,7 +6,9 @@ import ( ) type EnhancePacketConn = packet.EnhancePacketConn +type WaitReadFrom = packet.WaitReadFrom +var NilPut = packet.NilPut var NewEnhancePacketConn = packet.NewEnhancePacketConn var NewThreadSafePacketConn = packet.NewThreadSafePacketConn var NewRefPacketConn = packet.NewRefPacketConn diff --git a/common/net/packet/packet.go b/common/net/packet/packet.go index 6c9542c1..a3f1dd72 100644 --- a/common/net/packet/packet.go +++ b/common/net/packet/packet.go @@ -10,6 +10,8 @@ type WaitReadFrom interface { WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) } +func NilPut() {} + type EnhancePacketConn interface { net.PacketConn WaitReadFrom diff --git a/transport/tuic/conn.go b/transport/tuic/conn.go index 567f6ce5..d46a3556 100644 --- a/transport/tuic/conn.go +++ b/transport/tuic/conn.go @@ -197,6 +197,22 @@ func (q *quicStreamPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err err return } +func (q *quicStreamPacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) { + if q.inputConn != nil { + var packet Packet + packet, err = ReadPacket(q.inputConn) + if err != nil { + return + } + data = packet.DATA + put = N.NilPut + addr = packet.ADDR.UDPAddr() + } else { + err = net.ErrClosed + } + return +} + func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { if q.udpRelayMode != "quic" && len(p) > q.maxUdpRelayPacketSize { return 0, quic.ErrMessageTooLarge(q.maxUdpRelayPacketSize)