From 6ed02b47bfce0f9531d6c3120bf45ca633862928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 6 Dec 2023 20:48:32 +0800 Subject: [PATCH] Implement read waiter for ss2022 UDP --- go.mod | 4 ++-- go.sum | 8 +++---- shadowaead/method.go | 8 +++++++ shadowaead_2022/method_wait.go | 42 ++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 shadowaead_2022/method_wait.go diff --git a/go.mod b/go.mod index 8334d88..8496b22 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,12 @@ module github.com/sagernet/sing-shadowsocks2 go 1.18 require ( - github.com/sagernet/sing v0.2.17 + github.com/sagernet/sing v0.2.19-0.20231206123939-86c131fc0547 golang.org/x/crypto v0.15.0 lukechampine.com/blake3 v1.2.1 ) require ( github.com/klauspost/cpuid/v2 v2.0.9 // indirect - golang.org/x/sys v0.14.0 // indirect + golang.org/x/sys v0.15.0 // indirect ) diff --git a/go.sum b/go.sum index 0512153..6aeb7a5 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,10 @@ github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/sagernet/sing v0.2.17 h1:vMPKb3MV0Aa5ws4dCJkRI8XEjrsUcDn810czd0FwmzI= -github.com/sagernet/sing v0.2.17/go.mod h1:OL6k2F0vHmEzXz2KW19qQzu172FDgSbUSODylighuVo= +github.com/sagernet/sing v0.2.19-0.20231206123939-86c131fc0547 h1:Mco2TffcdPMlmzbcGmbKd6Vm6dEBJH2VgjO6H1XfZzY= +github.com/sagernet/sing v0.2.19-0.20231206123939-86c131fc0547/go.mod h1:Ce5LNojQOgOiWhiD8pPD6E9H7e2KgtOe3Zxx4Ou5u80= golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= lukechampine.com/blake3 v1.2.1 h1:YuqqRuaqsGV71BV/nm9xlI0MKUv4QC54jQnBChWbGnI= lukechampine.com/blake3 v1.2.1/go.mod h1:0OFRp7fBtAylGVCO40o87sbupkyIGgbpv1+M1k1LM6k= diff --git a/shadowaead/method.go b/shadowaead/method.go index ef24bfa..595f82b 100644 --- a/shadowaead/method.go +++ b/shadowaead/method.go @@ -350,6 +350,14 @@ func (c *clientPacketConn) RearHeadroom() int { return shadowio.Overhead } +func (c *clientPacketConn) ReaderMTU() int { + return MaxPacketSize +} + +func (c *clientPacketConn) WriterMTU() int { + return MaxPacketSize +} + func (c *clientPacketConn) Upstream() any { return c.AbstractConn } diff --git a/shadowaead_2022/method_wait.go b/shadowaead_2022/method_wait.go new file mode 100644 index 0000000..625e7e6 --- /dev/null +++ b/shadowaead_2022/method_wait.go @@ -0,0 +1,42 @@ +package shadowaead_2022 + +import ( + "github.com/sagernet/sing/common/buf" + "github.com/sagernet/sing/common/bufio" + M "github.com/sagernet/sing/common/metadata" + N "github.com/sagernet/sing/common/network" +) + +var _ N.PacketReadWaitCreator = (*clientPacketConn)(nil) + +func (c *clientPacketConn) CreateReadWaiter() (N.PacketReadWaiter, bool) { + readWaiter, isReadWaiter := bufio.CreateReadWaiter(c.reader) + if !isReadWaiter { + return nil, false + } + return &clientPacketReadWaiter{c, readWaiter}, true +} + +var _ N.PacketReadWaiter = (*clientPacketReadWaiter)(nil) + +type clientPacketReadWaiter struct { + *clientPacketConn + readWaiter N.ReadWaiter +} + +func (w *clientPacketReadWaiter) InitializeReadWaiter(newBuffer func() *buf.Buffer) { + w.readWaiter.InitializeReadWaiter(newBuffer) +} + +func (w *clientPacketReadWaiter) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) { + buffer, err = w.readWaiter.WaitReadBuffer() + if err != nil { + return + } + destination, err = w.readPacket(buffer) + if err != nil { + buffer.Release() + return nil, M.Socksaddr{}, err + } + return +}