From 9f8bb476339240bba0d0e4c13b0b5e6a492fb37b 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: Wed, 30 Oct 2024 10:26:43 +0800 Subject: [PATCH] Fix: Apply mutex when visiting Attributes as well (#3921) https://github.com/XTLS/Xray-core/pull/3921#issuecomment-2445689462 --- common/protocol/http/sniff.go | 2 +- common/session/session.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/common/protocol/http/sniff.go b/common/protocol/http/sniff.go index e85a0792..40968294 100644 --- a/common/protocol/http/sniff.go +++ b/common/protocol/http/sniff.go @@ -63,7 +63,7 @@ func SniffHTTP(b []byte, c context.Context) (*SniffHeader, error) { ShouldSniffAttr := true // If content.Attributes have information, that means it comes from HTTP inbound PlainHTTP mode. // It will set attributes, so skip it. - if content == nil || len(content.Attributes) != 0 { + if content == nil || content.AttributeLen() != 0 { ShouldSniffAttr = false } if err := beginWithHTTPMethod(b); err != nil { diff --git a/common/session/session.go b/common/session/session.go index deb01f8c..4573b216 100644 --- a/common/session/session.go +++ b/common/session/session.go @@ -128,8 +128,24 @@ func (c *Content) SetAttribute(name string, value string) { // Attribute retrieves additional string attributes from content. func (c *Content) Attribute(name string) string { + c.mu.Lock() + c.isLocked = true + defer func() { + c.isLocked = false + c.mu.Unlock() + }() if c.Attributes == nil { return "" } return c.Attributes[name] } + +func (c *Content) AttributeLen() int { + c.mu.Lock() + c.isLocked = true + defer func() { + c.isLocked = false + c.mu.Unlock() + }() + return len(c.Attributes) +} \ No newline at end of file