handler.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package outbound
  2. import (
  3. "context"
  4. "v2ray.com/core"
  5. "v2ray.com/core/app/proxyman"
  6. "v2ray.com/core/app/proxyman/mux"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/proxy"
  10. "v2ray.com/core/transport/internet"
  11. "v2ray.com/core/transport/pipe"
  12. )
  13. type Handler struct {
  14. config *core.OutboundHandlerConfig
  15. senderSettings *proxyman.SenderConfig
  16. proxy proxy.Outbound
  17. outboundManager core.OutboundHandlerManager
  18. mux *mux.ClientManager
  19. }
  20. func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (core.OutboundHandler, error) {
  21. v := core.MustFromContext(ctx)
  22. h := &Handler{
  23. config: config,
  24. outboundManager: v.OutboundHandlerManager(),
  25. }
  26. if config.SenderSettings != nil {
  27. senderSettings, err := config.SenderSettings.GetInstance()
  28. if err != nil {
  29. return nil, err
  30. }
  31. switch s := senderSettings.(type) {
  32. case *proxyman.SenderConfig:
  33. h.senderSettings = s
  34. default:
  35. return nil, newError("settings is not SenderConfig")
  36. }
  37. }
  38. proxyConfig, err := config.ProxySettings.GetInstance()
  39. if err != nil {
  40. return nil, err
  41. }
  42. rawProxyHandler, err := common.CreateObject(ctx, proxyConfig)
  43. if err != nil {
  44. return nil, err
  45. }
  46. proxyHandler, ok := rawProxyHandler.(proxy.Outbound)
  47. if !ok {
  48. return nil, newError("not an outbound handler")
  49. }
  50. if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
  51. config := h.senderSettings.MultiplexSettings
  52. if config.Concurrency < 1 || config.Concurrency > 1024 {
  53. return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
  54. }
  55. h.mux = mux.NewClientManager(proxyHandler, h, config)
  56. }
  57. h.proxy = proxyHandler
  58. return h, nil
  59. }
  60. // Tag implements core.OutboundHandler.
  61. func (h *Handler) Tag() string {
  62. return h.config.Tag
  63. }
  64. // Dispatch implements proxy.Outbound.Dispatch.
  65. func (h *Handler) Dispatch(ctx context.Context, link *core.Link) {
  66. if h.mux != nil {
  67. if err := h.mux.Dispatch(ctx, link); err != nil {
  68. newError("failed to process mux outbound traffic").Base(err).WithContext(ctx).WriteToLog()
  69. pipe.CloseError(link.Writer)
  70. }
  71. } else {
  72. if err := h.proxy.Process(ctx, link, h); err != nil {
  73. // Ensure outbound ray is properly closed.
  74. newError("failed to process outbound traffic").Base(err).WithContext(ctx).WriteToLog()
  75. pipe.CloseError(link.Writer)
  76. } else {
  77. common.Must(common.Close(link.Writer))
  78. }
  79. pipe.CloseError(link.Reader)
  80. }
  81. }
  82. // Dial implements proxy.Dialer.Dial().
  83. func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  84. if h.senderSettings != nil {
  85. if h.senderSettings.ProxySettings.HasTag() {
  86. tag := h.senderSettings.ProxySettings.Tag
  87. handler := h.outboundManager.GetHandler(tag)
  88. if handler != nil {
  89. newError("proxying to ", tag, " for dest ", dest).AtDebug().WithContext(ctx).WriteToLog()
  90. ctx = proxy.ContextWithTarget(ctx, dest)
  91. uplinkReader, uplinkWriter := pipe.New()
  92. downlinkReader, downlinkWriter := pipe.New()
  93. go handler.Dispatch(ctx, &core.Link{Reader: uplinkReader, Writer: downlinkWriter})
  94. return net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader)), nil
  95. }
  96. newError("failed to get outbound handler with tag: ", tag).AtWarning().WithContext(ctx).WriteToLog()
  97. }
  98. if h.senderSettings.Via != nil {
  99. ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
  100. }
  101. if h.senderSettings.StreamSettings != nil {
  102. ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
  103. }
  104. }
  105. return internet.Dial(ctx, dest)
  106. }
  107. // GetOutbound implements proxy.GetOutbound.
  108. func (h *Handler) GetOutbound() proxy.Outbound {
  109. return h.proxy
  110. }
  111. // Start implements common.Runnable.
  112. func (h *Handler) Start() error {
  113. return nil
  114. }
  115. // Close implements common.Closable.
  116. func (h *Handler) Close() error {
  117. common.Close(h.mux)
  118. return nil
  119. }