2020-11-25 13:01:53 +02:00
|
|
|
package dispatcher
|
|
|
|
|
|
|
|
import (
|
2021-03-07 06:39:50 +02:00
|
|
|
"context"
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/protocol/bittorrent"
|
|
|
|
"github.com/xtls/xray-core/common/protocol/http"
|
|
|
|
"github.com/xtls/xray-core/common/protocol/tls"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SniffResult interface {
|
|
|
|
Protocol() string
|
|
|
|
Domain() string
|
|
|
|
}
|
|
|
|
|
2021-03-07 06:39:50 +02:00
|
|
|
type protocolSniffer func(context.Context, []byte) (SniffResult, error)
|
|
|
|
|
|
|
|
type protocolSnifferWithMetadata struct {
|
|
|
|
protocolSniffer protocolSniffer
|
|
|
|
// A Metadata sniffer will be invoked on connection establishment only, with nil body,
|
|
|
|
// for both TCP and UDP connections
|
|
|
|
// It will not be shown as a traffic type for routing unless there is no other successful sniffing.
|
|
|
|
metadataSniffer bool
|
|
|
|
}
|
2020-11-25 13:01:53 +02:00
|
|
|
|
|
|
|
type Sniffer struct {
|
2021-03-07 06:39:50 +02:00
|
|
|
sniffer []protocolSnifferWithMetadata
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 06:39:50 +02:00
|
|
|
func NewSniffer(ctx context.Context) *Sniffer {
|
|
|
|
ret := &Sniffer{
|
|
|
|
sniffer: []protocolSnifferWithMetadata{
|
|
|
|
{func(c context.Context, b []byte) (SniffResult, error) { return http.SniffHTTP(b) }, false},
|
|
|
|
{func(c context.Context, b []byte) (SniffResult, error) { return tls.SniffTLS(b) }, false},
|
|
|
|
{func(c context.Context, b []byte) (SniffResult, error) { return bittorrent.SniffBittorrent(b) }, false},
|
2020-11-25 13:01:53 +02:00
|
|
|
},
|
|
|
|
}
|
2021-03-07 06:39:50 +02:00
|
|
|
if sniffer, err := newFakeDNSSniffer(ctx); err == nil {
|
2021-10-20 08:15:49 +03:00
|
|
|
others := ret.sniffer
|
2021-03-07 06:39:50 +02:00
|
|
|
ret.sniffer = append(ret.sniffer, sniffer)
|
2021-10-20 08:15:49 +03:00
|
|
|
fakeDNSThenOthers, err := newFakeDNSThenOthers(ctx, sniffer, others)
|
|
|
|
if err == nil {
|
|
|
|
ret.sniffer = append([]protocolSnifferWithMetadata{fakeDNSThenOthers}, ret.sniffer...)
|
|
|
|
}
|
2021-03-07 06:39:50 +02:00
|
|
|
}
|
|
|
|
return ret
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var errUnknownContent = newError("unknown content")
|
|
|
|
|
2021-03-07 06:39:50 +02:00
|
|
|
func (s *Sniffer) Sniff(c context.Context, payload []byte) (SniffResult, error) {
|
|
|
|
var pendingSniffer []protocolSnifferWithMetadata
|
|
|
|
for _, si := range s.sniffer {
|
|
|
|
s := si.protocolSniffer
|
|
|
|
if si.metadataSniffer {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result, err := s(c, payload)
|
2020-11-25 13:01:53 +02:00
|
|
|
if err == common.ErrNoClue {
|
2021-03-07 06:39:50 +02:00
|
|
|
pendingSniffer = append(pendingSniffer, si)
|
2020-11-25 13:01:53 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && result != nil {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pendingSniffer) > 0 {
|
|
|
|
s.sniffer = pendingSniffer
|
|
|
|
return nil, common.ErrNoClue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errUnknownContent
|
|
|
|
}
|
2021-03-07 06:39:50 +02:00
|
|
|
|
|
|
|
func (s *Sniffer) SniffMetadata(c context.Context) (SniffResult, error) {
|
|
|
|
var pendingSniffer []protocolSnifferWithMetadata
|
|
|
|
for _, si := range s.sniffer {
|
|
|
|
s := si.protocolSniffer
|
|
|
|
if !si.metadataSniffer {
|
|
|
|
pendingSniffer = append(pendingSniffer, si)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result, err := s(c, nil)
|
|
|
|
if err == common.ErrNoClue {
|
|
|
|
pendingSniffer = append(pendingSniffer, si)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && result != nil {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pendingSniffer) > 0 {
|
|
|
|
s.sniffer = pendingSniffer
|
|
|
|
return nil, common.ErrNoClue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errUnknownContent
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompositeResult(domainResult SniffResult, protocolResult SniffResult) SniffResult {
|
|
|
|
return &compositeResult{domainResult: domainResult, protocolResult: protocolResult}
|
|
|
|
}
|
|
|
|
|
|
|
|
type compositeResult struct {
|
|
|
|
domainResult SniffResult
|
|
|
|
protocolResult SniffResult
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c compositeResult) Protocol() string {
|
|
|
|
return c.protocolResult.Protocol()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c compositeResult) Domain() string {
|
|
|
|
return c.domainResult.Domain()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c compositeResult) ProtocolForDomainResult() string {
|
|
|
|
return c.domainResult.Protocol()
|
|
|
|
}
|
|
|
|
|
|
|
|
type SnifferResultComposite interface {
|
|
|
|
ProtocolForDomainResult() string
|
|
|
|
}
|
2021-10-20 08:15:49 +03:00
|
|
|
|
|
|
|
type SnifferIsProtoSubsetOf interface {
|
|
|
|
IsProtoSubsetOf(protocolName string) bool
|
|
|
|
}
|