U
    
W[û  ã                   @   s~   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ G d	d
„ d
ejeƒZG dd„ dejƒZdd„ ZdS )z@
Protocol wrapper that provides HAProxy PROXY protocol support.
é    )Úpolicies)Ú
interfaces)Ú_WrapperServerEndpointé   )ÚInvalidProxyHeader)ÚV1Parser)ÚV2Parserc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚHAProxyProtocolWrappera  
    A Protocol wrapper that provides HAProxy support.

    This protocol reads the PROXY stream header, v1 or v2, parses the provided
    connection data, and modifies the behavior of getPeer and getHost to return
    the data provided by the PROXY header.
    c                 C   s    t j | ||¡ d | _d | _d S ©N)r   ÚProtocolWrapperÚ__init__Ú
_proxyInfoÚ_parser)ÚselfÚfactoryÚwrappedProtocol© r   úD/usr/lib/python3/dist-packages/twisted/protocols/haproxy/_wrapper.pyr      s    zHAProxyProtocolWrapper.__init__c                 C   sÜ   | j d k	r| j |¡S | jd kr”t|ƒdkr`|d d… tjkr`t|dd… ƒd@ dkr`tƒ | _n4t|ƒdkrˆ|d d… tj	krˆtƒ | _n|  
¡  d S z&| j |¡\| _ }|r¸| j |¡ W n tk
rÖ   |  
¡  Y nX d S )Né   é   é   éð   é    é   é   )r   r   ÚdataReceivedr   Úlenr   ZPREFIXÚordr   ZPROXYSTRZloseConnectionZfeedr   )r   ÚdataZ	remainingr   r   r   r   #   s(    


ÿþý

z#HAProxyProtocolWrapper.dataReceivedc                 C   s    | j r| j jr| j jS | j ¡ S r
   )r   ÚsourceÚ	transportÚgetPeer©r   r   r   r   r!   <   s    zHAProxyProtocolWrapper.getPeerc                 C   s    | j r| j jr| j jS | j ¡ S r
   )r   Zdestinationr    ÚgetHostr"   r   r   r   r#   B   s    zHAProxyProtocolWrapper.getHostN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__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 )ÚHAProxyWrappingFactoryzL
    A Factory wrapper that adds PROXY protocol support to connections.
    c                 C   s.   t j | j¡r| j ¡ }n
| jjj}d|f S )z“
        Annotate the wrapped factory's log prefix with some text indicating
        the PROXY protocol is in use.

        @rtype: C{str}
        z
%s (PROXY))r   ZILoggingContextZ
providedByZwrappedFactoryÚ	logPrefixÚ	__class__r$   )r   r)   r   r   r   r)   O   s    
z HAProxyWrappingFactory.logPrefixN)r$   r%   r&   r'   r	   Zprotocolr)   r   r   r   r   r(   I   s   r(   c                 C   s
   t | tƒS )a±  
    Wrap an endpoint with PROXY protocol support, so that the transport's
    C{getHost} and C{getPeer} methods reflect the attributes of the proxied
    connection rather than the underlying connection.

    @param wrappedEndpoint: The underlying listening endpoint.
    @type wrappedEndpoint: L{IStreamServerEndpoint}

    @return: a new listening endpoint that speaks the PROXY protocol.
    @rtype: L{IStreamServerEndpoint}
    )r   r(   )ZwrappedEndpointr   r   r   ÚproxyEndpoint^   s    r+   N)r'   Ztwisted.protocolsr   Ztwisted.internetr   Ztwisted.internet.endpointsr   Ú_exceptionsr   Z	_v1parserr   Z	_v2parserr   r   Úobjectr	   ZWrappingFactoryr(   r+   r   r   r   r   Ú<module>   s   5