
    M/ep&              
           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
Z G d de
j                        Z G d d      Z	 ddeeeee   f      dedee   dee   fdZy)z7Module contains classes used by the Nginx Configurator.    N)Any)List)Optional)Sequence)Set)Union)common
add_headerc                        e Zd ZdZdZed   Zdededededed	ed
df fdZe	ded
e
d    fd       Zdded
efdZd
efdZd
efdZd
ef fdZdd d
ef fdZded
efdZ x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:
       https://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   hostportssldefaultipv6ipv6onlyreturnNc                     t         |   ||f       || _        || _        || _        || _        || j                  v | _        y N)super__init__r   r   r   r   UNSPECIFIED_IPV4_ADDRESSESunspecified_address)selfr   r   r   r   r   r   	__class__s          =/usr/lib/python3/dist-packages/certbot_nginx/_internal/obj.pyr   zAddr.__init__,   sC    $&	 #'4+J+J#J     str_addrc                    |j                  d      }d}d}d}d}d}d}|j                  d      }	|	j                  d      ryt        j                  d|	      }
|
r)d}|
j                         }|	|
j                         d	z   d }n<|	j                  d
      }t        j                  d|d         rd}|d   }n
|d   }|d   }|r2|j                         }|dk(  rd}n|dk(  rd}n|dk(  rd}n|dk(  rd}|r2 | ||||||      S )zInitialize Addr from string. Fr   r   zunix:Nz\[.*\]T   :z^\d+$   r   default_serverr   zipv6only=on)splitpop
startswithrematchgroupend	partition)clsr    partsr   r   r   r   r   r   addr
ipv6_matchtupnextparts                r   
fromstringzAddr.fromstring5   s6    s# yy| ??7# XXi.
D##%D
(*+,D ..%Cxx#a&)1v 1v1v yy{H5 --Y&]*  4sGT8<<r   include_defaultc                 
   d}| j                   d   r| j                   d   rd| j                   z  }n.| j                   d   r| j                   d   }n| j                   d   }| j                  r|r|dz  }| j                  r|dz  }|S )z$Return string representation of Addrr   r   r#   z%s:%sz default_serverz ssl)r3   r   r   )r   r6   r0   s      r   	to_stringzAddr.to_stringi   sx    88A;488A;dhh&EXXa[HHQKEHHQKE<<O&&E88VOEr   c                 "    | j                         S r   )r8   r   s    r   __str__zAddr.__str__z   s    ~~r   c                 .    d| j                         z   dz   S )NzAddr())r;   r:   s    r   __repr__zAddr.__repr__}   s    '#--r   c                      t         |          S r   )r   __hash__)r   r   s    r   r@   zAddr.__hash__   s     w!!r   otherc                 8   | j                   r|j                   rst        j                  | j                  | j                  d   f| j
                        t        j                  |j                  |j                  d   f|j
                        k(  S t        |   |      S )z3Check ip/port equality, with IPv6 support.
        r#   )r   r	   r   CANONICAL_UNSPECIFIED_ADDRESSr3   r   r   __eq__)r   rA   r   s     r   super_eqzAddr.super_eq   s    
 ##(A(A;; B B $ -.2ii9;; C C %		! ./4zz;; ; w~e$$r   c                     t        || j                        rG| j                  |      xr4 | j                  |j                  k(  xr | j                  |j                  k(  S y)NF)
isinstancer   rE   r   r   r   rA   s     r   rD   zAddr.__eq__   sP    eT^^,MM%( 2HH		)2LLEMM13 r   )T)__name__
__module____qualname____doc__r   rC   strboolr   classmethodr   r5   r8   r;   r>   intr@   rE   r   rD   __classcell__)r   s   @r   r   r      s    2 "6$>q$A!KS K K$ K KK'+K04K 1=# 1=(6*: 1= 1=f  "   .# ."# "

%f 
% 
%C D r   r   c                       e Zd ZdZdedee   dededee   de	e
   de	e   d	d
fdZd	efdZd	efdZde
d	efdZd	efdZded	efdZde	e
   d	efdZd	efdZd	efdZd	efdZy
)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

    filepaddrsr   enablednamesrawpathr   Nc                 f    || _         || _        || _        || _        || _        || _        || _        y)zInitialize a VH.N)rT   rU   rW   r   rV   rX   rY   )r   rT   rU   r   rV   rW   rX   rY   s           r   r   zVirtualHost.__init__   s5     


	r   c           
          dj                  d t        | j                  t              D              }d| j                  d|dt        | j                        d| j                  d| j                  
