sniffer.go 3.7 KB

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