std_client.go 3.3 KB

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