handler.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) (*Handler, 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, outboundRay ray.OutboundRay) {
  66. if h.mux != nil {
  67. err := h.mux.Dispatch(ctx, outboundRay)
  68. if err != nil {
  69. newError("failed to process outbound traffic").Base(err).WithContext(ctx).WriteToLog()
  70. outboundRay.OutboundOutput().CloseError()
  71. }
  72. } else {
  73. err := h.proxy.Process(ctx, outboundRay, h)
  74. // Ensure outbound ray is properly closed.
  75. if err != nil {
  76. newError("failed to process outbound traffic").Base(err).WithContext(ctx).WriteToLog()
  77. outboundRay.OutboundOutput().CloseError()
  78. } else {
  79. outboundRay.OutboundOutput().Close()
  80. }
  81. outboundRay.OutboundInput().CloseError()
  82. }
  83. }
  84. // Dial implements proxy.Dialer.Dial().
  85. func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  86. if h.senderSettings != nil {
  87. if h.senderSettings.ProxySettings.HasTag() {
  88. tag := h.senderSettings.ProxySettings.Tag
  89. handler := h.outboundManager.GetHandler(tag)
  90. if handler != nil {
  91. newError("proxying to ", tag, " for dest ", dest).AtDebug().WithContext(ctx).WriteToLog()
  92. ctx = proxy.ContextWithTarget(ctx, dest)
  93. stream := ray.New(ctx)
  94. go handler.Dispatch(ctx, stream)
  95. return ray.NewConnection(stream.InboundOutput(), stream.InboundInput()), nil
  96. }
  97. newError("failed to get outbound handler with tag: ", tag).AtWarning().WithContext(ctx).WriteToLog()
  98. }
  99. if h.senderSettings.Via != nil {
  100. ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
  101. }
  102. if h.senderSettings.StreamSettings != nil {
  103. ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
  104. }
  105. }
  106. return internet.Dial(ctx, dest)
  107. }
  108. // GetOutbound implements proxy.GetOutbound.
  109. func (h *Handler) GetOutbound() proxy.Outbound {
  110. return h.proxy
  111. }
  112. // Start implements common.Runnable.
  113. func (h *Handler) Start() error {
  114. return nil
  115. }
  116. // Close implements common.Closable.
  117. func (h *Handler) Close() error {
  118. common.Close(h.mux)
  119. return nil
  120. }