44 "context"
55 "crypto/tls"
66 "net"
7+ "time"
78)
89
910type (
@@ -17,6 +18,8 @@ type Config interface {
1718 SetServerName (serverName string )
1819 NextProtos () []string
1920 SetNextProtos (nextProto []string )
21+ HandshakeTimeout () time.Duration
22+ SetHandshakeTimeout (timeout time.Duration )
2023 STDConfig () (* STDConfig , error )
2124 Client (conn net.Conn ) (Conn , error )
2225 Clone () Config
@@ -51,6 +54,11 @@ type Conn interface {
5154}
5255
5356func ClientHandshake (ctx context.Context , conn net.Conn , config Config ) (Conn , error ) {
57+ if handshakeTimeout := config .HandshakeTimeout (); handshakeTimeout > 0 {
58+ var cancel context.CancelFunc
59+ ctx , cancel = context .WithTimeout (ctx , handshakeTimeout )
60+ defer cancel ()
61+ }
5462 if compatServer , isCompat := config .(ConfigCompat ); isCompat {
5563 return compatServer .ClientHandshake (ctx , conn )
5664 }
@@ -66,6 +74,11 @@ func ClientHandshake(ctx context.Context, conn net.Conn, config Config) (Conn, e
6674}
6775
6876func ServerHandshake (ctx context.Context , conn net.Conn , config ServerConfig ) (Conn , error ) {
77+ if handshakeTimeout := config .HandshakeTimeout (); handshakeTimeout > 0 {
78+ var cancel context.CancelFunc
79+ ctx , cancel = context .WithTimeout (ctx , handshakeTimeout )
80+ defer cancel ()
81+ }
6982 if compatServer , isCompat := config .(ServerConfigCompat ); isCompat {
7083 return compatServer .ServerHandshake (ctx , conn )
7184 }
0 commit comments