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