Improve ReshapeMultiBuffer (#1636)

* Improve ReshapeMultiBuffer

* Improve again

* Always resize
This commit is contained in:
Hellojack 2023-02-25 00:42:02 +08:00 committed by GitHub
parent 7f16f4ccd9
commit 267d93f7bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -469,38 +469,36 @@ func XtlsFilterTls(buffer buf.MultiBuffer, numberOfPacketToFilter *int, enableXt
// ReshapeMultiBuffer prepare multi buffer for padding stucture (max 21 bytes) // ReshapeMultiBuffer prepare multi buffer for padding stucture (max 21 bytes)
func ReshapeMultiBuffer(ctx context.Context, buffer buf.MultiBuffer) buf.MultiBuffer { func ReshapeMultiBuffer(ctx context.Context, buffer buf.MultiBuffer) buf.MultiBuffer {
needReshape := false needReshape := 0
for _, b := range buffer { for _, b := range buffer {
if b.Len() >= buf.Size-21 { if b.Len() >= buf.Size-21 {
needReshape = true needReshape += 1
} }
} }
if !needReshape { if needReshape == 0 {
return buffer return buffer
} }
mb2 := make(buf.MultiBuffer, 0, len(buffer)) mb2 := make(buf.MultiBuffer, 0, len(buffer)+needReshape)
print := "" toPrint := ""
for _, b := range buffer { for i, buffer1 := range buffer {
if b.Len() >= buf.Size-21 { if buffer1.Len() >= buf.Size-21 {
index := int32(bytes.LastIndex(b.Bytes(), tlsApplicationDataStart)) index := int32(bytes.LastIndex(buffer1.Bytes(), tlsApplicationDataStart))
if index <= 0 { if index <= 0 {
index = buf.Size / 2 index = buf.Size / 2
} }
buffer1 := buf.New()
buffer2 := buf.New() buffer2 := buf.New()
buffer1.Write(b.BytesTo(index)) buffer2.Write(buffer1.BytesFrom(index))
buffer2.Write(b.BytesFrom(index)) buffer1.Resize(0, index)
mb2 = append(mb2, buffer1, buffer2) 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 { } else {
newbuffer := buf.New() mb2 = append(mb2, buffer1)
newbuffer.Write(b.Bytes()) toPrint += " " + strconv.Itoa(int(buffer1.Len()))
mb2 = append(mb2, newbuffer)
print += " " + strconv.Itoa(int(b.Len()))
} }
buffer[i] = nil
} }
buf.ReleaseMulti(buffer) buffer = buffer[:0]
newError("ReshapeMultiBuffer ", print).WriteToLog(session.ExportIDToError(ctx)) newError("ReshapeMultiBuffer ", toPrint).WriteToLog(session.ExportIDToError(ctx))
return mb2 return mb2
} }
@ -524,7 +522,7 @@ func XtlsPadding(b *buf.Buffer, command byte, userUUID *[]byte, ctx context.Cont
*userUUID = nil *userUUID = nil
} }
newbuffer.Write([]byte{command, byte(contantLen >> 8), byte(contantLen), byte(paddingLen >> 8), byte(paddingLen)}) newbuffer.Write([]byte{command, byte(contantLen >> 8), byte(contantLen), byte(paddingLen >> 8), byte(paddingLen)})
if (b != nil) { if b != nil {
newbuffer.Write(b.Bytes()) newbuffer.Write(b.Bytes())
b.Release() b.Release()
b = nil b = nil