handler.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package outbound
  2. import (
  3. "context"
  4. "v2ray.com/core"
  5. "v2ray.com/core/app/proxyman"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/mux"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/session"
  10. "v2ray.com/core/features/outbound"
  11. "v2ray.com/core/proxy"
  12. "v2ray.com/core/transport"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/core/transport/pipe"
  15. )
  16. // Handler is an implements of outbound.Handler.
  17. type Handler struct {
  18. tag string
  19. senderSettings *proxyman.SenderConfig
  20. streamSettings *internet.MemoryStreamConfig
  21. proxy proxy.Outbound
  22. outboundManager outbound.Manager
  23. mux *mux.ClientManager
  24. }
  25. // NewHandler create a new Handler based on the given configuration.
  26. func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbound.Handler, error) {
  27. v := core.MustFromContext(ctx)
  28. h := &Handler{
  29. tag: config.Tag,
  30. outboundManager: v.GetFeature(outbound.ManagerType()).(outbound.Manager),
  31. }
  32. if config.SenderSettings != nil {
  33. senderSettings, err := config.SenderSettings.GetInstance()
  34. if err != nil {
  35. return nil, err
  36. }
  37. switch s := senderSettings.(type) {
  38. case *proxyman.SenderConfig:
  39. h.senderSettings = s
  40. mss, err := internet.ToMemoryStreamConfig(s.StreamSettings)
  41. if err != nil {
  42. return nil, newError("failed to parse stream settings").Base(err).AtWarning()
  43. }
  44. h.streamSettings = mss
  45. default:
  46. return nil, newError("settings is not SenderConfig")
  47. }
  48. }
  49. proxyConfig, err := config.ProxySettings.GetInstance()
  50. if err != nil {
  51. return nil, err
  52. }
  53. rawProxyHandler, err := common.CreateObject(ctx, proxyConfig)
  54. if err != nil {
  55. return nil, err
  56. }
  57. proxyHandler, ok := rawProxyHandler.(proxy.Outbound)
  58. if !ok {
  59. return nil, newError("not an outbound handler")
  60. }
  61. if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
  62. config := h.senderSettings.MultiplexSettings
  63. if config.Concurrency < 1 || config.Concurrency > 1024 {
  64. return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
  65. }
  66. h.mux = &mux.ClientManager{
  67. Picker: &mux.IncrementalWorkerPicker{
  68. Factory: &mux.DialingWorkerFactory{
  69. Proxy: proxyHandler,
  70. Dialer: h,
  71. Strategy: mux.ClientStrategy{
  72. MaxConcurrency: config.Concurrency,
  73. MaxConnection: 128,
  74. },
  75. },
  76. },
  77. }
  78. }
  79. h.proxy = proxyHandler
  80. return h, nil
  81. }
  82. // Tag implements outbound.Handler.
  83. func (h *Handler) Tag() string {
  84. return h.tag
  85. }
  86. // Dispatch implements proxy.Outbound.Dispatch.
  87. func (h *Handler) Dispatch(ctx context.Context, link *transport.Link) {
  88. if h.mux != nil {
  89. if err := h.mux.Dispatch(ctx, link); err != nil {
  90. newError("failed to process mux outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  91. pipe.CloseError(link.Writer)
  92. }
  93. } else {
  94. if err := h.proxy.Process(ctx, link, h); err != nil {
  95. // Ensure outbound ray is properly closed.
  96. newError("failed to process outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  97. pipe.CloseError(link.Writer)
  98. } else {
  99. common.Must(common.Close(link.Writer))
  100. }
  101. pipe.CloseError(link.Reader)
  102. }
  103. }
  104. // Dial implements internet.Dialer.
  105. func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  106. if h.senderSettings != nil {
  107. if h.senderSettings.ProxySettings.HasTag() {
  108. tag := h.senderSettings.ProxySettings.Tag
  109. handler := h.outboundManager.GetHandler(tag)
  110. if handler != nil {
  111. newError("proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog(session.ExportIDToError(ctx))
  112. ctx = session.ContextWithOutbound(ctx, &session.Outbound{
  113. Target: dest,
  114. })
  115. opts := pipe.OptionsFromContext(ctx)
  116. uplinkReader, uplinkWriter := pipe.New(opts...)
  117. downlinkReader, downlinkWriter := pipe.New(opts...)
  118. go handler.Dispatch(ctx, &transport.Link{Reader: uplinkReader, Writer: downlinkWriter})
  119. return net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader)), nil
  120. }
  121. newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog(session.ExportIDToError(ctx))
  122. }
  123. if h.senderSettings.Via != nil {
  124. outbound := session.OutboundFromContext(ctx)
  125. if outbound == nil {
  126. outbound = new(session.Outbound)
  127. ctx = session.ContextWithOutbound(ctx, outbound)
  128. }
  129. outbound.Gateway = h.senderSettings.Via.AsAddress()
  130. }
  131. ctx = internet.ContextWithStreamSettings(ctx, h.streamSettings)
  132. }
  133. return internet.Dial(ctx, dest)
  134. }
  135. // GetOutbound implements proxy.GetOutbound.
  136. func (h *Handler) GetOutbound() proxy.Outbound {
  137. return h.proxy
  138. }
  139. // Start implements common.Runnable.
  140. func (h *Handler) Start() error {
  141. return nil
  142. }
  143. // Close implements common.Closable.
  144. func (h *Handler) Close() error {
  145. common.Close(h.mux)
  146. return nil
  147. }