std_client.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. DNSName: serverName,
  76. Intermediates: x509.NewCertPool(),
  77. }
  78. for _, cert := range state.PeerCertificates[1:] {
  79. verifyOptions.Intermediates.AddCert(cert)
  80. }
  81. _, err := state.PeerCertificates[0].Verify(verifyOptions)
  82. return err
  83. }
  84. }
  85. if len(options.ALPN) > 0 {
  86. tlsConfig.NextProtos = options.ALPN
  87. }
  88. if options.MinVersion != "" {
  89. minVersion, err := ParseTLSVersion(options.MinVersion)
  90. if err != nil {
  91. return nil, E.Cause(err, "parse min_version")
  92. }
  93. tlsConfig.MinVersion = minVersion
  94. }
  95. if options.MaxVersion != "" {
  96. maxVersion, err := ParseTLSVersion(options.MaxVersion)
  97. if err != nil {
  98. return nil, E.Cause(err, "parse max_version")
  99. }
  100. tlsConfig.MaxVersion = maxVersion
  101. }
  102. if options.CipherSuites != nil {
  103. find:
  104. for _, cipherSuite := range options.CipherSuites {
  105. for _, tlsCipherSuite := range tls.CipherSuites() {
  106. if cipherSuite == tlsCipherSuite.Name {
  107. tlsConfig.CipherSuites = append(tlsConfig.CipherSuites, tlsCipherSuite.ID)
  108. continue find
  109. }
  110. }
  111. return nil, E.New("unknown cipher_suite: ", cipherSuite)
  112. }
  113. }
  114. var certificate []byte
  115. if len(options.Certificate) > 0 {
  116. certificate = []byte(strings.Join(options.Certificate, "\n"))
  117. } else if options.CertificatePath != "" {
  118. content, err := os.ReadFile(options.CertificatePath)
  119. if err != nil {
  120. return nil, E.Cause(err, "read certificate")
  121. }
  122. certificate = content
  123. }
  124. if len(certificate) > 0 {
  125. certPool := x509.NewCertPool()
  126. if !certPool.AppendCertsFromPEM(certificate) {
  127. return nil, E.New("failed to parse certificate:\n\n", certificate)
  128. }
  129. tlsConfig.RootCAs = certPool
  130. }
  131. stdConfig := &STDClientConfig{ctx, &tlsConfig, options.Fragment, time.Duration(options.FragmentFallbackDelay), options.RecordFragment}
  132. if options.ECH != nil && options.ECH.Enabled {
  133. return parseECHClientConfig(ctx, stdConfig, options)
  134. } else {
  135. return stdConfig, nil
  136. }
  137. }