ó
Á£ô_c           @   sŠ  d  Z  d d l Z d d l Z d d l Z d d l 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 e j e ƒ Z d	 e	 j f d
 „  ƒ  YZ d d d „  ƒ  YZ d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e j f d „  ƒ  YZ d e e f d „  ƒ  YZ d e f d „  ƒ  YZ d e j f d „  ƒ  YZ d e	 j f d „  ƒ  YZ d S(   s1   Support for standalone client challenge solvers. iÿÿÿÿN(   t   BaseHTTPServer(   t   http_client(   t   socketserver(   t
   challenges(   t   crypto_util(   t   Listt	   TLSServerc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s   Generic TLS Server.c         O   s•   | j  d t ƒ |  _ |  j r- t j |  _ n t j |  _ | j  d i  ƒ |  _ | j  d t j	 ƒ |  _
 | j  d t ƒ |  _ t j j |  | | Ž d  S(   Nt   ipv6t   certst   methodt   allow_reuse_address(   t   popt   FalseR   t   sockett   AF_INET6t   address_familyt   AF_INETR   R   t   _DEFAULT_SSL_METHODR	   t   TrueR
   R   t	   TCPServert   __init__(   t   selft   argst   kwargs(    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR      s    	c      	   C   s=   t  j |  j d |  j d t |  d d  ƒ d |  j ƒ|  _ d  S(   Nt   cert_selectiont   alpn_selectiont   _alpn_selectionR	   (   R   t	   SSLSocketR   t   _cert_selectiont   getattrt   NoneR	   (   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyt
   _wrap_sock"   s    c         C   s   | j  ƒ  } |  j j | d ƒ S(   s.   Callback selecting certificate for connection.N(   t   get_servernameR   t   getR   (   R   t
   connectiont   server_name(    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR   (   s    c         C   s   |  j  ƒ  t j j |  ƒ S(   N(   R   R   R   t   server_bind(   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR$   -   s    
(   t   __name__t
   __module__t   __doc__R   R   R   R$   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR      s
   			t   ACMEServerMixinc           B   s   e  Z d  Z d Z e Z RS(   s"   ACME server common settings mixin.s'   ACME client standalone challenge solver(   R%   R&   R'   t   server_versionR   R
   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR(   2   s   t   BaseDualNetworkedServersc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s  Base class for a pair of IPv6 and IPv4 servers that tries to do everything
       it's asked for both servers, but where failures in one server don't
       affect the other.

       If two servers are instantiated, they will serve on the same port.
       c   
      O   s\  | d } g  |  _  g  |  _ xt t g D]} yp | | d <| d f | f | d } | f | } | | | Ž  }	 t j d | d | d | r— d n d ƒ Wnt t j k
 r|  j rè t j d | d | d | rÞ d n d ƒ q9t j d	 | d | d | rd n d ƒ q) X|  j j |	 ƒ |	 j j	 ƒ  d } q) W|  j sXt j d
 ƒ ‚ n  d  S(   Ni   R   i    i   s$   Successfully bound to %s:%s using %st   IPv6t   IPv4s‚   Certbot wasn't able to bind to %s:%s using %s, this is often expected due to the dual stack nature of IPv6 socket implementations.s    Failed to bind to %s:%s using %ss   Could not bind to IPv4 or IPv6.(
   t   threadst   serversR   R   t   loggert   debugR   t   errort   appendt   getsockname(
   R   t   ServerClasst   server_addresst   remaining_argsR   t   portt
   ip_versiont   new_addresst   new_argst   server(    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR   A   s2    
			

	
	c         C   sG   x@ |  j  D]5 } t j d | j ƒ } | j ƒ  |  j j | ƒ q
 Wd S(   s*   Wraps socketserver.TCPServer.serve_forevert   targetN(   R.   t	   threadingt   Threadt   serve_forevert   startR-   R2   (   R   R;   t   thread(    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR?   k   s
    	
c         C   s#   g  |  j  D] } | j j ƒ  ^ q
 S(   s/   Wraps socketserver.TCPServer.socket.getsockname(   R.   R   R3   (   R   R;   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyt   getsocknamess   s    c         C   sS   x% |  j  D] } | j ƒ  | j ƒ  q
 Wx |  j D] } | j ƒ  q2 Wg  |  _ d S(   sp   Wraps socketserver.TCPServer.shutdown, socketserver.TCPServer.server_close, and
           threading.Thread.joinN(   R.   t   shutdownt   server_closeR-   t   join(   R   R;   RA   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyt   shutdown_and_server_closew   s    
(   R%   R&   R'   R   R?   RB   RF   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR*   9   s
   	*		t   TLSALPN01Serverc           B   s2   e  Z d  Z d Z e d „ Z d „  Z d „  Z RS(   s   TLSALPN01 Server.s
   acme-tls/1c         C   s,   t  j |  | t d | d | ƒ| |  _ d  S(   NR   R   (   R   R   t   _BaseRequestHandlerWithLoggingt   challenge_certs(   R   R5   R   RI   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR   ‡   s    c         C   s/   | j  ƒ  } t j d | ƒ |  j j | d  ƒ S(   Ns)   Serving challenge cert for server name %s(   R    R/   R0   RI   R!   R   (   R   R"   R#   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR      s    c         C   sY   t  | ƒ d k r? | d |  j k r? t j d |  j ƒ |  j St j d t | ƒ ƒ d S(   s!   Callback to select alpn protocol.i   i    s   Agreed on %s ALPNs#   Cannot agree on ALPN proto. Got: %st    (   t   lent   ACME_TLS_1_PROTOCOLR/   R0   t   str(   R   t   _connectiont   alpn_protos(    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR   ™   s
    %(   R%   R&   R'   RL   R   R   R   R   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRG   ‚   s
   	t
   HTTPServerc           B   s   e  Z d  Z d „  Z RS(   s   Generic HTTP Server.c         O   sS   | j  d t ƒ |  _ |  j r- t j |  _ n t j |  _ t j j	 |  | | Ž d  S(   NR   (
   R   R   R   R   R   R   R   R    RP   R   (   R   R   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR   §   s
    	(   R%   R&   R'   R   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRP   ¤   s   t   HTTP01Serverc           B   s   e  Z d  Z e d d „ Z RS(   s   HTTP01 Server.i   c         C   s/   t  j |  | t j d | d | ƒ d | ƒd  S(   Nt   simple_http_resourcest   timeoutR   (   RP   R   t   HTTP01RequestHandlert   partial_init(   R   R5   t	   resourcesR   RS   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR   ³   s    (   R%   R&   R'   R   R   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRQ   °   s   t   HTTP01DualNetworkedServersc           B   s   e  Z d  Z d „  Z RS(   s`   HTTP01Server Wrapper. Tries everything for both. Failures for one don't
       affect the other.c         O   s   t  j |  t | | Ž d  S(   N(   R*   R   RQ   (   R   R   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR   ½   s    (   R%   R&   R'   R   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRW   ¹   s   RT   c           B   sn   e  Z d  Z e j d d ƒ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z e d
 „  ƒ Z RS(   sÍ   HTTP01 challenge handler.

    Adheres to the stdlib's `socketserver.BaseRequestHandler` interface.

    :ivar set simple_http_resources: A set of `HTTP01Resource`
        objects. TODO: better name?

    t   HTTP01Resources   chall response validationc         O   sG   | j  d t ƒ  ƒ |  _ | j  d d ƒ |  _ t j j |  | | Ž d  S(   NRR   RS   i   (   R   t   setRR   RS   R    t   BaseHTTPRequestHandlerR   (   R   R   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR   Í   s    c         G   s"   t  j d |  j d | | ƒ d S(   s   Log arbitrary message.s	   %s - - %si    N(   R/   R0   t   client_address(   R   t   formatR   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyt   log_messageÒ   s    c         C   s!   |  j  d ƒ t j j |  ƒ d S(   s   Handle request.s   Incoming requestN(   R]   R    RZ   t   handle(   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR^   Ö   s    c         C   sS   |  j  d k r |  j ƒ  n3 |  j  j d t j j ƒ rE |  j ƒ  n
 |  j ƒ  d  S(   Nt   /(   t   patht   handle_indext
   startswithR   t   HTTP01t   URI_ROOT_PATHt   handle_simple_http_resourcet
   handle_404(   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyt   do_GETÛ   s
    c         C   sG   |  j  d ƒ |  j d d ƒ |  j ƒ  |  j j |  j j j ƒ  ƒ d S(   s   Handle index page.iÈ   s   Content-Types	   text/htmlN(   t   send_responset   send_headert   end_headerst   wfilet   writeR;   R)   t   encode(   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRa   ã   s    
c         C   sD   |  j  t j d d ƒ|  j d d ƒ |  j ƒ  |  j j d ƒ d S(   s   Handler 404 Not Found errors.t   messages	   Not Founds   Content-types	   text/htmlt   404N(   Rh   R   t	   NOT_FOUNDRi   Rj   Rk   Rl   (   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRf   ê   s    
c         C   s    x† |  j  D]n } | j j |  j k r
 |  j d | j j d ƒ ƒ |  j t j ƒ |  j ƒ  |  j	 j
 | j j ƒ  ƒ d Sq
 W|  j d ƒ |  j d |  j ƒ d S(   s$   Handle HTTP01 provisioned resources.s   Serving HTTP01 with token %rt   tokenNs   No resources to serves0   %s does not correspond to any resource. ignoring(   RR   t   challR`   R]   Rm   Rh   R   t   OKRj   Rk   Rl   t
   validation(   R   t   resource(    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRe   ñ   s    	
	c         C   s   t  j |  d | d | ƒS(   s¿   Partially initialize this handler.

        This is useful because `socketserver.BaseServer` takes
        uninitialized handler and initializes it with the current
        request.

        RR   RS   (   t	   functoolst   partial(   t   clsRR   RS   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRU      s    	(   R%   R&   R'   t   collectionst
   namedtupleRX   R   R]   R^   Rg   Ra   Rf   Re   t   classmethodRU   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRT   Á   s   							RH   c           B   s    e  Z d  Z d „  Z d „  Z RS(   s    BaseRequestHandler with logging.c         G   s"   t  j d |  j d | | ƒ d S(   s   Log arbitrary message.s	   %s - - %si    N(   R/   R0   R[   (   R   R\   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR]     s    c         C   s!   |  j  d ƒ t j j |  ƒ d S(   s   Handle request.s   Incoming requestN(   R]   R   t   BaseRequestHandlerR^   (   R   (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyR^     s    (   R%   R&   R'   R]   R^   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyRH     s   	(    (   R'   Ry   Rv   t   loggingR   R=   t	   six.movesR    R   R   t   acmeR   R   t   acme.magic_typingR   t	   getLoggerR%   R/   R   R   R(   t   objectR*   RG   RP   RQ   RW   RZ   RT   R|   RH   (    (    (    s3   /usr/lib/python2.7/site-packages/acme/standalone.pyt   <module>   s*   I"	M