handler.go 3.8 KB

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