default.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "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(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
  21. space := app.SpaceFromContext(ctx)
  22. if space == nil {
  23. return nil, errors.New("DefaultDispatcher: No space in context.")
  24. }
  25. d := &DefaultDispatcher{}
  26. space.OnInitialize(func() error {
  27. d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
  28. if d.ohm == nil {
  29. return errors.New("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
  30. }
  31. d.router = router.FromSpace(space)
  32. return nil
  33. })
  34. return d, nil
  35. }
  36. func (DefaultDispatcher) Interface() interface{} {
  37. return (*dispatcher.Interface)(nil)
  38. }
  39. func (v *DefaultDispatcher) DispatchToOutbound(ctx context.Context) ray.InboundRay {
  40. dispatcher := v.ohm.GetDefaultHandler()
  41. destination := proxy.DestinationFromContext(ctx)
  42. if !destination.IsValid() {
  43. panic("Dispatcher: Invalid destination.")
  44. }
  45. if v.router != nil {
  46. if tag, err := v.router.TakeDetour(ctx); 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(ctx)
  58. var waitFunc func() error
  59. if allowPassiveConnection, ok := proxy.AllowPassiveConnectionFromContext(ctx); ok && 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(ctx, waitFunc, direct, dispatcher)
  69. return direct
  70. }
  71. func (v *DefaultDispatcher) waitAndDispatch(ctx context.Context, wait func() error, link ray.OutboundRay, dispatcher proxyman.OutboundHandler) {
  72. if err := wait(); err != nil {
  73. log.Info("DefaultDispatcher: Failed precondition: ", err)
  74. link.OutboundInput().CloseError()
  75. link.OutboundOutput().CloseError()
  76. return
  77. }
  78. dispatcher.Dispatch(ctx, link)
  79. }
  80. func init() {
  81. common.Must(common.RegisterConfig((*dispatcher.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  82. return NewDefaultDispatcher(ctx, config.(*dispatcher.Config))
  83. }))
  84. }
  85. type waitDataInspector struct {
  86. hasData chan bool
  87. }
  88. func (wdi *waitDataInspector) Input(*buf.Buffer) {
  89. select {
  90. case wdi.hasData <- true:
  91. default:
  92. }
  93. }
  94. func (wdi *waitDataInspector) WaitForData() bool {
  95. select {
  96. case <-wdi.hasData:
  97. return true
  98. case <-time.After(time.Minute):
  99. return false
  100. }
  101. }
  102. func waitForData(wdi *waitDataInspector) func() error {
  103. return func() error {
  104. if wdi.WaitForData() {
  105. return nil
  106. }
  107. return errors.New("DefaultDispatcher: No data.")
  108. }
  109. }
  110. func noOpWait() func() error {
  111. return func() error {
  112. return nil
  113. }
  114. }