github.com/sagernet/sing-box@v1.9.0-rc.20/common/sniff/tls.go (about)

     1  package sniff
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"io"
     7  
     8  	"github.com/sagernet/sing-box/adapter"
     9  	C "github.com/sagernet/sing-box/constant"
    10  	"github.com/sagernet/sing/common/bufio"
    11  )
    12  
    13  func TLSClientHello(ctx context.Context, reader io.Reader) (*adapter.InboundContext, error) {
    14  	var clientHello *tls.ClientHelloInfo
    15  	err := tls.Server(bufio.NewReadOnlyConn(reader), &tls.Config{
    16  		GetConfigForClient: func(argHello *tls.ClientHelloInfo) (*tls.Config, error) {
    17  			clientHello = argHello
    18  			return nil, nil
    19  		},
    20  	}).HandshakeContext(ctx)
    21  	if clientHello != nil {
    22  		return &adapter.InboundContext{Protocol: C.ProtocolTLS, Domain: clientHello.ServerName}, nil
    23  	}
    24  	return nil, err
    25  }