Skip to content

Commit

Permalink
Implement read deadline for packet connections
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Aug 27, 2024
1 parent b2aa8a0 commit f1bfea7
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
24 changes: 16 additions & 8 deletions hysteria/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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(),
}
}

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

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

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions hysteria/packet_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hysteria

import (
"io"
"os"

"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
Expand Down Expand Up @@ -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
}
}
24 changes: 16 additions & 8 deletions hysteria2/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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(),
}
}

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

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

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions hysteria2/packet_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hysteria2

import (
"io"
"os"

"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
Expand Down Expand Up @@ -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
}
}
28 changes: 18 additions & 10 deletions tuic/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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(),
}
}

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

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

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions tuic/packet_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tuic

import (
"io"
"os"

"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
Expand Down Expand Up @@ -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
}
}

0 comments on commit f1bfea7

Please sign in to comment.