Skip to content

Commit

Permalink
Added utls to http2 transport
Browse files Browse the repository at this point in the history
  • Loading branch information
HirbodBehnam authored and yuhan6665 committed Oct 13, 2022
1 parent ed9b99c commit 93c7ebe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
21 changes: 13 additions & 8 deletions transport/internet/http/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
}

httpSettings := streamSettings.ProtocolSettings.(*Config)
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
if tlsConfig == nil {
tlsConfigs := tls.ConfigFromStreamSettings(streamSettings)
if tlsConfigs == nil {
return nil, newError("TLS must be enabled for http transport.").AtWarning()
}
sockopt := streamSettings.SocketSettings
Expand Down Expand Up @@ -74,7 +74,12 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
return nil, err
}

cn := gotls.Client(pconn, tlsConfig)
var cn tls.Interface
if fingerprint, ok := tls.Fingerprints[tlsConfigs.Fingerprint]; ok {
cn = tls.UClient(pconn, tlsConfig, fingerprint).(*tls.UConn)
} else {
cn = tls.Client(pconn, tlsConfig).(*tls.Conn)
}
if err := cn.Handshake(); err != nil {
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
return nil, err
Expand All @@ -85,16 +90,16 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
return nil, err
}
}
state := cn.ConnectionState()
if p := state.NegotiatedProtocol; p != http2.NextProtoTLS {
return nil, newError("http2: unexpected ALPN protocol " + p + "; want q" + http2.NextProtoTLS).AtError()
negotiatedProtocol, negotiatedProtocolIsMutual := cn.NegotiatedProtocol()
if negotiatedProtocol != http2.NextProtoTLS {
return nil, newError("http2: unexpected ALPN protocol " + negotiatedProtocol + "; want q" + http2.NextProtoTLS).AtError()
}
if !state.NegotiatedProtocolIsMutual {
if !negotiatedProtocolIsMutual {
return nil, newError("http2: could not negotiate protocol mutually").AtError()
}
return cn, nil
},
TLSClientConfig: tlsConfig.GetTLSConfig(tls.WithDestination(dest)),
TLSClientConfig: tlsConfigs.GetTLSConfig(tls.WithDestination(dest)),
}

if httpSettings.IdleTimeout > 0 || httpSettings.HealthCheckTimeout > 0 {
Expand Down
17 changes: 17 additions & 0 deletions transport/internet/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func (c *Conn) HandshakeAddress() net.Address {
return net.ParseAddress(state.ServerName)
}

func (c *Conn) NegotiatedProtocol() (name string, mutual bool) {
state := c.ConnectionState()
return state.NegotiatedProtocol, state.NegotiatedProtocolIsMutual
}

// Client initiates a TLS client handshake on the given connection.
func Client(c net.Conn, config *tls.Config) net.Conn {
tlsConn := tls.Client(c, config)
Expand Down Expand Up @@ -61,6 +66,11 @@ func (c *UConn) HandshakeAddress() net.Address {
return net.ParseAddress(state.ServerName)
}

func (c *UConn) NegotiatedProtocol() (name string, mutual bool) {
state := c.ConnectionState()
return state.NegotiatedProtocol, state.NegotiatedProtocolIsMutual
}

func UClient(c net.Conn, config *tls.Config, fingerprint *utls.ClientHelloID) net.Conn {
utlsConn := utls.UClient(c, copyConfig(config), *fingerprint)
return &UConn{UConn: utlsConn}
Expand All @@ -80,3 +90,10 @@ var Fingerprints = map[string]*utls.ClientHelloID{
"safari": &utls.HelloIOS_Auto,
"randomized": &utls.HelloRandomized,
}

type Interface interface {
net.Conn
Handshake() error
VerifyHostname(host string) error
NegotiatedProtocol() (name string, mutual bool)
}

1 comment on commit 93c7ebe

@lambdaxyzt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haji , @HirbodBehnam you are so great , tech me master , LOL !!

Please sign in to comment.