o
    cS$                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	l	m
Z
 dd
lmZ ddlmZ ddlmZ G dd de
jZG dd dZdS )z8Module contains classes used by the Apache Configurator.    N)Any)Iterable)Optional)Pattern)Set)Union)common)ApacheBlockNode)AugeasBlockNode)DualBlockNodec                       s   e Zd ZdZdedefddZdefddZde	f fdd	Z
d
d defddZde	fddZd
d defddZdefddZdedejfddZ  ZS )AddrzRepresents an Apache address.otherreturnc                 C   s@   t || jr| j|jkp| jd |jd ko|  o| S dS )zTThis is defined as equivalent within Apache.

        ip_addr:* == ip_addr

        r   F)
isinstance	__class__tupis_wildcardselfr    r   I/opt/certbot/lib/python3.10/site-packages/certbot_apache/_internal/obj.py__eq__   s   zAddr.__eq__c                 C   s   dt | j dS )Nz"certbot_apache._internal.obj.Addr())reprr   r   r   r   r   __repr__   s   zAddr.__repr__c                    s
   t   S N)super__hash__r   r   r   r   r   "   s   
zAddr.__hash__addrc                 C   s   |  |   kS )zAReturns if addr.get_addr() is more specific than self.get_addr().)_rank_specific_addrr   r    r   r   r   _addr_less_specific'   s   zAddr._addr_less_specificc                 C   s$   |   dkrdS |   dkrdS dS )zzReturns numerical rank for get_addr()

        :returns: 2 - FQ, 1 - wildcard, 0 - _default_
        :rtype: int

        	_default_r   *      )get_addrr   r   r   r   r!   ,   s
   zAddr._rank_specific_addrc                 C   s>   |  |rdS |  | kr|  s|  | krdS dS )a  Returns if address could conflict with correct function of self.

        Could addr take away service provided by self within Apache?

        .. note::IP Address is more important than wildcard.
            Connection from 127.0.0.1:80 with choices of *:80 and 127.0.0.1:*
            chooses 127.0.0.1:\*

        .. todo:: Handle domain name addrs...

        Examples:

        =========================================  =====
        ``127.0.0.1:\*.conflicts(127.0.0.1:443)``  True
        ``127.0.0.1:443.conflicts(127.0.0.1:\*)``  False
        ``\*:443.conflicts(\*:80)``                False
        ``_default_:443.conflicts(\*:443)``        True
        =========================================  =====

        TF)r#   r(   r   get_portr"   r   r   r   	conflicts9   s   
zAddr.conflictsc                 C   s   | j d dkp| j d  S )z'Returns if address has a wildcard port.r&   r%   )r   r   r   r   r   r   U   s   zAddr.is_wildcardportc                 C   s   |   r| S | |S )zReturns the least specific address that resolves on the port.

        Examples:

        - ``1.2.3.4:443`` -> ``1.2.3.4:<port>``
        - ``1.2.3.4:*`` -> ``1.2.3.4:*``

        :param str port: Desired port

        )r   get_addr_obj)r   r+   r   r   r   get_sni_addrY   s   
zAddr.get_sni_addr)__name__
__module____qualname____doc__r   boolr   strr   intr   r#   r!   r*   r   r   r   r-   __classcell__r   r   r   r   r      s    r   c                   @   s   e Zd ZU dZedZeed< 			d%de	de	de
d	 d
ededee	 dee
e	  deded  deeeeef  ddfddZde
e	 fddZde	fddZde	fddZdedefddZdefddZdee defdd Zd&d!d d"edefd#d$ZdS )'VirtualHosta  Represents an Apache Virtualhost.

    :ivar str filep: file path of VH
    :ivar str path: Augeas path to virtual host
    :ivar set addrs: Virtual Host addresses (:class:`set` of
        :class:`common.Addr`)
    :ivar str name: ServerName of VHost
    :ivar list aliases: Server aliases of vhost
        (:class:`list` of :class:`str`)

    :ivar bool ssl: SSLEngine on in vhost
    :ivar bool enabled: Virtual host is enabled
    :ivar bool modmacro: VirtualHost is using mod_macro
    :ivar VirtualHost ancestor: A non-SSL VirtualHost this is based on

    https://httpd.apache.org/docs/2.4/vhosts/details.html

    .. todo:: Any vhost that includes the magic _default_ wildcard is given the
              same ServerName as the main server.

    z^(?:.+://)?([^ :$]*)
strip_nameNFfilepathpathaddrsr   sslenablednamealiasesmodmacroancestornoder   c                 C   sN   || _ || _|| _|| _|dur|nt | _|| _|| _|| _|	| _	|
| _
dS )zInitialize a VH.N)filepr9   r:   r=   setr>   r;   r<   r?   r@   rA   )r   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   r   r   r   __init__   s   
zVirtualHost.__init__c                 C   s8   t  }|| j | jdur|tj| jd  |S )zReturn a set of all names.Nr   )rC   updater>   r=   addr6   r7   findall)r   	all_namesr   r   r   	get_names   s
   
