std_client.go 3.3 KB

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