From 4328c535a9e6d765e37a907787b50dc11a3b99ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 23 Mar 2023 15:14:21 +0800 Subject: [PATCH] Improve timeout canceler --- common/canceler/instance.go | 48 ------------------------------------ common/canceler/packet.go | 49 ------------------------------------- go.mod | 4 +-- go.sum | 8 +++--- inbound/tun.go | 5 ---- outbound/default.go | 2 +- outbound/dns.go | 2 +- 7 files changed, 8 insertions(+), 110 deletions(-) delete mode 100644 common/canceler/instance.go delete mode 100644 common/canceler/packet.go diff --git a/common/canceler/instance.go b/common/canceler/instance.go deleted file mode 100644 index df9c1680..00000000 --- a/common/canceler/instance.go +++ /dev/null @@ -1,48 +0,0 @@ -package canceler - -import ( - "context" - "time" -) - -type Instance struct { - ctx context.Context - cancelFunc context.CancelFunc - timer *time.Timer - timeout time.Duration -} - -func New(ctx context.Context, cancelFunc context.CancelFunc, timeout time.Duration) *Instance { - instance := &Instance{ - ctx, - cancelFunc, - time.NewTimer(timeout), - timeout, - } - go instance.wait() - return instance -} - -func (i *Instance) Update() bool { - if !i.timer.Stop() { - return false - } - if !i.timer.Reset(i.timeout) { - return false - } - return true -} - -func (i *Instance) wait() { - select { - case <-i.timer.C: - case <-i.ctx.Done(): - } - i.Close() -} - -func (i *Instance) Close() error { - i.timer.Stop() - i.cancelFunc() - return nil -} diff --git a/common/canceler/packet.go b/common/canceler/packet.go deleted file mode 100644 index a964896e..00000000 --- a/common/canceler/packet.go +++ /dev/null @@ -1,49 +0,0 @@ -package canceler - -import ( - "context" - "time" - - "github.com/sagernet/sing/common" - "github.com/sagernet/sing/common/buf" - M "github.com/sagernet/sing/common/metadata" - N "github.com/sagernet/sing/common/network" -) - -type PacketConn struct { - N.PacketConn - instance *Instance -} - -func NewPacketConn(ctx context.Context, conn N.PacketConn, timeout time.Duration) (context.Context, N.PacketConn) { - ctx, cancel := context.WithCancel(ctx) - instance := New(ctx, cancel, timeout) - return ctx, &PacketConn{conn, instance} -} - -func (c *PacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) { - destination, err = c.PacketConn.ReadPacket(buffer) - if err == nil { - c.instance.Update() - } - return -} - -func (c *PacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { - err := c.PacketConn.WritePacket(buffer, destination) - if err == nil { - c.instance.Update() - } - return err -} - -func (c *PacketConn) Close() error { - return common.Close( - c.PacketConn, - c.instance, - ) -} - -func (c *PacketConn) Upstream() any { - return c.PacketConn -} diff --git a/go.mod b/go.mod index 4a519227..ada6badc 100644 --- a/go.mod +++ b/go.mod @@ -25,11 +25,11 @@ require ( github.com/sagernet/gomobile v0.0.0-20221130124640-349ebaa752ca github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32 github.com/sagernet/reality v0.0.0-20230312150606-35ea9af0e0b8 - github.com/sagernet/sing v0.2.1-0.20230318094614-4bbf5f2c3046 + github.com/sagernet/sing v0.2.1-0.20230323071235-f8038854d286 github.com/sagernet/sing-dns v0.1.4 github.com/sagernet/sing-shadowsocks v0.2.0 github.com/sagernet/sing-shadowtls v0.1.0 - github.com/sagernet/sing-tun v0.1.3-0.20230315134716-fe89bbded22d + github.com/sagernet/sing-tun v0.1.3-0.20230323073325-35d565af6515 github.com/sagernet/sing-vmess v0.1.3 github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9 diff --git a/go.sum b/go.sum index 477af0d5..e311a808 100644 --- a/go.sum +++ b/go.sum @@ -111,16 +111,16 @@ github.com/sagernet/reality v0.0.0-20230312150606-35ea9af0e0b8 h1:4M3+0/kqvJuTsi github.com/sagernet/reality v0.0.0-20230312150606-35ea9af0e0b8/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= -github.com/sagernet/sing v0.2.1-0.20230318094614-4bbf5f2c3046 h1:/+ZWbxRvQmco9ES2qT5Eh/x/IiQRjAcUyRG/vQ4dpxc= -github.com/sagernet/sing v0.2.1-0.20230318094614-4bbf5f2c3046/go.mod h1:9uHswk2hITw8leDbiLS/xn0t9nzBcbePxzm9PJhwdlw= +github.com/sagernet/sing v0.2.1-0.20230323071235-f8038854d286 h1:0Td2b5l1KgrdlOnbRWgFFWsyb0TLoq/tP6j9Lut4JN0= +github.com/sagernet/sing v0.2.1-0.20230323071235-f8038854d286/go.mod h1:9uHswk2hITw8leDbiLS/xn0t9nzBcbePxzm9PJhwdlw= github.com/sagernet/sing-dns v0.1.4 h1:7VxgeoSCiiazDSaXXQVcvrTBxFpOePPq/4XdgnUDN+0= github.com/sagernet/sing-dns v0.1.4/go.mod h1:1+6pCa48B1AI78lD+/i/dLgpw4MwfnsSpZo0Ds8wzzk= github.com/sagernet/sing-shadowsocks v0.2.0 h1:ILDWL7pwWfkPLEbviE/MyCgfjaBmJY/JVVY+5jhSb58= github.com/sagernet/sing-shadowsocks v0.2.0/go.mod h1:ysYzszRLpNzJSorvlWRMuzU6Vchsp7sd52q+JNY4axw= github.com/sagernet/sing-shadowtls v0.1.0 h1:05MYce8aR5xfKIn+y7xRFsdKhKt44QZTSEQW+lG5IWQ= github.com/sagernet/sing-shadowtls v0.1.0/go.mod h1:Kn1VUIprdkwCgkS6SXYaLmIpKzQbqBIKJBMY+RvBhYc= -github.com/sagernet/sing-tun v0.1.3-0.20230315134716-fe89bbded22d h1:1gt4Hu2fHCrmL2NZYCNJ3nCgeczuhK09oCMni9mZmZk= -github.com/sagernet/sing-tun v0.1.3-0.20230315134716-fe89bbded22d/go.mod h1:KnRkwaDHbb06zgeNPu0LQ8A+vA9myMxKEgHN1brCPHg= +github.com/sagernet/sing-tun v0.1.3-0.20230323073325-35d565af6515 h1:r25BJqn3o34g+bDdhoRU65zimKPfGCTv7nHuysyJojo= +github.com/sagernet/sing-tun v0.1.3-0.20230323073325-35d565af6515/go.mod h1:1xzFt4zJ7CzXdbgPcy7+Lsg4ypZo0ivDNZ0oQdL3zEY= github.com/sagernet/sing-vmess v0.1.3 h1:q/+tsF46dvvapL6CpQBgPHJ6nQrDUZqEtLHCbsjO7iM= github.com/sagernet/sing-vmess v0.1.3/go.mod h1:GVXqAHwe9U21uS+Voh4YBIrADQyE4F9v0ayGSixSQAE= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 h1:HuE6xSwco/Xed8ajZ+coeYLmioq0Qp1/Z2zczFaV8as= diff --git a/inbound/tun.go b/inbound/tun.go index 1791ca19..8ea7b384 100644 --- a/inbound/tun.go +++ b/inbound/tun.go @@ -5,10 +5,8 @@ import ( "net" "strconv" "strings" - "time" "github.com/sagernet/sing-box/adapter" - "github.com/sagernet/sing-box/common/canceler" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/experimental/libbox/platform" "github.com/sagernet/sing-box/log" @@ -208,9 +206,6 @@ func (t *Tun) NewConnection(ctx context.Context, conn net.Conn, upstreamMetadata func (t *Tun) NewPacketConnection(ctx context.Context, conn N.PacketConn, upstreamMetadata M.Metadata) error { ctx = log.ContextWithNewID(ctx) - if tun.NeedTimeoutFromContext(ctx) { - ctx, conn = canceler.NewPacketConn(ctx, conn, time.Duration(t.udpTimeout)*time.Second) - } var metadata adapter.InboundContext metadata.Inbound = t.tag metadata.InboundType = C.TypeTun diff --git a/outbound/default.go b/outbound/default.go index 3b5c0c1b..005b9da9 100644 --- a/outbound/default.go +++ b/outbound/default.go @@ -8,12 +8,12 @@ import ( "time" "github.com/sagernet/sing-box/adapter" - "github.com/sagernet/sing-box/common/canceler" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/bufio" + "github.com/sagernet/sing/common/canceler" E "github.com/sagernet/sing/common/exceptions" N "github.com/sagernet/sing/common/network" ) diff --git a/outbound/dns.go b/outbound/dns.go index fa25ac1c..075e2849 100644 --- a/outbound/dns.go +++ b/outbound/dns.go @@ -7,11 +7,11 @@ import ( "os" "github.com/sagernet/sing-box/adapter" - "github.com/sagernet/sing-box/common/canceler" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-dns" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" + "github.com/sagernet/sing/common/canceler" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" "github.com/sagernet/sing/common/task"