From d06fd03dd8046ce6bd11e22424163313de43eedc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 31 Jul 2022 09:46:05 +0800 Subject: [PATCH] Fix tcp keep alive --- common/dialer/default.go | 11 +---------- common/mux/service.go | 4 ++-- constant/timeout.go | 1 - inbound/default.go | 2 -- 4 files changed, 3 insertions(+), 15 deletions(-) diff --git a/common/dialer/default.go b/common/dialer/default.go index 6cb00139..efe7d574 100644 --- a/common/dialer/default.go +++ b/common/dialer/default.go @@ -9,7 +9,6 @@ import ( "github.com/sagernet/sing-box/common/warning" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/option" - "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/control" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" @@ -113,15 +112,7 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia } func (d *DefaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) { - conn, err := d.Dialer.DialContext(ctx, network, address.Unwrap().String()) - if err != nil { - return nil, err - } - if tcpConn, isTCP := common.Cast[*net.TCPConn](conn); isTCP { - tcpConn.SetKeepAlive(true) - tcpConn.SetKeepAlivePeriod(C.TCPKeepAlivePeriod) - } - return conn, nil + return d.Dialer.DialContext(ctx, network, address.Unwrap().String()) } func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { diff --git a/common/mux/service.go b/common/mux/service.go index f7b2ab4d..4f329a1c 100644 --- a/common/mux/service.go +++ b/common/mux/service.go @@ -38,8 +38,8 @@ func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Ha go func() { logger.InfoContext(ctx, "inbound multiplex connection to ", metadata.Destination) hErr := router.RouteConnection(ctx, &ServerConn{ExtendedConn: bufio.NewExtendedConn(stream)}, metadata) + stream.Close() if hErr != nil { - stream.Close() errorHandler.NewError(ctx, hErr) } }() @@ -54,8 +54,8 @@ func NewConnection(ctx context.Context, router adapter.Router, errorHandler E.Ha packetConn = &ServerPacketAddrConn{ExtendedConn: bufio.NewExtendedConn(stream)} } hErr := router.RoutePacketConnection(ctx, packetConn, metadata) + stream.Close() if hErr != nil { - stream.Close() errorHandler.NewError(ctx, hErr) } }() diff --git a/constant/timeout.go b/constant/timeout.go index 96d8ce32..dd5db92a 100644 --- a/constant/timeout.go +++ b/constant/timeout.go @@ -4,7 +4,6 @@ import "time" const ( TCPTimeout = 5 * time.Second - TCPKeepAlivePeriod = 30 * time.Second ReadPayloadTimeout = 300 * time.Millisecond DNSTimeout = 10 * time.Second QUICTimeout = 30 * time.Second diff --git a/inbound/default.go b/inbound/default.go index 0c3ea90e..dd73a6c4 100644 --- a/inbound/default.go +++ b/inbound/default.go @@ -154,8 +154,6 @@ func (a *myInboundAdapter) loopTCPIn() { if err != nil { return } - conn.SetKeepAlive(true) - conn.SetKeepAlivePeriod(C.TCPKeepAlivePeriod) go func() { ctx := log.ContextWithNewID(a.ctx) var metadata adapter.InboundContext