tls.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package dialer
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "net"
  7. "net/netip"
  8. "os"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/option"
  11. E "github.com/sagernet/sing/common/exceptions"
  12. M "github.com/sagernet/sing/common/metadata"
  13. N "github.com/sagernet/sing/common/network"
  14. )
  15. type TLSDialer struct {
  16. dialer N.Dialer
  17. config *tls.Config
  18. }
  19. func TLSConfig(serverAddress string, options option.OutboundTLSOptions) (*tls.Config, error) {
  20. if !options.Enabled {
  21. return nil, nil
  22. }
  23. var serverName string
  24. if options.ServerName != "" {
  25. serverName = options.ServerName
  26. } else if serverAddress != "" {
  27. if _, err := netip.ParseAddr(serverName); err == nil {
  28. serverName = serverAddress
  29. }
  30. }
  31. if serverName == "" && !options.Insecure {
  32. return nil, E.New("missing server_name or insecure=true")
  33. }
  34. var tlsConfig tls.Config
  35. if options.DisableSNI {
  36. tlsConfig.ServerName = "127.0.0.1"
  37. } else {
  38. tlsConfig.ServerName = serverName
  39. }
  40. if options.Insecure {
  41. tlsConfig.InsecureSkipVerify = options.Insecure
  42. } else if options.DisableSNI {
  43. tlsConfig.InsecureSkipVerify = true
  44. tlsConfig.VerifyConnection = func(state tls.ConnectionState) error {
  45. verifyOptions := x509.VerifyOptions{
  46. DNSName: serverName,
  47. Intermediates: x509.NewCertPool(),
  48. }
  49. for _, cert := range state.PeerCertificates[1:] {
  50. verifyOptions.Intermediates.AddCert(cert)
  51. }
  52. _, err := state.PeerCertificates[0].Verify(verifyOptions)
  53. return err
  54. }
  55. }
  56. if len(options.ALPN) > 0 {
  57. tlsConfig.NextProtos = options.ALPN
  58. }
  59. if options.MinVersion != "" {
  60. minVersion, err := option.ParseTLSVersion(options.MinVersion)
  61. if err != nil {
  62. return nil, E.Cause(err, "parse min_version")
  63. }
  64. tlsConfig.MinVersion = minVersion
  65. }
  66. if options.MaxVersion != "" {
  67. maxVersion, err := option.ParseTLSVersion(options.MaxVersion)
  68. if err != nil {
  69. return nil, E.Cause(err, "parse max_version")
  70. }
  71. tlsConfig.MaxVersion = maxVersion
  72. }
  73. if options.CipherSuites != nil {
  74. find:
  75. for _, cipherSuite := range options.CipherSuites {
  76. for _, tlsCipherSuite := range tls.CipherSuites() {
  77. if cipherSuite == tlsCipherSuite.Name {
  78. tlsConfig.CipherSuites = append(tlsConfig.CipherSuites, tlsCipherSuite.ID)
  79. continue find
  80. }
  81. }
  82. return nil, E.New("unknown cipher_suite: ", cipherSuite)
  83. }
  84. }
  85. var certificate []byte
  86. if options.Certificate != "" {
  87. certificate = []byte(options.Certificate)
  88. } else if options.CertificatePath != "" {
  89. content, err := os.ReadFile(options.CertificatePath)
  90. if err != nil {
  91. return nil, E.Cause(err, "read certificate")
  92. }
  93. certificate = content
  94. }
  95. if len(certificate) > 0 {
  96. certPool := x509.NewCertPool()
  97. if !certPool.AppendCertsFromPEM(certificate) {
  98. return nil, E.New("failed to parse certificate:\n\n", certificate)
  99. }
  100. tlsConfig.RootCAs = certPool
  101. }
  102. return &tlsConfig, nil
  103. }
  104. func NewTLS(dialer N.Dialer, serverAddress string, options option.OutboundTLSOptions) (N.Dialer, error) {
  105. if !options.Enabled {
  106. return dialer, nil
  107. }
  108. tlsConfig, err := TLSConfig(serverAddress, options)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return &TLSDialer{
  113. dialer: dialer,
  114. config: tlsConfig,
  115. }, nil
  116. }
  117. func (d *TLSDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  118. if network != N.NetworkTCP {
  119. return nil, os.ErrInvalid
  120. }
  121. conn, err := d.dialer.DialContext(ctx, network, destination)
  122. if err != nil {
  123. return nil, err
  124. }
  125. return TLSClient(ctx, conn, d.config)
  126. }
  127. func (d *TLSDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  128. return nil, os.ErrInvalid
  129. }
  130. func TLSClient(ctx context.Context, conn net.Conn, tlsConfig *tls.Config) (*tls.Conn, error) {
  131. tlsConn := tls.Client(conn, tlsConfig)
  132. ctx, cancel := context.WithTimeout(ctx, C.TCPTimeout)
  133. defer cancel()
  134. err := tlsConn.HandshakeContext(ctx)
  135. return tlsConn, err
  136. }