U
    ÔZjÈ#  ã                   @   sJ   d Z ddlZddlmZ ddlmZ G dd„ dejƒZG dd„ deƒZdS )	z8Module contains classes used by the Apache Configurator.é    N)ÚSet)Úcommonc                       s`   e Zd ZdZdd„ Zdd„ Zdd„ Z‡ fdd	„Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Z‡  ZS )ÚAddrzRepresents an Apache address.c                 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©ÚselfÚother© r   úU/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot_apache/obj.pyÚ__eq__   s    ÿþzAddr.__eq__c                 C   s   |   |¡ S ©N©r   r	   r   r   r   Ú__ne__   s    zAddr.__ne__c                 C   s   dt | jƒ d S )Nzcertbot_apache.obj.Addr(ú))Úreprr   ©r
   r   r   r   Ú__repr__   s    zAddr.__repr__c                    s   t t| ƒ ¡ S r   )Úsuperr   Ú__hash__r   ©r   r   r   r      s    zAddr.__hash__c                 C   s   |  ¡ |   ¡ kS )zAReturns if addr.get_addr() is more specific than self.get_addr().)Ú_rank_specific_addr©r
   Úaddrr   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

        Z	_default_r   Ú*é   é   )Úget_addrr   r   r   r   r   '   s
    zAddr._rank_specific_addrc                 C   s>   |   |¡rdS |  ¡ | ¡ kr:|  ¡ s6|  ¡ | ¡ 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   Zget_portr   r   r   r   Ú	conflicts4   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   P   s    zAddr.is_wildcardc                 C   s   |   ¡ r| S |  |¡S )zÛReturns 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   Zget_addr_obj)r
   Úportr   r   r   Úget_sni_addrT   s    zAddr.get_sni_addr)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r!   r   r#   Ú__classcell__r   r   r   r   r      s   r   c                   @   sf   e Zd ZdZe d¡Zddd„Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zd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^(?:.+://)?([^ :$]*)NFc
           
      C   sH   || _ || _|| _|| _|dk	r$|ntƒ | _|| _|| _|| _|	| _	dS )zInitialize a VH.N)
ÚfilepÚpathÚaddrsÚnameÚsetÚaliasesÚsslÚenabledÚmodmacroÚancestor)
r
   r*   r+   r,   r0   r1   r-   r/   r2   r3   r   r   r   Ú__init__~   s    zVirtualHost.__init__c                 C   s8   t ƒ }| | j¡ | jdk	r4| tj | j¡d ¡ |S )zReturn a set of all names.Nr   )r.   Úupdater/   r-   Úaddr)   Ú
strip_nameÚfindall)r
   Z	all_namesr   r   r   Ú	get_names�   s
    
zVirtualHost.get_namesc              
   C   sp   dj | j| jd dd„ | jD ƒ¡| jd k	r0| jndd dd„ | jD ƒ¡| jrPdnd| jr\dnd| j	rhdndd	�S )
NzžFile: {filename}
Vhost path: {vhpath}
Addresses: {addrs}
Name: {name}
Aliases: {aliases}
TLS Enabled: {tls}
Site Enabled: {active}
mod_macro Vhost: {modmacro}ú, c                 s   s   | ]}t |ƒV  qd S r   ©Ústr©Ú.0r   r   r   r   Ú	<genexpr>£   s     z&VirtualHost.__str__.<locals>.<genexpr>Ú c                 s   s   | ]
}|V  qd S r   r   )r>   r-   r   r   r   r?   ¥   s     ÚYesÚNo)ÚfilenameZvhpathr,   r-   r/   ÚtlsZactiver2   )
Úformatr*   r+   Újoinr,   r-   r/   r0   r1   r2   r   r   r   r   Ú__str__—   s    ñÿzVirtualHost.__str__c                 C   s:   dj | jd dd„ | jD ƒ¡d |  ¡ ¡| jr2dndd�S )z5Return a representation of VHost to be used in dialogzBFile: {filename}
Addresses: {addrs}
Names: {names}
HTTPS: {https}
r:   c                 s   s   | ]}t |ƒV  qd S r   r;   r=   r   r   r   r?   ²   s     z+VirtualHost.display_repr.<locals>.<genexpr>rA   rB   )rC   r,   ÚnamesÚhttps)rE   r*   rF   r,   r9   r0   r   r   r   r   Údisplay_reprª   s    ùÿzVirtualHost.display_reprc                 C   sh   t || jƒrd| j|jkob| j|jkob| j|jkob|  ¡ | ¡ kob| j|jkob| j|jkob| j|jkS dS )NF)	r   r   r*   r+   r,   r9   r0   r1   r2   r	   r   r   r   r   ·   s    
ÿþ
ý
ü
ûzVirtualHost.__eq__c                 C   s   |   |¡ S r   r   r	   r   r   r   r   Â   s    zVirtualHost.__ne__c                 C   s.   t | j| jt| jƒt|  ¡ ƒ| j| j| jfƒS r   )	Úhashr*   r+   Útupler,   r9   r0   r1   r2   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,   Zpot_addrr   r   r   r   r!   Ê   s
    

zVirtualHost.conflictsc                 C   s®   |s.|  ¡ |   ¡ krdS | jdk	s(| jr8dS n
| jr8dS t|jƒt| jƒkrPdS tƒ }|jD ]L}| jD ]:}| ¡ | ¡ krf||krf| ¡ |krf| | ¡ ¡  q\qf 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)r9   r-   r/   Úlenr,   r.   r    r6   )r
   ZvhostZgenericZalready_foundr   Z
local_addrr   r   r   Úsame_serverÝ   s*    

ÿ
þzVirtualHost.same_server)NNFN)F)r$   r%   r&   r'   ÚreÚcompiler7   r4   r9   rG   rJ   r   r   r   r!   rN   r   r   r   r   r)   e   s   
      ÿ

r)   )	r'   rO   Zacme.magic_typingr   Zcertbot.pluginsr   r   Úobjectr)   r   r   r   r   Ú<module>   s
   ]