shadowtls.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package outbound
  2. import (
  3. "context"
  4. "crypto/hmac"
  5. "crypto/sha1"
  6. "net"
  7. "os"
  8. "github.com/sagernet/sing-box/adapter"
  9. "github.com/sagernet/sing-box/common/dialer"
  10. "github.com/sagernet/sing-box/common/tls"
  11. C "github.com/sagernet/sing-box/constant"
  12. "github.com/sagernet/sing-box/log"
  13. "github.com/sagernet/sing-box/option"
  14. "github.com/sagernet/sing-box/transport/shadowtls"
  15. "github.com/sagernet/sing/common"
  16. E "github.com/sagernet/sing/common/exceptions"
  17. M "github.com/sagernet/sing/common/metadata"
  18. N "github.com/sagernet/sing/common/network"
  19. )
  20. var _ adapter.Outbound = (*ShadowTLS)(nil)
  21. type ShadowTLS struct {
  22. myOutboundAdapter
  23. dialer N.Dialer
  24. serverAddr M.Socksaddr
  25. tlsConfig tls.Config
  26. version int
  27. password string
  28. }
  29. func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowTLSOutboundOptions) (*ShadowTLS, error) {
  30. outbound := &ShadowTLS{
  31. myOutboundAdapter: myOutboundAdapter{
  32. protocol: C.TypeShadowTLS,
  33. network: []string{N.NetworkTCP},
  34. router: router,
  35. logger: logger,
  36. tag: tag,
  37. },
  38. dialer: dialer.New(router, options.DialerOptions),
  39. serverAddr: options.ServerOptions.Build(),
  40. password: options.Password,
  41. }
  42. if options.TLS == nil || !options.TLS.Enabled {
  43. return nil, C.ErrTLSRequired
  44. }
  45. outbound.version = options.Version
  46. switch options.Version {
  47. case 0:
  48. fallthrough
  49. case 1:
  50. options.TLS.MinVersion = "1.2"
  51. options.TLS.MaxVersion = "1.2"
  52. case 2:
  53. case 3:
  54. options.TLS.MinVersion = "1.3"
  55. options.TLS.MaxVersion = "1.3"
  56. default:
  57. return nil, E.New("unknown shadowtls protocol version: ", options.Version)
  58. }
  59. var err error
  60. if options.Version != 3 {
  61. outbound.tlsConfig, err = tls.NewClient(router, options.Server, common.PtrValueOrDefault(options.TLS))
  62. } else {
  63. outbound.tlsConfig, err = shadowtls.NewClientTLSConfig(options.Server, common.PtrValueOrDefault(options.TLS), options.Password)
  64. }
  65. if err != nil {
  66. return nil, err
  67. }
  68. return outbound, nil
  69. }
  70. func (s *ShadowTLS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  71. switch N.NetworkName(network) {
  72. case N.NetworkTCP:
  73. default:
  74. return nil, os.ErrInvalid
  75. }
  76. conn, err := s.dialer.DialContext(ctx, N.NetworkTCP, s.serverAddr)
  77. if err != nil {
  78. return nil, err
  79. }
  80. switch s.version {
  81. default:
  82. fallthrough
  83. case 1:
  84. _, err = tls.ClientHandshake(ctx, conn, s.tlsConfig)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return conn, nil
  89. case 2:
  90. hashConn := shadowtls.NewHashReadConn(conn, s.password)
  91. _, err = tls.ClientHandshake(ctx, hashConn, s.tlsConfig)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return shadowtls.NewClientConn(hashConn), nil
  96. case 3:
  97. streamWrapper := shadowtls.NewStreamWrapper(conn, s.password)
  98. _, err = tls.ClientHandshake(ctx, streamWrapper, s.tlsConfig)
  99. if err != nil {
  100. return nil, err
  101. }
  102. authorized, serverRandom, readHMAC := streamWrapper.Authorized()
  103. if !authorized {
  104. return nil, E.New("traffic hijacked or TLS1.3 is not supported")
  105. }
  106. hmacAdd := hmac.New(sha1.New, []byte(s.password))
  107. hmacAdd.Write(serverRandom)
  108. hmacAdd.Write([]byte("C"))
  109. hmacVerify := hmac.New(sha1.New, []byte(s.password))
  110. hmacVerify.Write(serverRandom)
  111. hmacVerify.Write([]byte("S"))
  112. return shadowtls.NewVerifiedConn(conn, hmacAdd, hmacVerify, readHMAC), nil
  113. }
  114. }
  115. func (s *ShadowTLS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  116. return nil, os.ErrInvalid
  117. }
  118. func (s *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  119. return NewConnection(ctx, s, conn, metadata)
  120. }
  121. func (s *ShadowTLS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  122. return os.ErrInvalid
  123. }