default.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package dispatcher
  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"
  7. "v2ray.com/core/app/proxyman"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/buf"
  10. "v2ray.com/core/common/net"
  11. "v2ray.com/core/proxy"
  12. "v2ray.com/core/transport/ray"
  13. )
  14. var (
  15. errSniffingTimeout = newError("timeout on sniffing")
  16. )
  17. // DefaultDispatcher is a default implementation of Dispatcher.
  18. type DefaultDispatcher struct {
  19. ohm core.OutboundHandlerManager
  20. router core.Router
  21. }
  22. // NewDefaultDispatcher create a new DefaultDispatcher.
  23. func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) {
  24. v := core.MustFromContext(ctx)
  25. d := &DefaultDispatcher{
  26. ohm: v.OutboundHandlerManager(),
  27. router: v.Router(),
  28. }
  29. if err := v.RegisterFeature((*core.Dispatcher)(nil), d); err != nil {
  30. return nil, newError("unable to register Dispatcher")
  31. }
  32. return d, nil
  33. }
  34. // Start implements app.Application.
  35. func (*DefaultDispatcher) Start() error {
  36. return nil
  37. }
  38. // Close implements app.Application.
  39. func (*DefaultDispatcher) Close() error { return nil }
  40. // Dispatch implements core.Dispatcher.
  41. func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
  42. if !destination.IsValid() {
  43. panic("Dispatcher: Invalid destination.")
  44. }
  45. ctx = proxy.ContextWithTarget(ctx, destination)
  46. outbound := ray.NewRay(ctx)
  47. sniferList := proxyman.ProtocoSniffersFromContext(ctx)
  48. if destination.Address.Family().IsDomain() || len(sniferList) == 0 {
  49. go d.routedDispatch(ctx, outbound, destination)
  50. } else {
  51. go func() {
  52. domain, err := snifer(ctx, sniferList, outbound)
  53. if err == nil {
  54. newError("sniffed domain: ", domain).WithContext(ctx).WriteToLog()
  55. destination.Address = net.ParseAddress(domain)
  56. ctx = proxy.ContextWithTarget(ctx, destination)
  57. }
  58. d.routedDispatch(ctx, outbound, destination)
  59. }()
  60. }
  61. return outbound, nil
  62. }
  63. func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound ray.OutboundRay) (string, error) {
  64. payload := buf.New()
  65. defer payload.Release()
  66. sniffer := NewSniffer(sniferList)
  67. totalAttempt := 0
  68. for {
  69. select {
  70. case <-ctx.Done():
  71. return "", ctx.Err()
  72. default:
  73. totalAttempt++
  74. if totalAttempt > 5 {
  75. return "", errSniffingTimeout
  76. }
  77. outbound.OutboundInput().Peek(payload)
  78. if !payload.IsEmpty() {
  79. domain, err := sniffer.Sniff(payload.Bytes())
  80. if err != ErrMoreData {
  81. return domain, err
  82. }
  83. }
  84. if payload.IsFull() {
  85. return "", ErrInvalidData
  86. }
  87. time.Sleep(time.Millisecond * 100)
  88. }
  89. }
  90. }
  91. func (d *DefaultDispatcher) routedDispatch(ctx context.Context, outbound ray.OutboundRay, destination net.Destination) {
  92. dispatcher := d.ohm.GetDefaultHandler()
  93. if d.router != nil {
  94. if tag, err := d.router.PickRoute(ctx); err == nil {
  95. if handler := d.ohm.GetHandler(tag); handler != nil {
  96. newError("taking detour [", tag, "] for [", destination, "]").WithContext(ctx).WriteToLog()
  97. dispatcher = handler
  98. } else {
  99. newError("nonexisting tag: ", tag).AtWarning().WithContext(ctx).WriteToLog()
  100. }
  101. } else {
  102. newError("default route for ", destination).WithContext(ctx).WriteToLog()
  103. }
  104. }
  105. dispatcher.Dispatch(ctx, outbound)
  106. }
  107. func init() {
  108. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  109. return NewDefaultDispatcher(ctx, config.(*Config))
  110. }))
  111. }