1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/app/dispatcher/sniffer.go

56 lines
1.2 KiB
Go
Raw Normal View History

2019-02-01 14:08:21 -05:00
// +build !confonly
package dispatcher
import (
"v2ray.com/core/common"
2018-07-16 07:47:00 -04:00
"v2ray.com/core/common/protocol/bittorrent"
"v2ray.com/core/common/protocol/http"
"v2ray.com/core/common/protocol/tls"
)
2018-07-16 07:47:00 -04:00
type SniffResult interface {
Protocol() string
Domain() string
}
2018-07-16 07:47:00 -04:00
type protocolSniffer func([]byte) (SniffResult, error)
2018-07-16 07:47:00 -04:00
type Sniffer struct {
sniffer []protocolSniffer
}
2018-07-16 07:47:00 -04: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 07:47:00 -04:00
}
2018-07-16 07:47:00 -04:00
var errUnknownContent = newError("unknown content")
2018-07-16 07:47:00 -04:00
func (s *Sniffer) Sniff(payload []byte) (SniffResult, error) {
var pendingSniffer []protocolSniffer
for _, s := range s.sniffer {
result, err := s(payload)
if err == common.ErrNoClue {
2018-07-16 07:47:00 -04:00
pendingSniffer = append(pendingSniffer, s)
continue
}
2018-07-16 07:47:00 -04:00
if err == nil && result != nil {
return result, nil
}
}
2018-07-16 07:47:00 -04:00
if len(pendingSniffer) > 0 {
s.sniffer = pendingSniffer
return nil, common.ErrNoClue
}
2017-06-05 17:27:50 -04:00
2018-07-16 07:47:00 -04:00
return nil, errUnknownContent
2017-06-05 17:27:50 -04:00
}