a
    )(b                     @   s   d dgZ ddlmZ ddlmZm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 G d	d dejZG d
d  d eZdS )SecondaryAuthoritySecondaryAuthorityService    )service)defertask)clientcommondnsresolve)FileAuthority)failurelog)nativeStringc                   @   sD   e Zd ZdZdZdZdd Zedd Zdd	 Z	d
d Z
dd ZdS )r   a:  
    A service that keeps one or more authorities up to date by doing hourly
    zone transfers from a master.

    @ivar primary: IP address of the master.
    @type primary: L{str}

    @ivar domains: An authority for each domain mirrored from the master.
    @type domains: L{list} of L{SecondaryAuthority}
    N5   c                    s"   t  | _ fdd|D | _dS )a  
        @param primary: The IP address of the server from which to perform
        zone transfers.
        @type primary: L{str}

        @param domains: A sequence of domain names for which to perform
        zone transfers.
        @type domains: L{list} of L{bytes}
        c                    s   g | ]}t  |qS  )r   .0dprimaryr   6lib/python3.9/site-packages/twisted/names/secondary.py
<listcomp>+       z6SecondaryAuthorityService.__init__.<locals>.<listcomp>N)r   r   domains)selfr   r   r   r   r   __init__    s    

z"SecondaryAuthorityService.__init__c                    s0    \}}| |g }||_  fdd|D |_|S )a  
        Construct a new L{SecondaryAuthorityService} from a tuple giving a
        server address and a C{str} giving the name of a domain for which this
        is an authority.

        @param serverAddress: A two-tuple, the first element of which is a
            C{str} giving an IP address and the second element of which is a
            C{int} giving a port number.  Together, these define where zone
            transfers will be attempted from.

        @param domains: Domain names for which to perform zone transfers.
        @type domains: sequence of L{bytes}

        @return: A new instance of L{SecondaryAuthorityService}.
        c                    s   g | ]}t  |qS r   )r   fromServerAddressAndDomainr   serverAddressr   r   r   A   s   zISecondaryAuthorityService.fromServerAddressAndDomains.<locals>.<listcomp>)_portr   )clsr   r   r   portr   r   r   r   fromServerAddressAndDomains-   s    

z5SecondaryAuthorityService.fromServerAddressAndDomainsc                 C   s   t | jS )z_
        Get a resolver for the transferred domains.

        @rtype: L{ResolverChain}
        )r
   ZResolverChainr   r   r   r   r   getAuthorityG   s    z&SecondaryAuthorityService.getAuthorityc                 C   sV   t j|  dd | jD | _d}ddlm} | jD ]}|||jd |d7 }q4d S )Nc                 S   s   g | ]}t |jqS r   )r   ZLoopingCalltransferr   r   r   r   r   Q   r   z:SecondaryAuthorityService.startService.<locals>.<listcomp>r   reactori     )	r   ServicestartServicer   callstwisted.internetr'   Z	callLaterstart)r   ir'   cr   r   r   r*   O   s    
z&SecondaryAuthorityService.startServicec                 C   s$   t j|  | jD ]}|  qd S N)r   r)   stopServicer+   stop)r   r/   r   r   r   r1   Z   s    
z%SecondaryAuthorityService.stopService)__name__
__module____qualname____doc__r+   r   r   classmethodr"   r$   r*   r1   r   r   r   r   r      s   
c                   @   sr   e Zd ZdZdZd ZZdZdZdd Z	e
dd Zd	d
 ZdddZdd Zdd Zdd Zdd Zdd ZdS )r   a  
    An Authority that keeps itself updated by performing zone transfers.

    @ivar primary: The IP address of the server from which zone transfers will
    be attempted.
    @type primary: L{str}

    @ivar _port: The port number of the server from which zone transfers will
    be attempted.
    @type _port: L{int}

    @ivar domain: The domain for which this is the secondary authority.
    @type domain: L{bytes}

    @ivar _reactor: The reactor to use to perform the zone transfers, or
    L{None} to use the global reactor.
    FNr   c                 C   s&   t j|  t|| _t|| _dS )z
        @param domain: The domain for which this will be the secondary
            authority.
        @type domain: L{bytes} or L{str}
        N)r   ZResolverBaser   r   r   r	   ZdomainStringdomain)r   Z	primaryIPr8   r   r   r   r   x   s    	
zSecondaryAuthority.__init__c                 C   s   |\}}| ||}||_ |S )af  
        Construct a new L{SecondaryAuthority} from a tuple giving a server
        address and a C{bytes} giving the name of a domain for which this is an
        authority.

        @param serverAddress: A two-tuple, the first element of which is a
            C{str} giving an IP address and the second element of which is a
            C{int} giving a port number.  Together, these define where zone
            transfers will be attempted from.

        @param domain: A C{bytes} giving the domain to transfer.
        @type domain: L{bytes}

        @return: A new instance of L{SecondaryAuthority}.
        )r   )r    r   r8   r   r!   Z	secondaryr   r   r   r      s    
z-SecondaryAuthority.fromServerAddressAndDomainc                 C   s^   | j r
dS d| _| j}|du r*ddlm} tj| j| jfg|d}|	| j
| j| jS )z
        Attempt a zone transfer.

        @returns: A L{Deferred} that fires with L{None} when attempted zone
            transfer has completed.
        NTr   r&   )Zserversr'   )transferringZtransfering_reactorr,   r'   r   ZResolverr   r   Z
lookupZoner8   ZaddCallback_cbZoneZ
addErrback_ebZone)r   r'   Zresolverr   r   r   r%      s    	zSecondaryAuthority.transferc                 C   s4   | j r| js"ttt|S t	| ||||S r0   )
soarecordsr   Zfailr   ZFailurer	   ZDomainErrorr   _lookup)r   namer    typeZtimeoutr   r   r   r?      s    zSecondaryAuthority._lookupc                 C   sf   |\}}}i  | _ }|D ]H}| jsD|jtjkrD|jj |jf| _q||jj g 	|j qd S r0   )
r>   r=   rA   r	   ZSOAr@   lowerZpayload
setdefaultappend)r   ZzoneZans_rZrecr   r   r   r;      s    

zSecondaryAuthority._cbZonec                 C   s$   t d| j| jf  t | d S )Nz/Updating %s from %s failed during zone transfer)r   msgr8   r   errr   r   r   r   r   r<      s    
zSecondaryAuthority._ebZonec                 C   s   |   | j| j d S r0   )r%   ZaddCallbacks_cbTransferred_ebTransferredr#   r   r   r   update   s    zSecondaryAuthority.updatec                 C   s
   d| _ d S )NF)r9   )r   resultr   r   r   rJ      s    z!SecondaryAuthority._cbTransferredc                 C   s*   d| _ td| j| jf  t| d S )NFz2Transferring %s from %s failed after zone transfer)Ztransferredr   rG   r8   r   rH   rI   r   r   r   rK      s    
z!SecondaryAuthority._ebTransferred)N)r3   r4   r5   r6   r9   r=   r>   r   r:   r   r7   r   r%   r?   r;   r<   rL   rJ   rK   r   r   r   r   r   `   s   

	N)__all__Ztwisted.applicationr   r,   r   r   Ztwisted.namesr   r   r	   r
   Ztwisted.names.authorityr   Ztwisted.pythonr   r   Ztwisted.python.compatr   r)   r   r   r   r   r   r   <module>   s   P