From 1629c8e14857e253f634c9f6e230cfe6993ce890 Mon Sep 17 00:00:00 2001 From: patterniha Date: Wed, 14 May 2025 23:22:50 +0330 Subject: [PATCH 1/6] Sniffer: fix infinite loop stuck --- app/dispatcher/default.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/dispatcher/default.go b/app/dispatcher/default.go index 54864cbb64d6..ec81b3fe96ca 100644 --- a/app/dispatcher/default.go +++ b/app/dispatcher/default.go @@ -2,6 +2,7 @@ package dispatcher import ( "context" + "io" "regexp" "strings" "sync" @@ -373,7 +374,14 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, netw return nil, ctx.Err() default: cachingStartingTimeStamp := time.Now() + payloadLen := payload.Len() cacheErr := cReader.Cache(payload, cacheDeadline) + if cacheErr != nil { + return nil, cacheErr + } + if payload.Len() == payloadLen { + return nil, io.EOF + } cachingTimeElapsed := time.Since(cachingStartingTimeStamp) 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 totalAttempt++ 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: return result, err } + } else { + return nil, io.EOF } - if totalAttempt >= 2 || cacheDeadline <= 0 { + if totalAttempt >= 32 || cacheDeadline <= 0 { return nil, errSniffingTimeout } } From 3ba7f5f6d3b2ac7b474c1320cd41f907199c2607 Mon Sep 17 00:00:00 2001 From: patterniha Date: Thu, 15 May 2025 07:25:38 +0330 Subject: [PATCH 2/6] some changes --- app/dispatcher/default.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/dispatcher/default.go b/app/dispatcher/default.go index ec81b3fe96ca..b329292ce6cf 100644 --- a/app/dispatcher/default.go +++ b/app/dispatcher/default.go @@ -391,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 totalAttempt++ case protocol.ErrProtoNeedMoreData: // Protocol Need More Data: protocol matches, but need more data to complete sniffing - totalAttempt++ + break default: return result, err } } else { return nil, io.EOF } - if totalAttempt >= 32 || cacheDeadline <= 0 { + if totalAttempt >= 2 || cacheDeadline <= 0 { return nil, errSniffingTimeout } } From f3e1c28a20d469246b9a5883bd4cc0fe10f90980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Thu, 15 May 2025 12:29:06 +0800 Subject: [PATCH 3/6] Update default.go --- app/dispatcher/default.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/app/dispatcher/default.go b/app/dispatcher/default.go index b329292ce6cf..deffb3d1a89d 100644 --- a/app/dispatcher/default.go +++ b/app/dispatcher/default.go @@ -2,7 +2,6 @@ package dispatcher import ( "context" - "io" "regexp" "strings" "sync" @@ -374,14 +373,7 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, netw return nil, ctx.Err() default: cachingStartingTimeStamp := time.Now() - payloadLen := payload.Len() cacheErr := cReader.Cache(payload, cacheDeadline) - if cacheErr != nil { - return nil, cacheErr - } - if payload.Len() == payloadLen { - return nil, io.EOF - } cachingTimeElapsed := time.Since(cachingStartingTimeStamp) cacheDeadline -= cachingTimeElapsed @@ -391,12 +383,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 totalAttempt++ case protocol.ErrProtoNeedMoreData: // Protocol Need More Data: protocol matches, but need more data to complete sniffing - break + if cacheErr != nil { // Cache error (e.g. timeout) counts for failed attempt + totalAttempt++ + } default: return result, err } } else { - return nil, io.EOF + totalAttempt++ } if totalAttempt >= 2 || cacheDeadline <= 0 { return nil, errSniffingTimeout From b5715246c14e5381c0f1631953c0f1e44a35c2f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Thu, 15 May 2025 13:23:12 +0800 Subject: [PATCH 4/6] Add err check --- app/dispatcher/default.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/dispatcher/default.go b/app/dispatcher/default.go index deffb3d1a89d..51685cc88601 100644 --- a/app/dispatcher/default.go +++ b/app/dispatcher/default.go @@ -373,7 +373,10 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, netw return nil, ctx.Err() default: cachingStartingTimeStamp := time.Now() - cacheErr := cReader.Cache(payload, cacheDeadline) + err := cReader.Cache(payload, cacheDeadline) + if err != nil { + return nil, err + } cachingTimeElapsed := time.Since(cachingStartingTimeStamp) cacheDeadline -= cachingTimeElapsed @@ -383,9 +386,7 @@ 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 totalAttempt++ 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++ - } + // in this case, do not add totalAttempt(allow to read until timeout) default: return result, err } From 820c51a05dbe90089e5ca5f0a75508115af5b674 Mon Sep 17 00:00:00 2001 From: patterniha Date: Thu, 15 May 2025 17:36:10 +0330 Subject: [PATCH 5/6] fix `fakedns+others` --- app/dispatcher/default.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dispatcher/default.go b/app/dispatcher/default.go index 51685cc88601..ff21ededb94b 100644 --- a/app/dispatcher/default.go +++ b/app/dispatcher/default.go @@ -226,7 +226,7 @@ func (d *DefaultDispatcher) shouldOverride(ctx context.Context, result SniffResu if strings.HasPrefix(protocolString, p) || strings.HasPrefix(p, protocolString) { return true } - if fkr0, ok := d.fdns.(dns.FakeDNSEngineRev0); ok && protocolString != "bittorrent" && p == "fakedns" && + if fkr0, ok := d.fdns.(dns.FakeDNSEngineRev0); ok && protocolString != "bittorrent" && p == "fakedns+others" && fkr0.IsIPInIPPool(destination.Address) { errors.LogInfo(ctx, "Using sniffer ", protocolString, " since the fake DNS missed") return true From 04cd404334dd85ea108713ff4468814703a74f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Fri, 16 May 2025 19:56:35 +0800 Subject: [PATCH 6/6] Update default.go --- app/dispatcher/default.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dispatcher/default.go b/app/dispatcher/default.go index ff21ededb94b..51685cc88601 100644 --- a/app/dispatcher/default.go +++ b/app/dispatcher/default.go @@ -226,7 +226,7 @@ func (d *DefaultDispatcher) shouldOverride(ctx context.Context, result SniffResu if strings.HasPrefix(protocolString, p) || strings.HasPrefix(p, protocolString) { return true } - if fkr0, ok := d.fdns.(dns.FakeDNSEngineRev0); ok && protocolString != "bittorrent" && p == "fakedns+others" && + if fkr0, ok := d.fdns.(dns.FakeDNSEngineRev0); ok && protocolString != "bittorrent" && p == "fakedns" && fkr0.IsIPInIPPool(destination.Address) { errors.LogInfo(ctx, "Using sniffer ", protocolString, " since the fake DNS missed") return true