From 8c143feec8ce11dcc280cd9fa77a827ea6dee9f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 30 Oct 2024 13:09:05 +0800 Subject: [PATCH] Increase timeouts --- common/dialer/default.go | 2 +- common/urltest/urltest.go | 2 ++ constant/timeout.go | 3 ++- experimental/clashapi/server_resources.go | 4 ++-- experimental/libbox/http.go | 6 +++--- route/router_geo_resources.go | 5 ++--- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/common/dialer/default.go b/common/dialer/default.go index 488e8000..b4bca55e 100644 --- a/common/dialer/default.go +++ b/common/dialer/default.go @@ -81,7 +81,7 @@ func NewDefault(router adapter.Router, options option.DialerOptions) (*DefaultDi if options.ConnectTimeout != 0 { dialer.Timeout = time.Duration(options.ConnectTimeout) } else { - dialer.Timeout = C.TCPTimeout + dialer.Timeout = C.TCPConnectTimeout } // TODO: Add an option to customize the keep alive period dialer.KeepAlive = C.TCPKeepAliveInitial diff --git a/common/urltest/urltest.go b/common/urltest/urltest.go index 001f2e15..9efd0404 100644 --- a/common/urltest/urltest.go +++ b/common/urltest/urltest.go @@ -8,6 +8,7 @@ import ( "sync" "time" + C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" @@ -113,6 +114,7 @@ func URLTest(ctx context.Context, link string, detour N.Dialer) (t uint16, err e CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, + Timeout: C.TCPTimeout, } defer client.CloseIdleConnections() resp, err := client.Do(req.WithContext(ctx)) diff --git a/constant/timeout.go b/constant/timeout.go index b270a050..67ae6f66 100644 --- a/constant/timeout.go +++ b/constant/timeout.go @@ -5,7 +5,8 @@ import "time" const ( TCPKeepAliveInitial = 10 * time.Minute TCPKeepAliveInterval = 75 * time.Second - TCPTimeout = 5 * time.Second + TCPConnectTimeout = 5 * time.Second + TCPTimeout = 15 * time.Second ReadPayloadTimeout = 300 * time.Millisecond DNSTimeout = 10 * time.Second QUICTimeout = 30 * time.Second diff --git a/experimental/clashapi/server_resources.go b/experimental/clashapi/server_resources.go index d6d22b53..a5c79e0c 100644 --- a/experimental/clashapi/server_resources.go +++ b/experimental/clashapi/server_resources.go @@ -9,9 +9,9 @@ import ( "os" "path/filepath" "strings" - "time" "github.com/sagernet/sing-box/adapter" + C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" @@ -60,7 +60,7 @@ func (s *Server) downloadExternalUI() error { httpClient := &http.Client{ Transport: &http.Transport{ ForceAttemptHTTP2: true, - TLSHandshakeTimeout: 5 * time.Second, + TLSHandshakeTimeout: C.TCPTimeout, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { return detour.DialContext(ctx, network, M.ParseSocksaddr(addr)) }, diff --git a/experimental/libbox/http.go b/experimental/libbox/http.go index 87c5bed6..3e1a04d0 100644 --- a/experimental/libbox/http.go +++ b/experimental/libbox/http.go @@ -17,8 +17,8 @@ import ( "os" "strconv" "sync" - "time" + C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/bufio" E "github.com/sagernet/sing/common/exceptions" @@ -69,8 +69,9 @@ type httpClient struct { func NewHTTPClient() HTTPClient { client := new(httpClient) - client.client.Timeout = 15 * time.Second client.client.Transport = &client.transport + client.transport.ForceAttemptHTTP2 = true + client.transport.TLSHandshakeTimeout = C.TCPTimeout client.transport.TLSClientConfig = &client.tls client.transport.DisableKeepAlives = true return client @@ -127,7 +128,6 @@ func (c *httpClient) TrySocks5(port int32) { } func (c *httpClient) KeepAlive() { - c.transport.ForceAttemptHTTP2 = true c.transport.DisableKeepAlives = false } diff --git a/route/router_geo_resources.go b/route/router_geo_resources.go index 14364d21..4760bba3 100644 --- a/route/router_geo_resources.go +++ b/route/router_geo_resources.go @@ -7,7 +7,6 @@ import ( "net/http" "os" "path/filepath" - "time" "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/common/geoip" @@ -158,7 +157,7 @@ func (r *Router) downloadGeoIPDatabase(savePath string) error { httpClient := &http.Client{ Transport: &http.Transport{ ForceAttemptHTTP2: true, - TLSHandshakeTimeout: 5 * time.Second, + TLSHandshakeTimeout: C.TCPTimeout, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { return detour.DialContext(ctx, network, M.ParseSocksaddr(addr)) }, @@ -213,7 +212,7 @@ func (r *Router) downloadGeositeDatabase(savePath string) error { httpClient := &http.Client{ Transport: &http.Transport{ ForceAttemptHTTP2: true, - TLSHandshakeTimeout: 5 * time.Second, + TLSHandshakeTimeout: C.TCPTimeout, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { return detour.DialContext(ctx, network, M.ParseSocksaddr(addr)) },