std_client.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package tls
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "net"
  7. "net/netip"
  8. "os"
  9. "strings"
  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. if _, err := netip.ParseAddr(serverName); err != nil {
  44. serverName = serverAddress
  45. }
  46. }
  47. if serverName == "" && !options.Insecure {
  48. return nil, E.New("missing server_name or insecure=true")
  49. }
  50. var tlsConfig tls.Config
  51. tlsConfig.Time = ntp.TimeFuncFromContext(ctx)
  52. if options.DisableSNI {
  53. tlsConfig.ServerName = "127.0.0.1"
  54. } else {
  55. tlsConfig.ServerName = serverName
  56. }
  57. if options.Insecure {
  58. tlsConfig.InsecureSkipVerify = options.Insecure
  59. } else if options.DisableSNI {
  60. tlsConfig.InsecureSkipVerify = true
  61. tlsConfig.VerifyConnection = func(state tls.ConnectionState) error {
  62. verifyOptions := x509.VerifyOptions{
  63. DNSName: serverName,
  64. Intermediates: x509.NewCertPool(),
  65. }
  66. for _, cert := range state.PeerCertificates[1:] {
  67. verifyOptions.Intermediates.AddCert(cert)
  68. }
  69. _, err := state.PeerCertificates[0].Verify(verifyOptions)
  70. return err
  71. }
  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 len(options.Certificate) > 0 {
  104. certificate = []byte(strings.Join(options.Certificate, "\n"))
  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. return &STDClientConfig{&tlsConfig}, nil
  120. }