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