o
    ¯b]  ã                   @   s¤   d Z ddlmZ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 dd	lmZ dd
lmZ G dd„ de	jƒZG dd„ de	jƒZdejdefdd„ZdS )z@
Protocol wrapper that provides HAProxy PROXY protocol support.
é    )ÚOptionalÚUnion)Ú
interfaces)Ú_WrapperServerEndpoint)Úpoliciesé   )Ú_info)ÚInvalidProxyHeader)ÚV1Parser)ÚV2Parserc                       s`   e Zd ZdZdejdejf‡ fdd„Zde	ddfd	d
„Z
dejfdd„Zdejfdd„Z‡  Z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.
    ÚfactoryÚwrappedProtocolc                    s   t ƒ  ||¡ d | _d | _d S ©N)ÚsuperÚ__init__Ú
_proxyInfoÚ_parser)Úselfr   r   ©Ú	__class__© úD/usr/lib/python3/dist-packages/twisted/protocols/haproxy/_wrapper.pyr      s   
zHAProxyProtocolWrapper.__init__ÚdataÚreturnNc                 C   sê   | j d ur| j |¡S | j}|d u rPt|ƒdkr4|d d… tjkr4t|dd… ƒd@ dkr4tƒ  | _}nt|ƒdkrJ|d d… tj	krJtƒ  | _}n|  
¡  d S z| |¡\| _ }|rd| j |¡ W d S W d S  tyt   |  
¡  Y d S w )Né   é   é   éð   é    é   é   )r   r   ÚdataReceivedr   Úlenr   ÚPREFIXÚordr
   ÚPROXYSTRÚloseConnectionÚfeedr	   )r   r   ÚparserÚ	remainingr   r   r   r"   $   s(   
ÿÿz#HAProxyProtocolWrapper.dataReceivedc                 C   ó*   | j r| j jr| j jS | jsJ ‚| j ¡ S r   )r   ÚsourceÚ	transportÚgetPeer©r   r   r   r   r.   =   ó   

zHAProxyProtocolWrapper.getPeerc                 C   r+   r   )r   Údestinationr-   ÚgetHostr/   r   r   r   r2   C   r0   zHAProxyProtocolWrapper.getHost)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚWrappingFactoryr   Ú	IProtocolr   Úbytesr"   ÚIAddressr.   r2   Ú__classcell__r   r   r   r   r      s    ÿÿr   c                   @   s"   e Zd ZdZeZdefdd„ZdS )ÚHAProxyWrappingFactoryzL
    A Factory wrapper that adds PROXY protocol support to connections.
    r   c                 C   s.   t j | j¡r| j ¡ }n| jjj}|› d�S )z“
        Annotate the wrapped factory's log prefix with some text indicating
        the PROXY protocol is in use.

        @rtype: C{str}
        z (PROXY))r   ÚILoggingContextÚ
providedByÚwrappedFactoryÚ	logPrefixr   r3   )r   r@   r   r   r   r@   Q   s   

z HAProxyWrappingFactory.logPrefixN)r3   r4   r5   r6   r   ÚprotocolÚstrr@   r   r   r   r   r<   J   s    r<   ÚwrappedEndpointr   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<   )rC   r   r   r   ÚproxyEndpoint_   s   
rD   N)r6   Útypingr   r   Útwisted.internetr   Útwisted.internet.endpointsr   Útwisted.protocolsr   Ú r   Ú_exceptionsr	   Ú	_v1parserr
   Ú	_v2parserr   ÚProtocolWrapperr   r7   r<   ÚIStreamServerEndpointrD   r   r   r   r   Ú<module>   s    6ÿþ