Add default tcp keep alive settings

This commit is contained in:
世界 2022-07-24 21:29:39 +08:00
parent 9e9e6f7ee6
commit 7d8a7c5c7d
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 15 additions and 5 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/control" "github.com/sagernet/sing/common/control"
M "github.com/sagernet/sing/common/metadata" 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) { 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) { func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {

View file

@ -3,8 +3,9 @@ package constant
import "time" import "time"
const ( const (
DefaultTCPTimeout = 5 * time.Second DefaultTCPTimeout = 5 * time.Second
ReadPayloadTimeout = 300 * time.Millisecond DefaultTCPKeepAlivePeriod = 20 * time.Second
URLTestTimeout = DefaultTCPTimeout ReadPayloadTimeout = 300 * time.Millisecond
DefaultURLTestInterval = 1 * time.Minute URLTestTimeout = DefaultTCPTimeout
DefaultURLTestInterval = 1 * time.Minute
) )