def do_http_challenges(client, authzs): port = int(PORT) challs = [get_chall(a, challenges.HTTP01) for a in authzs] answers = set([http_01_answer(client, c) for c in challs]) server = standalone.HTTP01Server(("", port), answers) thread = threading.Thread(target=server.serve_forever) thread.start() # cleanup has to be called on any exception, or when validation is done. # Otherwise the process won't terminate. def cleanup(): server.shutdown() server.server_close() thread.join() try: # Loop until the HTTP01Server is ready. while True: try: urllib2.urlopen("http://localhost:%d" % port) break except urllib2.URLError: time.sleep(0.1) for chall_body in challs: client.answer_challenge(chall_body, chall_body.response(client.net.key)) except Exception: cleanup() raise return cleanup
def do_http_challenges(client, authzs): port = 5002 challs = [get_chall(a, challenges.HTTP01) for a in authzs] answers = set([http_01_answer(client, c) for c in challs]) server = standalone.HTTP01Server(("", port), answers) thread = threading.Thread(target=server.serve_forever) thread.start() # Loop until the HTTP01Server is ready. while True: try: urllib2.urlopen("http://localhost:%d" % port) break except urllib2.URLError: time.sleep(0.1) for chall_body in challs: client.answer_challenge(chall_body, chall_body.response(client.key)) def cleanup(): server.shutdown() server.server_close() thread.join() return cleanup
def auth_and_issue(domains, email=None, cert_output=None, client=None): """Make authzs for each of the given domains, set up a server to answer the challenges in those authzs, tell the ACME server to validate the challenges, then poll for the authzs to be ready and issue a cert.""" if client is None: client = make_client(email) authzs, challenges = make_authzs(client, domains) port = 5002 answers = set([http_01_answer(client, c) for c in challenges]) server = standalone.HTTP01Server(("", port), answers) thread = threading.Thread(target=server.serve_forever) thread.start() # Loop until the HTTP01Server is ready. while True: try: urllib2.urlopen("http://localhost:%d" % port) break except urllib2.URLError: time.sleep(0.1) try: for chall_body in challenges: client.answer_challenge(chall_body, chall_body.response(client.key)) cert_resource = issue(client, authzs, cert_output) return cert_resource finally: server.shutdown() server.server_close() thread.join()
def run(self, port, challenge_type): """Run ACME server on specified ``port``. This method is idempotent, i.e. all calls with the same pair of ``(port, challenge_type)`` will reuse the same server. :param int port: Port to run the server on. :param challenge_type: Subclass of `acme.challenges.Challenge`, either `acme.challenge.HTTP01` or `acme.challenges.TLSSNI01`. :returns: Server instance. :rtype: ACMEServerMixin """ assert challenge_type in (challenges.TLSSNI01, challenges.HTTP01) if port in self._instances: return self._instances[port].server address = ("", port) try: if challenge_type is challenges.TLSSNI01: server = acme_standalone.TLSSNI01Server(address, self.certs) else: # challenges.HTTP01 if not self.config.http01_use_tls: server = acme_standalone.HTTP01Server( address, self.http_01_resources) else: # HTTP01 with TLS server = acme_standalone.HTTP01TLSServer( address, self.http_01_resources) except socket.error as error: raise errors.StandaloneBindError(error, port) thread = threading.Thread( # pylint: disable=no-member target=server.serve_forever) thread.start() # if port == 0, then random free port on OS is taken # pylint: disable=no-member real_port = server.socket.getsockname()[1] self._instances[real_port] = self._Instance(server, thread) return server