std_client.go 3.4 KB

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