sniffer.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package dispatcher
  2. import (
  3. "context"
  4. "github.com/v2fly/v2ray-core/v4/common"
  5. "github.com/v2fly/v2ray-core/v4/common/net"
  6. "github.com/v2fly/v2ray-core/v4/common/protocol/bittorrent"
  7. "github.com/v2fly/v2ray-core/v4/common/protocol/dns"
  8. "github.com/v2fly/v2ray-core/v4/common/protocol/http"
  9. "github.com/v2fly/v2ray-core/v4/common/protocol/quic"
  10. "github.com/v2fly/v2ray-core/v4/common/protocol/tls"
  11. )
  12. type SniffResult interface {
  13. Protocol() string
  14. Domain() string
  15. }
  16. type protocolSniffer func(context.Context, []byte) (SniffResult, error)
  17. type protocolSnifferWithMetadata struct {
  18. protocolSniffer protocolSniffer
  19. // A Metadata sniffer will be invoked on connection establishment only, with nil body,
  20. // for both TCP and UDP connections
  21. // It will not be shown as a traffic type for routing unless there is no other successful sniffing.
  22. metadataSniffer bool
  23. network net.Network
  24. }
  25. type Sniffer struct {
  26. sniffer []protocolSnifferWithMetadata
  27. }
  28. func NewSniffer(ctx context.Context) *Sniffer {
  29. ret := &Sniffer{
  30. sniffer: []protocolSnifferWithMetadata{
  31. {func(c context.Context, b []byte) (SniffResult, error) { return http.SniffHTTP(b) }, false, net.Network_TCP},
  32. {func(c context.Context, b []byte) (SniffResult, error) { return tls.SniffTLS(b) }, false, net.Network_TCP},
  33. {func(c context.Context, b []byte) (SniffResult, error) { return quic.SniffQUIC(b) }, false, net.Network_UDP},
  34. {func(c context.Context, b []byte) (SniffResult, error) { return bittorrent.SniffBittorrent(b) }, false, net.Network_TCP},
  35. {func(c context.Context, b []byte) (SniffResult, error) { return bittorrent.SniffUTP(b) }, false, net.Network_UDP},
  36. {func(c context.Context, b []byte) (SniffResult, error) { return dns.SniffDNS(b) }, false, net.Network_Unknown},
  37. },
  38. }
  39. if sniffer, err := newFakeDNSSniffer(ctx); err == nil {
  40. others := ret.sniffer
  41. ret.sniffer = append(ret.sniffer, sniffer)
  42. fakeDNSThenOthers, err := newFakeDNSThenOthers(ctx, sniffer, others)
  43. if err == nil {
  44. ret.sniffer = append([]protocolSnifferWithMetadata{fakeDNSThenOthers}, ret.sniffer...)
  45. }
  46. }
  47. return ret
  48. }
  49. var errUnknownContent = newError("unknown content")
  50. func (s *Sniffer) Sniff(c context.Context, payload []byte, network net.Network) (SniffResult, error) {
  51. var pendingSniffer []protocolSnifferWithMetadata
  52. for _, si := range s.sniffer {
  53. s := si.protocolSniffer
  54. if si.metadataSniffer {
  55. continue
  56. }
  57. if si.network != network && si.network != net.Network_Unknown {
  58. continue
  59. }
  60. result, err := s(c, payload)
  61. if err == common.ErrNoClue {
  62. pendingSniffer = append(pendingSniffer, si)
  63. continue
  64. }
  65. if err == nil && result != nil {
  66. return result, nil
  67. }
  68. }
  69. if len(pendingSniffer) > 0 {
  70. s.sniffer = pendingSniffer
  71. return nil, common.ErrNoClue
  72. }
  73. return nil, errUnknownContent
  74. }
  75. func (s *Sniffer) SniffMetadata(c context.Context) (SniffResult, error) {
  76. var pendingSniffer []protocolSnifferWithMetadata
  77. for _, si := range s.sniffer {
  78. s := si.protocolSniffer
  79. if !si.metadataSniffer {
  80. pendingSniffer = append(pendingSniffer, si)
  81. continue
  82. }
  83. result, err := s(c, nil)
  84. if err == common.ErrNoClue {
  85. pendingSniffer = append(pendingSniffer, si)
  86. continue
  87. }
  88. if err == nil && result != nil {
  89. return result, nil
  90. }
  91. }
  92. if len(pendingSniffer) > 0 {
  93. s.sniffer = pendingSniffer
  94. return nil, common.ErrNoClue
  95. }
  96. return nil, errUnknownContent
  97. }
  98. func CompositeResult(domainResult SniffResult, protocolResult SniffResult) SniffResult {
  99. return &compositeResult{domainResult: domainResult, protocolResult: protocolResult}
  100. }
  101. type compositeResult struct {
  102. domainResult SniffResult
  103. protocolResult SniffResult
  104. }
  105. func (c compositeResult) Protocol() string {
  106. return c.protocolResult.Protocol()
  107. }
  108. func (c compositeResult) Domain() string {
  109. return c.domainResult.Domain()
  110. }
  111. func (c compositeResult) ProtocolForDomainResult() string {
  112. return c.domainResult.Protocol()
  113. }
  114. type SnifferResultComposite interface {
  115. ProtocolForDomainResult() string
  116. }
  117. type SnifferIsProtoSubsetOf interface {
  118. IsProtoSubsetOf(protocolName string) bool
  119. }