S )	N, c              3   2   K   | ]  }t        |        y wr   rM   .0r1   s     r   	<genexpr>z&VirtualHost.__str__.<locals>.<genexpr>   s     O4SYO   keyzfile: z
addrs: z
names: z
ssl: z

enabled: )	joinsortedrU   rM   rT   listrW   r   rV   )r   addr_strs     r   r;   zVirtualHost.__str__   sP    99O6$**#3NOO "&X!%djj!1488T\\K 	Lr   c                 N    d| j                         j                  dd      z   dz   S )NzVirtualHost(
r\   z)
)r;   replacer:   s    r   r>   zVirtualHost.__repr__   s%     6 6tT BBUJJr   rA   c                    t        || j                        r| j                  |j                  k(  xr t        | j                  t
              t        |j                  t
              k(  xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S y)Nrc   F)
rG   r   rT   rf   rU   rM   rW   r   rV   rY   rH   s     r   rD   zVirtualHost.__eq__   s    eT^^,JJ%++- ,4::3/6%++33OO,JJ%++-, HH		), LLEMM1	,
 II+- r   c                     t        | j                  t        | j                        t        | j                        t        | j
                        | j                  | j                  f      S r   )hashrT   tuplerY   rU   rW   r   rV   r:   s    r   r@   zVirtualHost.__hash__   sI    TZZtyy!14::&djj(9XXt||- . 	.r   header_namec                 @    t        | j                  t        |      }|du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_directiverX   ADD_HEADER_DIRECTIVE)r   rp   founds      r   
has_headerzVirtualHost.has_header   s"    
  *>LD  r   testc                     t        dt        | j                        t        |      z
  dz         D ]"  }| j                  ||t        |      z    |k(  s" y y)zFDetermine if raw server block contains test list at top level
        r   r#   TF)rangelenrX   )r   rv   is      r   contains_listzVirtualHost.contains_list   sS     q#dhh-#d)3a78 	Axx!c$i-(D0	 r   c                 B    | j                   D ]  }|j                  s y y)zRReturn true if one or more of the listen directives in vhost supports
        IPv6TFrU   r   r   as     r   ipv6_enabledzVirtualHost.ipv6_enabled   s&      	Avv	 r   c                 \    | j                   sy| j                   D ]  }|j                  r y y)zRReturn true if one or more of the listen directives in vhost are IPv4
        onlyTFr}   r~   s     r   ipv4_enabledzVirtualHost.ipv4_enabled   s0     zz 	A66	 r   c                     dj                  | j                  dj                  d | j                  D              dj                  | j                        | j
                  rd      S d      S )z5Return a representation of VHost to be used in dialogzBFile: {filename}
Addresses: {addrs}
Names: {names}
HTTPS: {https}
r\   c              3   2   K   | ]  }t        |        y wr   r^   r_   s     r   ra   z+VirtualHost.display_repr.<locals>.<genexpr>   s     AdD	Arb   YesNo)filenamerU   rW   https)formatrT   re   rU   rW   r   r:   s    r   display_reprzVirtualHost.display_repr   sg      &viiAdjjAAii

+#xxe	  &  3		4 .2	  &  3		4r   )rI   rJ   rK   rL   rM   r   r   rN   r   r   r   rP   r   r;   r>   rD   r@   ru   r{   r   r   r    r   r   rS   rS      s    	c 	(4. 	t 	d 	C	'+Cy	8<S		FJ		L 	LK# K	C 	D 	.# .
!c !d !$s)  d d 
4c 
4r   rS   
directivesdirective_namematch_contentr   c                     | rt        | t              ry| d   k(  r| v r| S fd| D        }t        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   8   K   | ]  }t        |        y wr   )rr   )r`   liner   r   s     r   ra   z"_find_directive.<locals>.<genexpr>  s     [t^]C[s   c              3   &   K   | ]	  }||  y wr   r   )r`   ms     r   ra   z"_find_directive.<locals>.<genexpr>  s     5qq}5s   )rG   rM   next)r   r   r   matchess    `` r   rr   rr     sS    
 J4 !}&"mz&A[PZ[G5G5t<<r   r   )rL   r*   typingr   r   r   r   r   r   certbot.pluginsr	   rs   r   rS   rM   rr   r   r   r   <module>r      s    = 	       "# G6;; GTe4 e4R 48=sDI~)> ? =QT =#+C==<DSM=r   