diff --git a/common/dialer/default.go b/common/dialer/default.go index c9f1df89..4cf13883 100644 --- a/common/dialer/default.go +++ b/common/dialer/default.go @@ -9,6 +9,7 @@ import ( "github.com/sagernet/sing-box/adapter" 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" @@ -69,7 +70,15 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia } func (d *DefaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) { - return d.Dialer.DialContext(ctx, network, address.Unwrap().String()) + 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.DefaultTCPKeepAlivePeriod) + } + return conn, nil } func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { diff --git a/constant/timeout.go b/constant/timeout.go index 26037dfa..57cced2c 100644 --- a/constant/timeout.go +++ b/constant/timeout.go @@ -3,8 +3,9 @@ package constant import "time" const ( - DefaultTCPTimeout = 5 * time.Second - ReadPayloadTimeout = 300 * time.Millisecond - URLTestTimeout = DefaultTCPTimeout - DefaultURLTestInterval = 1 * time.Minute + DefaultTCPTimeout = 5 * time.Second + DefaultTCPKeepAlivePeriod = 20 * time.Second + ReadPayloadTimeout = 300 * time.Millisecond + URLTestTimeout = DefaultTCPTimeout + DefaultURLTestInterval = 1 * time.Minute )