utls_client.go 4.2 KB

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