<s> """<STR_LIT>""" <EOL> import argparse <EOL> import collections <EOL> import functools <EOL> import logging <EOL> import os <EOL> import sys <EOL> from six . moves import BaseHTTPServer <EOL> from six . moves import http_client <EOL> from six . moves import socketserver <EOL> import OpenSSL <EOL> from acme import challenges <EOL> from acme import crypto_util <EOL> logger = logging . getLogger ( __name__ ) <EOL> class TLSServer ( socketserver . TCPServer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . certs = kwargs . pop ( "<STR_LIT>" , { } ) <EOL> self . method = kwargs . pop ( <EOL> "<STR_LIT>" , crypto_util . _DEFAULT_TLSSNI01_SSL_METHOD ) <EOL> self . allow_reuse_address = kwargs . pop ( "<STR_LIT>" , True ) <EOL> socketserver . TCPServer . __init__ ( self , * args , ** kwargs ) <EOL> def _wrap_sock ( self ) : <EOL> self . socket = crypto_util . SSLSocket ( <EOL> self . socket , certs = self . certs , method = self . method ) <EOL> def server_bind ( self ) : <EOL> self . _wrap_sock ( ) <EOL> return socketserver . TCPServer . server_bind ( self ) <EOL> class ACMEServerMixin : <EOL> """<STR_LIT>""" <EOL> server_version = "<STR_LIT>" <EOL> allow_reuse_address = True <EOL> class TLSSNI01Server ( TLSServer , ACMEServerMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server_address , certs ) : <EOL> TLSServer . __init__ ( <EOL> self , server_address , BaseRequestHandlerWithLogging , certs = certs ) <EOL> class BaseRequestHandlerWithLogging ( socketserver . BaseRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def log_message ( self , format , * args ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" , self . client_address [ <NUM_LIT:0> ] , format % args ) <EOL> def handle ( self ) : <EOL> """<STR_LIT>""" <EOL> self . log_message ( "<STR_LIT>" ) <EOL> socketserver . BaseRequestHandler . handle ( self ) <EOL> class HTTP01Server ( BaseHTTPServer . HTTPServer , ACMEServerMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server_address , resources ) : <EOL> BaseHTTPServer . HTTPServer . __init__ ( <EOL> self , server_address , HTTP01RequestHandler . partial_init ( <EOL> simple_http_resources = resources ) ) <EOL> class HTTP01RequestHandler ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> HTTP01Resource = collections . namedtuple ( <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . simple_http_resources = kwargs . pop ( "<STR_LIT>" , set ( ) ) <EOL> socketserver . BaseRequestHandler . __init__ ( self , * args , ** kwargs ) <EOL> def log_message ( self , format , * args ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" , self . client_address [ <NUM_LIT:0> ] , format % args ) <EOL> def handle ( self ) : <EOL> """<STR_LIT>""" <EOL> self . log_message ( "<STR_LIT>" ) <EOL> BaseHTTPServer . BaseHTTPRequestHandler . handle ( self ) <EOL> def do_GET ( self ) : <EOL> if self . path == "<STR_LIT:/>" : <EOL> self . handle_index ( ) <EOL> elif self . path . startswith ( "<STR_LIT:/>" + challenges . HTTP01 . URI_ROOT_PATH ) : <EOL> self . handle_simple_http_resource ( ) <EOL> else : <EOL> self . handle_404 ( ) <EOL> def handle_index ( self ) : <EOL> """<STR_LIT>""" <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( self . server . server_version . encode ( ) ) <EOL> def handle_404 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . send_response ( http_client . NOT_FOUND , message = "<STR_LIT>" ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( b"<STR_LIT>" ) <EOL> def handle_simple_http_resource ( self ) : <EOL> """<STR_LIT>""" <EOL> for resource in self . simple_http_resources : <EOL> if resource . chall . path == self . path : <EOL> self . log_message ( "<STR_LIT>" , <EOL> resource . chall . encode ( "<STR_LIT>" ) ) <EOL> self . send_response ( http_client . OK ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( resource . validation . encode ( ) ) <EOL> return <EOL> else : <EOL> self . log_message ( "<STR_LIT>" ) <EOL> self . log_message ( "<STR_LIT>" , <EOL> self . path ) <EOL> @ classmethod <EOL> def partial_init ( cls , simple_http_resources ) : <EOL> """<STR_LIT>""" <EOL> return functools . partial ( <EOL> cls , simple_http_resources = simple_http_resources ) <EOL> def simple_tls_sni_01_server ( cli_args , forever = True ) : <EOL> """<STR_LIT>""" <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" , default = <NUM_LIT:0> , help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> args = parser . parse_args ( cli_args [ <NUM_LIT:1> : ] ) <EOL> certs = { } <EOL> _ , hosts , _ = next ( os . walk ( '<STR_LIT:.>' ) ) <EOL> for host in hosts : <EOL> with open ( os . path . join ( host , "<STR_LIT>" ) ) as cert_file : <EOL> cert_contents = cert_file . read ( ) <EOL> with open ( os . path . join ( host , "<STR_LIT>" ) ) as key_file : <EOL> key_contents = key_file . read ( ) <EOL> certs [ host . encode ( ) ] = ( <EOL> OpenSSL . crypto . load_privatekey ( <EOL> OpenSSL . crypto . FILETYPE_PEM , key_contents ) , <EOL> OpenSSL . crypto . load_certificate ( <EOL> OpenSSL . crypto . FILETYPE_PEM , cert_contents ) ) <EOL> server = TLSSNI01Server ( ( '<STR_LIT>' , int ( args . port ) ) , certs = certs ) <EOL> logger . info ( "<STR_LIT>" , * server . socket . getsockname ( ) [ : <NUM_LIT:2> ] ) <EOL> if forever : <EOL> server . serve_forever ( ) <EOL> else : <EOL> server . handle_request ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( simple_tls_sni_01_server ( sys . argv ) ) </s>
