Ϫf]dZddlmZmZddlmZddlmZddlm Z ddl m Z ddl m Z dd lmZdd lmZGd d e j$ZGd de j(Zdej,defdZy)z@ Protocol wrapper that provides HAProxy PROXY protocol support. )OptionalUnion) interfaces)_WrapperServerEndpoint)policies)_info)InvalidProxyHeader)V1Parser)V2ParserceZdZdZdej dejffd Zde ddfdZ dejfd Z dejfd Z xZS) 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. factorywrappedProtocolcBt|||d|_d|_yN)super__init__ _proxyInfo_parser)selfrr __class__s D/usr/lib/python3/dist-packages/twisted/protocols/haproxy/_wrapper.pyrzHAProxyProtocolWrapper.__init__s" /2598< datareturnNc>|j|jj|S|j}|t |dk\r<|ddt j k(r&t|dddzdk(rt x|_}nGt |dk\r(|ddtjk(rtx|_}n|jy |j|\|_}|r|jj|yy#t$r|jYywxYw)N ) rr dataReceivedrlenr PREFIXordr PROXYSTRloseConnectionfeedr )rrparser remainings rr%z#HAProxyProtocolWrapper.dataReceived$s ?? &''44T: : >D R"I0R $z1T9(0 2 vTaD!H0A0A$A(0 2 v##% ")/T): &DOY$$11)<! "    ! "s6DDDc|jr,|jjr|jjS|jsJ|jjSr)rsource transportgetPeerrs rr1zHAProxyProtocolWrapper.getPeer=sD ??t55??)) )~~~~~%%''rc|jr,|jjr|jjS|jsJ|jjSr)r destinationr0getHostr2s rr5zHAProxyProtocolWrapper.getHostCsD ??t::??.. .~~~~~%%''r)__name__ __module__ __qualname____doc__rWrappingFactoryr IProtocolrbytesr%IAddressr1r5 __classcell__)rs@rrrs`=//=BLBVBV=""4"2(,,( (,,(rrc eZdZdZeZdefdZy)HAProxyWrappingFactoryzL A Factory wrapper that adds PROXY protocol support to connections. rctjj|jr|jj }n |jj j }|dS)z Annotate the wrapped factory's log prefix with some text indicating the PROXY protocol is in use. @rtype: C{str} z (PROXY))rILoggingContext providedBywrappedFactory logPrefixrr6)rrEs rrEz HAProxyWrappingFactory.logPrefixQsV  % % 0 01D1D E++557I++55>>IH%%rN)r6r7r8r9rprotocolstrrErrr@r@Js&H &3 &rr@wrappedEndpointrc"t|tS)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} )rr@)rIs r proxyEndpointrK_s "/3I JJrN)r9typingrrtwisted.internetrtwisted.internet.endpointsrtwisted.protocolsrr _exceptionsr _v1parserr _v2parserr ProtocolWrapperrr:r@IStreamServerEndpointrKrHrrrVsg #'=&+3(X553(l&X55&*K55KKr