mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package sniff
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"io"
|
|
"slices"
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
"github.com/sagernet/sing/common/bufio"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
)
|
|
|
|
const extensionEncryptedClientHello = 0xfe0d
|
|
|
|
func TLSClientHello(ctx context.Context, metadata *adapter.InboundContext, reader io.Reader) error {
|
|
var clientHello *tls.ClientHelloInfo
|
|
err := tls.Server(bufio.NewReadOnlyConn(reader), &tls.Config{
|
|
GetConfigForClient: func(argHello *tls.ClientHelloInfo) (*tls.Config, error) {
|
|
clientHello = argHello
|
|
return nil, nil
|
|
},
|
|
}).HandshakeContext(ctx)
|
|
if clientHello != nil {
|
|
metadata.Protocol = C.ProtocolTLS
|
|
if domain := M.ParseSocksaddrHostPort(clientHello.ServerName, 0).Fqdn; domain != "" && (domain != metadata.ECHPublicName || !slices.Contains(clientHello.Extensions, extensionEncryptedClientHello)) {
|
|
metadata.Domain = domain
|
|
}
|
|
return nil
|
|
}
|
|
if errors.Is(err, io.ErrUnexpectedEOF) {
|
|
return E.Cause1(ErrNeedMoreData, err)
|
|
} else {
|
|
return err
|
|
}
|
|
}
|