Skip to content

Commit

Permalink
refactor peeking strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Apr 26, 2017
1 parent 39005d8 commit 93d913b
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions app/dispatcher/impl/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package impl

import (
"context"

"time"

"v2ray.com/core/app"
Expand Down Expand Up @@ -81,6 +82,26 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
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) {
payload := buf.New()
defer payload.Release()
Expand All @@ -90,34 +111,22 @@ func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound
select {
case <-ctx.Done():
return "", ctx.Err()
case <-time.After(time.Millisecond * 100):
default:
totalAttempt++
if totalAttempt > 5 {
return "", errSniffingTimeout
}
outbound.OutboundInput().Peek(payload)
if payload.IsEmpty() {
continue
}
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 !payload.IsEmpty() {
domain, err := trySnif(sniferList, payload.Bytes())
if err != ErrMoreData {
return domain, err
}
}
if payload.IsFull() {
return "", ErrInvalidData
}
time.Sleep(time.Millisecond * 100)
}
}
}
Expand Down

0 comments on commit 93d913b

Please sign in to comment.