Sniffer: fix infinite loop stuck

This commit is contained in:
patterniha 2025-05-14 23:22:50 +03:30
parent 72170d8b6b
commit 1629c8e148

View file

@ -2,6 +2,7 @@ package dispatcher
import ( import (
"context" "context"
"io"
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
@ -373,7 +374,14 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, netw
return nil, ctx.Err() return nil, ctx.Err()
default: default:
cachingStartingTimeStamp := time.Now() cachingStartingTimeStamp := time.Now()
payloadLen := payload.Len()
cacheErr := cReader.Cache(payload, cacheDeadline) cacheErr := cReader.Cache(payload, cacheDeadline)
if cacheErr != nil {
return nil, cacheErr
}
if payload.Len() == payloadLen {
return nil, io.EOF
}
cachingTimeElapsed := time.Since(cachingStartingTimeStamp) cachingTimeElapsed := time.Since(cachingStartingTimeStamp)
cacheDeadline -= cachingTimeElapsed cacheDeadline -= cachingTimeElapsed
@ -383,14 +391,14 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, netw
case common.ErrNoClue: // No Clue: protocol not matches, and sniffer cannot determine whether there will be a match or not case common.ErrNoClue: // No Clue: protocol not matches, and sniffer cannot determine whether there will be a match or not
totalAttempt++ totalAttempt++
case protocol.ErrProtoNeedMoreData: // Protocol Need More Data: protocol matches, but need more data to complete sniffing case protocol.ErrProtoNeedMoreData: // Protocol Need More Data: protocol matches, but need more data to complete sniffing
if cacheErr != nil { // Cache error (e.g. timeout) counts for failed attempt totalAttempt++
totalAttempt++
}
default: default:
return result, err return result, err
} }
} else {
return nil, io.EOF
} }
if totalAttempt >= 2 || cacheDeadline <= 0 { if totalAttempt >= 32 || cacheDeadline <= 0 {
return nil, errSniffingTimeout return nil, errSniffingTimeout
} }
} }