router.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package core
  2. import (
  3. "context"
  4. "sync"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/buf"
  7. "v2ray.com/core/common/errors"
  8. "v2ray.com/core/common/net"
  9. )
  10. // Link is a utility for connecting between an inbound and an outbound proxy handler.
  11. type Link struct {
  12. Reader buf.Reader
  13. Writer buf.Writer
  14. }
  15. // Dispatcher is a feature that dispatches inbound requests to outbound handlers based on rules.
  16. // Dispatcher is required to be registered in a V2Ray instance to make V2Ray function properly.
  17. type Dispatcher interface {
  18. Feature
  19. // Dispatch returns a Ray for transporting data for the given request.
  20. Dispatch(ctx context.Context, dest net.Destination) (*Link, error)
  21. }
  22. type syncDispatcher struct {
  23. sync.RWMutex
  24. Dispatcher
  25. }
  26. func (d *syncDispatcher) Dispatch(ctx context.Context, dest net.Destination) (*Link, error) {
  27. d.RLock()
  28. defer d.RUnlock()
  29. if d.Dispatcher == nil {
  30. return nil, newError("Dispatcher not set.").AtError()
  31. }
  32. return d.Dispatcher.Dispatch(ctx, dest)
  33. }
  34. func (d *syncDispatcher) Start() error {
  35. d.RLock()
  36. defer d.RUnlock()
  37. if d.Dispatcher == nil {
  38. return newError("Dispatcher not set.").AtError()
  39. }
  40. return d.Dispatcher.Start()
  41. }
  42. func (d *syncDispatcher) Close() error {
  43. d.RLock()
  44. defer d.RUnlock()
  45. return common.Close(d.Dispatcher)
  46. }
  47. func (d *syncDispatcher) Set(disp Dispatcher) {
  48. if disp == nil {
  49. return
  50. }
  51. d.Lock()
  52. defer d.Unlock()
  53. common.Close(d.Dispatcher) // nolint: errorcheck
  54. d.Dispatcher = disp
  55. }
  56. var (
  57. // ErrNoClue is for the situation that existing information is not enough to make a decision. For example, Router may return this error when there is no suitable route.
  58. ErrNoClue = errors.New("not enough information for making a decision")
  59. )
  60. // Router is a feature to choose an outbound tag for the given request.
  61. type Router interface {
  62. Feature
  63. // PickRoute returns a tag of an OutboundHandler based on the given context.
  64. PickRoute(ctx context.Context) (string, error)
  65. }
  66. type syncRouter struct {
  67. sync.RWMutex
  68. Router
  69. }
  70. func (r *syncRouter) PickRoute(ctx context.Context) (string, error) {
  71. r.RLock()
  72. defer r.RUnlock()
  73. if r.Router == nil {
  74. return "", ErrNoClue
  75. }
  76. return r.Router.PickRoute(ctx)
  77. }
  78. func (r *syncRouter) Start() error {
  79. r.RLock()
  80. defer r.RUnlock()
  81. if r.Router == nil {
  82. return nil
  83. }
  84. return r.Router.Start()
  85. }
  86. func (r *syncRouter) Close() error {
  87. r.RLock()
  88. defer r.RUnlock()
  89. return common.Close(r.Router)
  90. }
  91. func (r *syncRouter) Set(router Router) {
  92. if router == nil {
  93. return
  94. }
  95. r.Lock()
  96. defer r.Unlock()
  97. common.Close(r.Router) // nolint: errcheck
  98. r.Router = router
  99. }