zVirtualHost.get_namesc                 C   s   d| j  d| j dddd | jD  d| jd ur| jnd d	dd
d | jD  d| jr1dnd d| jr9dnd d| jrCd S d S )NFile: z
Vhost path: 
Addresses: , c                 s       | ]}t |V  qd S r   r3   .0r    r   r   r   	<genexpr>       z&VirtualHost.__str__.<locals>.<genexpr>z
Name:  z

Aliases: c                 s   s    | ]}|V  qd S r   r   )rP   r=   r   r   r   rQ      s    z
TLS Enabled: YesNoz
Site Enabled: z
mod_macro Vhost: )	rB   r9   joinr:   r=   r>   r;   r<   r?   r   r   r   r   __str__   s&   
zVirtualHost.__str__c              	   C   sL   d| j  dddd | jD  dd|   d| jr!d d
	S d	 d
	S )z5Return a representation of VHost to be used in dialogrJ   rK   rL   c                 s   rM   r   rN   rO   r   r   r   rQ      rR   z+VirtualHost.display_repr.<locals>.<genexpr>z
Names: z
HTTPS: rT   rU   
)rB   rV   r:   rI   r;   r   r   r   r   display_repr   s   
zVirtualHost.display_reprr   c                 C   sh   t || jr2| j|jko1| j|jko1| j|jko1|  | ko1| j|jko1| j|jko1| j|jkS dS )NF)	r   r   rB   r9   r:   rI   r;   r<   r?   r   r   r   r   r      s   



zVirtualHost.__eq__c                 C   s.   t | j| jt| jt|  | j| j| jfS r   )	hashrB   r9   tupler:   rI   r;   r<   r?   r   r   r   r   r      s   
zVirtualHost.__hash__c                 C   s,   |D ]}| j D ]}||r  dS qqdS )aE  See if vhost conflicts with any of the addrs.

        This determines whether or not these addresses would/could overwrite
        the vhost addresses.

        :param addrs: Iterable Addresses
        :type addrs: Iterable :class:~obj.Addr

        :returns: If addresses conflicts with vhost
        :rtype: bool

        TF)r:   r*   )r   r:   pot_addrr    r   r   r   r*      s   

zVirtualHost.conflictsvhostgenericc                 C   s   |s|  |   krdS | jdus| jrdS n| jrdS t|jt| jkr(dS t }|jD ]&}| jD ]}| | krP||krP| |vrP||   nq3 dS q.dS )aX  Determines if the vhost is the same 'server'.

        Used in redirection - indicates whether or not the two virtual hosts
        serve on the exact same IP combinations, but different ports.
        The generic flag indicates that that we're trying to match to a
        default or generic vhost

        .. todo:: Handle _default_

        FNT)rI   r=   r>   lenr:   rC   r(   rF   )r   r]   r^   already_foundr    
local_addrr   r   r   same_server   s0   


zVirtualHost.same_server)NNFNN)F)r.   r/   r0   r1   recompiler7   r   __annotations__r3   r   r2   r   r   r	   r
   r   rD   rI   rW   rY   r   r   r4   r   r   r   r*   rb   r   r   r   r   r6   j   s8   
 


	r6   )r1   rc   typingr   r   r   r   r   r   certbot.pluginsr   %certbot_apache._internal.apacheparserr	   %certbot_apache._internal.augeasparserr
   #certbot_apache._internal.dualparserr   r   r6   r   r   r   r   <module>   s    Z