handler.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/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. // 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.NewClientManager(proxyHandler, h, config.Concurrency)
  67. }
  68. h.proxy = proxyHandler
  69. return h, nil
  70. }
  71. // Tag implements outbound.Handler.
  72. func (h *Handler) Tag() string {
  73. return h.tag
  74. }
  75. // Dispatch implements proxy.Outbound.Dispatch.
  76. func (h *Handler) Dispatch(ctx context.Context, link *vio.Link) {
  77. if h.mux != nil {
  78. if err := h.mux.Dispatch(ctx, link); err != nil {
  79. newError("failed to process mux outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  80. pipe.CloseError(link.Writer)
  81. }
  82. } else {
  83. if err := h.proxy.Process(ctx, link, h); err != nil {
  84. // Ensure outbound ray is properly closed.
  85. newError("failed to process outbound traffic").Base(err).WriteToLog(session.ExportIDToError(ctx))
  86. pipe.CloseError(link.Writer)
  87. } else {
  88. common.Must(common.Close(link.Writer))
  89. }
  90. pipe.CloseError(link.Reader)
  91. }
  92. }
  93. // Dial implements internet.Dialer.
  94. func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  95. if h.senderSettings != nil {
  96. if h.senderSettings.ProxySettings.HasTag() {
  97. tag := h.senderSettings.ProxySettings.Tag
  98. handler := h.outboundManager.GetHandler(tag)
  99. if handler != nil {
  100. newError("proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog(session.ExportIDToError(ctx))
  101. ctx = session.ContextWithOutbound(ctx, &session.Outbound{
  102. Target: dest,
  103. })
  104. opts := pipe.OptionsFromContext(ctx)
  105. uplinkReader, uplinkWriter := pipe.New(opts...)
  106. downlinkReader, downlinkWriter := pipe.New(opts...)
  107. go handler.Dispatch(ctx, &vio.Link{Reader: uplinkReader, Writer: downlinkWriter})
  108. return net.NewConnection(net.ConnectionInputMulti(uplinkWriter), net.ConnectionOutputMulti(downlinkReader)), nil
  109. }
  110. newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog(session.ExportIDToError(ctx))
  111. }
  112. if h.senderSettings.Via != nil {
  113. outbound := session.OutboundFromContext(ctx)
  114. if outbound == nil {
  115. outbound = new(session.Outbound)
  116. ctx = session.ContextWithOutbound(ctx, outbound)
  117. }
  118. outbound.Gateway = h.senderSettings.Via.AsAddress()
  119. }
  120. ctx = internet.ContextWithStreamSettings(ctx, h.streamSettings)
  121. }
  122. return internet.Dial(ctx, dest)
  123. }
  124. // GetOutbound implements proxy.GetOutbound.
  125. func (h *Handler) GetOutbound() proxy.Outbound {
  126. return h.proxy
  127. }
  128. // Start implements common.Runnable.
  129. func (h *Handler) Start() error {
  130. return nil
  131. }
  132. // Close implements common.Closable.
  133. func (h *Handler) Close() error {
  134. common.Close(h.mux)
  135. return nil
  136. }