Skip to content

Commit

Permalink
Fix: tls handshake requires a timeout (#1893)
Browse files Browse the repository at this point in the history
  • Loading branch information
Septrum101 authored Jan 15, 2022
1 parent 8f3385b commit 9732efe
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions constant/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
const (
DefaultTCPTimeout = 5 * time.Second
DefaultUDPTimeout = DefaultTCPTimeout
DefaultTLSTimeout = DefaultTCPTimeout
)

type Connection interface {
Expand Down
8 changes: 7 additions & 1 deletion transport/gun/gun.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gun

import (
"bufio"
"context"
"crypto/tls"
"encoding/binary"
"errors"
Expand All @@ -17,6 +18,7 @@ import (
"time"

"github.com/Dreamacro/clash/common/pool"
C "github.com/Dreamacro/clash/constant"

"go.uber.org/atomic"
"golang.org/x/net/http2"
Expand Down Expand Up @@ -173,7 +175,11 @@ func NewHTTP2Client(dialFn DialFn, tlsConfig *tls.Config) *http2.Transport {
}

cn := tls.Client(pconn, cfg)
if err := cn.Handshake(); err != nil {

// fix tls handshake not timeout
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
defer cancel()
if err := cn.HandshakeContext(ctx); err != nil {
pconn.Close()
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion transport/trojan/trojan.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package trojan

import (
"context"
"crypto/sha256"
"crypto/tls"
"encoding/binary"
Expand All @@ -12,6 +13,7 @@ import (
"sync"

"github.com/Dreamacro/clash/common/pool"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/transport/socks5"
"github.com/Dreamacro/clash/transport/vmess"
)
Expand Down Expand Up @@ -68,7 +70,11 @@ func (t *Trojan) StreamConn(conn net.Conn) (net.Conn, error) {
}

tlsConn := tls.Client(conn, tlsConfig)
if err := tlsConn.Handshake(); err != nil {

// fix tls handshake not timeout
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
defer cancel()
if err := tlsConn.HandshakeContext(ctx); err != nil {
return nil, err
}

Expand Down
9 changes: 8 additions & 1 deletion transport/vmess/tls.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package vmess

import (
"context"
"crypto/tls"
"net"

C "github.com/Dreamacro/clash/constant"
)

type TLSConfig struct {
Expand All @@ -19,6 +22,10 @@ func StreamTLSConn(conn net.Conn, cfg *TLSConfig) (net.Conn, error) {
}

tlsConn := tls.Client(conn, tlsConfig)
err := tlsConn.Handshake()

// fix tls handshake not timeout
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
defer cancel()
err := tlsConn.HandshakeContext(ctx)
return tlsConn, err
}

0 comments on commit 9732efe

Please sign in to comment.