tls.go 3.6 KB

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