default.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/log"
  8. "v2ray.com/core/app/proxyman"
  9. "v2ray.com/core/app/router"
  10. "v2ray.com/core/common"
  11. "v2ray.com/core/common/buf"
  12. "v2ray.com/core/common/errors"
  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) Start() error {
  37. return nil
  38. }
  39. func (DefaultDispatcher) Close() {}
  40. func (DefaultDispatcher) Interface() interface{} {
  41. return (*dispatcher.Interface)(nil)
  42. }
  43. func (v *DefaultDispatcher) DispatchToOutbound(ctx context.Context) ray.InboundRay {
  44. dispatcher := v.ohm.GetDefaultHandler()
  45. destination := proxy.DestinationFromContext(ctx)
  46. if !destination.IsValid() {
  47. panic("Dispatcher: Invalid destination.")
  48. }
  49. if v.router != nil {
  50. if tag, err := v.router.TakeDetour(ctx); err == nil {
  51. if handler := v.ohm.GetHandler(tag); handler != nil {
  52. log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].")
  53. dispatcher = handler
  54. } else {
  55. log.Warning("DefaultDispatcher: Nonexisting tag: ", tag)
  56. }
  57. } else {
  58. log.Info("DefaultDispatcher: Default route for ", destination)
  59. }
  60. }
  61. direct := ray.NewRay(ctx)
  62. var waitFunc func() error
  63. if allowPassiveConnection, ok := proxy.AllowPassiveConnectionFromContext(ctx); ok && allowPassiveConnection {
  64. waitFunc = noOpWait()
  65. } else {
  66. wdi := &waitDataInspector{
  67. hasData: make(chan bool, 1),
  68. }
  69. direct.AddInspector(wdi)
  70. waitFunc = waitForData(wdi)
  71. }
  72. go v.waitAndDispatch(ctx, waitFunc, direct, dispatcher)
  73. return direct
  74. }
  75. func (v *DefaultDispatcher) waitAndDispatch(ctx context.Context, wait func() error, link ray.OutboundRay, dispatcher proxyman.OutboundHandler) {
  76. if err := wait(); err != nil {
  77. log.Info("DefaultDispatcher: Failed precondition: ", err)
  78. link.OutboundInput().CloseError()
  79. link.OutboundOutput().CloseError()
  80. return
  81. }
  82. dispatcher.Dispatch(ctx, link)
  83. }
  84. func init() {
  85. common.Must(common.RegisterConfig((*dispatcher.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  86. return NewDefaultDispatcher(ctx, config.(*dispatcher.Config))
  87. }))
  88. }
  89. type waitDataInspector struct {
  90. hasData chan bool
  91. }
  92. func (wdi *waitDataInspector) Input(*buf.Buffer) {
  93. select {
  94. case wdi.hasData <- true:
  95. default:
  96. }
  97. }
  98. func (wdi *waitDataInspector) WaitForData() bool {
  99. select {
  100. case <-wdi.hasData:
  101. return true
  102. case <-time.After(time.Minute):
  103. return false
  104. }
  105. }
  106. func waitForData(wdi *waitDataInspector) func() error {
  107. return func() error {
  108. if wdi.WaitForData() {
  109. return nil
  110. }
  111. return errors.New("DefaultDispatcher: No data.")
  112. }
  113. }
  114. func noOpWait() func() error {
  115. return func() error {
  116. return nil
  117. }
  118. }