std_client.go 4.0 KB

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