Fix: vmess aead writer concurrent write (#1350)

This commit is contained in:
sprov 2021-04-13 23:32:53 +08:00 committed by GitHub
parent b59d45c660
commit a40274e2a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,6 +5,7 @@ import (
"encoding/binary"
"errors"
"io"
"sync"
"github.com/Dreamacro/clash/common/pool"
)
@ -15,6 +16,8 @@ type aeadWriter struct {
nonce [32]byte
count uint16
iv []byte
writeLock sync.Mutex
}
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) {
w.writeLock.Lock()
buf := pool.Get(pool.RelayBufferSize)
defer pool.Put(buf)
defer func() {
w.writeLock.Unlock()
pool.Put(buf)
}()
length := len(b)
for {
if length == 0 {