U
    ÔZjõ$  ã                   @   sT   d Z ddlZddlZddlmZ dZG dd„ dejƒZG dd„ deƒZdd	d
„Z	dS )z7Module contains classes used by the Nginx Configurator.é    N)ÚcommonÚ
add_headerc                       sr   e Zd ZdZdZed Z‡ fdd„Zedd„ ƒZdd	d
„Z	dd„ Z
dd„ Z‡ fdd„Z‡ fdd„Zdd„ Z‡  ZS )ÚAddra  Represents an Nginx address, i.e. what comes after the 'listen'
    directive.

    According to the `documentation`_, this may be address[:port], port,
    or unix:path. The latter is ignored here.

    The default value if no directive is specified is \*:80 (superuser)
    or \*:8000 (otherwise). If no port is specified, the default is
    80. If no address is specified, listen on all addresses.

    .. _documentation:
       http://nginx.org/en/docs/http/ngx_http_core_module.html#listen

    .. todo:: Old-style nginx configs define SSL vhosts in a separate
              block instead of using 'ssl' in the listen directive.

    :param str addr: addr part of vhost address, may be hostname, IPv4, IPv6,
        "", or "\*"
    :param str port: port number or "\*" or ""
    :param bool ssl: Whether the directive includes 'ssl'
    :param bool default: Whether the directive includes 'default_server'
    :param bool default: Whether this is an IPv6 address
    :param bool ipv6only: Whether the directive includes 'ipv6only=on'

    )Ú Ú*z0.0.0.0r   c                    s<   t t| ƒ ||f¡ || _|| _|| _|| _|| jk| _d S ©N)	Úsuperr   Ú__init__ÚsslÚdefaultÚipv6Úipv6onlyÚUNSPECIFIED_IPV4_ADDRESSESÚunspecified_address)ÚselfÚhostÚportr
   r   r   r   ©Ú	__class__© úT/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot_nginx/obj.pyr	   '   s    zAddr.__init__c                 C   sú   |  d¡}d}d}d}d}d}d}| d¡}	|	 d¡r:dS t d|	¡}
|
rld}|
 ¡ }|	|
 ¡ d	 d… }n8|	 d
¡}t d|d ¡r”d}|d }n|d }|d }|rè| ¡ }|dkr¾d}q¤|dkrÌd}q¤|dkrÚd}q¤|dkr¤d}q¤| ||||||ƒS )zInitialize Addr from string.ú Fr   r   zunix:Nz\[.*\]Té   ú:z^\d+$é   r
   Zdefault_serverr   zipv6only=on)ÚsplitÚpopÚ
startswithÚreÚmatchÚgroupÚendÚ	partition)ÚclsZstr_addrÚpartsr
   r   r   r   r   r   ÚaddrZ
ipv6_matchÚtupZnextpartr   r   r   Ú
fromstring0   s@    




zAddr.fromstringTc                 C   sh   d}| j d r$| j d r$d| j  }n | j d r:| j d }n
| j d }| jrV|rV|d7 }| jrd|d7 }|S )z$Return string representation of Addrr   r   r   z%s:%sz default_serverz ssl)r&   r   r
   )r   Úinclude_defaultr$   r   r   r   Ú	to_stringd   s    


zAddr.to_stringc                 C   s   |   ¡ S r   )r)   ©r   r   r   r   Ú__str__u   s    zAddr.__str__c                 C   s   d|   ¡  d S )NzAddr(ú))r+   r*   r   r   r   Ú__repr__x   s    zAddr.__repr__c                    s   t t| ƒ ¡ S r   )r   r   Ú__hash__r*   r   r   r   r.   {   s    zAddr.__hash__c                    sP   | j r@|j r@t | j| jd f| j¡t |j|jd f|j¡kS tt| ƒ |¡S )z3Check ip/port equality, with IPv6 support.
        r   )r   r   r   ÚCANONICAL_UNSPECIFIED_ADDRESSr&   r   r   Ú__eq__©r   Úotherr   r   r   Úsuper_eq€   s    ÿÿÿÿþzAddr.super_eqc                 C   s2   t || jƒr.|  |¡o,| j|jko,| j|jkS dS )NF)Ú
isinstancer   r3   r
   r   r1   r   r   r   r0   Œ   s    

ÿ
þzAddr.__eq__)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r/   r	   Úclassmethodr'   r)   r+   r-   r.   r3   r0   Ú__classcell__r   r   r   r   r   
   s   	
