chore: Vision padding upgrade

This commit is contained in:
gVisor bot 2023-03-04 21:47:06 +08:00
parent c1ca3d0d6f
commit 30213afed5
2 changed files with 31 additions and 20 deletions

View file

@ -241,7 +241,7 @@ func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error {
command = commandPaddingEnd command = commandPaddingEnd
// disable XTLS // disable XTLS
vc.readProcess = false //vc.readProcess = false
vc.writeFilterApplicationData = false vc.writeFilterApplicationData = false
vc.packetsToFilter = 0 vc.packetsToFilter = 0
} else if buffer.Len() > 6 && bytes.Equal(buffer.To(3), tlsApplicationDataStart) || 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 vc.writeFilterApplicationData = false
} }
ApplyPadding(buffer, command, nil) ApplyPadding(buffer, command, nil, vc.isTLS)
err := vc.ExtendedWriter.WriteBuffer(buffer) err := vc.ExtendedWriter.WriteBuffer(buffer)
if err != nil { if err != nil {
return err return err
@ -276,7 +276,7 @@ func (vc *Conn) WriteBuffer(buffer *buf.Buffer) error {
} }
vc.writeFilterApplicationData = false vc.writeFilterApplicationData = false
} }
ApplyPadding(buffer2, command, nil) ApplyPadding(buffer2, command, nil, vc.isTLS)
err = vc.ExtendedWriter.WriteBuffer(buffer2) err = vc.ExtendedWriter.WriteBuffer(buffer2)
if vc.writeDirect { if vc.writeDirect {
vc.ExtendedWriter = N.NewExtendedWriter(vc.Conn) 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 isVision && !vc.dst.UDP && !vc.dst.Mux {
if len(p) == 0 { if len(p) == 0 {
WriteWithPadding(buffer, nil, commandPaddingContinue, vc.id) WriteWithPadding(buffer, nil, commandPaddingContinue, vc.id, vc.isTLS)
} else { } else {
vc.FilterTLS(p) vc.FilterTLS(p)
if vc.isTLS { //if vc.isTLS {
WriteWithPadding(buffer, p, commandPaddingContinue, vc.id) WriteWithPadding(buffer, p, commandPaddingContinue, vc.id, vc.isTLS)
} else { //} else {
buf.Must(buf.Error(buffer.Write(p))) // buf.Must(buf.Error(buffer.Write(p)))
//
// disable XTLS // // disable XTLS
vc.readProcess = false // vc.readProcess = false
vc.writeFilterApplicationData = false // vc.writeFilterApplicationData = false
vc.packetsToFilter = 0 // vc.packetsToFilter = 0
} //}
} }
} else { } else {
buf.Must(buf.Error(buffer.Write(p))) buf.Must(buf.Error(buffer.Write(p)))

View file

@ -19,16 +19,22 @@ const (
commandPaddingDirect byte = 0x02 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)) contentLen := int32(len(p))
var paddingLen int32 var paddingLen int32
if contentLen < 900 { if contentLen < 900 && paddingTLS {
log.Debugln("long padding")
paddingLen = rand.Int31n(500) + 900 - contentLen 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 if userUUID != nil { // unnecessary, but keep the same with Xray
buffer.Write(userUUID.Bytes()) buffer.Write(userUUID.Bytes())
} }
buffer.WriteByte(command) buffer.WriteByte(command)
binary.BigEndian.PutUint16(buffer.Extend(2), uint16(contentLen)) binary.BigEndian.PutUint16(buffer.Extend(2), uint16(contentLen))
binary.BigEndian.PutUint16(buffer.Extend(2), uint16(paddingLen)) 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) 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()) contentLen := int32(buffer.Len())
var paddingLen int32 var paddingLen int32
if contentLen < 900 { if contentLen < 900 && paddingTLS {
log.Debugln("long padding")
paddingLen = rand.Int31n(500) + 900 - contentLen 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(paddingLen))
binary.BigEndian.PutUint16(buffer.ExtendHeader(2), uint16(contentLen)) binary.BigEndian.PutUint16(buffer.ExtendHeader(2), uint16(contentLen))
buffer.ExtendHeader(1)[0] = command buffer.ExtendHeader(1)[0] = command