From 6dcacf3b5e651af7e62e9c100baa383c791b9aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 24 Aug 2023 11:07:22 +0800 Subject: [PATCH] Fix sniffer --- common/sniff/sniff.go | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/common/sniff/sniff.go b/common/sniff/sniff.go index 4d990092..424311a8 100644 --- a/common/sniff/sniff.go +++ b/common/sniff/sniff.go @@ -22,23 +22,29 @@ func PeekStream(ctx context.Context, conn net.Conn, buffer *buf.Buffer, timeout if timeout == 0 { timeout = C.ReadPayloadTimeout } - err := conn.SetReadDeadline(time.Now().Add(timeout)) - if err != nil { - return nil, E.Cause(err, "set read deadline") - } - _, err = buffer.ReadOnceFrom(conn) - err = E.Errors(err, conn.SetReadDeadline(time.Time{})) - if err != nil { - return nil, E.Cause(err, "read payload") - } - var metadata *adapter.InboundContext + deadline := time.Now().Add(timeout) var errors []error - for _, sniffer := range sniffers { - metadata, err = sniffer(ctx, bytes.NewReader(buffer.Bytes())) - if metadata != nil { - return metadata, nil + + for i := 0; i < 3; i++ { + err := conn.SetReadDeadline(deadline) + if err != nil { + return nil, E.Cause(err, "set read deadline") + } + _, err = buffer.ReadOnceFrom(conn) + err = E.Errors(err, conn.SetReadDeadline(time.Time{})) + if err != nil { + if i > 0 { + break + } + return nil, E.Cause(err, "read payload") + } + for _, sniffer := range sniffers { + metadata, err := sniffer(ctx, bytes.NewReader(buffer.Bytes())) + if metadata != nil { + return metadata, nil + } + errors = append(errors, err) } - errors = append(errors, err) } return nil, E.Errors(errors...) }