default.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package impl
  2. import (
  3. "v2ray.com/core/app"
  4. "v2ray.com/core/app/dispatcher"
  5. "v2ray.com/core/app/proxyman"
  6. "v2ray.com/core/app/router"
  7. "v2ray.com/core/common/buf"
  8. "v2ray.com/core/common/errors"
  9. "v2ray.com/core/common/log"
  10. v2net "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/serial"
  12. "v2ray.com/core/proxy"
  13. "v2ray.com/core/transport/ray"
  14. )
  15. type DefaultDispatcher struct {
  16. ohm proxyman.OutboundHandlerManager
  17. router *router.Router
  18. }
  19. func NewDefaultDispatcher(space app.Space) *DefaultDispatcher {
  20. d := &DefaultDispatcher{}
  21. space.InitializeApplication(func() error {
  22. return d.Initialize(space)
  23. })
  24. return d
  25. }
  26. // Initialize initializes the dispatcher.
  27. // Private: Used by app.Space only.
  28. func (v *DefaultDispatcher) Initialize(space app.Space) error {
  29. if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
  30. return errors.New("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
  31. }
  32. v.ohm = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
  33. if space.HasApp(router.APP_ID) {
  34. v.router = space.GetApp(router.APP_ID).(*router.Router)
  35. }
  36. return nil
  37. }
  38. // Release implements common.Releasable.Release().
  39. func (v *DefaultDispatcher) Release() {
  40. }
  41. func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
  42. direct := ray.NewRay()
  43. dispatcher := v.ohm.GetDefaultHandler()
  44. destination := session.Destination
  45. if v.router != nil {
  46. if tag, err := v.router.TakeDetour(session); err == nil {
  47. if handler := v.ohm.GetHandler(tag); handler != nil {
  48. log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].")
  49. dispatcher = handler
  50. } else {
  51. log.Warning("DefaultDispatcher: Nonexisting tag: ", tag)
  52. }
  53. } else {
  54. log.Info("DefaultDispatcher: Default route for ", destination)
  55. }
  56. }
  57. if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
  58. go dispatcher.Dispatch(destination, buf.NewLocal(32), direct)
  59. } else {
  60. go v.FilterPacketAndDispatch(destination, direct, dispatcher)
  61. }
  62. return direct
  63. }
  64. // FilterPacketAndDispatch waits for a payload from source and starts dispatching.
  65. // Private: Visible for testing.
  66. func (v *DefaultDispatcher) FilterPacketAndDispatch(destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
  67. payload, err := link.OutboundInput().Read()
  68. if err != nil {
  69. log.Info("DefaultDispatcher: No payload towards ", destination, ", stopping now.")
  70. link.OutboundInput().Release()
  71. link.OutboundOutput().Release()
  72. return
  73. }
  74. dispatcher.Dispatch(destination, payload, link)
  75. }
  76. type DefaultDispatcherFactory struct{}
  77. func (v DefaultDispatcherFactory) Create(space app.Space, config interface{}) (app.Application, error) {
  78. return NewDefaultDispatcher(space), nil
  79. }
  80. func (v DefaultDispatcherFactory) AppId() app.ID {
  81. return dispatcher.APP_ID
  82. }
  83. func init() {
  84. app.RegisterApplicationFactory(serial.GetMessageType(new(dispatcher.Config)), DefaultDispatcherFactory{})
  85. }