Add easy loading of previously generated certs.

This commit is contained in:
Solderpunk 2020-05-11 23:27:48 +02:00
parent 676ab85a9e
commit a05ecfad4f
1 changed files with 30 additions and 8 deletions

38
av98.py
View File

@ -14,6 +14,7 @@ import cgi
import codecs import codecs
import collections import collections
import fnmatch import fnmatch
import glob
import io import io
import mimetypes import mimetypes
import os import os
@ -421,14 +422,18 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
print("This will allow the site to recognise you across requests.") print("This will allow the site to recognise you across requests.")
print("What do you want to do?") print("What do you want to do?")
print("1. Give up.") print("1. Give up.")
print("2. Load client certificate from file and retry the request.") print("2. Generate new certificate and retry the request.")
print("3. Generate new certificate and retry the request.") print("3. Load previously generated certificate from file.")
print("4. Load certificate from file and retry the request.")
choice = input("> ").strip() choice = input("> ").strip()
if choice == "2": if choice == "2":
self._load_client_cert() self._generate_persistent_client_cert()
self._go_to_gi(gi, update_hist, handle) self._go_to_gi(gi, update_hist, handle)
elif choice == "3": elif choice == "3":
self._generate_persistent_client_cert() self._choose_client_cert()
self._go_to_gi(gi, update_hist, handle)
elif choice == "4":
self._load_client_cert()
self._go_to_gi(gi, update_hist, handle) self._go_to_gi(gi, update_hist, handle)
else: else:
print("Giving up.") print("Giving up.")
@ -752,6 +757,20 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
os.system(cmd) os.system(cmd)
self._activate_client_cert(certfile, keyfile) self._activate_client_cert(certfile, keyfile)
def _choose_client_cert(self):
certdir = os.path.join(self.config_dir, "certs")
certs = glob.glob(os.path.join(certdir, "*.crt"))
certdir = {}
for n, cert in enumerate(certs):
certdir[str(n+1)] = (cert, os.path.splitext(cert)[0] + ".key")
print("{}. {}".format(n+1, os.path.splitext(os.path.basename(cert))[0]))
choice = input("> ").strip()
if choice in certdir:
certfile, keyfile = certdir[choice]
self._activate_client_cert(certfile, keyfile)
else:
print("What?")
def _activate_client_cert(self, certfile, keyfile): def _activate_client_cert(self, certfile, keyfile):
self.client_certs["active"] = (certfile, keyfile) self.client_certs["active"] = (certfile, keyfile)
self.active_cert_domains = [] self.active_cert_domains = []
@ -852,17 +871,20 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
if self.client_certs["active"]: if self.client_certs["active"]:
print("Active certificate: {}".format(self.client_certs["active"][0])) print("Active certificate: {}".format(self.client_certs["active"][0]))
print("1. Deactivate client certificate.") print("1. Deactivate client certificate.")
print("2. Load client certificate from file.") print("2. Generate new certificate.")
print("3. Generate new certificate.") print("3. Load previously generated certificate.")
print("4. Load client certificate from file.")
print("Enter blank line to exit certificate manager.") print("Enter blank line to exit certificate manager.")
choice = input("> ").strip() choice = input("> ").strip()
if choice == "1": if choice == "1":
print("Deactivating client certificate.") print("Deactivating client certificate.")
self._deactivate_client_cert() self._deactivate_client_cert()
elif choice == "2": elif choice == "2":
self._load_client_cert() self._generate_persistent_client_cert()
elif choice == "3": elif choice == "3":
self._generate_client_cert() self._choose_client_cert()
elif choice == "4":
self._load_client_cert()
else: else:
print("Aborting.") print("Aborting.")