default.go 3.3 KB

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