Fix sniffer

This commit is contained in:
世界 2023-08-24 11:07:22 +08:00
parent edad4d1ce7
commit 6dcacf3b5e
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -22,24 +22,30 @@ func PeekStream(ctx context.Context, conn net.Conn, buffer *buf.Buffer, timeout
if timeout == 0 { if timeout == 0 {
timeout = C.ReadPayloadTimeout timeout = C.ReadPayloadTimeout
} }
err := conn.SetReadDeadline(time.Now().Add(timeout)) deadline := time.Now().Add(timeout)
var errors []error
for i := 0; i < 3; i++ {
err := conn.SetReadDeadline(deadline)
if err != nil { if err != nil {
return nil, E.Cause(err, "set read deadline") return nil, E.Cause(err, "set read deadline")
} }
_, err = buffer.ReadOnceFrom(conn) _, err = buffer.ReadOnceFrom(conn)
err = E.Errors(err, conn.SetReadDeadline(time.Time{})) err = E.Errors(err, conn.SetReadDeadline(time.Time{}))
if err != nil { if err != nil {
if i > 0 {
break
}
return nil, E.Cause(err, "read payload") return nil, E.Cause(err, "read payload")
} }
var metadata *adapter.InboundContext
var errors []error
for _, sniffer := range sniffers { for _, sniffer := range sniffers {
metadata, err = sniffer(ctx, bytes.NewReader(buffer.Bytes())) metadata, err := sniffer(ctx, bytes.NewReader(buffer.Bytes()))
if metadata != nil { if metadata != nil {
return metadata, nil return metadata, nil
} }
errors = append(errors, err) errors = append(errors, err)
} }
}
return nil, E.Errors(errors...) return nil, E.Errors(errors...)
} }