U
    
W[O	  ã                   @   sv   d Z ddlmZ ddlmZ G dd„ dejƒZG dd„ deƒZG dd	„ d	ejƒZ	G d
d„ deƒZ
G dd„ dejƒZdS )z
A simple port forwarder.
é    )Úprotocol)Úlogc                   @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ ZdS )	ÚProxyTNc                 C   s
   || _ d S ©N)Úpeer)Úselfr   © r   ú?/usr/lib/python3/dist-packages/twisted/protocols/portforward.pyÚsetPeer   s    zProxy.setPeerc                 C   s8   | j d k	r| j j ¡  d | _ n| jr4t d|f ¡ d S )NzUnable to connect to peer: %s)r   Ú	transportÚloseConnectionÚnoisyr   Úmsg)r   Úreasonr   r   r	   ÚconnectionLost   s
    
zProxy.connectionLostc                 C   s   | j j |¡ d S r   )r   r   Úwrite)r   Údatar   r   r	   ÚdataReceived   s    zProxy.dataReceived)Ú__name__Ú
__module__Ú__qualname__r   r   r
   r   r   r   r   r   r	   r      s
   r   c                   @   s   e Zd Zdd„ ZdS )ÚProxyClientc                 C   s@   | j  | ¡ | j | j jd¡ | j j | jd¡ | j j ¡  d S )NT)r   r
   r   ZregisterProducerZresumeProducing)r   r   r   r	   ÚconnectionMade$   s    zProxyClient.connectionMadeN)r   r   r   r   r   r   r   r	   r   #   s   r   c                   @   s(   e Zd ZeZdd„ Zdd„ Zdd„ ZdS )ÚProxyClientFactoryc                 C   s
   || _ d S r   )Úserver)r   r   r   r   r	   Ú	setServer7   s    zProxyClientFactory.setServerc                 O   s$   t jj| f|ž|Ž}| | j¡ |S r   )r   ÚClientFactoryÚbuildProtocolr
   r   )r   ÚargsÚkwZprotr   r   r	   r   ;   s    z ProxyClientFactory.buildProtocolc                 C   s   | j j ¡  d S r   )r   r   r   )r   Z	connectorr   r   r   r	   ÚclientConnectionFailedA   s    z)ProxyClientFactory.clientConnectionFailedN)r   r   r   r   r   r   r   r    r   r   r   r	   r   3   s   r   c                   @   s   e Zd ZeZdZdd„ ZdS )ÚProxyServerNc                 C   sT   | j  ¡  |  ¡ }| | ¡ | jd kr8ddlm} || _| j | jj| jj	|¡ d S )Nr   )Úreactor)
r   ZpauseProducingÚclientProtocolFactoryr   r"   Útwisted.internetZ
connectTCPÚfactoryÚhostÚport)r   Zclientr"   r   r   r	   r   K   s    


zProxyServer.connectionMade)r   r   r   r   r#   r"   r   r   r   r   r	   r!   F   s   r!   c                   @   s   e Zd ZdZeZdd„ ZdS )ÚProxyFactoryz%
    Factory for port forwarder.
    c                 C   s   || _ || _d S r   )r&   r'   )r   r&   r'   r   r   r	   Ú__init__a   s    zProxyFactory.__init__N)r   r   r   Ú__doc__r!   r   r)   r   r   r   r	   r(   Z   s   r(   N)r*   r$   r   Ztwisted.pythonr   ZProtocolr   r   r   r   r!   ZFactoryr(   r   r   r   r	   Ú<module>   s   