3
r   c                   @   s`   e Zd Z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„ Zdd„ Zdd„ ZdS )ÚVirtualHostaû  Represents an Nginx Virtualhost.

    :ivar str filep: file path of VH
    :ivar set addrs: Virtual Host addresses (:class:`set` of :class:`Addr`)
    :ivar set names: Server names/aliases of vhost
        (:class:`list` of :class:`str`)
    :ivar list raw: The raw form of the parsed server block

    :ivar bool ssl: SSLEngine on in vhost
    :ivar bool enabled: Virtual host is enabled
    :ivar list path: The indices into the parsed file used to access
        the server block defining the vhost

    c                 C   s.   || _ || _|| _|| _|| _|| _|| _dS )zInitialize a VH.N)ÚfilepÚaddrsÚnamesr
   ÚenabledÚrawÚpath)r   r<   r=   r
   r?   r>   r@   rA   r   r   r   r	   ¤   s    zVirtualHost.__init__c                 C   s<   d  dd„ t| jtd�D ƒ¡}d| j|t| jƒ| j| jf S )Nú, c                 s   s   | ]}t |ƒV  qd S r   ©Ústr©Ú.0r%   r   r   r   Ú	<genexpr>°   s     z&VirtualHost.__str__.<locals>.<genexpr>©Úkeyz0file: %s
addrs: %s
names: %s
ssl: %s
enabled: %s)	ÚjoinÚsortedr=   rD   r<   Úlistr>   r
   r?   )r   Úaddr_strr   r   r   r+   ¯   s      ÿüzVirtualHost.__str__c                 C   s   d|   ¡  dd¡ d S )NzVirtualHost(Ú
rB   z)
)r+   Úreplacer*   r   r   r   r-   º   s    zVirtualHost.__repr__c                 C   sh   t || jƒrd| j|jkobt| jtd�t|jtd�kob| j|jkob| j|jkob| j|jkob| j	|j	kS dS )NrH   F)
r4   r   r<   rK   r=   rD   r>   r
   r?   rA   r1   r   r   r   r0   ½   s    ÿ
þ
ý
ü
ûzVirtualHost.__eq__c                 C   s,   t | jt| jƒt| jƒt| jƒ| j| jfƒS r   )Úhashr<   ÚtuplerA   r=   r>   r
   r?   r*   r   r   r   r.   È   s      þzVirtualHost.__hash__c                 C   s   t | jt|ƒ}|dk	S )z¶Determine if this server block has a particular header set.
        :param str header_name: The name of the header to check for, e.g.
            'Strict-Transport-Security'
        N)Ú_find_directiver@   ÚADD_HEADER_DIRECTIVE)r   Úheader_nameÚfoundr   r   r   Ú
has_headerÍ   s    zVirtualHost.has_headerc                 C   sJ   t j dt| jƒt|ƒ d ¡D ]$}| j||t|ƒ … |kr  dS q dS )zFDetermine if raw server block contains test list at top level
        r   r   TF)ÚsixÚmovesÚrangeÚlenr@   )r   ÚtestÚir   r   r   Úcontains_listÕ   s    $zVirtualHost.contains_listc                 C   s   | j D ]}|jr dS qdS )zRReturn true if one or more of the listen directives in vhost supports
        IPv6TF©r=   r   ©r   Úar   r   r   Úipv6_enabledÝ   s    
zVirtualHost.ipv6_enabledc                 C   s&   | j s
dS | j D ]}|js dS qdS )zRReturn true if one or more of the listen directives in vhost are IPv4
        onlyTFr^   r_   r   r   r   Úipv4_enabledå   s    
zVirtualHost.ipv4_enabledc                 C   s8   dj | jd dd„ | jD ƒ¡d | j¡| jr0dndd�S )z5Return a representation of VHost to be used in dialogzBFile: {filename}
Addresses: {addrs}
Names: {names}
HTTPS: {https}
rB   c                 s   s   | ]}t |ƒV  qd S r   rC   rE   r   r   r   rG   ÷   s     z+VirtualHost.display_repr.<locals>.<genexpr>ZYesZNo)Úfilenamer=   r>   Úhttps)Úformatr<   rJ   r=   r>   r
   r*   r   r   r   Údisplay_reprï   s    
ùÿzVirtualHost.display_reprN)r5   r6   r7   r8   r	   r+   r-   r0   r.   rV   r]   ra   rb   rf   r   r   r   r   r;   ”   s   
r;   c                    s\   | rt | tjƒrdS | d ˆ kr4ˆdks0ˆ| kr4| S ‡ ‡fdd„| D ƒ}tdd„ |D ƒdƒS )z–Find a directive of type directive_name in directives. If match_content is given,
       Searches for `match_content` in the directive arguments.
    Nr   c                 3   s   | ]}t |ˆ ˆƒV  qd S r   )rR   )rF   Úline©Údirective_nameÚmatch_contentr   r   rG     s     z"_find_directive.<locals>.<genexpr>c                 s   s   | ]}|d k	r|V  qd S r   r   )rF   Úmr   r   r   rG   	  s      )r4   rW   Ústring_typesÚnext)Z
directivesri   rj   Úmatchesr   rh   r   rR   û   s    ÿÿrR   )N)
r8   r   rW   Zcertbot.pluginsr   rS   r   Úobjectr;   rR   r   r   r   r   Ú<module>   s    g