Add shadow-tls support

This commit is contained in:
世界 2023-02-21 19:19:47 +08:00
parent 4d5c3195d2
commit d6c2a9aab7
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
25 changed files with 1311 additions and 130 deletions

View file

@ -23,13 +23,18 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
tlsConfig := config.GetTLSConfig(tls.WithDestination(dest))
if fingerprint := tls.GetFingerprint(config.Fingerprint); fingerprint != nil {
conn = tls.UClient(conn, tlsConfig, fingerprint)
if err := conn.(*tls.UConn).Handshake(); err != nil {
return nil, err
}
customClient, loaded := tls.CustomClientFromContext(ctx)
if loaded {
conn = customClient(conn, tlsConfig)
} else {
conn = tls.Client(conn, tlsConfig)
if fingerprint := tls.GetFingerprint(config.Fingerprint); fingerprint != nil {
conn = tls.UClient(conn, tlsConfig, fingerprint)
if err := conn.(*tls.UConn).Handshake(); err != nil {
return nil, err
}
} else {
conn = tls.Client(conn, tlsConfig)
}
}
} else if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil {
xtlsConfig := config.GetXTLSConfig(xtls.WithDestination(dest))

View file

@ -0,0 +1,21 @@
package tls
import (
"context"
"crypto/tls"
"github.com/xtls/xray-core/common/net"
)
type customClientKey struct{}
type CustomClientFunc func(conn net.Conn, config *tls.Config) net.Conn
func CustomClientFromContext(ctx context.Context) (CustomClientFunc, bool) {
client, loaded := ctx.Value(customClientKey{}).(CustomClientFunc)
return client, loaded
}
func ContextWithCustomClient(ctx context.Context, customClient CustomClientFunc) context.Context {
return context.WithValue(ctx, customClientKey{}, customClient)
}