default.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package impl
  2. import (
  3. "time"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dispatcher"
  6. "v2ray.com/core/app/proxyman"
  7. "v2ray.com/core/app/router"
  8. "v2ray.com/core/common/buf"
  9. "v2ray.com/core/common/errors"
  10. "v2ray.com/core/common/log"
  11. v2net "v2ray.com/core/common/net"
  12. "v2ray.com/core/common/serial"
  13. "v2ray.com/core/proxy"
  14. "v2ray.com/core/transport/ray"
  15. )
  16. type DefaultDispatcher struct {
  17. ohm proxyman.OutboundHandlerManager
  18. router *router.Router
  19. }
  20. func NewDefaultDispatcher(space app.Space) *DefaultDispatcher {
  21. d := &DefaultDispatcher{}
  22. space.InitializeApplication(func() error {
  23. return d.Initialize(space)
  24. })
  25. return d
  26. }
  27. // Initialize initializes the dispatcher.
  28. // Private: Used by app.Space only.
  29. func (v *DefaultDispatcher) Initialize(space app.Space) error {
  30. if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
  31. return errors.New("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
  32. }
  33. v.ohm = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
  34. if space.HasApp(router.APP_ID) {
  35. v.router = space.GetApp(router.APP_ID).(*router.Router)
  36. }
  37. return nil
  38. }
  39. // Release implements common.Releasable.Release().
  40. func (v *DefaultDispatcher) Release() {
  41. }
  42. func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
  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. direct := ray.NewRay()
  58. var waitFunc func() error
  59. if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
  60. waitFunc = noOpWait()
  61. } else {
  62. wdi := &waitDataInspector{
  63. hasData: make(chan bool, 1),
  64. }
  65. direct.AddInspector(wdi)
  66. waitFunc = waitForData(wdi)
  67. }
  68. go v.waitAndDispatch(waitFunc, destination, direct, dispatcher)
  69. return direct
  70. }
  71. func (v *DefaultDispatcher) waitAndDispatch(wait func() error, destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
  72. if err := wait(); err != nil {
  73. log.Info("DefaultDispatcher: Failed precondition: ", err)
  74. link.OutboundInput().ForceClose()
  75. link.OutboundOutput().Close()
  76. return
  77. }
  78. dispatcher.Dispatch(destination, link)
  79. }
  80. type DefaultDispatcherFactory struct{}
  81. func (v DefaultDispatcherFactory) Create(space app.Space, config interface{}) (app.Application, error) {
  82. return NewDefaultDispatcher(space), nil
  83. }
  84. func (v DefaultDispatcherFactory) AppId() app.ID {
  85. return dispatcher.APP_ID
  86. }
  87. func init() {
  88. app.RegisterApplicationFactory(serial.GetMessageType(new(dispatcher.Config)), DefaultDispatcherFactory{})
  89. }
  90. type waitDataInspector struct {
  91. hasData chan bool
  92. }
  93. func (wdi *waitDataInspector) Input(*buf.Buffer) {
  94. select {
  95. case wdi.hasData <- true:
  96. default:
  97. }
  98. }
  99. func (wdi *waitDataInspector) WaitForData() bool {
  100. select {
  101. case <-wdi.hasData:
  102. return true
  103. case <-time.After(time.Minute):
  104. return false
  105. }
  106. }
  107. func waitForData(wdi *waitDataInspector) func() error {
  108. return func() error {
  109. if wdi.WaitForData() {
  110. return nil
  111. }
  112. return errors.New("DefaultDispatcher: No data.")
  113. }
  114. }
  115. func noOpWait() func() error {
  116. return func() error {
  117. return nil
  118. }
  119. }