shadowtls.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package outbound
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/common/dialer"
  8. "github.com/sagernet/sing-box/common/tls"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/log"
  11. "github.com/sagernet/sing-box/option"
  12. "github.com/sagernet/sing-shadowtls"
  13. "github.com/sagernet/sing/common"
  14. M "github.com/sagernet/sing/common/metadata"
  15. N "github.com/sagernet/sing/common/network"
  16. )
  17. var _ adapter.Outbound = (*ShadowTLS)(nil)
  18. type ShadowTLS struct {
  19. myOutboundAdapter
  20. client *shadowtls.Client
  21. }
  22. func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowTLSOutboundOptions) (*ShadowTLS, error) {
  23. outbound := &ShadowTLS{
  24. myOutboundAdapter: myOutboundAdapter{
  25. protocol: C.TypeShadowTLS,
  26. network: []string{N.NetworkTCP},
  27. router: router,
  28. logger: logger,
  29. tag: tag,
  30. },
  31. }
  32. if options.TLS == nil || !options.TLS.Enabled {
  33. return nil, C.ErrTLSRequired
  34. }
  35. if options.Version == 0 {
  36. options.Version = 1
  37. }
  38. if options.Version == 1 {
  39. options.TLS.MinVersion = "1.2"
  40. options.TLS.MaxVersion = "1.2"
  41. }
  42. tlsConfig, err := tls.NewClient(router, options.Server, common.PtrValueOrDefault(options.TLS))
  43. if err != nil {
  44. return nil, err
  45. }
  46. var tlsHandshakeFunc shadowtls.TLSHandshakeFunc
  47. switch options.Version {
  48. case 1, 2:
  49. tlsHandshakeFunc = func(ctx context.Context, conn net.Conn, _ shadowtls.TLSSessionIDGeneratorFunc) error {
  50. return common.Error(tls.ClientHandshake(ctx, conn, tlsConfig))
  51. }
  52. case 3:
  53. if idConfig, loaded := tlsConfig.(tls.WithSessionIDGenerator); loaded {
  54. tlsHandshakeFunc = func(ctx context.Context, conn net.Conn, sessionIDGenerator shadowtls.TLSSessionIDGeneratorFunc) error {
  55. idConfig.SetSessionIDGenerator(sessionIDGenerator)
  56. return common.Error(tls.ClientHandshake(ctx, conn, tlsConfig))
  57. }
  58. } else {
  59. stdTLSConfig, err := tlsConfig.Config()
  60. if err != nil {
  61. return nil, err
  62. }
  63. tlsHandshakeFunc = shadowtls.DefaultTLSHandshakeFunc(options.Password, stdTLSConfig)
  64. }
  65. }
  66. client, err := shadowtls.NewClient(shadowtls.ClientConfig{
  67. Version: options.Version,
  68. Password: options.Password,
  69. Server: options.ServerOptions.Build(),
  70. Dialer: dialer.New(router, options.DialerOptions),
  71. TLSHandshake: tlsHandshakeFunc,
  72. Logger: logger,
  73. })
  74. if err != nil {
  75. return nil, err
  76. }
  77. outbound.client = client
  78. return outbound, nil
  79. }
  80. func (s *ShadowTLS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  81. switch N.NetworkName(network) {
  82. case N.NetworkTCP:
  83. return s.client.DialContext(ctx)
  84. default:
  85. return nil, os.ErrInvalid
  86. }
  87. }
  88. func (s *ShadowTLS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  89. return nil, os.ErrInvalid
  90. }
  91. func (s *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  92. return NewConnection(ctx, s, conn, metadata)
  93. }
  94. func (s *ShadowTLS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  95. return os.ErrInvalid
  96. }