2018-01-10 06:22:37 -05:00
|
|
|
package dispatcher
|
2017-04-24 18:19:22 -04:00
|
|
|
|
|
|
|
import (
|
2018-10-11 14:43:37 -04:00
|
|
|
"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"
|
2017-04-24 18:19:22 -04:00
|
|
|
)
|
|
|
|
|
2018-07-16 07:47:00 -04:00
|
|
|
type SniffResult interface {
|
|
|
|
Protocol() string
|
|
|
|
Domain() string
|
2017-04-24 18:19:22 -04:00
|
|
|
}
|
|
|
|
|
2018-07-16 07:47:00 -04:00
|
|
|
type protocolSniffer func([]byte) (SniffResult, error)
|
2017-04-24 18:19:22 -04:00
|
|
|
|
2018-07-16 07:47:00 -04:00
|
|
|
type Sniffer struct {
|
|
|
|
sniffer []protocolSniffer
|
2017-04-24 18:19:22 -04:00
|
|
|
}
|
|
|
|
|
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) },
|
|
|
|
},
|
2017-04-24 18:19:22 -04:00
|
|
|
}
|
2018-07-16 07:47:00 -04:00
|
|
|
}
|
2017-04-24 18:19:22 -04:00
|
|
|
|
2018-07-16 07:47:00 -04:00
|
|
|
var errUnknownContent = newError("unknown content")
|
2017-04-24 18:19:22 -04:00
|
|
|
|
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)
|
2018-10-11 14:43:37 -04:00
|
|
|
if err == common.ErrNoClue {
|
2018-07-16 07:47:00 -04:00
|
|
|
pendingSniffer = append(pendingSniffer, s)
|
|
|
|
continue
|
2017-04-24 18:19:22 -04:00
|
|
|
}
|
|
|
|
|
2018-07-16 07:47:00 -04:00
|
|
|
if err == nil && result != nil {
|
|
|
|
return result, nil
|
2017-04-24 18:19:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 07:47:00 -04:00
|
|
|
if len(pendingSniffer) > 0 {
|
|
|
|
s.sniffer = pendingSniffer
|
2018-10-11 14:43:37 -04:00
|
|
|
return nil, common.ErrNoClue
|
2017-04-24 18:19:22 -04:00
|
|
|
}
|
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
|
|
|
}
|