handler.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/common/session"
  10. "v2ray.com/core/common/vio"
  11. "v2ray.com/core/features/outbound"
  12. "v2ray.com/core/proxy"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/core/transport/pipe"
  15. )
  16. type Handler struct {
  17. config *core.OutboundHandlerConfig
  18. senderSettings *proxyman.SenderConfig
  19. streamSettings *internet.MemoryStreamConfig
  20. proxy proxy.Outbound
  21. outboundManager outbound.Manager
  22. mux *mux.ClientManager
  23. }
  24. func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbound.Handler, error) {
  25. v := core.MustFromContext(ctx)
  26. h := &Handler{
  27. config: config,
  28. outboundManager: v.OutboundHandlerManager(),
  29. }
  30. if config.SenderSettings != nil {
  31. senderSettings, err := config.SenderSettings.GetInstance()
  32. if err != nil {
  33. return nil, err
  34. }
  35. switch s := senderSettings.(type) {
  36. case *proxyman.SenderConfig:
  37. h.senderSettings = s
  38. mss, err := internet.ToMemoryStreamConfig(s.StreamSettings)
  39. if err != nil {
  40. return nil, newError("failed to parse stream settings").Base(err).AtWarning()
  41. }
  42. h.streamSettings = mss
  43. default:
  44. return nil, newError("settings is not SenderConfig")
  45. }
  46. }
  47. proxyConfig, err := config.ProxySettings.GetInstance()
  48. if err != nil {
  49. return nil, err
  50. }
  51. rawProxyHandler, err := common.CreateObject(ctx, proxyConfig)
  52. if err != nil {
  53. return nil, err
  54. }
  55. proxyHandler, ok := rawProxyHandler.(proxy.Outbound)
  56. if !ok {
  57. return nil, newError("not an outbound handler")
  58. }
  59. if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
  60. config := h.senderSettings.MultiplexSettings
  61. if config.Concurrency < 1 || config.Concurrency > 1024 {
  62. return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
  63. }
  64. h.mux = mux.NewClientManager(proxyHandler, h, config)
  65. }
  66. h.proxy = proxyHandler
  67. return h, nil
  68. }
  69. // Tag implements outbound.Handler.
  70. func (h *Handler) Tag() string {
  71. return h.config.Tag
  72. }
  73. // Dispatch implements proxy.Outbound.Dispatch.
  74. func (h *Handler) Dispatch(ctx context.Context, link *vio.Link) {
  75. if h.mux != nil {
  76. if err := h.mux.Dispatch(ctx, link); err != nil {
  77. newError("failed to process mux outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  78. pipe.CloseError(link.Writer)
  79. }
  80. } else {
  81. if err := h.proxy.Process(ctx, link, h); err != nil {
  82. // Ensure outbound ray is properly closed.
  83. newError("failed to process outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  84. pipe.CloseError(link.Writer)
  85. } else {
  86. common.Must(common.Close(link.Writer))
  87. }
  88. pipe.CloseError(link.Reader)
  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(session.ExportIDToError(ctx))
  99. ctx = session.ContextWithOutbound(ctx, &session.Outbound{
  100. Target: dest,
  101. })
  102. opts := pipe.OptionsFromContext(ctx)
  103. uplinkReader, uplinkWriter := pipe.New(opts...)
  104. downlinkReader, downlinkWriter := pipe.New(opts...)
  105. go handler.Dispatch(ctx, &vio.Link{Reader: uplinkReader, Writer: downlinkWriter})
  106. return net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader)), nil
  107. }
  108. newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog(session.ExportIDToError(ctx))
  109. }
  110. if h.senderSettings.Via != nil {
  111. outbound := session.OutboundFromContext(ctx)
  112. if outbound == nil {
  113. outbound = new(session.Outbound)
  114. ctx = session.ContextWithOutbound(ctx, outbound)
  115. }
  116. outbound.Gateway = h.senderSettings.Via.AsAddress()
  117. }
  118. ctx = internet.ContextWithStreamSettings(ctx, h.streamSettings)
  119. }
  120. return internet.Dial(ctx, dest)
  121. }
  122. // GetOutbound implements proxy.GetOutbound.
  123. func (h *Handler) GetOutbound() proxy.Outbound {
  124. return h.proxy
  125. }
  126. // Start implements common.Runnable.
  127. func (h *Handler) Start() error {
  128. return nil
  129. }
  130. // Close implements common.Closable.
  131. func (h *Handler) Close() error {
  132. common.Close(h.mux)
  133. return nil
  134. }