Skip to content

Commit 5745dea

Browse files
RPRXStarProxima
andcommitted
VLESS Multiplex (mux.cool): Apply MutexWriter to avoid concurrent writes to the underlayer
#7337 (comment) #7345 (comment) Fixes #5845 and #7336 --------- Co-authored-by: StarProxima <[email protected]>
1 parent 2e4c13d commit 5745dea

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎common/mux/client.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ var (
188188

189189
// NewClientWorker creates a new mux.Client.
190190
func NewClientWorker(stream transport.Link, s ClientStrategy) (*ClientWorker, error) {
191+
stream.Writer = &MutexWriter{Writer: stream.Writer}
191192
c := &ClientWorker{
192193
sessionManager: NewSessionManager(),
193194
link: stream,

‎common/mux/server.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type ServerWorker struct {
9595
func NewServerWorker(ctx context.Context, d routing.Dispatcher, link *transport.Link) (*ServerWorker, error) {
9696
worker := &ServerWorker{
9797
dispatcher: d,
98-
link: link,
98+
link: &transport.Link{Reader: link.Reader, Writer: &MutexWriter{Writer: link.Writer}},
9999
sessionManager: NewSessionManager(),
100100
done: done.New(),
101101
timer: time.NewTicker(60 * time.Second),

‎common/mux/writer.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package mux
22

33
import (
4+
"sync"
5+
46
"github.com/xtls/xray-core/common"
57
"github.com/xtls/xray-core/common/buf"
68
"github.com/xtls/xray-core/common/net"
@@ -9,6 +11,17 @@ import (
911
"github.com/xtls/xray-core/common/session"
1012
)
1113

14+
type MutexWriter struct {
15+
sync.Mutex
16+
buf.Writer
17+
}
18+
19+
func (w *MutexWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
20+
w.Lock()
21+
defer w.Unlock()
22+
return w.Writer.WriteMultiBuffer(mb)
23+
}
24+
1225
type Writer struct {
1326
dest net.Destination
1427
writer buf.Writer

0 commit comments

Comments
 (0)