mirror of https://github.com/v2ray/v2ray-core
refactor peeking strategy
parent
39005d8fc4
commit
93d913b959
|
@ -4,6 +4,7 @@ package impl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"v2ray.com/core/app"
|
"v2ray.com/core/app"
|
||||||
|
@ -81,6 +82,26 @@ 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()
|
||||||
|
@ -90,27 +111,14 @@ func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return "", ctx.Err()
|
return "", ctx.Err()
|
||||||
case <-time.After(time.Millisecond * 100):
|
default:
|
||||||
totalAttempt++
|
totalAttempt++
|
||||||
if totalAttempt > 5 {
|
if totalAttempt > 5 {
|
||||||
return "", errSniffingTimeout
|
return "", errSniffingTimeout
|
||||||
}
|
}
|
||||||
outbound.OutboundInput().Peek(payload)
|
outbound.OutboundInput().Peek(payload)
|
||||||
if payload.IsEmpty() {
|
if !payload.IsEmpty() {
|
||||||
continue
|
domain, err := trySnif(sniferList, payload.Bytes())
|
||||||
}
|
|
||||||
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(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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue