std_client.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package tls
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "net"
  7. "os"
  8. "strings"
  9. "time"
  10. "github.com/sagernet/sing-box/adapter"
  11. "github.com/sagernet/sing-box/common/tlsfragment"
  12. "github.com/sagernet/sing-box/option"
  13. E "github.com/sagernet/sing/common/exceptions"
  14. "github.com/sagernet/sing/common/ntp"
  15. )
  16. type STDClientConfig struct {
  17. ctx context.Context
  18. config *tls.Config
  19. fragment bool
  20. fragmentFallbackDelay time.Duration
  21. recordFragment bool
  22. }
  23. func (c *STDClientConfig) ServerName() string {
  24. return c.config.ServerName
  25. }
  26. func (c *STDClientConfig) SetServerName(serverName string) {
  27. c.config.ServerName = serverName
  28. }
  29. func (c *STDClientConfig) NextProtos() []string {
  30. return c.config.NextProtos
  31. }
  32. func (c *STDClientConfig) SetNextProtos(nextProto []string) {
  33. c.config.NextProtos = nextProto
  34. }
  35. func (c *STDClientConfig) Config() (*STDConfig, error) {
  36. return c.config, nil
  37. }
  38. func (c *STDClientConfig) Client(conn net.Conn) (Conn, error) {
  39. if c.recordFragment {
  40. conn = tf.NewConn(conn, c.ctx, c.fragment, c.recordFragment, c.fragmentFallbackDelay)
  41. }
  42. return tls.Client(conn, c.config), nil
  43. }
  44. func (c *STDClientConfig) Clone() Config {
  45. return &STDClientConfig{c.ctx, c.config.Clone(), c.fragment, c.fragmentFallbackDelay, c.recordFragment}
  46. }
  47. func (c *STDClientConfig) ECHConfigList() []byte {
  48. return c.config.EncryptedClientHelloConfigList
  49. }
  50. func (c *STDClientConfig) SetECHConfigList(EncryptedClientHelloConfigList []byte) {
  51. c.config.EncryptedClientHelloConfigList = EncryptedClientHelloConfigList
  52. }
  53. func NewSTDClient(ctx context.Context, serverAddress string, options option.OutboundTLSOptions) (Config, error) {
  54. var serverName string
  55. if options.ServerName != "" {
  56. serverName = options.ServerName
  57. } else if serverAddress != "" {
  58. serverName = serverAddress
  59. }
  60. if serverName == "" && !options.Insecure {
  61. return nil, E.New("missing server_name or insecure=true")
  62. }
  63. var tlsConfig tls.Config
  64. tlsConfig.Time = ntp.TimeFuncFromContext(ctx)
  65. tlsConfig.RootCAs = adapter.RootPoolFromContext(ctx)
  66. if !options.DisableSNI {
  67. tlsConfig.ServerName = serverName
  68. }
  69. if options.Insecure {
  70. tlsConfig.InsecureSkipVerify = options.Insecure
  71. } else if options.DisableSNI {
  72. tlsConfig.InsecureSkipVerify = true
  73. tlsConfig.VerifyConnection = func(state tls.ConnectionState) error {
  74. verifyOptions := x509.VerifyOptions{
  75. Roots: tlsConfig.RootCAs,
  76. DNSName: serverName,
  77. Intermediates: x509.NewCertPool(),
  78. }
  79. for _, cert := range state.PeerCertificates[1:] {
  80. verifyOptions.Intermediates.AddCert(cert)
  81. }
  82. if tlsConfig.Time != nil {
  83. verifyOptions.CurrentTime = tlsConfig.Time()
  84. }
  85. _, err := state.PeerCertificates[0].Verify(verifyOptions)
  86. return err
  87. }
  88. }
  89. if len(options.ALPN) > 0 {
  90. tlsConfig.NextProtos = options.ALPN
  91. }
  92. if options.MinVersion != "" {
  93. minVersion, err := ParseTLSVersion(options.MinVersion)
  94. if err != nil {
  95. return nil, E.Cause(err, "parse min_version")
  96. }
  97. tlsConfig.MinVersion = minVersion
  98. }
  99. if options.MaxVersion != "" {
  100. maxVersion, err := ParseTLSVersion(options.MaxVersion)
  101. if err != nil {
  102. return nil, E.Cause(err, "parse max_version")
  103. }
  104. tlsConfig.MaxVersion = maxVersion
  105. }
  106. if options.CipherSuites != nil {
  107. find:
  108. for _, cipherSuite := range options.CipherSuites {
  109. for _, tlsCipherSuite := range tls.CipherSuites() {
  110. if cipherSuite == tlsCipherSuite.Name {
  111. tlsConfig.CipherSuites = append(tlsConfig.CipherSuites, tlsCipherSuite.ID)
  112. continue find
  113. }
  114. }
  115. return nil, E.New("unknown cipher_suite: ", cipherSuite)
  116. }
  117. }
  118. var certificate []byte
  119. if len(options.Certificate) > 0 {
  120. certificate = []byte(strings.Join(options.Certificate, "\n"))
  121. } else if options.CertificatePath != "" {
  122. content, err := os.ReadFile(options.CertificatePath)
  123. if err != nil {
  124. return nil, E.Cause(err, "read certificate")
  125. }
  126. certificate = content
  127. }
  128. if len(certificate) > 0 {
  129. certPool := x509.NewCertPool()
  130. if !certPool.AppendCertsFromPEM(certificate) {
  131. return nil, E.New("failed to parse certificate:\n\n", certificate)
  132. }
  133. tlsConfig.RootCAs = certPool
  134. }
  135. stdConfig := &STDClientConfig{ctx, &tlsConfig, options.Fragment, time.Duration(options.FragmentFallbackDelay), options.RecordFragment}
  136. if options.ECH != nil && options.ECH.Enabled {
  137. return parseECHClientConfig(ctx, stdConfig, options)
  138. } else {
  139. return stdConfig, nil
  140. }
  141. }