default.go 3.1 KB

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