
    tf                     (   d Z ddlZddlZddlmZmZmZmZ ddlm	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mZ d	dlmZmZmZmZ  G d de          Z G d de          ZdZdZdZdZ  e	ej!                   G d d                      Z"dS )zD
IProxyParser implementation for version two of the PROXY protocol.
    N)CallableTupleTypeUnion)implementer)ValueConstantValues)Literal)address)compat   )_info_interfaces)InvalidNetworkProtocolInvalidProxyHeaderMissingAddressDataconvertErrorc                   j    e Zd ZdZ ed          Z ed          Z ed          Z ed          ZdS )	NetFamilyz(
    Values for the 'family' field.
    r          0   N)	__name__
__module____qualname____doc__r   UNSPECINETINET6UNIX     b/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/protocols/haproxy/_v2parser.pyr   r      sV          ]4  F=DM$E=DDDr"   r   c                   T    e Zd ZdZ ed          Z ed          Z ed          ZdS )NetProtocolz&
    Values for 'protocol' field.
    r   r      N)r   r   r   r   r   r   STREAMDGRAMr!   r"   r#   r%   r%   )   sG          ]1F]1FM!EEEr"   r%         LOCALPROXYc                       e Zd ZdZdZdgZeedZdddddddZ	ddZ
ded	eeej        ef         ed         f         fdZeded	efd            Zeded	efd            Zeded	ej        fd            Zd
S )V2Parserzn
    PROXY protocol version two header parser.

    Version two of the PROXY protocol is a binary format.
    s   

 
QUIT
r   )r   r   z!4s4s2Hz	!16s16s2Hz	!108s108s)      !   "   1   2   returnNc                     d| _         d S )Nr"   )buffer)selfs    r#   __init__zV2Parser.__init__P   s    r"   dataNNc                 t   | xj         |z  c_         t          | j                   dk     rt                      t          j        d| j         dd                   d         dz   }t          | j                   |k     rdS | j         d|         | j         |d         }}d| _         |                     |          }||fS )a  
        Consume a chunk of data and attempt to parse it.

        @param data: A bytestring.
        @type data: bytes

        @return: A two-tuple containing, in order, a L{_interfaces.IProxyInfo}
            and any bytes fed to the parser that followed the end of the
            header.  Both of these values are None until a complete header is
            parsed.

        @raises InvalidProxyHeader: If the bytes fed to the parser create an
            invalid PROXY header.
        r   z!H   r   r;   Nr"   )r7   lenr   structunpackparse)r8   r:   sizeheader	remaininginfos         r#   feedzV2Parser.feedS   s    " 	tt{b  $&&&}T4;r"u#566q9B>t{d""< K.DEE0B	zz&!!i  r"   
bytestringc                 d    d                     d t          j        |           D                       S )a7  
        Convert packed 32-bit IPv4 address bytes into a dotted-quad ASCII bytes
        representation of that address.

        @param bytestring: 4 octets representing an IPv4 address.
        @type bytestring: L{bytes}

        @return: a dotted-quad notation IPv4 address.
        @rtype: L{bytes}
           .c              3   b   K   | ]*}d t          |          fz                      d          V  +dS )z%iasciiN)ordencode).0bs     r#   	<genexpr>z(V2Parser._bytesToIPv4.<locals>.<genexpr>}   sN       
 
34TSVVI%%g..
 
 
 
 
 
r"   )joinr   	iterbytes)rG   s    r#   _bytesToIPv4zV2Parser._bytesToIPv4q   sA     yy 
 
8>8H8T8T
 
 
 
 
 	
