refactor peeking strategy

pull/298/merge
Darien Raymond 2017-04-26 21:24:58 +02:00
parent 39005d8fc4
commit 93d913b959
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
1 changed files with 25 additions and 16 deletions

View File

@ -4,6 +4,7 @@ package impl
import ( import (
"context" "context"
"time" "time"
"v2ray.com/core/app" "v2ray.com/core/app"
@ -81,24 +82,7 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
return outbound, nil return outbound, nil
} }
func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound ray.OutboundRay) (string, error) { func trySnif(sniferList []proxyman.KnownProtocols, b []byte) (string, error) {
payload := buf.New()
defer payload.Release()
totalAttempt := 0
for {
select {
case <-ctx.Done():
return "", ctx.Err()
case <-time.After(time.Millisecond * 100):
totalAttempt++
if totalAttempt > 5 {
return "", errSniffingTimeout
}
outbound.OutboundInput().Peek(payload)
if payload.IsEmpty() {
continue
}
for _, protocol := range sniferList { for _, protocol := range sniferList {
var f func([]byte) (string, error) var f func([]byte) (string, error)
switch protocol { switch protocol {
@ -110,7 +94,31 @@ func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound
panic("Unsupported protocol") panic("Unsupported protocol")
} }
domain, err := f(payload.Bytes()) 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) {
payload := buf.New()
defer payload.Release()
totalAttempt := 0
for {
select {
case <-ctx.Done():
return "", ctx.Err()
default:
totalAttempt++
if totalAttempt > 5 {
return "", errSniffingTimeout
}
outbound.OutboundInput().Peek(payload)
if !payload.IsEmpty() {
domain, err := trySnif(sniferList, payload.Bytes())
if err != ErrMoreData { if err != ErrMoreData {
return domain, err return domain, err
} }
@ -118,6 +126,7 @@ func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound
if payload.IsFull() { if payload.IsFull() {
return "", ErrInvalidData return "", ErrInvalidData
} }
time.Sleep(time.Millisecond * 100)
} }
} }
} }