handler.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/ray"
  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. case *proxyman.UnixSenderConfig:
  35. return NewUnixHandler(ctx, config)
  36. default:
  37. return nil, newError("settings is not SenderConfig")
  38. }
  39. }
  40. proxyConfig, err := config.ProxySettings.GetInstance()
  41. if err != nil {
  42. return nil, err
  43. }
  44. rawProxyHandler, err := common.CreateObject(ctx, proxyConfig)
  45. if err != nil {
  46. return nil, err
  47. }
  48. proxyHandler, ok := rawProxyHandler.(proxy.Outbound)
  49. if !ok {
  50. return nil, newError("not an outbound handler")
  51. }
  52. if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
  53. config := h.senderSettings.MultiplexSettings
  54. if config.Concurrency < 1 || config.Concurrency > 1024 {
  55. return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
  56. }
  57. h.mux = mux.NewClientManager(proxyHandler, h, config)
  58. }
  59. h.proxy = proxyHandler
  60. return h, nil
  61. }
  62. // Tag implements core.OutboundHandler.
  63. func (h *Handler) Tag() string {
  64. return h.config.Tag
  65. }
  66. // Dispatch implements proxy.Outbound.Dispatch.
  67. func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
  68. if h.mux != nil {
  69. err := h.mux.Dispatch(ctx, outboundRay)
  70. if err != nil {
  71. newError("failed to process outbound traffic").Base(err).WithContext(ctx).WriteToLog()
  72. outboundRay.OutboundOutput().CloseError()
  73. }
  74. } else {
  75. err := h.proxy.Process(ctx, outboundRay, h)
  76. // Ensure outbound ray is properly closed.
  77. if err != nil {
  78. newError("failed to process outbound traffic").Base(err).WithContext(ctx).WriteToLog()
  79. outboundRay.OutboundOutput().CloseError()
  80. } else {
  81. outboundRay.OutboundOutput().Close()
  82. }
  83. outboundRay.OutboundInput().CloseError()
  84. }
  85. }
  86. // Dial implements proxy.Dialer.Dial().
  87. func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  88. if h.senderSettings != nil {
  89. if h.senderSettings.ProxySettings.HasTag() {
  90. tag := h.senderSettings.ProxySettings.Tag
  91. handler := h.outboundManager.GetHandler(tag)
  92. if handler != nil {
  93. newError("proxying to ", tag, " for dest ", dest).AtDebug().WithContext(ctx).WriteToLog()
  94. ctx = proxy.ContextWithTarget(ctx, dest)
  95. stream := ray.New(ctx)
  96. go handler.Dispatch(ctx, stream)
  97. return ray.NewConnection(stream.InboundOutput(), stream.InboundInput()), nil
  98. }
  99. newError("failed to get outbound handler with tag: ", tag).AtWarning().WithContext(ctx).WriteToLog()
  100. }
  101. if h.senderSettings.Via != nil {
  102. ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
  103. }
  104. if h.senderSettings.StreamSettings != nil {
  105. ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
  106. }
  107. }
  108. return internet.Dial(ctx, dest)
  109. }
  110. // GetOutbound implements proxy.GetOutbound.
  111. func (h *Handler) GetOutbound() proxy.Outbound {
  112. return h.proxy
  113. }
  114. // Start implements common.Runnable.
  115. func (h *Handler) Start() error {
  116. return nil
  117. }
  118. // Close implements common.Closable.
  119. func (h *Handler) Close() error {
  120. common.Close(h.mux)
  121. return nil
  122. }