U
    ]#                     @   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   4/usr/lib/python3/dist-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 )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   Zget_addr_obj)r
   Z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+   r/   r0   r,   r.   r1   r2   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 )
NzFile: {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.   ZtlsZactiver1   )
formatr)   r*   joinr+   r,   r.   r/   r0   r1   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}
r9   c                 s   s   | ]}t |V  qd S r   r:   r<   r   r   r   r>      s     z+VirtualHost.display_repr.<locals>.<genexpr>r@   rA   )rB   r+   namesZhttps)rC   r)   rD   r+   r8   r/   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+   r8   r/   r0   r1   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+   r8   r/   r0   r1   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)r8   r,   r.   lenr+   r-   r    r5   )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compiler6   r3   r8   rE   rG   r   r   r   r!   rK   r   r   r   r   r(   e   s   
      

r(   )	r&   rL   Zacme.magic_typingr   Zcertbot.pluginsr   r   objectr(   r   r   r   r   <module>   s
   ]