default.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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"
  9. "v2ray.com/core/common/buf"
  10. "v2ray.com/core/common/errors"
  11. "v2ray.com/core/common/log"
  12. v2net "v2ray.com/core/common/net"
  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.OnInitialize(func() error {
  23. d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
  24. if d.ohm == nil {
  25. return errors.New("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
  26. }
  27. d.router = router.FromSpace(space)
  28. return nil
  29. })
  30. return d
  31. }
  32. func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
  33. dispatcher := v.ohm.GetDefaultHandler()
  34. destination := session.Destination
  35. if v.router != nil {
  36. if tag, err := v.router.TakeDetour(session); err == nil {
  37. if handler := v.ohm.GetHandler(tag); handler != nil {
  38. log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].")
  39. dispatcher = handler
  40. } else {
  41. log.Warning("DefaultDispatcher: Nonexisting tag: ", tag)
  42. }
  43. } else {
  44. log.Info("DefaultDispatcher: Default route for ", destination)
  45. }
  46. }
  47. direct := ray.NewRay()
  48. var waitFunc func() error
  49. if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
  50. waitFunc = noOpWait()
  51. } else {
  52. wdi := &waitDataInspector{
  53. hasData: make(chan bool, 1),
  54. }
  55. direct.AddInspector(wdi)
  56. waitFunc = waitForData(wdi)
  57. }
  58. go v.waitAndDispatch(waitFunc, destination, direct, dispatcher)
  59. return direct
  60. }
  61. func (v *DefaultDispatcher) waitAndDispatch(wait func() error, destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
  62. if err := wait(); err != nil {
  63. log.Info("DefaultDispatcher: Failed precondition: ", err)
  64. link.OutboundInput().CloseError()
  65. link.OutboundOutput().Close()
  66. return
  67. }
  68. dispatcher.Dispatch(destination, link)
  69. }
  70. type DefaultDispatcherFactory struct{}
  71. func (v DefaultDispatcherFactory) Create(space app.Space, config interface{}) (app.Application, error) {
  72. return NewDefaultDispatcher(space), nil
  73. }
  74. func init() {
  75. common.Must(app.RegisterApplicationFactory((*dispatcher.Config)(nil), DefaultDispatcherFactory{}))
  76. }
  77. type waitDataInspector struct {
  78. hasData chan bool
  79. }
  80. func (wdi *waitDataInspector) Input(*buf.Buffer) {
  81. select {
  82. case wdi.hasData <- true:
  83. default:
  84. }
  85. }
  86. func (wdi *waitDataInspector) WaitForData() bool {
  87. select {
  88. case <-wdi.hasData:
  89. return true
  90. case <-time.After(time.Minute):
  91. return false
  92. }
  93. }
  94. func waitForData(wdi *waitDataInspector) func() error {
  95. return func() error {
  96. if wdi.WaitForData() {
  97. return nil
  98. }
  99. return errors.New("DefaultDispatcher: No data.")
  100. }
  101. }
  102. func noOpWait() func() error {
  103. return func() error {
  104. return nil
  105. }
  106. }