From 9dcd4277436640acea4c0b9897450cab2eefb23f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 16 Sep 2023 22:30:42 +0800 Subject: [PATCH] Fix v2ray websocket transport --- transport/v2raywebsocket/conn.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/transport/v2raywebsocket/conn.go b/transport/v2raywebsocket/conn.go index 7bf9df1d..1faeaa36 100644 --- a/transport/v2raywebsocket/conn.go +++ b/transport/v2raywebsocket/conn.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "os" + "sync" "time" C "github.com/sagernet/sing-box/constant" @@ -93,6 +94,7 @@ type EarlyWebsocketConn struct { *Client ctx context.Context conn *WebsocketConn + access sync.Mutex create chan struct{} err error } @@ -146,6 +148,11 @@ func (c *EarlyWebsocketConn) writeRequest(content []byte) error { } func (c *EarlyWebsocketConn) Write(b []byte) (n int, err error) { + if c.conn != nil { + return c.conn.Write(b) + } + c.access.Lock() + defer c.access.Unlock() if c.conn != nil { return c.conn.Write(b) } @@ -159,6 +166,11 @@ func (c *EarlyWebsocketConn) Write(b []byte) (n int, err error) { } func (c *EarlyWebsocketConn) WriteBuffer(buffer *buf.Buffer) error { + if c.conn != nil { + return c.conn.WriteBuffer(buffer) + } + c.access.Lock() + defer c.access.Unlock() if c.conn != nil { return c.conn.WriteBuffer(buffer) }