r"   c                     t          j        |           d                    fdt          ddd          D                       S )a=  
        Convert packed 128-bit IPv6 address bytes into a colon-separated ASCII
        bytes representation of that address.

        @param bytestring: 16 octets representing an IPv6 address.
        @type bytestring: L{bytes}

        @return: a dotted-quad notation IPv6 address.
        @rtype: L{bytes}
           :c              3   x   K   | ]4}t          ||d z            d          d                    d          V  5dS )   r   xrK   N)intrM   )rN   rO   	hexStrings     r#   rP   z(V2Parser._bytesToIPv6.<locals>.<genexpr>   sb       
 
 Ia!a%i("--1199'BB
 
 
 
 
 
r"   r   r   rW   )binasciib2a_hexrQ   range)rG   rZ   s    @r#   _bytesToIPv6zV2Parser._bytesToIPv6   s[     $Z00	yy 
 
 
 
1b!__
 
 
 
 
 	
r"   linec           
         |dd         }d}t          t          t                    5  t          |dd                   }t          |dd                   }ddd           n# 1 swxY w Y   || j        k    rt                      |t
          z  |t          z  }}|| j        vs	|| j        vrt                      | j        |         t          k    rt          j        |dd          S |t
          z  |t          z  }	}t          t          t                    5  t                              |          }t                               |	          }	ddd           n# 1 swxY w Y   |t          j        u s|	t           j        u rt          j        |dd          S | j        |         }
|ddt'          j        |
          z            }|t          j        u rt          t&          j        t.                    5  t'          j        |
|          \  }}ddd           n# 1 swxY w Y   t          j        |t3          j        |                    d                    t3          j        |                    d                              S d}|	t           j        u rd}t2          j        }| j        }|t          j        u rt2          j         }| j!        }t          t&          j        t.                    5  t'          j        |
|          }|\  }}}}ddd           n# 1 swxY w Y   t          j        | || ||          "                                |           || ||          "                                |                    S )	a  
        Parse a bytestring as a full PROXY protocol header.

        @param line: A bytestring that represents a valid HAProxy PROXY
            protocol version 2 header.
        @type line: bytes

        @return: A L{_interfaces.IProxyInfo} containing the
            parsed data.

        @raises InvalidProxyHeader: If the bytestring does not represent a
            valid PROXY header.
        N      r=   r       TCPUDP)#r   
IndexErrorr   rL   PREFIX_HIGH_LOWVERSIONSCOMMANDS_LOCALCOMMANDr   	ProxyInfo
ValueErrorr   r   lookupByValuer%   r   ADDRESSFORMATSr?   calcsizer    errorr   r@   r   UNIXAddressrstripr(   IPv4AddressrS   r   IPv6Addressr^   decode)clsr_   prefixaddrInfoversionCommandfamilyProtoversioncommandfamilynetprotoaddressFormatsourcedestaddrTypeaddrCls
addrParserrE   sPortdPorts                      r#   rA   zV2Parser.parse   s?    crc*&899 	+ 	+ !be--Nd2b5k**K		+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ SZ$&&&)E1>D3H#,&&'*E*E$&&&< M11?4t444&.d0B*&<== 	; 	;,,V44F"00::H	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; Y%%%[5G)G)G?4t444*;7R&/-"@"@@@AY^##fl,>?? F F%}]HEEF F F F F F F F F F F F F F F?#FMM'$:$:;;#DKK$8$899   ;@{(((H  	 03/?
Y_$$)G)J&,(:;; 	. 	.=99D)-&FD%	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. GHjj0077995AAGHjj..5577??
 
 	
sG   /A""A&)A&5EEE+HHH L		LL)r5   N)r   r   r   r   rg   rj   rl   _PROXYCOMMANDrk   rp   r9   bytesr   r   r   rm   rF   staticmethodrS   r^   classmethodrA   r!   r"   r#   r.   r.   9   sA         AFtH ]33H 
 
N   !!	uU_e+,eJ.??	@! ! ! !< 
 
5 
 
 
 \
 
 
5 
 
 
 \
" E
 E
5? E
 E
 E
 [E
 E
 E
r"   r.   )#r   r[   r?   typingr   r   r   r   zope.interfacer   
constantlyr   r	   typing_extensionsr
   twisted.internetr   twisted.pythonr    r   r   _exceptionsr   r   r   r   r   r%   rh   ri   rl   r   IProxyParserr.   r!   r"   r#   <module>r      s      / / / / / / / / / / / / & & & & & & , , , , , , , , % % % % % % $ $ $ $ $ $ ! ! ! ! ! !                                      &    	 [%&&_
 _
 _
 _
 _
 _
 _
 '&_
 _
 _
r"   