Fix: vmess aead writer concurrent write (#1350)

This commit is contained in:
gVisor bot 2021-04-13 23:32:53 +08:00
parent 70930965c7
commit 85be8690c0

View file

@ -5,6 +5,7 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"io" "io"
"sync"
"github.com/Dreamacro/clash/common/pool" "github.com/Dreamacro/clash/common/pool"
) )
@ -15,6 +16,8 @@ type aeadWriter struct {
nonce [32]byte nonce [32]byte
count uint16 count uint16
iv []byte iv []byte
writeLock sync.Mutex
} }
func newAEADWriter(w io.Writer, aead cipher.AEAD, iv []byte) *aeadWriter { func newAEADWriter(w io.Writer, aead cipher.AEAD, iv []byte) *aeadWriter {
@ -22,8 +25,12 @@ func newAEADWriter(w io.Writer, aead cipher.AEAD, iv []byte) *aeadWriter {
} }
func (w *aeadWriter) Write(b []byte) (n int, err error) { func (w *aeadWriter) Write(b []byte) (n int, err error) {
w.writeLock.Lock()
buf := pool.Get(pool.RelayBufferSize) buf := pool.Get(pool.RelayBufferSize)
defer pool.Put(buf) defer func() {
w.writeLock.Unlock()
pool.Put(buf)
}()
length := len(b) length := len(b)
for { for {
if length == 0 { if length == 0 {