handler.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/proxy"
  11. "v2ray.com/core/transport/internet"
  12. "v2ray.com/core/transport/pipe"
  13. )
  14. type Handler struct {
  15. config *core.OutboundHandlerConfig
  16. senderSettings *proxyman.SenderConfig
  17. streamSettings *internet.MemoryStreamConfig
  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.MustFromContext(ctx)
  24. h := &Handler{
  25. config: config,
  26. outboundManager: v.OutboundHandlerManager(),
  27. }
  28. if config.SenderSettings != nil {
  29. senderSettings, err := config.SenderSettings.GetInstance()
  30. if err != nil {
  31. return nil, err
  32. }
  33. switch s := senderSettings.(type) {
  34. case *proxyman.SenderConfig:
  35. h.senderSettings = s
  36. mss, err := internet.ToMemoryStreamConfig(s.StreamSettings)
  37. if err != nil {
  38. return nil, newError("failed to parse stream settings").Base(err).AtWarning()
  39. }
  40. h.streamSettings = mss
  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, link *core.Link) {
  73. if h.mux != nil {
  74. if err := h.mux.Dispatch(ctx, link); err != nil {
  75. newError("failed to process mux outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  76. pipe.CloseError(link.Writer)
  77. }
  78. } else {
  79. if err := h.proxy.Process(ctx, link, h); err != nil {
  80. // Ensure outbound ray is properly closed.
  81. newError("failed to process outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  82. pipe.CloseError(link.Writer)
  83. } else {
  84. common.Must(common.Close(link.Writer))
  85. }
  86. pipe.CloseError(link.Reader)
  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(session.ExportIDToError(ctx))
  97. ctx = proxy.ContextWithTarget(ctx, dest)
  98. opts := pipe.OptionsFromContext(ctx)
  99. uplinkReader, uplinkWriter := pipe.New(opts...)
  100. downlinkReader, downlinkWriter := pipe.New(opts...)
  101. go handler.Dispatch(ctx, &core.Link{Reader: uplinkReader, Writer: downlinkWriter})
  102. return net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader)), nil
  103. }
  104. newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog(session.ExportIDToError(ctx))
  105. }
  106. if h.senderSettings.Via != nil {
  107. ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
  108. }
  109. ctx = internet.ContextWithStreamSettings(ctx, h.streamSettings)
  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.Closable.
  122. func (h *Handler) Close() error {
  123. common.Close(h.mux)
  124. return nil
  125. }