From 8771fa5c17e8eb430739eb125bc283f3dc0b32df Mon Sep 17 00:00:00 2001 From: H1JK <106379370+h1jk@users.noreply.github.com> Date: Sat, 4 Mar 2023 21:47:06 +0800 Subject: [PATCH] chore: Vision padding upgrade --- transport/vless/conn.go | 28 ++++++++++++++-------------- transport/vless/vision.go | 23 +++++++++++++++++------ 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/transport/vless/conn.go b/transport/vless/conn.go index c8477090..1f7d2cb3 100644 --- a/transport/vless/conn.go +++ b/transport/vless/conn.go @@ -241,7 +241,7 @@ func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error { command = commandPaddingEnd // disable XTLS - vc.readProcess = false + //vc.readProcess = false vc.writeFilterApplicationData = false vc.packetsToFilter = 0 } else if buffer.Len() > 6 && bytes.Equal(buffer.To(3), tlsApplicationDataStart) || vc.packetsToFilter <= 0 { @@ -252,7 +252,7 @@ func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error { } vc.writeFilterApplicationData = false } - ApplyPadding(buffer, command, nil) + ApplyPadding(buffer, command, nil, vc.isTLS) err := vc.ExtendedWriter.WriteBuffer(buffer) if err != nil { return err @@ -276,7 +276,7 @@ func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error { } vc.writeFilterApplicationData = false } - ApplyPadding(buffer2, command, nil) + ApplyPadding(buffer2, command, nil, vc.isTLS) err = vc.ExtendedWriter.WriteBuffer(buffer2) if vc.writeDirect { vc.ExtendedWriter = N.NewExtendedWriter(vc.Conn) @@ -352,19 +352,19 @@ func (vc *Conn) sendRequest(p []byte) bool { if isVision && !vc.dst.UDP && !vc.dst.Mux { if len(p) == 0 { - WriteWithPadding(buffer, nil, commandPaddingContinue, vc.id) + WriteWithPadding(buffer, nil, commandPaddingContinue, vc.id, vc.isTLS) } else { vc.FilterTLS(p) - if vc.isTLS { - WriteWithPadding(buffer, p, commandPaddingContinue, vc.id) - } else { - buf.Must(buf.Error(buffer.Write(p))) - - // disable XTLS - vc.readProcess = false - vc.writeFilterApplicationData = false - vc.packetsToFilter = 0 - } + //if vc.isTLS { + WriteWithPadding(buffer, p, commandPaddingContinue, vc.id, vc.isTLS) + //} else { + // buf.Must(buf.Error(buffer.Write(p))) + // + // // disable XTLS + // vc.readProcess = false + // vc.writeFilterApplicationData = false + // vc.packetsToFilter = 0 + //} } } else { buf.Must(buf.Error(buffer.Write(p))) diff --git a/transport/vless/vision.go b/transport/vless/vision.go index f87a6870..14d701b4 100644 --- a/transport/vless/vision.go +++ b/transport/vless/vision.go @@ -19,16 +19,22 @@ const ( commandPaddingDirect byte = 0x02 ) -func WriteWithPadding(buffer *buf.Buffer, p []byte, command byte, userUUID *uuid.UUID) { +func WriteWithPadding(buffer *buf.Buffer, p []byte, command byte, userUUID *uuid.UUID, paddingTLS bool) { contentLen := int32(len(p)) var paddingLen int32 - if contentLen < 900 { + if contentLen < 900 && paddingTLS { + log.Debugln("long padding") paddingLen = rand.Int31n(500) + 900 - contentLen + } else { + paddingLen = rand.Int31n(256) + } + if paddingLen > buf.BufferSize-21-contentLen { + paddingLen = buf.BufferSize - 21 - contentLen } - if userUUID != nil { // unnecessary, but keep the same with Xray buffer.Write(userUUID.Bytes()) } + buffer.WriteByte(command) binary.BigEndian.PutUint16(buffer.Extend(2), uint16(contentLen)) binary.BigEndian.PutUint16(buffer.Extend(2), uint16(paddingLen)) @@ -37,13 +43,18 @@ func WriteWithPadding(buffer *buf.Buffer, p []byte, command byte, userUUID *uuid log.Debugln("XTLS Vision write padding1: command=%v, payloadLen=%v, paddingLen=%v", command, contentLen, paddingLen) } -func ApplyPadding(buffer *buf.Buffer, command byte, userUUID *uuid.UUID) { +func ApplyPadding(buffer *buf.Buffer, command byte, userUUID *uuid.UUID, paddingTLS bool) { contentLen := int32(buffer.Len()) var paddingLen int32 - if contentLen < 900 { + if contentLen < 900 && paddingTLS { + log.Debugln("long padding") paddingLen = rand.Int31n(500) + 900 - contentLen + } else { + paddingLen = rand.Int31n(256) + } + if paddingLen > buf.BufferSize-21-contentLen { + paddingLen = buf.BufferSize - 21 - contentLen } - binary.BigEndian.PutUint16(buffer.ExtendHeader(2), uint16(paddingLen)) binary.BigEndian.PutUint16(buffer.ExtendHeader(2), uint16(contentLen)) buffer.ExtendHeader(1)[0] = command