5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

クソアプリAdvent Calendar 2022

Day 17

Web上でSSL証明書を発行【Certbot】

Posted at

コマンドしなくてもレッツ円栗プトで発行できるようにしてみました。

See the Pen Untitled by John Doe (@04) on CodePen.

初期化

sudo apt install -y zip certbot
server.py
from http.server import HTTPServer, SimpleHTTPRequestHandler
import os
import re
import ssl
import subprocess
import threading
from socketserver import ThreadingMixIn

class ThreadingSimpleServer(ThreadingMixIn,HTTPServer):
    pass

class CustomHTTPRequestHandler(SimpleHTTPRequestHandler):

    def do_POST(self):
        domain = os.path.basename(self.path)
        if not re.match(r'^([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,}$', domain):
            self.send_response(400)
            self.send_header( "Access-Control-Allow-Origin", "*")
            self.end_headers()
            return
        self.send_response(200)
        self.send_header('Content-Type', 'text/event-stream')
        self.send_header( "Access-Control-Allow-Origin", "*")
        self.end_headers()
        domain = os.path.basename(self.path)
        proc = subprocess.Popen(f'sudo certbot certonly --standalone --register-unsafely-without-email --agree-tos --non-interactive -d "{domain}"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        for line in proc.stdout:
            self.wfile.write(line)
        zip_command = f'[ -d "/etc/letsencrypt/live/{domain}" ] && sudo zip -jrq "{domain}.zip" "/etc/letsencrypt/live/{domain}" && echo "\nCertificates: https://certbot.cf/{domain}.zip"'
        proc = subprocess.Popen(zip_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        for line in proc.stdout:
            self.wfile.write(line)


httpd = ThreadingSimpleServer(('0.0.0.0', 443), CustomHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile='../privkey.pem', certfile="../fullchain.pem", server_side=True)
print("Server running on https://certbot.cf")
httpd.serve_forever()
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?