Implement read deadline for packet connections

This commit is contained in:
世界 2024-08-18 09:22:08 +08:00
parent caa7813f48
commit bfeee81d26
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 59 additions and 26 deletions

View file

@ -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"
"github.com/sagernet/sing/common/rw"
)
@ -130,18 +131,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(),
}
}
@ -154,6 +157,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
}
}
@ -171,6 +176,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
}
}
@ -308,7 +315,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 {

View file

@ -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
}
}

View file

@ -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 {

View file

@ -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
}
}

View file

@ -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 {

View file

@ -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
}
}