handler.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package outbound
  2. import (
  3. "context"
  4. "io"
  5. "v2ray.com/core"
  6. "v2ray.com/core/app/proxyman"
  7. "v2ray.com/core/app/proxyman/mux"
  8. "v2ray.com/core/common/errors"
  9. "v2ray.com/core/common/net"
  10. "v2ray.com/core/proxy"
  11. "v2ray.com/core/transport/internet"
  12. "v2ray.com/core/transport/ray"
  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) (*Handler, error) {
  22. v := core.FromContext(ctx)
  23. if v == nil {
  24. return nil, newError("V is not in context")
  25. }
  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. default:
  39. return nil, newError("settings is not SenderConfig")
  40. }
  41. }
  42. proxyConfig, err := config.ProxySettings.GetInstance()
  43. if err != nil {
  44. return nil, err
  45. }
  46. proxyHandler, err := proxy.CreateOutboundHandler(ctx, proxyConfig)
  47. if err != nil {
  48. return nil, err
  49. }
  50. if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
  51. config := h.senderSettings.MultiplexSettings
  52. if config.Concurrency < 1 || config.Concurrency > 1024 {
  53. return nil, newError("invalid mux concurrency: ", config.Concurrency).AtWarning()
  54. }
  55. h.mux = mux.NewClientManager(proxyHandler, h, config)
  56. }
  57. h.proxy = proxyHandler
  58. return h, nil
  59. }
  60. // Tag implements core.OutboundHandler.
  61. func (h *Handler) Tag() string {
  62. return h.config.Tag
  63. }
  64. // Dispatch implements proxy.Outbound.Dispatch.
  65. func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
  66. if h.mux != nil {
  67. err := h.mux.Dispatch(ctx, outboundRay)
  68. if err != nil {
  69. newError("failed to process outbound traffic").Base(err).WriteToLog()
  70. outboundRay.OutboundOutput().CloseError()
  71. }
  72. } else {
  73. err := h.proxy.Process(ctx, outboundRay, h)
  74. // Ensure outbound ray is properly closed.
  75. if err != nil && errors.Cause(err) != io.EOF {
  76. newError("failed to process outbound traffic").Base(err).WriteToLog()
  77. outboundRay.OutboundOutput().CloseError()
  78. } else {
  79. outboundRay.OutboundOutput().Close()
  80. }
  81. outboundRay.OutboundInput().CloseError()
  82. }
  83. }
  84. var zeroAddr net.Addr = &net.TCPAddr{IP: []byte{0, 0, 0, 0}, Port: 0}
  85. // Dial implements proxy.Dialer.Dial().
  86. func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
  87. if h.senderSettings != nil {
  88. if h.senderSettings.ProxySettings.HasTag() {
  89. tag := h.senderSettings.ProxySettings.Tag
  90. handler := h.outboundManager.GetHandler(tag)
  91. if handler != nil {
  92. newError("proxying to ", tag, " for dest ", dest).AtDebug().WriteToLog()
  93. ctx = proxy.ContextWithTarget(ctx, dest)
  94. stream := ray.NewRay(ctx)
  95. go handler.Dispatch(ctx, stream)
  96. return ray.NewConnection(stream.InboundOutput(), stream.InboundInput()), nil
  97. }
  98. newError("failed to get outbound handler with tag: ", tag).AtWarning().WriteToLog()
  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. func (h *Handler) GetOutbound() proxy.Outbound {
  110. return h.proxy
  111. }