From ad53b42a68715f390e851c0602f7c1fde13e2888 Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Thu, 5 Dec 2019 14:12:29 +0800 Subject: [PATCH] Fix: vmess websocket udp crash --- component/vmess/websocket.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/component/vmess/websocket.go b/component/vmess/websocket.go index ae737d94..30d2c3ae 100644 --- a/component/vmess/websocket.go +++ b/component/vmess/websocket.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "strings" + "sync" "time" "github.com/gorilla/websocket" @@ -17,6 +18,10 @@ type websocketConn struct { conn *websocket.Conn reader io.Reader remoteAddr net.Addr + + // https://godoc.org/github.com/gorilla/websocket#hdr-Concurrency + rMux sync.Mutex + wMux sync.Mutex } type WebsocketConfig struct { @@ -29,6 +34,8 @@ type WebsocketConfig struct { // Read implements net.Conn.Read() func (wsc *websocketConn) Read(b []byte) (int, error) { + wsc.rMux.Lock() + defer wsc.rMux.Unlock() for { reader, err := wsc.getReader() if err != nil { @@ -46,6 +53,8 @@ func (wsc *websocketConn) Read(b []byte) (int, error) { // Write implements io.Writer. func (wsc *websocketConn) Write(b []byte) (int, error) { + wsc.wMux.Lock() + defer wsc.wMux.Unlock() if err := wsc.conn.WriteMessage(websocket.BinaryMessage, b); err != nil { return 0, err }