handler.go 3.9 KB

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