std_client.go 3.2 KB

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