U
    ]$                     @   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)commonZ
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__ 3/usr/lib/python3/dist-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 rd}|d }n|d }|d }|r| }|dkrd}q|dkrd}q|dkrd}q|dkrd}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   Z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   r1   r	   r
   r/   r   r   r   r.      s    


zAddr.__eq__)T)__name__
__module____qualname____doc__r   r-   r   classmethodr&   r'   r)   r+   r,   r1   r.   __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>   r?   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;   rB   r:   listr<   r	   r=   )r   Zaddr_strr   r   r   r)      s      zVirtualHost.__str__c                 C   s   d|   dd d S )NzVirtualHost(
r@   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 )NrF   F)
r2   r   r:   rI   r;   rB   r<   r	   r=   r?   r/   r   r   r   r.      s    



zVirtualHost.__eq__c                 C   s,   t | jt| jt| jt| j| j| jfS r   )hashr:   tupler?   r;   r<   r	   r=   r(   r   r   r   r,      s      zVirtualHost.__hash__c                 C   s   t | jt|}|dk	S )zDetermine 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   Z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)sixZmovesrangelenr>   )r   Z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
        onlyTFrX   rY   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}
r@   c                 s   s   | ]}t |V  qd S r   rA   rC   r   r   r   rE      s     z+VirtualHost.display_repr.<locals>.<genexpr>ZYesZNo)filenamer;   r<   Zhttps)formatr:   rH   r;   r<   r	   r(   r   r   r   display_repr   s    
zVirtualHost.display_reprN)r3   r4   r5   r6   r   r)   r+   r.   r,   rR   rW   r[   r\   r_   r   r   r   r   r9      s   
r9   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 )zFind 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   )rO   )rD   linedirective_namematch_contentr   r   rE     s     z"_find_directive.<locals>.<genexpr>c                 s   s   | ]}|d k	r|V  qd S r   r   )rD   mr   r   r   rE   	  s      )r2   rS   Zstring_typesnext)Z
directivesrb   rc   Zmatchesr   ra   r   rO      s    rO   )N)
r6   r   rS   Zcertbot.pluginsr   rP   r   objectr9   rO   r   r   r   r   <module>   s    g