mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-17 20:54:36 +03:00
QUIC sniffer: Optimize the code (#4655)
Some checks are pending
Build docker image / build-image (push) Waiting to run
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Waiting to run
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Waiting to run
Build and Release / build (ppc64le, linux) (push) Waiting to run
Build and Release / build (riscv64, linux) (push) Waiting to run
Build and Release / build (s390x, linux) (push) Waiting to run
Build and Release / build (386, freebsd, ) (push) Waiting to run
Build and Release / build (386, linux, ) (push) Waiting to run
Build and Release / build (386, openbsd, ) (push) Waiting to run
Build and Release / build (386, windows, ) (push) Waiting to run
Build and Release / build (amd64, darwin, ) (push) Waiting to run
Build and Release / build (amd64, freebsd, ) (push) Waiting to run
Build and Release / build (amd64, linux, ) (push) Waiting to run
Build and Release / build (amd64, openbsd, ) (push) Waiting to run
Build and Release / build (amd64, windows, ) (push) Waiting to run
Build and Release / build (arm, 5, linux) (push) Waiting to run
Build and Release / build (arm, 6, linux) (push) Waiting to run
Build and Release / build (arm, 7, freebsd) (push) Waiting to run
Build and Release / build (arm, 7, linux) (push) Waiting to run
Build and Release / build (arm, 7, openbsd) (push) Waiting to run
Build and Release / build (arm, 7, windows) (push) Waiting to run
Build and Release / build (arm64, android) (push) Waiting to run
Build and Release / build (arm64, darwin) (push) Waiting to run
Build and Release / build (arm64, freebsd) (push) Waiting to run
Build and Release / build (arm64, linux) (push) Waiting to run
Build and Release / build (arm64, openbsd) (push) Waiting to run
Build and Release / build (arm64, windows) (push) Waiting to run
Build and Release / build (loong64, linux) (push) Waiting to run
Build and Release / build (mips, linux) (push) Waiting to run
Build and Release / build (mips64, linux) (push) Waiting to run
Build and Release / build (mips64le, linux) (push) Waiting to run
Build and Release / build (mipsle, linux) (push) Waiting to run
Build and Release / build (ppc64, linux) (push) Waiting to run
Test / test (macos-latest) (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
Test / test (windows-latest) (push) Waiting to run
Some checks are pending
Build docker image / build-image (push) Waiting to run
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Waiting to run
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Waiting to run
Build and Release / build (ppc64le, linux) (push) Waiting to run
Build and Release / build (riscv64, linux) (push) Waiting to run
Build and Release / build (s390x, linux) (push) Waiting to run
Build and Release / build (386, freebsd, ) (push) Waiting to run
Build and Release / build (386, linux, ) (push) Waiting to run
Build and Release / build (386, openbsd, ) (push) Waiting to run
Build and Release / build (386, windows, ) (push) Waiting to run
Build and Release / build (amd64, darwin, ) (push) Waiting to run
Build and Release / build (amd64, freebsd, ) (push) Waiting to run
Build and Release / build (amd64, linux, ) (push) Waiting to run
Build and Release / build (amd64, openbsd, ) (push) Waiting to run
Build and Release / build (amd64, windows, ) (push) Waiting to run
Build and Release / build (arm, 5, linux) (push) Waiting to run
Build and Release / build (arm, 6, linux) (push) Waiting to run
Build and Release / build (arm, 7, freebsd) (push) Waiting to run
Build and Release / build (arm, 7, linux) (push) Waiting to run
Build and Release / build (arm, 7, openbsd) (push) Waiting to run
Build and Release / build (arm, 7, windows) (push) Waiting to run
Build and Release / build (arm64, android) (push) Waiting to run
Build and Release / build (arm64, darwin) (push) Waiting to run
Build and Release / build (arm64, freebsd) (push) Waiting to run
Build and Release / build (arm64, linux) (push) Waiting to run
Build and Release / build (arm64, openbsd) (push) Waiting to run
Build and Release / build (arm64, windows) (push) Waiting to run
Build and Release / build (loong64, linux) (push) Waiting to run
Build and Release / build (mips, linux) (push) Waiting to run
Build and Release / build (mips64, linux) (push) Waiting to run
Build and Release / build (mips64le, linux) (push) Waiting to run
Build and Release / build (mipsle, linux) (push) Waiting to run
Build and Release / build (ppc64, linux) (push) Waiting to run
Test / test (macos-latest) (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
Test / test (windows-latest) (push) Waiting to run
Based on 2eed70e17d
This commit is contained in:
@@ -43,7 +43,7 @@ func (r *cachedReader) Cache(b *buf.Buffer, deadline time.Duration) error {
|
|||||||
r.cache, _ = buf.MergeMulti(r.cache, mb)
|
r.cache, _ = buf.MergeMulti(r.cache, mb)
|
||||||
}
|
}
|
||||||
b.Clear()
|
b.Clear()
|
||||||
rawBytes := b.Extend(b.Cap())
|
rawBytes := b.Extend(min(r.cache.Len(), b.Cap()))
|
||||||
n := r.cache.Copy(rawBytes)
|
n := r.cache.Copy(rawBytes)
|
||||||
b.Resize(0, int32(n))
|
b.Resize(0, int32(n))
|
||||||
r.Unlock()
|
r.Unlock()
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/quic-go/quic-go/quicvarint"
|
"github.com/quic-go/quic-go/quicvarint"
|
||||||
"github.com/xtls/xray-core/common"
|
"github.com/xtls/xray-core/common"
|
||||||
"github.com/xtls/xray-core/common/buf"
|
"github.com/xtls/xray-core/common/buf"
|
||||||
"github.com/xtls/xray-core/common/bytespool"
|
|
||||||
"github.com/xtls/xray-core/common/errors"
|
"github.com/xtls/xray-core/common/errors"
|
||||||
"github.com/xtls/xray-core/common/protocol"
|
"github.com/xtls/xray-core/common/protocol"
|
||||||
ptls "github.com/xtls/xray-core/common/protocol/tls"
|
ptls "github.com/xtls/xray-core/common/protocol/tls"
|
||||||
@@ -53,9 +52,9 @@ func SniffQUIC(b []byte) (*SniffHeader, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Crypto data separated across packets
|
// Crypto data separated across packets
|
||||||
cryptoLen := 0
|
cryptoLen := int32(0)
|
||||||
cryptoData := bytespool.Alloc(32767)
|
cryptoDataBuf := buf.NewWithSize(32767)
|
||||||
defer bytespool.Free(cryptoData)
|
defer cryptoDataBuf.Release()
|
||||||
cache := buf.New()
|
cache := buf.New()
|
||||||
defer cache.Release()
|
defer cache.Release()
|
||||||
|
|
||||||
@@ -143,7 +142,7 @@ func SniffQUIC(b []byte) (*SniffHeader, error) {
|
|||||||
|
|
||||||
cache.Clear()
|
cache.Clear()
|
||||||
mask := cache.Extend(int32(block.BlockSize()))
|
mask := cache.Extend(int32(block.BlockSize()))
|
||||||
block.Encrypt(mask, b[hdrLen+4:hdrLen+4+16])
|
block.Encrypt(mask, b[hdrLen+4:hdrLen+4+len(mask)])
|
||||||
b[0] ^= mask[0] & 0xf
|
b[0] ^= mask[0] & 0xf
|
||||||
packetNumberLength := int(b[0]&0x3 + 1)
|
packetNumberLength := int(b[0]&0x3 + 1)
|
||||||
for i := range packetNumberLength {
|
for i := range packetNumberLength {
|
||||||
@@ -217,15 +216,15 @@ func SniffQUIC(b []byte) (*SniffHeader, error) {
|
|||||||
if err != nil || length > uint64(buffer.Len()) {
|
if err != nil || length > uint64(buffer.Len()) {
|
||||||
return nil, io.ErrUnexpectedEOF
|
return nil, io.ErrUnexpectedEOF
|
||||||
}
|
}
|
||||||
if cryptoLen < int(offset+length) {
|
currentCryptoLen := int32(offset + length)
|
||||||
newCryptoLen := int(offset + length)
|
if cryptoLen < currentCryptoLen {
|
||||||
if len(cryptoData) < newCryptoLen {
|
if cryptoDataBuf.Cap() < currentCryptoLen {
|
||||||
return nil, io.ErrShortBuffer
|
return nil, io.ErrShortBuffer
|
||||||
}
|
}
|
||||||
wipeBytes(cryptoData[cryptoLen:newCryptoLen])
|
cryptoDataBuf.Extend(currentCryptoLen - cryptoLen)
|
||||||
cryptoLen = newCryptoLen
|
cryptoLen = currentCryptoLen
|
||||||
}
|
}
|
||||||
if _, err := buffer.Read(cryptoData[offset : offset+length]); err != nil { // Field: Crypto Data
|
if _, err := buffer.Read(cryptoDataBuf.BytesRange(int32(offset), currentCryptoLen)); err != nil { // Field: Crypto Data
|
||||||
return nil, io.ErrUnexpectedEOF
|
return nil, io.ErrUnexpectedEOF
|
||||||
}
|
}
|
||||||
case 0x1c: // CONNECTION_CLOSE frame, only 0x1c is permitted in initial packet
|
case 0x1c: // CONNECTION_CLOSE frame, only 0x1c is permitted in initial packet
|
||||||
@@ -250,7 +249,7 @@ func SniffQUIC(b []byte) (*SniffHeader, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tlsHdr := &ptls.SniffHeader{}
|
tlsHdr := &ptls.SniffHeader{}
|
||||||
err = ptls.ReadClientHello(cryptoData[:cryptoLen], tlsHdr)
|
err = ptls.ReadClientHello(cryptoDataBuf.BytesRange(0, cryptoLen), tlsHdr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// The crypto data may have not been fully recovered in current packets,
|
// The crypto data may have not been fully recovered in current packets,
|
||||||
// So we continue to sniff rest packets.
|
// So we continue to sniff rest packets.
|
||||||
@@ -263,12 +262,6 @@ func SniffQUIC(b []byte) (*SniffHeader, error) {
|
|||||||
return nil, protocol.ErrProtoNeedMoreData
|
return nil, protocol.ErrProtoNeedMoreData
|
||||||
}
|
}
|
||||||
|
|
||||||
func wipeBytes(b []byte) {
|
|
||||||
for i := range len(b) {
|
|
||||||
b[i] = 0x0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func hkdfExpandLabel(hash crypto.Hash, secret, context []byte, label string, length int) []byte {
|
func hkdfExpandLabel(hash crypto.Hash, secret, context []byte, label string, length int) []byte {
|
||||||
b := make([]byte, 3, 3+6+len(label)+1+len(context))
|
b := make([]byte, 3, 3+6+len(label)+1+len(context))
|
||||||
binary.BigEndian.PutUint16(b, uint16(length))
|
binary.BigEndian.PutUint16(b, uint16(length))
|
||||||
|
|||||||
Reference in New Issue
Block a user