From 267d93f7bd02773ce5074928767e2f28bd3eb770 Mon Sep 17 00:00:00 2001 From: Hellojack <106379370+H1JK@users.noreply.github.com> Date: Sat, 25 Feb 2023 00:42:02 +0800 Subject: [PATCH] Improve ReshapeMultiBuffer (#1636) * Improve ReshapeMultiBuffer * Improve again * Always resize --- proxy/vless/encoding/encoding.go | 36 +++++++++++++++----------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/proxy/vless/encoding/encoding.go b/proxy/vless/encoding/encoding.go index 1e8aaa92..fa5b438c 100644 --- a/proxy/vless/encoding/encoding.go +++ b/proxy/vless/encoding/encoding.go @@ -469,38 +469,36 @@ func XtlsFilterTls(buffer buf.MultiBuffer, numberOfPacketToFilter *int, enableXt // ReshapeMultiBuffer prepare multi buffer for padding stucture (max 21 bytes) func ReshapeMultiBuffer(ctx context.Context, buffer buf.MultiBuffer) buf.MultiBuffer { - needReshape := false + needReshape := 0 for _, b := range buffer { if b.Len() >= buf.Size-21 { - needReshape = true + needReshape += 1 } } - if !needReshape { + if needReshape == 0 { return buffer } - mb2 := make(buf.MultiBuffer, 0, len(buffer)) - print := "" - for _, b := range buffer { - if b.Len() >= buf.Size-21 { - index := int32(bytes.LastIndex(b.Bytes(), tlsApplicationDataStart)) + mb2 := make(buf.MultiBuffer, 0, len(buffer)+needReshape) + toPrint := "" + for i, buffer1 := range buffer { + if buffer1.Len() >= buf.Size-21 { + index := int32(bytes.LastIndex(buffer1.Bytes(), tlsApplicationDataStart)) if index <= 0 { index = buf.Size / 2 } - buffer1 := buf.New() buffer2 := buf.New() - buffer1.Write(b.BytesTo(index)) - buffer2.Write(b.BytesFrom(index)) + buffer2.Write(buffer1.BytesFrom(index)) + buffer1.Resize(0, index) mb2 = append(mb2, buffer1, buffer2) - print += " " + strconv.Itoa(int(buffer1.Len())) + " " + strconv.Itoa(int(buffer2.Len())) + toPrint += " " + strconv.Itoa(int(buffer1.Len())) + " " + strconv.Itoa(int(buffer2.Len())) } else { - newbuffer := buf.New() - newbuffer.Write(b.Bytes()) - mb2 = append(mb2, newbuffer) - print += " " + strconv.Itoa(int(b.Len())) + mb2 = append(mb2, buffer1) + toPrint += " " + strconv.Itoa(int(buffer1.Len())) } + buffer[i] = nil } - buf.ReleaseMulti(buffer) - newError("ReshapeMultiBuffer ", print).WriteToLog(session.ExportIDToError(ctx)) + buffer = buffer[:0] + newError("ReshapeMultiBuffer ", toPrint).WriteToLog(session.ExportIDToError(ctx)) return mb2 } @@ -524,7 +522,7 @@ func XtlsPadding(b *buf.Buffer, command byte, userUUID *[]byte, ctx context.Cont *userUUID = nil } newbuffer.Write([]byte{command, byte(contantLen >> 8), byte(contantLen), byte(paddingLen >> 8), byte(paddingLen)}) - if (b != nil) { + if b != nil { newbuffer.Write(b.Bytes()) b.Release() b = nil