2018-05-10 15:22:19 +03:00
|
|
|
import struct
|
2019-03-10 03:00:11 +03:00
|
|
|
import random
|
|
|
|
import os
|
2018-05-10 15:22:19 +03:00
|
|
|
|
2019-03-12 03:12:55 +03:00
|
|
|
from .connection import Connection, PacketCodec
|
2018-05-10 15:22:19 +03:00
|
|
|
|
|
|
|
|
2019-03-12 03:12:55 +03:00
|
|
|
class IntermediatePacketCodec(PacketCodec):
|
2019-03-10 03:00:11 +03:00
|
|
|
tag = b'\xee\xee\xee\xee'
|
2019-03-12 03:12:55 +03:00
|
|
|
obfuscate_tag = tag
|
2019-03-10 03:00:11 +03:00
|
|
|
|
|
|
|
def encode_packet(self, data):
|
|
|
|
return struct.pack('<i', len(data)) + data
|
|
|
|
|
|
|
|
async def read_packet(self, reader):
|
|
|
|
length = struct.unpack('<i', await reader.readexactly(4))[0]
|
|
|
|
return await reader.readexactly(length)
|
|
|
|
|
|
|
|
|
2019-03-12 03:12:55 +03:00
|
|
|
class RandomizedIntermediatePacketCodec(IntermediatePacketCodec):
|
2019-03-10 03:00:11 +03:00
|
|
|
"""
|
|
|
|
Data packets are aligned to 4bytes. This codec adds random bytes of size
|
|
|
|
from 0 to 3 bytes, which are ignored by decoder.
|
|
|
|
"""
|
2019-03-12 03:12:55 +03:00
|
|
|
tag = None
|
|
|
|
obfuscate_tag = b'\xdd\xdd\xdd\xdd'
|
2019-03-10 03:00:11 +03:00
|
|
|
|
|
|
|
def encode_packet(self, data):
|
|
|
|
pad_size = random.randint(0, 3)
|
|
|
|
padding = os.urandom(pad_size)
|
|
|
|
return super().encode_packet(data + padding)
|
|
|
|
|
|
|
|
async def read_packet(self, reader):
|
|
|
|
packet_with_padding = await super().read_packet(reader)
|
|
|
|
pad_size = len(packet_with_padding) % 4
|
|
|
|
if pad_size > 0:
|
|
|
|
return packet_with_padding[:-pad_size]
|
|
|
|
return packet_with_padding
|
2019-03-12 03:12:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ConnectionTcpIntermediate(Connection):
|
|
|
|
"""
|
|
|
|
Intermediate mode between `ConnectionTcpFull` and `ConnectionTcpAbridged`.
|
|
|
|
Always sends 4 extra bytes for the packet length.
|
|
|
|
"""
|
|
|
|
packet_codec = IntermediatePacketCodec
|