From 593799a98825f36ae7a67f7d9ce8f62d4ce21362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 26 Jul 2022 06:56:13 +0800 Subject: [PATCH] Fix clash tracker timeout --- adapter/experimental.go | 8 ++++++-- constant/timeout.go | 2 +- experimental/clashapi/server.go | 10 ++++++---- experimental/clashapi/trafficontrol/tracker.go | 9 +++++++++ go.mod | 2 +- go.sum | 4 ++-- route/router.go | 8 ++++++-- test/go.mod | 2 +- test/go.sum | 4 ++-- 9 files changed, 34 insertions(+), 15 deletions(-) diff --git a/adapter/experimental.go b/adapter/experimental.go index 1215a234..cb2b3b34 100644 --- a/adapter/experimental.go +++ b/adapter/experimental.go @@ -12,9 +12,13 @@ type ClashServer interface { TrafficController } +type Tracker interface { + Leave() +} + type TrafficController interface { - RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule) net.Conn - RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext, matchedRule Rule) N.PacketConn + RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule) (net.Conn, Tracker) + RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext, matchedRule Rule) (N.PacketConn, Tracker) } type OutboundGroup interface { diff --git a/constant/timeout.go b/constant/timeout.go index 7b9e55af..627f93ba 100644 --- a/constant/timeout.go +++ b/constant/timeout.go @@ -4,7 +4,7 @@ import "time" const ( TCPTimeout = 5 * time.Second - TCPKeepAlivePeriod = 20 * time.Second + TCPKeepAlivePeriod = 30 * time.Second ReadPayloadTimeout = 300 * time.Millisecond URLTestTimeout = TCPTimeout DefaultURLTestInterval = 1 * time.Minute diff --git a/experimental/clashapi/server.go b/experimental/clashapi/server.go index d7168019..36a81b4e 100644 --- a/experimental/clashapi/server.go +++ b/experimental/clashapi/server.go @@ -101,12 +101,14 @@ func (s *Server) Close() error { return s.httpServer.Close() } -func (s *Server) RoutedConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, matchedRule adapter.Rule) net.Conn { - return trafficontrol.NewTCPTracker(conn, s.trafficManager, castMetadata(metadata), s.router, matchedRule) +func (s *Server) RoutedConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, matchedRule adapter.Rule) (net.Conn, adapter.Tracker) { + tracker := trafficontrol.NewTCPTracker(conn, s.trafficManager, castMetadata(metadata), s.router, matchedRule) + return tracker, tracker } -func (s *Server) RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, matchedRule adapter.Rule) N.PacketConn { - return trafficontrol.NewUDPTracker(conn, s.trafficManager, castMetadata(metadata), s.router, matchedRule) +func (s *Server) RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, matchedRule adapter.Rule) (N.PacketConn, adapter.Tracker) { + tracker := trafficontrol.NewUDPTracker(conn, s.trafficManager, castMetadata(metadata), s.router, matchedRule) + return tracker, tracker } func castMetadata(metadata adapter.InboundContext) trafficontrol.Metadata { diff --git a/experimental/clashapi/trafficontrol/tracker.go b/experimental/clashapi/trafficontrol/tracker.go index 7ef547c3..d7d29cb1 100644 --- a/experimental/clashapi/trafficontrol/tracker.go +++ b/experimental/clashapi/trafficontrol/tracker.go @@ -31,6 +31,7 @@ type Metadata struct { type tracker interface { ID() string Close() error + Leave() } type trackerInfo struct { @@ -75,6 +76,10 @@ func (tt *tcpTracker) Close() error { return tt.Conn.Close() } +func (tt *tcpTracker) Leave() { + tt.manager.Leave(tt) +} + func NewTCPTracker(conn net.Conn, manager *Manager, metadata Metadata, router adapter.Router, rule adapter.Rule) *tcpTracker { uuid, _ := uuid.NewV4() @@ -158,6 +163,10 @@ func (ut *udpTracker) Close() error { return ut.PacketConn.Close() } +func (ut *udpTracker) Leave() { + ut.manager.Leave(ut) +} + func NewUDPTracker(conn N.PacketConn, manager *Manager, metadata Metadata, router adapter.Router, rule adapter.Rule) *udpTracker { uuid, _ := uuid.NewV4() diff --git a/go.mod b/go.mod index 09866a41..21e3494f 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/sagernet/sing v0.0.0-20220725141316-c15de13f4f68 github.com/sagernet/sing-dns v0.0.0-20220724053927-eb8d0d542175 github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f - github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9 + github.com/sagernet/sing-tun v0.0.0-20220725225208-3b0c717db3f5 github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 diff --git a/go.sum b/go.sum index 7d395e66..7ef946f2 100644 --- a/go.sum +++ b/go.sum @@ -43,8 +43,8 @@ github.com/sagernet/sing-dns v0.0.0-20220724053927-eb8d0d542175 h1:YpacS9+rDFcLG github.com/sagernet/sing-dns v0.0.0-20220724053927-eb8d0d542175/go.mod h1:2A34p89do4H4E9Ke046cJCMTdVqmvsXGWXzRwgeO2TQ= github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f h1:F6yiuKbBoXgWiuoP7R0YA14pDEl3emxA1mL7M16Q7gc= github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f/go.mod h1:cDrLwa3zwY8AaW6a4sjipn4xgdIr3CT8TPqSW6iFOi0= -github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9 h1:kzEyWi2iJcq1oNeCCRrJ1Oh/ogbHR0CZRZ/HMxzy+mo= -github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9/go.mod h1:p7QbUBs2ejf6UQsiHyy1xGAWOk9JWQjZTHy8pOmkWmo= +github.com/sagernet/sing-tun v0.0.0-20220725225208-3b0c717db3f5 h1:i8L1e3A3v/UerH577y4wTghN8nooQOLuIP+Z+N4q5jA= +github.com/sagernet/sing-tun v0.0.0-20220725225208-3b0c717db3f5/go.mod h1:p7QbUBs2ejf6UQsiHyy1xGAWOk9JWQjZTHy8pOmkWmo= github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a h1:durFxTP1xsOMeDt8x0AV/9BXAPa8uMQRKzPaVkGSOS0= github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a/go.mod h1:VjqeHNWtDVoExWInXB7QsCeMp5RozlnJhMgfbW/n4I0= github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= diff --git a/route/router.go b/route/router.go index e8a125fd..f33cf862 100644 --- a/route/router.go +++ b/route/router.go @@ -524,7 +524,9 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad return E.New("missing supported outbound, closing connection") } if r.trafficController != nil { - conn = r.trafficController.RoutedConnection(ctx, conn, metadata, matchedRule) + trackerConn, tracker := r.trafficController.RoutedConnection(ctx, conn, metadata, matchedRule) + defer tracker.Leave() + conn = trackerConn } return detour.NewConnection(ctx, conn, metadata) } @@ -569,7 +571,9 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m return E.New("missing supported outbound, closing packet connection") } if r.trafficController != nil { - conn = r.trafficController.RoutedPacketConnection(ctx, conn, metadata, matchedRule) + trackerConn, tracker := r.trafficController.RoutedPacketConnection(ctx, conn, metadata, matchedRule) + defer tracker.Leave() + conn = trackerConn } return detour.NewPacketConnection(ctx, conn, metadata) } diff --git a/test/go.mod b/test/go.mod index 1a21b999..67a99de5 100644 --- a/test/go.mod +++ b/test/go.mod @@ -42,7 +42,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sagernet/sing-dns v0.0.0-20220724053927-eb8d0d542175 // indirect github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f // indirect - github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9 // indirect + github.com/sagernet/sing-tun v0.0.0-20220725225208-3b0c717db3f5 // indirect github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/vishvananda/netlink v1.1.0 // indirect diff --git a/test/go.sum b/test/go.sum index 52241716..dfa9055c 100644 --- a/test/go.sum +++ b/test/go.sum @@ -70,8 +70,8 @@ github.com/sagernet/sing-dns v0.0.0-20220724053927-eb8d0d542175 h1:YpacS9+rDFcLG github.com/sagernet/sing-dns v0.0.0-20220724053927-eb8d0d542175/go.mod h1:2A34p89do4H4E9Ke046cJCMTdVqmvsXGWXzRwgeO2TQ= github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f h1:F6yiuKbBoXgWiuoP7R0YA14pDEl3emxA1mL7M16Q7gc= github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f/go.mod h1:cDrLwa3zwY8AaW6a4sjipn4xgdIr3CT8TPqSW6iFOi0= -github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9 h1:kzEyWi2iJcq1oNeCCRrJ1Oh/ogbHR0CZRZ/HMxzy+mo= -github.com/sagernet/sing-tun v0.0.0-20220720051454-d35c334b46c9/go.mod h1:p7QbUBs2ejf6UQsiHyy1xGAWOk9JWQjZTHy8pOmkWmo= +github.com/sagernet/sing-tun v0.0.0-20220725225208-3b0c717db3f5 h1:i8L1e3A3v/UerH577y4wTghN8nooQOLuIP+Z+N4q5jA= +github.com/sagernet/sing-tun v0.0.0-20220725225208-3b0c717db3f5/go.mod h1:p7QbUBs2ejf6UQsiHyy1xGAWOk9JWQjZTHy8pOmkWmo= github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a h1:durFxTP1xsOMeDt8x0AV/9BXAPa8uMQRKzPaVkGSOS0= github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a/go.mod h1:VjqeHNWtDVoExWInXB7QsCeMp5RozlnJhMgfbW/n4I0= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=