diff --git a/go.mod b/go.mod index 3e5664a..2f949c4 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/gofrs/uuid/v5 v5.2.0 github.com/sagernet/quic-go v0.46.0-beta.4 - github.com/sagernet/sing v0.4.1 + github.com/sagernet/sing v0.5.0-beta.1 golang.org/x/crypto v0.23.0 golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f ) diff --git a/go.sum b/go.sum index 38849ed..bed1a10 100644 --- a/go.sum +++ b/go.sum @@ -21,8 +21,8 @@ github.com/quic-go/qtls-go1-20 v0.4.1 h1:D33340mCNDAIKBqXuAvexTNMUByrYmFYVfKfDN5 github.com/quic-go/qtls-go1-20 v0.4.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k= github.com/sagernet/quic-go v0.46.0-beta.4 h1:k9f7VSKaM47AY6MPND0Qf1KRN7HwimPg9zdOFTXTiCk= github.com/sagernet/quic-go v0.46.0-beta.4/go.mod h1:zJmVdJUNqEDXfubf4KtIOUHHerggjBduiGRLNzJspcM= -github.com/sagernet/sing v0.4.1 h1:zVlpE+7k7AFoC2pv6ReqLf0PIHjihL/jsBl5k05PQFk= -github.com/sagernet/sing v0.4.1/go.mod h1:ieZHA/+Y9YZfXs2I3WtuwgyCZ6GPsIR7HdKb1SdEnls= +github.com/sagernet/sing v0.5.0-beta.1 h1:THZMZgJcDQxutE++6Ckih1HlvMtXple94RBGa6GSg2I= +github.com/sagernet/sing v0.5.0-beta.1/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= diff --git a/hysteria/packet.go b/hysteria/packet.go index e359615..e274a9c 100644 --- a/hysteria/packet.go +++ b/hysteria/packet.go @@ -20,6 +20,7 @@ import ( E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" + "github.com/sagernet/sing/common/pipe" ) var udpMessagePool = sync.Pool{ @@ -129,18 +130,20 @@ type udpPacketConn struct { defragger *udpDefragger onDestroy func() readWaitOptions N.ReadWaitOptions + readDeadline pipe.Deadline } func newUDPPacketConn(ctx context.Context, quicConn quic.Connection, onDestroy func()) *udpPacketConn { ctx, cancel := common.ContextWithCancelCause(ctx) return &udpPacketConn{ - ctx: ctx, - cancel: cancel, - quicConn: quicConn, - data: make(chan *udpMessage, 64), - udpMTU: 1200 - 3, - defragger: newUDPDefragger(), - onDestroy: onDestroy, + ctx: ctx, + cancel: cancel, + quicConn: quicConn, + data: make(chan *udpMessage, 64), + udpMTU: 1200 - 3, + defragger: newUDPDefragger(), + onDestroy: onDestroy, + readDeadline: pipe.MakeDeadline(), } } @@ -153,6 +156,8 @@ func (c *udpPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, return case <-c.ctx.Done(): return M.Socksaddr{}, io.ErrClosedPipe + case <-c.readDeadline.Wait(): + return M.Socksaddr{}, os.ErrDeadlineExceeded } } @@ -170,6 +175,8 @@ func (c *udpPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { return n, addr, nil case <-c.ctx.Done(): return 0, nil, io.ErrClosedPipe + case <-c.readDeadline.Wait(): + return 0, nil, os.ErrDeadlineExceeded } } @@ -307,7 +314,8 @@ func (c *udpPacketConn) SetDeadline(t time.Time) error { } func (c *udpPacketConn) SetReadDeadline(t time.Time) error { - return os.ErrInvalid + c.readDeadline.Set(t) + return nil } func (c *udpPacketConn) SetWriteDeadline(t time.Time) error { diff --git a/hysteria/packet_wait.go b/hysteria/packet_wait.go index 6476c36..243af15 100644 --- a/hysteria/packet_wait.go +++ b/hysteria/packet_wait.go @@ -2,6 +2,7 @@ package hysteria import ( "io" + "os" "github.com/sagernet/sing/common/buf" M "github.com/sagernet/sing/common/metadata" @@ -33,5 +34,7 @@ func (c *udpPacketConn) WaitReadPacket() (buffer *buf.Buffer, destination M.Sock return case <-c.ctx.Done(): return nil, M.Socksaddr{}, io.ErrClosedPipe + case <-c.readDeadline.Wait(): + return nil, M.Socksaddr{}, os.ErrDeadlineExceeded } } diff --git a/hysteria2/packet.go b/hysteria2/packet.go index 6e39b52..5d49154 100644 --- a/hysteria2/packet.go +++ b/hysteria2/packet.go @@ -21,6 +21,7 @@ import ( "github.com/sagernet/sing/common/cache" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" + "github.com/sagernet/sing/common/pipe" ) var udpMessagePool = sync.Pool{ @@ -126,18 +127,20 @@ type udpPacketConn struct { defragger *udpDefragger onDestroy func() readWaitOptions N.ReadWaitOptions + readDeadline pipe.Deadline } func newUDPPacketConn(ctx context.Context, quicConn quic.Connection, onDestroy func()) *udpPacketConn { ctx, cancel := common.ContextWithCancelCause(ctx) return &udpPacketConn{ - ctx: ctx, - cancel: cancel, - quicConn: quicConn, - data: make(chan *udpMessage, 64), - udpMTU: 1200 - 3, - defragger: newUDPDefragger(), - onDestroy: onDestroy, + ctx: ctx, + cancel: cancel, + quicConn: quicConn, + data: make(chan *udpMessage, 64), + udpMTU: 1200 - 3, + defragger: newUDPDefragger(), + onDestroy: onDestroy, + readDeadline: pipe.MakeDeadline(), } } @@ -150,6 +153,8 @@ func (c *udpPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, return case <-c.ctx.Done(): return M.Socksaddr{}, io.ErrClosedPipe + case <-c.readDeadline.Wait(): + return M.Socksaddr{}, os.ErrDeadlineExceeded } } @@ -167,6 +172,8 @@ func (c *udpPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { return n, addr, nil case <-c.ctx.Done(): return 0, nil, io.ErrClosedPipe + case <-c.readDeadline.Wait(): + return 0, nil, os.ErrDeadlineExceeded } } @@ -301,7 +308,8 @@ func (c *udpPacketConn) SetDeadline(t time.Time) error { } func (c *udpPacketConn) SetReadDeadline(t time.Time) error { - return os.ErrInvalid + c.readDeadline.Set(t) + return nil } func (c *udpPacketConn) SetWriteDeadline(t time.Time) error { diff --git a/hysteria2/packet_wait.go b/hysteria2/packet_wait.go index a183012..9d942e7 100644 --- a/hysteria2/packet_wait.go +++ b/hysteria2/packet_wait.go @@ -2,6 +2,7 @@ package hysteria2 import ( "io" + "os" "github.com/sagernet/sing/common/buf" M "github.com/sagernet/sing/common/metadata" @@ -33,5 +34,7 @@ func (c *udpPacketConn) WaitReadPacket() (buffer *buf.Buffer, destination M.Sock return case <-c.ctx.Done(): return nil, M.Socksaddr{}, io.ErrClosedPipe + case <-c.readDeadline.Wait(): + return nil, M.Socksaddr{}, os.ErrDeadlineExceeded } } diff --git a/tuic/packet.go b/tuic/packet.go index 306f63b..df02c66 100644 --- a/tuic/packet.go +++ b/tuic/packet.go @@ -20,6 +20,7 @@ import ( E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" + "github.com/sagernet/sing/common/pipe" ) var udpMessagePool = sync.Pool{ @@ -134,20 +135,22 @@ type udpPacketConn struct { defragger *udpDefragger onDestroy func() readWaitOptions N.ReadWaitOptions + readDeadline pipe.Deadline } func newUDPPacketConn(ctx context.Context, quicConn quic.Connection, udpStream bool, isServer bool, onDestroy func()) *udpPacketConn { ctx, cancel := common.ContextWithCancelCause(ctx) return &udpPacketConn{ - ctx: ctx, - cancel: cancel, - quicConn: quicConn, - data: make(chan *udpMessage, 64), - udpStream: udpStream, - isServer: isServer, - defragger: newUDPDefragger(), - onDestroy: onDestroy, - udpMTU: 1200 - 3, + ctx: ctx, + cancel: cancel, + quicConn: quicConn, + data: make(chan *udpMessage, 64), + udpStream: udpStream, + isServer: isServer, + defragger: newUDPDefragger(), + onDestroy: onDestroy, + udpMTU: 1200 - 3, + readDeadline: pipe.MakeDeadline(), } } @@ -160,6 +163,8 @@ func (c *udpPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, return case <-c.ctx.Done(): return M.Socksaddr{}, io.ErrClosedPipe + case <-c.readDeadline.Wait(): + return M.Socksaddr{}, os.ErrDeadlineExceeded } } @@ -176,6 +181,8 @@ func (c *udpPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { return n, addr, nil case <-c.ctx.Done(): return 0, nil, io.ErrClosedPipe + case <-c.readDeadline.Wait(): + return 0, nil, os.ErrDeadlineExceeded } } @@ -350,7 +357,8 @@ func (c *udpPacketConn) SetDeadline(t time.Time) error { } func (c *udpPacketConn) SetReadDeadline(t time.Time) error { - return os.ErrInvalid + c.readDeadline.Set(t) + return nil } func (c *udpPacketConn) SetWriteDeadline(t time.Time) error { diff --git a/tuic/packet_wait.go b/tuic/packet_wait.go index 57c57f8..6d353ba 100644 --- a/tuic/packet_wait.go +++ b/tuic/packet_wait.go @@ -2,6 +2,7 @@ package tuic import ( "io" + "os" "github.com/sagernet/sing/common/buf" M "github.com/sagernet/sing/common/metadata" @@ -33,5 +34,7 @@ func (c *udpPacketConn) WaitReadPacket() (buffer *buf.Buffer, destination M.Sock return case <-c.ctx.Done(): return nil, M.Socksaddr{}, io.ErrClosedPipe + case <-c.readDeadline.Wait(): + return nil, M.Socksaddr{}, os.ErrDeadlineExceeded } }