diff --git a/adapters/outbound/direct.go b/adapters/outbound/direct.go index 4a8a10e3..70a3f9eb 100644 --- a/adapters/outbound/direct.go +++ b/adapters/outbound/direct.go @@ -32,7 +32,7 @@ func (d *Direct) Type() C.AdapterType { } func (d *Direct) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { - c, err := net.Dial("tcp", net.JoinHostPort(metadata.String(), metadata.Port)) + c, err := net.DialTimeout("tcp", net.JoinHostPort(metadata.String(), metadata.Port), tcpTimeout) if err != nil { return } diff --git a/adapters/outbound/shadowsocks.go b/adapters/outbound/shadowsocks.go index 55056359..3943c060 100644 --- a/adapters/outbound/shadowsocks.go +++ b/adapters/outbound/shadowsocks.go @@ -54,7 +54,7 @@ func (ss *ShadowSocks) Type() C.AdapterType { } func (ss *ShadowSocks) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { - c, err := net.Dial("tcp", ss.server) + c, err := net.DialTimeout("tcp", ss.server, tcpTimeout) if err != nil { return nil, fmt.Errorf("%s connect error", ss.server) } diff --git a/adapters/outbound/socks5.go b/adapters/outbound/socks5.go index 1261f810..4fe1cfe1 100644 --- a/adapters/outbound/socks5.go +++ b/adapters/outbound/socks5.go @@ -46,7 +46,7 @@ func (ss *Socks5) Type() C.AdapterType { } func (ss *Socks5) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { - c, err := net.Dial("tcp", ss.addr) + c, err := net.DialTimeout("tcp", ss.addr, tcpTimeout) if err != nil { return nil, fmt.Errorf("%s connect error", ss.addr) } diff --git a/adapters/outbound/util.go b/adapters/outbound/util.go index ea8d6958..f1292546 100644 --- a/adapters/outbound/util.go +++ b/adapters/outbound/util.go @@ -10,6 +10,10 @@ import ( C "github.com/Dreamacro/clash/constant" ) +const ( + tcpTimeout = 5 * time.Second +) + // DelayTest get the delay for the specified URL func DelayTest(proxy C.Proxy, url string) (t int16, err error) { addr, err := urlToMetadata(url) diff --git a/adapters/outbound/vmess.go b/adapters/outbound/vmess.go index 075a9fc0..ea2bce5d 100644 --- a/adapters/outbound/vmess.go +++ b/adapters/outbound/vmess.go @@ -49,7 +49,7 @@ func (ss *Vmess) Type() C.AdapterType { } func (ss *Vmess) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { - c, err := net.Dial("tcp", ss.server) + c, err := net.DialTimeout("tcp", ss.server, tcpTimeout) if err != nil { return nil, fmt.Errorf("%s connect error", ss.server) }