default.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // Private: Used by app.Space only.
  27. func (v *DefaultDispatcher) Initialize(space app.Space) error {
  28. if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
  29. return errors.New("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
  30. }
  31. v.ohm = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
  32. if space.HasApp(router.APP_ID) {
  33. v.router = space.GetApp(router.APP_ID).(*router.Router)
  34. }
  35. return nil
  36. }
  37. func (v *DefaultDispatcher) Release() {
  38. }
  39. func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
  40. direct := ray.NewRay()
  41. dispatcher := v.ohm.GetDefaultHandler()
  42. destination := session.Destination
  43. if v.router != nil {
  44. if tag, err := v.router.TakeDetour(session); err == nil {
  45. if handler := v.ohm.GetHandler(tag); handler != nil {
  46. log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].")
  47. dispatcher = handler
  48. } else {
  49. log.Warning("DefaultDispatcher: Nonexisting tag: ", tag)
  50. }
  51. } else {
  52. log.Info("DefaultDispatcher: Default route for ", destination)
  53. }
  54. }
  55. if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
  56. go dispatcher.Dispatch(destination, buf.NewLocal(32), direct)
  57. } else {
  58. go v.FilterPacketAndDispatch(destination, direct, dispatcher)
  59. }
  60. return direct
  61. }
  62. // Private: Visible for testing.
  63. func (v *DefaultDispatcher) FilterPacketAndDispatch(destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
  64. payload, err := link.OutboundInput().Read()
  65. if err != nil {
  66. log.Info("DefaultDispatcher: No payload towards ", destination, ", stopping now.")
  67. link.OutboundInput().Release()
  68. link.OutboundOutput().Release()
  69. return
  70. }
  71. dispatcher.Dispatch(destination, payload, link)
  72. }
  73. type DefaultDispatcherFactory struct{}
  74. func (v DefaultDispatcherFactory) Create(space app.Space, config interface{}) (app.Application, error) {
  75. return NewDefaultDispatcher(space), nil
  76. }
  77. func (v DefaultDispatcherFactory) AppId() app.ID {
  78. return dispatcher.APP_ID
  79. }
  80. func init() {
  81. app.RegisterApplicationFactory(serial.GetMessageType(new(dispatcher.Config)), DefaultDispatcherFactory{})
  82. }