1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 18:00:43 +00:00
v2fly/app/dispatcher/sniffer.go

54 lines
1.1 KiB
Go
Raw Normal View History

package dispatcher
import (
2018-07-16 11:47:00 +00:00
"v2ray.com/core"
"v2ray.com/core/common/protocol/bittorrent"
"v2ray.com/core/common/protocol/http"
"v2ray.com/core/common/protocol/tls"
)
2018-07-16 11:47:00 +00:00
type SniffResult interface {
Protocol() string
Domain() string
}
2018-07-16 11:47:00 +00:00
type protocolSniffer func([]byte) (SniffResult, error)
2018-07-16 11:47:00 +00:00
type Sniffer struct {
sniffer []protocolSniffer
}
2018-07-16 11:47:00 +00:00
func NewSniffer() *Sniffer {
return &Sniffer{
sniffer: []protocolSniffer{
func(b []byte) (SniffResult, error) { return http.SniffHTTP(b) },
func(b []byte) (SniffResult, error) { return tls.SniffTLS(b) },
func(b []byte) (SniffResult, error) { return bittorrent.SniffBittorrent(b) },
},
}
2018-07-16 11:47:00 +00:00
}
2018-07-16 11:47:00 +00:00
var errUnknownContent = newError("unknown content")
2018-07-16 11:47:00 +00:00
func (s *Sniffer) Sniff(payload []byte) (SniffResult, error) {
var pendingSniffer []protocolSniffer
for _, s := range s.sniffer {
result, err := s(payload)
if err == core.ErrNoClue {
pendingSniffer = append(pendingSniffer, s)
continue
}
2018-07-16 11:47:00 +00:00
if err == nil && result != nil {
return result, nil
}
}
2018-07-16 11:47:00 +00:00
if len(pendingSniffer) > 0 {
s.sniffer = pendingSniffer
return nil, core.ErrNoClue
}
2017-06-05 21:27:50 +00:00
2018-07-16 11:47:00 +00:00
return nil, errUnknownContent
2017-06-05 21:27:50 +00:00
}