default.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
  40. dispatcher := v.ohm.GetDefaultHandler()
  41. destination := session.Destination
  42. if v.router != nil {
  43. if tag, err := v.router.TakeDetour(session); err == nil {
  44. if handler := v.ohm.GetHandler(tag); handler != nil {
  45. log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].")
  46. dispatcher = handler
  47. } else {
  48. log.Warning("DefaultDispatcher: Nonexisting tag: ", tag)
  49. }
  50. } else {
  51. log.Info("DefaultDispatcher: Default route for ", destination)
  52. }
  53. }
  54. direct := ray.NewRay()
  55. var waitFunc func() error
  56. if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
  57. waitFunc = noOpWait()
  58. } else {
  59. wdi := &waitDataInspector{
  60. hasData: make(chan bool, 1),
  61. }
  62. direct.AddInspector(wdi)
  63. waitFunc = waitForData(wdi)
  64. }
  65. go v.waitAndDispatch(waitFunc, destination, direct, dispatcher)
  66. return direct
  67. }
  68. func (v *DefaultDispatcher) waitAndDispatch(wait func() error, destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
  69. if err := wait(); err != nil {
  70. log.Info("DefaultDispatcher: Failed precondition: ", err)
  71. link.OutboundInput().ForceClose()
  72. link.OutboundOutput().Close()
  73. return
  74. }
  75. dispatcher.Dispatch(destination, link)
  76. }
  77. type DefaultDispatcherFactory struct{}
  78. func (v DefaultDispatcherFactory) Create(space app.Space, config interface{}) (app.Application, error) {
  79. return NewDefaultDispatcher(space), nil
  80. }
  81. func (v DefaultDispatcherFactory) AppId() app.ID {
  82. return dispatcher.APP_ID
  83. }
  84. func init() {
  85. app.RegisterApplicationFactory(serial.GetMessageType(new(dispatcher.Config)), DefaultDispatcherFactory{})
  86. }
  87. type waitDataInspector struct {
  88. hasData chan bool
  89. }
  90. func (wdi *waitDataInspector) Input(*buf.Buffer) {
  91. select {
  92. case wdi.hasData <- true:
  93. default:
  94. }
  95. }
  96. func (wdi *waitDataInspector) WaitForData() bool {
  97. select {
  98. case <-wdi.hasData:
  99. return true
  100. case <-time.After(time.Minute):
  101. return false
  102. }
  103. }
  104. func waitForData(wdi *waitDataInspector) func() error {
  105. return func() error {
  106. if wdi.WaitForData() {
  107. return nil
  108. }
  109. return errors.New("DefaultDispatcher: No data.")
  110. }
  111. }
  112. func noOpWait() func() error {
  113. return func() error {
  114. return nil
  115. }
  116. }