1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-17 23:06:30 -05:00

fixmultiplesniffer

This commit is contained in:
Darien Raymond 2017-06-05 23:27:50 +02:00
parent 72736b0447
commit 8a3288dc6c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 49 additions and 21 deletions

View File

@ -88,30 +88,11 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
return outbound, nil return outbound, nil
} }
func trySnif(sniferList []proxyman.KnownProtocols, b []byte) (string, error) {
for _, protocol := range sniferList {
var f func([]byte) (string, error)
switch protocol {
case proxyman.KnownProtocols_HTTP:
f = SniffHTTP
case proxyman.KnownProtocols_TLS:
f = SniffTLS
default:
panic("Unsupported protocol")
}
domain, err := f(b)
if err != ErrMoreData {
return domain, err
}
}
return "", ErrMoreData
}
func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound ray.OutboundRay) (string, error) { func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound ray.OutboundRay) (string, error) {
payload := buf.New() payload := buf.New()
defer payload.Release() defer payload.Release()
sniffer := NewSniffer(sniferList)
totalAttempt := 0 totalAttempt := 0
for { for {
select { select {
@ -124,7 +105,7 @@ func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound
} }
outbound.OutboundInput().Peek(payload) outbound.OutboundInput().Peek(payload)
if !payload.IsEmpty() { if !payload.IsEmpty() {
domain, err := trySnif(sniferList, payload.Bytes()) domain, err := sniffer.Sniff(payload.Bytes())
if err != ErrMoreData { if err != ErrMoreData {
return domain, err return domain, err
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"strings" "strings"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
) )
@ -166,3 +167,49 @@ func SniffTLS(b []byte) (string, error) {
} }
return ReadClientHello(b[5 : 5+headerLen]) return ReadClientHello(b[5 : 5+headerLen])
} }
type Sniffer struct {
slist []func([]byte) (string, error)
err []error
}
func NewSniffer(sniferList []proxyman.KnownProtocols) *Sniffer {
s := new(Sniffer)
for _, protocol := range sniferList {
var f func([]byte) (string, error)
switch protocol {
case proxyman.KnownProtocols_HTTP:
f = SniffHTTP
case proxyman.KnownProtocols_TLS:
f = SniffTLS
default:
panic("Unsupported protocol")
}
s.slist = append(s.slist, f)
}
s.err = make([]error, len(s.slist))
return s
}
func (s *Sniffer) Sniff(payload []byte) (string, error) {
sniffed := false
for idx, sniffer := range s.slist {
if s.err[idx] != nil {
continue
}
sniffed = true
domain, err := sniffer(payload)
if err == nil {
return domain, nil
}
if err != ErrMoreData {
s.err[idx] = err
}
}
if sniffed {
return "", ErrMoreData
}
return "", s.err[0]
}