11package tls
22
33import (
4+ "context"
45 "crypto/rand"
56 "crypto/tls"
67 "math/big"
8+ "time"
79
810 utls "github.com/refraction-networking/utls"
911 "github.com/xtls/xray-core/common/buf"
@@ -14,7 +16,7 @@ import (
1416
1517type Interface interface {
1618 net.Conn
17- Handshake ( ) error
19+ HandshakeContext ( ctx context. Context ) error
1820 VerifyHostname (host string ) error
1921 NegotiatedProtocol () (name string , mutual bool )
2022}
@@ -25,15 +27,25 @@ type Conn struct {
2527 * tls.Conn
2628}
2729
30+ const tlsCloseTimeout = 250 * time .Millisecond
31+
32+ func (c * Conn ) Close () error {
33+ timer := time .AfterFunc (tlsCloseTimeout , func () {
34+ c .Conn .NetConn ().Close ()
35+ })
36+ defer timer .Stop ()
37+ return c .Conn .Close ()
38+ }
39+
2840func (c * Conn ) WriteMultiBuffer (mb buf.MultiBuffer ) error {
2941 mb = buf .Compact (mb )
3042 mb , err := buf .WriteMultiBuffer (c , mb )
3143 buf .ReleaseMulti (mb )
3244 return err
3345}
3446
35- func (c * Conn ) HandshakeAddress ( ) net.Address {
36- if err := c .Handshake ( ); err != nil {
47+ func (c * Conn ) HandshakeAddressContext ( ctx context. Context ) net.Address {
48+ if err := c .HandshakeContext ( ctx ); err != nil {
3749 return nil
3850 }
3951 state := c .ConnectionState ()
@@ -64,8 +76,16 @@ type UConn struct {
6476 * utls.UConn
6577}
6678
67- func (c * UConn ) HandshakeAddress () net.Address {
68- if err := c .Handshake (); err != nil {
79+ func (c * UConn ) Close () error {
80+ timer := time .AfterFunc (tlsCloseTimeout , func () {
81+ c .Conn .NetConn ().Close ()
82+ })
83+ defer timer .Stop ()
84+ return c .Conn .Close ()
85+ }
86+
87+ func (c * UConn ) HandshakeAddressContext (ctx context.Context ) net.Address {
88+ if err := c .HandshakeContext (ctx ); err != nil {
6989 return nil
7090 }
7191 state := c .ConnectionState ()
@@ -77,7 +97,7 @@ func (c *UConn) HandshakeAddress() net.Address {
7797
7898// WebsocketHandshake basically calls UConn.Handshake inside it but it will only send
7999// http/1.1 in its ALPN.
80- func (c * UConn ) WebsocketHandshake ( ) error {
100+ func (c * UConn ) WebsocketHandshakeContext ( ctx context. Context ) error {
81101 // Build the handshake state. This will apply every variable of the TLS of the
82102 // fingerprint in the UConn
83103 if err := c .BuildHandshakeState (); err != nil {
@@ -99,7 +119,7 @@ func (c *UConn) WebsocketHandshake() error {
99119 if err := c .BuildHandshakeState (); err != nil {
100120 return err
101121 }
102- return c .Handshake ( )
122+ return c .HandshakeContext ( ctx )
103123}
104124
105125func (c * UConn ) NegotiatedProtocol () (name string , mutual bool ) {
@@ -118,7 +138,7 @@ func copyConfig(c *tls.Config) *utls.Config {
118138 ServerName : c .ServerName ,
119139 InsecureSkipVerify : c .InsecureSkipVerify ,
120140 VerifyPeerCertificate : c .VerifyPeerCertificate ,
121- KeyLogWriter : c .KeyLogWriter ,
141+ KeyLogWriter : c .KeyLogWriter ,
122142 }
123143}
124144
0 commit comments