U
    P|jA	  ã                   @   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   úA/tmp/pip-unpacked-wheel-audpp6lv/twisted/protocols/portforward.pyÚsetPeer   s    zProxy.setPeerc                 C   s8   | j d k	r| j j ¡  d | _ n| jr4t d|› �¡ d S )NzUnable to connect to peer: )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	   Ú	setServer2   s    zProxyClientFactory.setServerc                 O   s$   t jj| f|ž|Ž}| | j¡ |S r   )r   ÚClientFactoryÚbuildProtocolr
   r   )r   ÚargsÚkwZprotr   r   r	   r   5   s    z ProxyClientFactory.buildProtocolc                 C   s   | j j ¡  d S r   )r   r   r   )r   Z	connectorr   r   r   r	   ÚclientConnectionFailed:   s    z)ProxyClientFactory.clientConnectionFailedN)r   r   r   r   r   r   r   r    r   r   r   r	   r   /   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   Úclientr"   r   r   r	   r   B   s    


zProxyServer.connectionMade)r   r   r   r   r#   r"   r   r   r   r   r	   r!   >   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__X   s    zProxyFactory.__init__N)r   r   r   Ú__doc__r!   r   r*   r   r   r   r	   r)   Q   s   r)   N)r+   r$   r   Ztwisted.pythonr   ÚProtocolr   r   r   r   r!   ZFactoryr)   r   r   r   r	   Ú<module>   s   