@@ -2,6 +2,7 @@ package tcp
22
33import (
44 "context"
5+ gotls "crypto/tls"
56 "slices"
67 "strings"
78
@@ -15,10 +16,6 @@ import (
1516 "github.com/xtls/xray-core/transport/internet/tls"
1617)
1718
18- func IsFromMitm (str string ) bool {
19- return strings .ToLower (str ) == "frommitm"
20- }
21-
2219// Dial dials a new TCP connection to the given destination.
2320func Dial (ctx context.Context , dest net.Destination , streamSettings * internet.MemoryStreamConfig ) (stat.Connection , error ) {
2421 errors .LogInfo (ctx , "dialing TCP to " , dest )
@@ -30,14 +27,17 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
3027 if config := tls .ConfigFromStreamSettings (streamSettings ); config != nil {
3128 mitmServerName := session .MitmServerNameFromContext (ctx )
3229 mitmAlpn11 := session .MitmAlpn11FromContext (ctx )
33- tlsConfig := config .GetTLSConfig (tls .WithDestination (dest ))
34- if IsFromMitm (tlsConfig .ServerName ) {
35- tlsConfig .ServerName = mitmServerName
30+ var tlsConfig * gotls.Config
31+ if tls .IsFromMitm (config .ServerName ) {
32+ tlsConfig = config .GetTLSConfig (tls .WithOverrideName (mitmServerName ))
33+ } else {
34+ tlsConfig = config .GetTLSConfig (tls .WithDestination (dest ))
3635 }
36+
3737 isFromMitmVerify := false
3838 if r , ok := tlsConfig .Rand .(* tls.RandCarrier ); ok && len (r .VerifyPeerCertInNames ) > 0 {
3939 for i , name := range r .VerifyPeerCertInNames {
40- if IsFromMitm (name ) {
40+ if tls . IsFromMitm (name ) {
4141 isFromMitmVerify = true
4242 r .VerifyPeerCertInNames [0 ], r .VerifyPeerCertInNames [i ] = r .VerifyPeerCertInNames [i ], r .VerifyPeerCertInNames [0 ]
4343 r .VerifyPeerCertInNames = r .VerifyPeerCertInNames [1 :]
@@ -56,7 +56,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
5656 }
5757 }
5858 }
59- isFromMitmAlpn := len (tlsConfig .NextProtos ) == 1 && IsFromMitm (tlsConfig .NextProtos [0 ])
59+ isFromMitmAlpn := len (tlsConfig .NextProtos ) == 1 && tls . IsFromMitm (tlsConfig .NextProtos [0 ])
6060 if isFromMitmAlpn {
6161 if mitmAlpn11 {
6262 tlsConfig .NextProtos [0 ] = "http/1.1"
0 commit comments