3
uQhÓ  ã               @   sN   d dl Z ddlmZ ddlmZ ddlmZ G dd„ dƒZG d	d
„ d
eƒZdS )é    Né   )ÚAbridgedPacketCodec)ÚObfuscatedConnectioné   )Ú
AESModeCTRc               @   s4   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	„ ZdS )
ÚObfuscatedIONc             C   s,   |j | _ |j| _| j|jƒ\| _| _| _d S )N)Ú_readerÚ_writerÚinit_headerÚpacket_codecÚheaderÚ_encryptÚ_decrypt)ÚselfÚ
connection© r   úM/tmp/pip-build-2nz6shyl/telethon/telethon/network/connection/tcpobfuscated.pyÚ__init__   s    zObfuscatedIO.__init__c       
      C   sâ   d}x<t jdƒ}|d dkr|d d… |kr|dd	… d
krP qW t|ƒ}|ddd… }t|d	d… ƒ}t|dd… ƒ}t|d d… ƒ}t|dd… ƒ}t||ƒ}t||ƒ}	| j|dd…< |jt|ƒƒdd… |dd…< |||	fS )Nó   PVrGó   GET ó   POSTó   îîîîé@   r   éï   é   é   s       é7   é   r   é(   é8   é    é0   é<   )r   r   r   r   éÿÿÿÿ)ÚosÚurandomÚ	bytearrayÚbytesr   Zobfuscate_tagÚencrypt)
r   ÚkeywordsÚrandomZrandom_reversedZencrypt_keyZ
encrypt_ivZdecrypt_keyZ
decrypt_ivZ	encryptorZ	decryptorr   r   r   r
      s$    


zObfuscatedIO.init_headerc             Ã   s   | j j| jj|ƒI d H ƒS )N)r   r(   r   Úreadexactly)r   Únr   r   r   r+   /   s    zObfuscatedIO.readexactlyc             C   s   | j j| jj|ƒƒ d S )N)r	   Úwriter   r(   )r   Údatar   r   r   r-   2   s    zObfuscatedIO.write)	Ú__name__Ú
__module__Ú__qualname__r   r   Ústaticmethodr
   r+   r-   r   r   r   r   r   	   s
   r   c               @   s   e Zd ZdZeZeZdS )ÚConnectionTcpObfuscatedzî
    Mode that Telegram defines as "obfuscated2". Encodes the packet
    just like `ConnectionTcpAbridged`, but encrypts every message with
    a randomly generated key using the AES-CTR mode so the packets are
    harder to discern.
    N)r/   r0   r1   Ú__doc__r   Zobfuscated_ior   r   r   r   r   r   r3   6   s   r3   )	r$   Ztcpabridgedr   r   r   Zcryptor   r   r3   r   r   r   r   Ú<module>   s
   -