shadowtls.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 == 1 {
  36. options.TLS.MinVersion = "1.2"
  37. options.TLS.MaxVersion = "1.2"
  38. }
  39. tlsConfig, err := tls.NewClient(router, options.Server, common.PtrValueOrDefault(options.TLS))
  40. if err != nil {
  41. return nil, err
  42. }
  43. var tlsHandshakeFunc shadowtls.TLSHandshakeFunc
  44. switch options.Version {
  45. case 1, 2:
  46. tlsHandshakeFunc = func(ctx context.Context, conn net.Conn, _ shadowtls.TLSSessionIDGeneratorFunc) error {
  47. return common.Error(tls.ClientHandshake(ctx, conn, tlsConfig))
  48. }
  49. case 3:
  50. if idConfig, loaded := tlsConfig.(tls.ConfigWithSessionIDGenerator); loaded {
  51. tlsHandshakeFunc = func(ctx context.Context, conn net.Conn, sessionIDGenerator shadowtls.TLSSessionIDGeneratorFunc) error {
  52. idConfig.SetSessionIDGenerator(sessionIDGenerator)
  53. return common.Error(tls.ClientHandshake(ctx, conn, tlsConfig))
  54. }
  55. } else {
  56. stdTLSConfig, err := tlsConfig.Config()
  57. if err != nil {
  58. return nil, err
  59. }
  60. tlsHandshakeFunc = shadowtls.DefaultTLSHandshakeFunc(options.Password, stdTLSConfig)
  61. }
  62. }
  63. client, err := shadowtls.NewClient(shadowtls.ClientConfig{
  64. Version: options.Version,
  65. Password: options.Password,
  66. Server: options.ServerOptions.Build(),
  67. Dialer: dialer.New(router, options.DialerOptions),
  68. TLSHandshake: tlsHandshakeFunc,
  69. Logger: logger,
  70. })
  71. if err != nil {
  72. return nil, err
  73. }
  74. outbound.client = client
  75. return outbound, nil
  76. }
  77. func (s *ShadowTLS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  78. switch N.NetworkName(network) {
  79. case N.NetworkTCP:
  80. return s.client.DialContext(ctx)
  81. default:
  82. return nil, os.ErrInvalid
  83. }
  84. }
  85. func (s *ShadowTLS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  86. return nil, os.ErrInvalid
  87. }
  88. func (s *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  89. return NewConnection(ctx, s, conn, metadata)
  90. }
  91. func (s *ShadowTLS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  92. return os.ErrInvalid
  93. }