mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
When authentication failed, the reality client returned the error and sent the fallback request from a goroutine on the same connection. The TLS dialer closes the connection when the handshake returns an error, so the request always failed with "use of closed network connection" and the client closed the connection right after the target's handshake. Send the request within the handshake context before returning the error, so the caller owns the connection again once it gets the error.
327 lines
8.8 KiB
Go
327 lines
8.8 KiB
Go
//go:build with_utls
|
|
|
|
package tls
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/ecdh"
|
|
"crypto/ed25519"
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
mRand "math/rand"
|
|
"net"
|
|
"net/http"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
"unsafe"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
"github.com/sagernet/sing-box/option"
|
|
"github.com/sagernet/sing/common/debug"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
"github.com/sagernet/sing/common/logger"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
aTLS "github.com/sagernet/sing/common/tls"
|
|
|
|
utls "github.com/metacubex/utls"
|
|
"golang.org/x/crypto/hkdf"
|
|
"golang.org/x/net/http2"
|
|
)
|
|
|
|
var _ ConfigCompat = (*RealityClientConfig)(nil)
|
|
|
|
type RealityClientConfig struct {
|
|
uClient *UTLSClientConfig
|
|
publicKey []byte
|
|
shortID [8]byte
|
|
}
|
|
|
|
func NewRealityClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions) (Config, error) {
|
|
return newRealityClient(ctx, logger, serverAddress, options, false)
|
|
}
|
|
|
|
func newRealityClient(ctx context.Context, logger logger.ContextLogger, serverAddress string, options option.OutboundTLSOptions, allowEmptyServerName bool) (Config, error) {
|
|
if options.UTLS == nil || !options.UTLS.Enabled {
|
|
return nil, E.New("uTLS is required by reality client")
|
|
}
|
|
if options.Spoof != "" || options.SpoofMethod != "" {
|
|
return nil, E.New("spoof is unsupported in reality")
|
|
}
|
|
|
|
uClient, err := newUTLSClient(ctx, logger, serverAddress, options, allowEmptyServerName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
publicKey, err := base64.RawURLEncoding.DecodeString(options.Reality.PublicKey)
|
|
if err != nil {
|
|
return nil, E.Cause(err, "decode public_key")
|
|
}
|
|
if len(publicKey) != 32 {
|
|
return nil, E.New("invalid public_key")
|
|
}
|
|
var shortID [8]byte
|
|
decodedLen, err := hex.Decode(shortID[:], []byte(options.Reality.ShortID))
|
|
if err != nil {
|
|
return nil, E.Cause(err, "decode short_id")
|
|
}
|
|
if decodedLen > 8 {
|
|
return nil, E.New("invalid short_id")
|
|
}
|
|
|
|
var config Config = &RealityClientConfig{uClient.(*UTLSClientConfig), publicKey, shortID}
|
|
if options.KernelRx || options.KernelTx {
|
|
if !C.IsLinux {
|
|
return nil, E.New("kTLS is only supported on Linux")
|
|
}
|
|
config = &KTLSClientConfig{
|
|
Config: config,
|
|
logger: logger,
|
|
kernelTx: options.KernelTx,
|
|
kernelRx: options.KernelRx,
|
|
}
|
|
}
|
|
return config, nil
|
|
}
|
|
|
|
func (e *RealityClientConfig) ServerName() string {
|
|
return e.uClient.ServerName()
|
|
}
|
|
|
|
func (e *RealityClientConfig) SetServerName(serverName string) {
|
|
e.uClient.SetServerName(serverName)
|
|
}
|
|
|
|
func (e *RealityClientConfig) NextProtos() []string {
|
|
return e.uClient.NextProtos()
|
|
}
|
|
|
|
func (e *RealityClientConfig) SetNextProtos(nextProto []string) {
|
|
e.uClient.SetNextProtos(nextProto)
|
|
}
|
|
|
|
func (e *RealityClientConfig) HandshakeTimeout() time.Duration {
|
|
return e.uClient.HandshakeTimeout()
|
|
}
|
|
|
|
func (e *RealityClientConfig) SetHandshakeTimeout(timeout time.Duration) {
|
|
e.uClient.SetHandshakeTimeout(timeout)
|
|
}
|
|
|
|
func (e *RealityClientConfig) STDConfig() (*STDConfig, error) {
|
|
return nil, E.New("unsupported usage for reality")
|
|
}
|
|
|
|
func (e *RealityClientConfig) Client(conn net.Conn) (Conn, error) {
|
|
return ClientHandshake(context.Background(), conn, e)
|
|
}
|
|
|
|
func (e *RealityClientConfig) ClientHandshake(ctx context.Context, conn net.Conn) (aTLS.Conn, error) {
|
|
uConfig := e.uClient.config.Clone()
|
|
var nowTime time.Time
|
|
if uConfig.Time != nil {
|
|
nowTime = uConfig.Time()
|
|
} else {
|
|
nowTime = time.Now()
|
|
}
|
|
verifier := &realityVerifier{
|
|
serverName: e.uClient.ServerName(),
|
|
rootCAs: uConfig.RootCAs,
|
|
currentTime: nowTime,
|
|
}
|
|
uConfig.InsecureSkipVerify = true
|
|
uConfig.SessionTicketsDisabled = true
|
|
uConfig.VerifyPeerCertificate = verifier.VerifyPeerCertificate
|
|
uConn := utls.UClient(conn, uConfig, e.uClient.id)
|
|
verifier.UConn = uConn
|
|
err := uConn.BuildHandshakeState()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
hello := uConn.HandshakeState.Hello
|
|
hello.SessionId = make([]byte, 32)
|
|
copy(hello.Raw[39:], hello.SessionId)
|
|
|
|
hello.SessionId[0] = 1
|
|
hello.SessionId[1] = 8
|
|
hello.SessionId[2] = 2
|
|
binary.BigEndian.PutUint32(hello.SessionId[4:], uint32(nowTime.Unix()))
|
|
copy(hello.SessionId[8:], e.shortID[:])
|
|
if debug.Enabled {
|
|
fmt.Printf("REALITY hello.sessionId[:16]: %v\n", hello.SessionId[:16])
|
|
}
|
|
publicKey, err := ecdh.X25519().NewPublicKey(e.publicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keyShareKeys := uConn.HandshakeState.State13.KeyShareKeys
|
|
if keyShareKeys == nil {
|
|
return nil, E.New("nil KeyShareKeys")
|
|
}
|
|
ecdheKey := keyShareKeys.Ecdhe
|
|
if ecdheKey == nil {
|
|
return nil, E.New("nil ecdheKey")
|
|
}
|
|
authKey, err := ecdheKey.ECDH(publicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if authKey == nil {
|
|
return nil, E.New("nil auth_key")
|
|
}
|
|
verifier.authKey = authKey
|
|
_, err = hkdf.New(sha256.New, authKey, hello.Random[:20], []byte("REALITY")).Read(authKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
aesBlock, _ := aes.NewCipher(authKey)
|
|
aesGcmCipher, _ := cipher.NewGCM(aesBlock)
|
|
aesGcmCipher.Seal(hello.SessionId[:0], hello.Random[20:], hello.SessionId[:16], hello.Raw)
|
|
copy(hello.Raw[39:], hello.SessionId)
|
|
if debug.Enabled {
|
|
fmt.Printf("REALITY hello.sessionId: %v\n", hello.SessionId)
|
|
fmt.Printf("REALITY uConn.AuthKey: %v\n", authKey)
|
|
}
|
|
|
|
err = uConn.HandshakeContext(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if debug.Enabled {
|
|
fmt.Printf("REALITY Conn.Verified: %v\n", verifier.verified)
|
|
}
|
|
|
|
if !verifier.verified {
|
|
realityClientFallback(ctx, uConn, e.uClient.ServerName(), e.uClient.id)
|
|
return nil, E.New("reality verification failed")
|
|
}
|
|
|
|
return &realityClientConnWrapper{uConn}, nil
|
|
}
|
|
|
|
func realityClientFallback(ctx context.Context, uConn net.Conn, serverName string, fingerprint utls.ClientHelloID) {
|
|
defer uConn.Close()
|
|
client := &http.Client{
|
|
Transport: &http2.Transport{
|
|
DialTLSContext: func(ctx context.Context, network, addr string, config *tls.Config) (net.Conn, error) {
|
|
return uConn, nil
|
|
},
|
|
},
|
|
}
|
|
serverAddress := M.ParseSocksaddrHostPort(serverName, 0)
|
|
host := serverAddress.AddrString()
|
|
if serverAddress.IsIPv6() {
|
|
host = "[" + host + "]"
|
|
}
|
|
request, err := http.NewRequestWithContext(ctx, "GET", "https://"+host, nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
request.Header.Set("User-Agent", fingerprint.Client)
|
|
request.AddCookie(&http.Cookie{Name: "padding", Value: strings.Repeat("0", mRand.Intn(32)+30)})
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return
|
|
}
|
|
_, _ = io.Copy(io.Discard, response.Body)
|
|
response.Body.Close()
|
|
}
|
|
|
|
func (e *RealityClientConfig) Clone() Config {
|
|
return &RealityClientConfig{
|
|
e.uClient.Clone().(*UTLSClientConfig),
|
|
e.publicKey,
|
|
e.shortID,
|
|
}
|
|
}
|
|
|
|
type realityVerifier struct {
|
|
*utls.UConn
|
|
serverName string
|
|
rootCAs *x509.CertPool
|
|
currentTime time.Time
|
|
authKey []byte
|
|
verified bool
|
|
}
|
|
|
|
func (c *realityVerifier) VerifyPeerCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
|
p, _ := reflect.TypeFor[utls.Conn]().FieldByName("peerCertificates")
|
|
certs := *(*([]*x509.Certificate))(unsafe.Add(unsafe.Pointer(c.Conn), p.Offset))
|
|
if pub, ok := certs[0].PublicKey.(ed25519.PublicKey); ok {
|
|
h := hmac.New(sha512.New, c.authKey)
|
|
h.Write(pub)
|
|
if bytes.Equal(h.Sum(nil), certs[0].Signature) {
|
|
c.verified = true
|
|
return nil
|
|
}
|
|
}
|
|
opts := x509.VerifyOptions{
|
|
Roots: c.rootCAs,
|
|
CurrentTime: c.currentTime,
|
|
DNSName: c.serverName,
|
|
Intermediates: x509.NewCertPool(),
|
|
}
|
|
for _, cert := range certs[1:] {
|
|
opts.Intermediates.AddCert(cert)
|
|
}
|
|
if _, err := certs[0].Verify(opts); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type realityClientConnWrapper struct {
|
|
*utls.UConn
|
|
}
|
|
|
|
func (c *realityClientConnWrapper) ConnectionState() tls.ConnectionState {
|
|
state := c.Conn.ConnectionState()
|
|
//nolint:staticcheck
|
|
return tls.ConnectionState{
|
|
Version: state.Version,
|
|
HandshakeComplete: state.HandshakeComplete,
|
|
DidResume: state.DidResume,
|
|
CipherSuite: state.CipherSuite,
|
|
NegotiatedProtocol: state.NegotiatedProtocol,
|
|
NegotiatedProtocolIsMutual: state.NegotiatedProtocolIsMutual,
|
|
ServerName: state.ServerName,
|
|
PeerCertificates: state.PeerCertificates,
|
|
VerifiedChains: state.VerifiedChains,
|
|
SignedCertificateTimestamps: state.SignedCertificateTimestamps,
|
|
OCSPResponse: state.OCSPResponse,
|
|
TLSUnique: state.TLSUnique,
|
|
}
|
|
}
|
|
|
|
func (c *realityClientConnWrapper) Upstream() any {
|
|
return c.UConn
|
|
}
|
|
|
|
// Due to low implementation quality, the reality server intercepted half close and caused memory leaks.
|
|
// We fixed it by calling Close() directly.
|
|
func (c *realityClientConnWrapper) CloseWrite() error {
|
|
return c.Close()
|
|
}
|
|
|
|
func (c *realityClientConnWrapper) ReaderReplaceable() bool {
|
|
return true
|
|
}
|
|
|
|
func (c *realityClientConnWrapper) WriterReplaceable() bool {
|
|
return false
|
|
}
|