o
    >h;5                  
   @   s  d dl 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mZ d dlmZ zd dlmZ W n eyI   edZY nw z
d dl
mZ d	ZW n eyc   d
ZedZY nw zd dlmZ d dlmZ d dlmZ d	ZW n ey   d
ZY nw dZdddZdeeef de fddZ!esdde"de#dee  dee$ fddZ%ndde"de#dee  dee$ fddZ%esde"deee ef  fddZ&ndedeee ef  fddZ&deeef de#defddZ'dS )     N)OptionalUnionDictAny)Deferred)IPv4AddressUNIXAddress)
ITransportIProcessTransport)TransportDetails)PipeAddress)IPv6AddressTF)ISSLTransport)TLSMemoryBIOProtocol)
Connection)sleeppeer2strtransport_channel_idextract_peer_certificatecreate_transport_detailsc                 C   s*   |sddl m} t }|| |jd |S )aD  
    Inline sleep for use in co-routines (Twisted ``inlineCallback`` decorated functions).

    .. seealso::
       * `twisted.internet.defer.inlineCallbacks <http://twistedmatrix.com/documents/current/api/twisted.internet.defer.html#inlineCallbacks>`__
       * `twisted.internet.interfaces.IReactorTime <http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IReactorTime.html>`__

    :param delay: Time to sleep in seconds.
    :type delay: float
    :param reactor: The Twisted reactor to use.
    :type reactor: None or provider of ``IReactorTime``.
    r   )reactorN)twisted.internetr   r   	callLatercallback)delayr   d r   u/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/autobahn/twisted/util.pyr   D   s
   r   	transportreturnc                 C   s   t | rd| j}|S t| r\|  }t|tr%d|j|j	}|S t
r6t|tr6d|j|j	}|S t|trJ|jrFd|j}|S d}|S t|trXdt }|S d}|S d}|S )a  
    Return a *peer descriptor* given a Twisted transport, for example:

    * ``tcp4:127.0.0.1:52914``: a TCPv4 socket
    * ``unix:/tmp/server.sock``: a Unix domain socket
    * ``process:142092``: a Pipe originating from a spawning (parent) process
    * ``pipe``: a Pipe terminating in a spawned (child) process

    :returns: Returns a string representation of the peer of the Twisted transport.
    z
process:{}ztcp4:{0}:{1}ztcp6:{0}:{1}zunix:{0}unixzprocess:{0}unknown)r
   
providedByformatpidr	   getPeer
isinstancer   hostport	_HAS_IPV6r   r   namer   osgetppid)r   resaddrr   r   r   r   X   s0   




r   	is_serverchannel_id_typec                 C   s   |d u rdS t d|)N                                    zUcannot determine TLS channel ID of type "{}" when TLS is not available on this system)RuntimeErrorr#   )r   r/   r0   r   r   r   r      s   r   c                 C   s   |du rdS |dvrt d|t| ts t d|t| |  }|r+t|ts-J d}|dkrS||kr<| }n| }|du rFdS t	
 }|| | S td|)	a(  
        Return TLS channel ID of WAMP transport of the given TLS channel ID type.

        Application-layer user authentication protocols are vulnerable to generic credential forwarding attacks,
        where an authentication credential sent by a client C to a server M may then be used by M to impersonate C at
        another server S.
        To prevent such credential forwarding attacks, modern authentication protocols rely on channel bindings.
        For example, WAMP-cryptosign can use the tls-unique channel identifier provided by the TLS layer to strongly
        bind authentication credentials to the underlying channel, so that a credential received on one TLS channel
        cannot be forwarded on another.

        :param transport: The Twisted TLS transport to extract the TLS channel ID from. If the transport isn't
            TLS based, and non-empty ``channel_id_type`` is requested, ``None`` will be returned. If the transport
            is indeed TLS based, an empty ``channel_id_type`` of ``None`` is requested, 32 NUL bytes will be returned.
        :param is_server: Flag indicating that the transport is a server transport.
        :param channel_id_type: TLS channel ID type, if set currently only ``"tls-unique"`` is supported.
        :returns: The TLS channel ID (32 bytes).
        Nr1   )
tls-uniquez*invalid TLS channel ID type "{}" requestedz[cannot determine TLS channel ID of type "{}" when TLS is not available on this transport {}Tr3   z7should not arrive here (unhandled channel_id_type "{}"))r2   r#   r&   r   type	getHandler   get_peer_finishedget_finishedhashlibsha256updatedigestNotImplementedError)r   r/   r0   
connectionis_not_resumedtls_finished_msgmr   r   r   r      s.   


c                 C   s   dS )z~
        Dummy when no TLS is available.

        :param transport: Ignored.
        :return: Always return ``None``.
        Nr   )r   r   r   r   r      s   r   c                 C   s  t | r
t| dsdS |  }|rdd }d||d d||d d||d t| ||	 t
| || t
| || || g d	}t| D ]$}||}d|| d||| d
kd}|d | qdd| fd| ffD ]'\}}i ||< | D ]\}	}
||	}	||
}
d|
|| d|	 < qq|S dS )a  
        Extract TLS x509 client certificate information from a Twisted stream transport, and
        return a dict with x509 TLS client certificate information (if the client provided a
        TLS client certificate).

        :param transport: The secure transport from which to extract the peer certificate (if present).
        :returns: If the peer provided a certificate, the parsed certificate information set.
        getPeerCertificateNc                 S   s   t | tr
| dS | S )Nutf8)r&   bytesdecode)_valuer   r   r   maybe_bytes   s   

z-extract_peer_certificate.<locals>.maybe_bytesz{}md5sha1r9   )rG   rH   r9   expiredhashserialsignature_algorithmversion
not_before	not_after
extensionsr   )r*   valuecriticalrP   subjectissuer)r   r"   hasattrrA   r#   r;   upperboolhas_expiredsubject_name_hashintget_serial_numberget_signature_algorithmget_versionget_notBeforeget_notAfterrangeget_extension_countget_extensionget_short_nameget_criticalappendget_subject
get_issuerget_componentslower)r   certrF   resultiextext_infoentityr*   keyrQ   r   r   r   r      sB   









 c                 C   s   t | }t }ttdrt }nt }d}tr1t	| r1dt
| |di}tj}t| }d}	n	i }tj}d}d}	tj}
t||
||||||	||d
}|S )aB  
    Create transport details from Twisted transport.

    :param transport: The Twisted transport to extract information from.
    :param is_server: Flag indicating whether this transport side is a "server" (as in TCP server).
    :return: Transport details object filled with information from the Twisted transport.
    get_native_idr3   TNF)
channel_typechannel_framingpeerr/   own_pidown_tidown_fd	is_secure
channel_id	peer_cert)r   r+   getpidrU   	threadingrq   	get_ident_HAS_TLSr   r"   r   r   CHANNEL_TYPE_TLSr   CHANNEL_TYPE_TCPCHANNEL_FRAMING_WEBSOCKET)r   r/   ru   rv   rw   rx   rz   rs   r{   ry   rt   tdr   r   r   r     s,   

r   )N)(r+   r8   r}   typingr   r   r   r   twisted.internet.deferr   twisted.internet.addressr   r   twisted.internet.interfacesr	   r
   autobahn.wamp.typesr   twisted.internet.stdior   ImportErrorr4   r   r)   r   twisted.protocols.tlsr   OpenSSL.SSLr   r   __allr   strr   objectrW   rC   r   r   r   r   r   r   r   <module>   sL   
	'&$A 	"7