std_client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 newStdClient(serverAddress string, options option.OutboundTLSOptions) (Config, error) {
  15. var serverName string
  16. if options.ServerName != "" {
  17. serverName = options.ServerName
  18. } else if serverAddress != "" {
  19. if _, err := netip.ParseAddr(serverName); err != nil {
  20. serverName = serverAddress
  21. }
  22. }
  23. if serverName == "" && !options.Insecure {
  24. return nil, E.New("missing server_name or insecure=true")
  25. }
  26. var tlsConfig tls.Config
  27. if options.DisableSNI {
  28. tlsConfig.ServerName = "127.0.0.1"
  29. } else {
  30. tlsConfig.ServerName = serverName
  31. }
  32. if options.Insecure {
  33. tlsConfig.InsecureSkipVerify = options.Insecure
  34. } else if options.DisableSNI {
  35. tlsConfig.InsecureSkipVerify = true
  36. tlsConfig.VerifyConnection = func(state tls.ConnectionState) error {
  37. verifyOptions := x509.VerifyOptions{
  38. DNSName: serverName,
  39. Intermediates: x509.NewCertPool(),
  40. }
  41. for _, cert := range state.PeerCertificates[1:] {
  42. verifyOptions.Intermediates.AddCert(cert)
  43. }
  44. _, err := state.PeerCertificates[0].Verify(verifyOptions)
  45. return err
  46. }
  47. }
  48. if len(options.ALPN) > 0 {
  49. tlsConfig.NextProtos = options.ALPN
  50. }
  51. if options.MinVersion != "" {
  52. minVersion, err := ParseTLSVersion(options.MinVersion)
  53. if err != nil {
  54. return nil, E.Cause(err, "parse min_version")
  55. }
  56. tlsConfig.MinVersion = minVersion
  57. }
  58. if options.MaxVersion != "" {
  59. maxVersion, err := ParseTLSVersion(options.MaxVersion)
  60. if err != nil {
  61. return nil, E.Cause(err, "parse max_version")
  62. }
  63. tlsConfig.MaxVersion = maxVersion
  64. }
  65. if options.CipherSuites != nil {
  66. find:
  67. for _, cipherSuite := range options.CipherSuites {
  68. for _, tlsCipherSuite := range tls.CipherSuites() {
  69. if cipherSuite == tlsCipherSuite.Name {
  70. tlsConfig.CipherSuites = append(tlsConfig.CipherSuites, tlsCipherSuite.ID)
  71. continue find
  72. }
  73. }
  74. return nil, E.New("unknown cipher_suite: ", cipherSuite)
  75. }
  76. }
  77. var certificate []byte
  78. if options.Certificate != "" {
  79. certificate = []byte(options.Certificate)
  80. } else if options.CertificatePath != "" {
  81. content, err := os.ReadFile(options.CertificatePath)
  82. if err != nil {
  83. return nil, E.Cause(err, "read certificate")
  84. }
  85. certificate = content
  86. }
  87. if len(certificate) > 0 {
  88. certPool := x509.NewCertPool()
  89. if !certPool.AppendCertsFromPEM(certificate) {
  90. return nil, E.New("failed to parse certificate:\n\n", certificate)
  91. }
  92. tlsConfig.RootCAs = certPool
  93. }
  94. return &stdClientConfig{&tlsConfig}, nil
  95. }
  96. func (s *stdClientConfig) Config() (*STDConfig, error) {
  97. return s.config, nil
  98. }
  99. func (s *stdClientConfig) Client(conn net.Conn) Conn {
  100. return tls.Client(conn, s.config)
  101. }