default.go 3.4 KB

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