o
    "4g                     @  s$  d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZmZmZ ddlmZ zddlmZmZ ddlmZ dd	lmZ W n eyO   ed
w ddlmZ ddlmZ erdddlmZ e Ze e_e Z e e _dddZ!dddZ"dddZ#G dd deZ$G dd dZ%dS )z
DNS query support
    )annotationsN)randint)AnyDefaultDict
NamedTupleSequenceTYPE_CHECKING)defaultdict)ResolverCache)r
   )DNSExceptionzBthe module psycopg._dns requires the package 'dnspython' installed   )errors)conninfo)SRVparamsdict[str, Any]returnc           	        s   t dt g }g }g }t| I dH D ].}|ddur%||d  |ddur3||d  |ddurC|t|d  q|  }d	|}|rS||d< d	|}|r^||d< d	|}|ri||d< |S )a  
    Perform async DNS lookup of the hosts and return a new params dict.

    .. deprecated:: 3.1
        The use of this function is not necessary anymore, because
        `psycopg.AsyncConnection.connect()` performs non-blocking name
        resolution automatically.
    z@from psycopg 3.1, resolve_hostaddr_async() is not needed anymoreNhosthostaddrport,)
warningswarnDeprecationWarningr   conninfo_attempts_asyncgetappendstrcopyjoin)	r   hosts	hostaddrsportsattemptoutshosts
shostaddrssports r)   U/var/www/html/authentication-server/venv/lib/python3.10/site-packages/psycopg/_dns.pyresolve_hostaddr_async'   s6   	


r+   c                 C  s   t  | S )z/Apply SRV DNS lookup as defined in :RFC:`2782`.)Rfc2782Resolverresolver   r)   r)   r*   resolve_srvN   s   r/   c                   s   t  | I dH S )z$Async equivalent of `resolve_srv()`.N)r,   resolve_asyncr.   r)   r)   r*   resolve_srv_asyncS   s   r1   c                   @  s6   e Zd ZU ded< ded< dZded< dZded	< dS )
HostPortr   r   r   FbooltotryNz
str | Nonetarget)__name__
__module____qualname____annotations__r4   r5   r)   r)   r)   r*   r2   X   s
   
 r2   c                   @  sj   e Zd ZdZedZdddZddd	Zd ddZ	d!ddZ
d!ddZd"ddZd#ddZd$ddZdS )%r,   zImplement SRV RR Resolution as per RFC 2782

    The class is organised to minimise code duplication between the sync and
    the async paths.
    z9^(?P<service>_[^\.]+)\.(?P<proto>_[^\.]+)\.(?P<target>.+)r   r   r   c                 C  sN   |  |}|s	|S g }|D ]}|jr|| | q|| q| ||S )5Update the parameters host and port after SRV lookup.)_get_attemptsr4   extend_resolve_srvr   _return_paramsselfr   attemptshpshpr)   r)   r*   r-   h   s   
zRfc2782Resolver.resolvec                   sV   |  |}|s
|S g }|D ]}|jr|| |I dH  q|| q| ||S )r:   N)r;   r4   r<   _resolve_srv_asyncr   r>   r?   r)   r)   r*   r0   w   s   
zRfc2782Resolver.resolve_asynclist[HostPort]c                 C  s(  | dtj ddrg S | dtj dd}|d}t| dtj dd}|d}t|d	kr;|t|9 }t|t|krStd
t| dt| dg }d}t||D ]1\}}	| j	
|}
|
sn|	 dkrd}|
rw|
dnd}t||	d|d}nt||	d}|| q\|r|S g S )z
        Return the list of host, and for each host if SRV lookup must be tried.

        Return an empty list if no lookup is requested.
        r   
PGHOSTADDR r   PGHOSTr   r   PGPORTr   zcannot match z hosts with z port numbersFsrvTr5   N)r   r   r4   r5   r   r   )r   osenvironsplitr   leneOperationalErrorzip	re_srv_rrmatchlowergroupr2   r   )r@   r   host_arghosts_inport_argports_inr%   	srv_foundr   r   mr5   rC   r)   r)   r*   r;      s.   

zRfc2782Resolver._get_attemptsrC   r2   c                 C  s6   z	t |jd}W n ty   d}Y nw | ||S Nr   r)   )resolverr-   r   r   _get_solved_entriesr@   rC   ansr)   r)   r*   r=      s   zRfc2782Resolver._resolve_srvc                   s>   zt |jdI d H }W n ty   d}Y nw | ||S r]   )async_resolverr-   r   r   r_   r`   r)   r)   r*   rD      s   z"Rfc2782Resolver._resolve_srv_asyncentriesSequence[SRV]c                 C  sd   |s|j r|j dkrt|j |jdgS g S t|dkr(t|d j dkr(g S dd | |D S )NrJ   rK   r   r   .c                 S  s*   g | ]}t t|jd t|jdqS )re   rK   )r2   r   r5   rstripr   ).0entryr)   r)   r*   
<listcomp>   s    z7Rfc2782Resolver._get_solved_entries.<locals>.<listcomp>)r5   r   rU   r2   rO   r   sort_rfc2782)r@   rC   rc   r)   r)   r*   r_      s   z#Rfc2782Resolver._get_solved_entriesrB   c                 C  sJ   |st d| }ddd |D |d< ddd |D |d< |S )Nz!no host found after SRV RR lookupr   c                 s      | ]}|j V  qd S N)r   rg   rC   r)   r)   r*   	<genexpr>       z1Rfc2782Resolver._return_params.<locals>.<genexpr>r   c                 s  s    | ]}t |jV  qd S rl   )r   r   rm   r)   r)   r*   rn      s    r   )rP   rQ   r   r    )r@   r   rB   r%   r)   r)   r*   r>      s   
zRfc2782Resolver._return_paramsra   	list[SRV]c                 C  s   t t}g }|D ]
}||j | qt| D ]O\}}t|dkr+||d  q|jdd d tdd |D }|rht	d|}d}	t
|D ]\}
}|	|j7 }	|	|krX nqI|| ||j8 }||
= |s>q|S )zM
        Implement the priority/weight ordering defined in RFC 2782.
        r   r   c                 S  s   | j S rl   weight)entr)   r)   r*   <lambda>   s    z.Rfc2782Resolver.sort_rfc2782.<locals>.<lambda>)keyc                 s  rk   rl   rq   )rg   rs   r)   r)   r*   rn      ro   z/Rfc2782Resolver.sort_rfc2782.<locals>.<genexpr>)r	   listpriorityr   sorteditemsrO   sortsumr   	enumeraterr   )r@   ra   
prioritiesr%   rh   prirc   total_weightrcsumirs   r)   r)   r*   rj      s0   



zRfc2782Resolver.sort_rfc2782Nr   r   r   r   )r   r   r   rE   )rC   r2   r   rE   )rC   r2   rc   rd   r   rE   )r   r   rB   rE   r   r   )ra   rd   r   rp   )r6   r7   r8   __doc__recompilerS   r-   r0   r;   r=   rD   r_   r>   rj   r)   r)   r)   r*   r,   _   s    




'


r,   r   )&r   
__future__r   rL   r   r   randomr   typingr   r   r   r   r   collectionsr	   dns.resolverr
   r   dns.asyncresolverAsyncResolverdns.exceptionr   ImportErrorrG   r   rP   r   dns.rdtypes.IN.SRVr   r^   cacherb   r+   r/   r1   r2   r,   r)   r)   r)   r*   <module>   s<   

'
