mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-05-10 06:08:41 +00:00
Add sniff timeoit
This commit is contained in:
parent
be43f66b63
commit
95c2113250
7 changed files with 117 additions and 83 deletions
|
@ -31,10 +31,18 @@ type cachedReader struct {
|
|||
sync.Mutex
|
||||
reader *pipe.Reader
|
||||
cache buf.MultiBuffer
|
||||
|
||||
ReadBufferTimeout time.Duration
|
||||
}
|
||||
|
||||
func (r *cachedReader) Cache(b *buf.Buffer) {
|
||||
mb, _ := r.reader.ReadMultiBufferTimeout(time.Millisecond * 100)
|
||||
var timeout time.Duration
|
||||
if r.ReadBufferTimeout == 0 {
|
||||
timeout = time.Millisecond * 100
|
||||
} else {
|
||||
timeout = r.ReadBufferTimeout
|
||||
}
|
||||
mb, _ := r.reader.ReadMultiBufferTimeout(timeout)
|
||||
r.Lock()
|
||||
if !mb.IsEmpty() {
|
||||
r.cache, _ = buf.MergeMulti(r.cache, mb)
|
||||
|
@ -360,6 +368,8 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, netw
|
|||
|
||||
sniffer := NewSniffer(ctx)
|
||||
|
||||
cReader.ReadBufferTimeout = sniffer.Timeout
|
||||
|
||||
metaresult, metadataErr := sniffer.SniffMetadata(ctx)
|
||||
|
||||
if metadataOnly {
|
||||
|
|
|
@ -2,6 +2,7 @@ package dispatcher
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
|
@ -10,6 +11,7 @@ import (
|
|||
"github.com/xtls/xray-core/common/protocol/http"
|
||||
"github.com/xtls/xray-core/common/protocol/quic"
|
||||
"github.com/xtls/xray-core/common/protocol/tls"
|
||||
"github.com/xtls/xray-core/common/session"
|
||||
)
|
||||
|
||||
type SniffResult interface {
|
||||
|
@ -30,9 +32,11 @@ type protocolSnifferWithMetadata struct {
|
|||
|
||||
type Sniffer struct {
|
||||
sniffer []protocolSnifferWithMetadata
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func NewSniffer(ctx context.Context) *Sniffer {
|
||||
content := session.ContentFromContext(ctx)
|
||||
ret := &Sniffer{
|
||||
sniffer: []protocolSnifferWithMetadata{
|
||||
{func(c context.Context, b []byte) (SniffResult, error) { return http.SniffHTTP(b, c) }, false, net.Network_TCP},
|
||||
|
@ -41,6 +45,7 @@ func NewSniffer(ctx context.Context) *Sniffer {
|
|||
{func(c context.Context, b []byte) (SniffResult, error) { return quic.SniffQUIC(b) }, false, net.Network_UDP},
|
||||
{func(c context.Context, b []byte) (SniffResult, error) { return bittorrent.SniffUTP(b) }, false, net.Network_UDP},
|
||||
},
|
||||
Timeout: content.SniffingRequest.Timeout,
|
||||
}
|
||||
if sniffer, err := newFakeDNSSniffer(ctx); err == nil {
|
||||
others := ret.sniffer
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue