utls_client.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //go:build with_utls
  2. package tls
  3. import (
  4. "crypto/tls"
  5. "crypto/x509"
  6. "net"
  7. "net/netip"
  8. "os"
  9. "github.com/sagernet/sing-box/adapter"
  10. "github.com/sagernet/sing-box/option"
  11. E "github.com/sagernet/sing/common/exceptions"
  12. utls "github.com/sagernet/utls"
  13. )
  14. type UTLSClientConfig struct {
  15. config *utls.Config
  16. id utls.ClientHelloID
  17. }
  18. func (e *UTLSClientConfig) ServerName() string {
  19. return e.config.ServerName
  20. }
  21. func (e *UTLSClientConfig) SetServerName(serverName string) {
  22. e.config.ServerName = serverName
  23. }
  24. func (e *UTLSClientConfig) NextProtos() []string {
  25. return e.config.NextProtos
  26. }
  27. func (e *UTLSClientConfig) SetNextProtos(nextProto []string) {
  28. e.config.NextProtos = nextProto
  29. }
  30. func (e *UTLSClientConfig) Config() (*STDConfig, error) {
  31. return nil, E.New("unsupported usage for uTLS")
  32. }
  33. func (e *UTLSClientConfig) Client(conn net.Conn) Conn {
  34. return &utlsConnWrapper{utls.UClient(conn, e.config.Clone(), e.id)}
  35. }
  36. func (e *UTLSClientConfig) SetSessionIDGenerator(generator func(clientHello []byte, sessionID []byte) error) {
  37. e.config.SessionIDGenerator = generator
  38. }
  39. type utlsConnWrapper struct {
  40. *utls.UConn
  41. }
  42. func (c *utlsConnWrapper) ConnectionState() tls.ConnectionState {
  43. state := c.Conn.ConnectionState()
  44. return tls.ConnectionState{
  45. Version: state.Version,
  46. HandshakeComplete: state.HandshakeComplete,
  47. DidResume: state.DidResume,
  48. CipherSuite: state.CipherSuite,
  49. NegotiatedProtocol: state.NegotiatedProtocol,
  50. NegotiatedProtocolIsMutual: state.NegotiatedProtocolIsMutual,
  51. ServerName: state.ServerName,
  52. PeerCertificates: state.PeerCertificates,
  53. VerifiedChains: state.VerifiedChains,
  54. SignedCertificateTimestamps: state.SignedCertificateTimestamps,
  55. OCSPResponse: state.OCSPResponse,
  56. TLSUnique: state.TLSUnique,
  57. }
  58. }
  59. func (e *UTLSClientConfig) Clone() Config {
  60. return &UTLSClientConfig{
  61. config: e.config.Clone(),
  62. id: e.id,
  63. }
  64. }
  65. func NewUTLSClient(router adapter.Router, serverAddress string, options option.OutboundTLSOptions) (Config, error) {
  66. var serverName string
  67. if options.ServerName != "" {
  68. serverName = options.ServerName
  69. } else if serverAddress != "" {
  70. if _, err := netip.ParseAddr(serverName); err != nil {
  71. serverName = serverAddress
  72. }
  73. }
  74. if serverName == "" && !options.Insecure {
  75. return nil, E.New("missing server_name or insecure=true")
  76. }
  77. var tlsConfig utls.Config
  78. if options.DisableSNI {
  79. tlsConfig.ServerName = "127.0.0.1"
  80. } else {
  81. tlsConfig.ServerName = serverName
  82. }
  83. if options.Insecure {
  84. tlsConfig.InsecureSkipVerify = options.Insecure
  85. } else if options.DisableSNI {
  86. return nil, E.New("disable_sni is unsupported in uTLS")
  87. }
  88. if len(options.ALPN) > 0 {
  89. tlsConfig.NextProtos = options.ALPN
  90. }
  91. if options.MinVersion != "" {
  92. minVersion, err := ParseTLSVersion(options.MinVersion)
  93. if err != nil {
  94. return nil, E.Cause(err, "parse min_version")
  95. }
  96. tlsConfig.MinVersion = minVersion
  97. }
  98. if options.MaxVersion != "" {
  99. maxVersion, err := ParseTLSVersion(options.MaxVersion)
  100. if err != nil {
  101. return nil, E.Cause(err, "parse max_version")
  102. }
  103. tlsConfig.MaxVersion = maxVersion
  104. }
  105. if options.CipherSuites != nil {
  106. find:
  107. for _, cipherSuite := range options.CipherSuites {
  108. for _, tlsCipherSuite := range tls.CipherSuites() {
  109. if cipherSuite == tlsCipherSuite.Name {
  110. tlsConfig.CipherSuites = append(tlsConfig.CipherSuites, tlsCipherSuite.ID)
  111. continue find
  112. }
  113. }
  114. return nil, E.New("unknown cipher_suite: ", cipherSuite)
  115. }
  116. }
  117. var certificate []byte
  118. if options.Certificate != "" {
  119. certificate = []byte(options.Certificate)
  120. } else if options.CertificatePath != "" {
  121. content, err := os.ReadFile(options.CertificatePath)
  122. if err != nil {
  123. return nil, E.Cause(err, "read certificate")
  124. }
  125. certificate = content
  126. }
  127. if len(certificate) > 0 {
  128. certPool := x509.NewCertPool()
  129. if !certPool.AppendCertsFromPEM(certificate) {
  130. return nil, E.New("failed to parse certificate:\n\n", certificate)
  131. }
  132. tlsConfig.RootCAs = certPool
  133. }
  134. var id utls.ClientHelloID
  135. switch options.UTLS.Fingerprint {
  136. case "chrome", "":
  137. id = utls.HelloChrome_Auto
  138. case "firefox":
  139. id = utls.HelloFirefox_Auto
  140. case "edge":
  141. id = utls.HelloEdge_Auto
  142. case "safari":
  143. id = utls.HelloSafari_Auto
  144. case "360":
  145. id = utls.Hello360_Auto
  146. case "qq":
  147. id = utls.HelloQQ_Auto
  148. case "ios":
  149. id = utls.HelloIOS_Auto
  150. case "android":
  151. id = utls.HelloAndroid_11_OkHttp
  152. case "random":
  153. id = utls.HelloRandomized
  154. default:
  155. return nil, E.New("unknown uTLS fingerprint: ", options.UTLS.Fingerprint)
  156. }
  157. return &UTLSClientConfig{&tlsConfig, id}, nil
  158. }