default.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package impl
  2. //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg impl -path App,Dispatcher,Default
  3. import (
  4. "context"
  5. "time"
  6. "v2ray.com/core/app"
  7. "v2ray.com/core/app/dispatcher"
  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/net"
  13. "v2ray.com/core/proxy"
  14. "v2ray.com/core/transport/ray"
  15. )
  16. var (
  17. errSniffingTimeout = newError("timeout on sniffing")
  18. )
  19. var (
  20. _ app.Application = (*DefaultDispatcher)(nil)
  21. )
  22. // DefaultDispatcher is a default implementation of Dispatcher.
  23. type DefaultDispatcher struct {
  24. ohm proxyman.OutboundHandlerManager
  25. router *router.Router
  26. }
  27. // NewDefaultDispatcher create a new DefaultDispatcher.
  28. func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
  29. space := app.SpaceFromContext(ctx)
  30. if space == nil {
  31. return nil, newError("no space in context")
  32. }
  33. d := &DefaultDispatcher{}
  34. space.On(app.SpaceInitializing, func(interface{}) error {
  35. d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
  36. if d.ohm == nil {
  37. return newError("OutboundHandlerManager is not found in the space")
  38. }
  39. d.router = router.FromSpace(space)
  40. return nil
  41. })
  42. return d, nil
  43. }
  44. // Start implements app.Application.
  45. func (*DefaultDispatcher) Start() error {
  46. return nil
  47. }
  48. // Close implements app.Application.
  49. func (*DefaultDispatcher) Close() {}
  50. // Interface implements app.Application.
  51. func (*DefaultDispatcher) Interface() interface{} {
  52. return (*dispatcher.Interface)(nil)
  53. }
  54. // Dispatch implements Dispatcher.Interface.
  55. func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
  56. if !destination.IsValid() {
  57. panic("Dispatcher: Invalid destination.")
  58. }
  59. ctx = proxy.ContextWithTarget(ctx, destination)
  60. outbound := ray.NewRay(ctx)
  61. sniferList := proxyman.ProtocoSniffersFromContext(ctx)
  62. if destination.Address.Family().IsDomain() || len(sniferList) == 0 {
  63. go d.routedDispatch(ctx, outbound, destination)
  64. } else {
  65. go func() {
  66. domain, err := snifer(ctx, sniferList, outbound)
  67. if err == nil {
  68. newError("sniffed domain: ", domain).WriteToLog()
  69. destination.Address = net.ParseAddress(domain)
  70. ctx = proxy.ContextWithTarget(ctx, destination)
  71. }
  72. d.routedDispatch(ctx, outbound, destination)
  73. }()
  74. }
  75. return outbound, nil
  76. }
  77. func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound ray.OutboundRay) (string, error) {
  78. payload := buf.New()
  79. defer payload.Release()
  80. sniffer := NewSniffer(sniferList)
  81. totalAttempt := 0
  82. for {
  83. select {
  84. case <-ctx.Done():
  85. return "", ctx.Err()
  86. default:
  87. totalAttempt++
  88. if totalAttempt > 5 {
  89. return "", errSniffingTimeout
  90. }
  91. outbound.OutboundInput().Peek(payload)
  92. if !payload.IsEmpty() {
  93. domain, err := sniffer.Sniff(payload.Bytes())
  94. if err != ErrMoreData {
  95. return domain, err
  96. }
  97. }
  98. if payload.IsFull() {
  99. return "", ErrInvalidData
  100. }
  101. time.Sleep(time.Millisecond * 100)
  102. }
  103. }
  104. }
  105. func (d *DefaultDispatcher) routedDispatch(ctx context.Context, outbound ray.OutboundRay, destination net.Destination) {
  106. dispatcher := d.ohm.GetDefaultHandler()
  107. if d.router != nil {
  108. if tag, err := d.router.TakeDetour(ctx); err == nil {
  109. if handler := d.ohm.GetHandler(tag); handler != nil {
  110. newError("taking detour [", tag, "] for [", destination, "]").WriteToLog()
  111. dispatcher = handler
  112. } else {
  113. newError("nonexisting tag: ", tag).AtWarning().WriteToLog()
  114. }
  115. } else {
  116. newError("default route for ", destination).WriteToLog()
  117. }
  118. }
  119. dispatcher.Dispatch(ctx, outbound)
  120. }
  121. func init() {
  122. common.Must(common.RegisterConfig((*dispatcher.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  123. return NewDefaultDispatcher(ctx, config.(*dispatcher.Config))
  124. }))
  125. }