handler.go 3.9 KB

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