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