o
    b                     @   s^   d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z
 ddlmZ G dd de
jZdS )	z
A Factory for SSH servers.

See also L{twisted.conch.openssh_compat.factory} for OpenSSH compatibility.

Maintainer: Paul Swartz
    N)error)_kex
connection	transportuserauth)protocol)Loggerc                   @   sb   e Zd ZdZe ZejZe	j
ejdZdd Zdd Zdd Zd	d
 Zdd Zdd Zdd ZdS )
SSHFactoryz$
    A Factory for SSH servers.
    )   ssh-userauths   ssh-connectionc                 C   sZ   t | ds
|  | _t | ds|  | _| jr| jstdt | ds+|  | _dS dS )z4
        Check for public and private keys.
        
publicKeysprivateKeyszno host keys, failingprimesN)	hasattrgetPublicKeysr   getPrivateKeysr   r   
ConchError	getPrimesr   self r   ;/usr/lib/python3/dist-packages/twisted/conch/ssh/factory.pystartFactory"   s   





zSSHFactory.startFactoryc                 C   sB   t j| |}| j |_| js| jd dd |j	D |_	|S )a=  
        Create an instance of the server side of the SSH protocol.

        @type addr: L{twisted.internet.interfaces.IAddress} provider
        @param addr: The address at which the server will listen.

        @rtype: L{twisted.conch.ssh.transport.SSHServerTransport}
        @return: The built transport.
        zTdisabling non-fixed-group key exchange algorithms because we cannot find moduli filec                 S   s$   g | ]}t |st |r|qS r   )r   isFixedGroupisEllipticCurve).0kexAlgorithmr   r   r   
<listcomp>@   s    z,SSHFactory.buildProtocol.<locals>.<listcomp>)
r   FactorybuildProtocolr   keyssupportedPublicKeysr   _loginfosupportedKeyExchanges)r   addrtr   r   r   r   /   s   
zSSHFactory.buildProtocolc                 C      t d)z
        Called when the factory is started to get the public portions of the
        servers host keys.  Returns a dictionary mapping SSH key types to
        public key strings.

        @rtype: L{dict}
        zgetPublicKeys unimplementedNotImplementedErrorr   r   r   r   r   G      zSSHFactory.getPublicKeysc                 C   r&   )z
        Called when the factory is started to get the  private portions of the
        servers host keys.  Returns a dictionary mapping SSH key types to
        L{twisted.conch.ssh.keys.Key} objects.

        @rtype: L{dict}
        zgetPrivateKeys unimplementedr'   r   r   r   r   r   Q   r)   zSSHFactory.getPrivateKeysc                 C   s   dS )z
        Called when the factory is started to get Diffie-Hellman generators and
        primes to use.  Returns a dictionary mapping number of bits to lists
        of tuple of (generator, prime).

        @rtype: L{dict}
        Nr   r   r   r   r   r   [   s    zSSHFactory.getPrimesc                    s2   t | j  fddd}|d }t| j| S )z
        Return a tuple of (g, p) for a Diffe-Hellman process, with p being as
        close to bits bits as possible.

        @type bits: L{int}
        @rtype:     L{tuple}
        c                    s   t |   S )N)abs)ibitsr   r   <lambda>l   s    z'SSHFactory.getDHPrime.<locals>.<lambda>)keyr   )sortedr   r   randomchoice)r   r-   
primesKeysrealBitsr   r,   r   
getDHPrimed   s   zSSHFactory.getDHPrimec                 C   s    |dks	t |dr| j| S dS )z
        Return a class to use as a service for the given transport.

        @type transport:    L{transport.SSHServerTransport}
        @type service:      L{bytes}
        @rtype:             subclass of L{service.SSHService}
        r
   avatarN)r   services)r   r   servicer   r   r   
getServicep   s   
zSSHFactory.getServiceN)__name__
__module____qualname____doc__r   r!   r   SSHServerTransportr   r   SSHUserAuthServerr   SSHConnectionr7   r   r   r   r   r   r5   r9   r   r   r   r   r	      s    

	r	   )r=   r1   twisted.conchr   twisted.conch.sshr   r   r   r   twisted.internetr   twisted.loggerr   r   r	   r   r   r   r   <module>   s   	