Move client certificate details out of the options dictionary and into their own, so that the 'cert' and 'set' commands don't become parallel interfaces to the same config.

This commit is contained in:
Solderpunk 2020-05-10 12:59:26 +02:00
parent 6d4c8e2dc9
commit a2aff0d2a5
1 changed files with 11 additions and 14 deletions

25
av98.py
View File

@ -216,6 +216,10 @@ class GeminiClient(cmd.Cmd):
self.visited_hosts = set() self.visited_hosts = set()
self.waypoints = [] self.waypoints = []
self.client_certs = {
"active": None
}
self.options = { self.options = {
"debug" : False, "debug" : False,
"ipv6" : True, "ipv6" : True,
@ -223,8 +227,6 @@ class GeminiClient(cmd.Cmd):
"gopher_proxy" : "localhost:1965", "gopher_proxy" : "localhost:1965",
"width" : 80, "width" : 80,
"auto_follow_redirects" : True, "auto_follow_redirects" : True,
"client_certfile" : None,
"client_keyfile" : None,
} }
self.log = { self.log = {
@ -437,9 +439,9 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
# Rely on the server to only support sensible things, I guess... # Rely on the server to only support sensible things, I guess...
pass pass
# Load client certificate if needed # Load client certificate if needed
if self.options["client_certfile"]: if self.client_certs["active"]:
context.load_cert_chain(self.options["client_certfile"], certfile, keyfile = self.client_certs["active"]
self.options["client_keyfile"]) context.load_cert_chain(certfile, keyfile)
# Connect to remote host by any address possible # Connect to remote host by any address possible
err = None err = None
@ -672,22 +674,18 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
@restricted @restricted
def do_cert(self, line): def do_cert(self, line):
"""Set or clear a client certificate""" """Set or clear a client certificate"""
if self.options["client_certfile"]: if self.client_certs["active"]:
print("Deactivating client certificate.") print("Deactivating client certificate.")
self.options["client_certfile"] = None self.client_certs["active"] = None
self.options["client_keyfile"] = None
self.prompt = self.no_cert_prompt self.prompt = self.no_cert_prompt
else: else:
print("Loading client certificate file, in PEM format (blank line to cancel)") print("Loading client certificate file, in PEM format (blank line to cancel)")
certfile = input("Certfile path: ") certfile = input("Certfile path: ")
print("Loading private key file, in PEM format (blank line to cancel)") print("Loading private key file, in PEM format (blank line to cancel)")
keyfile = input("Keyfile path: ") keyfile = input("Keyfile path: ")
self.options["client_certfile"] = certfile self.client_certs["active"] = (certfile, keyfile)
self.options["client_keyfile"] = keyfile
self.prompt = self.cert_prompt self.prompt = self.cert_prompt
@restricted @restricted
def do_handler(self, line): def do_handler(self, line):
"""View or set handler commands for different MIME types.""" """View or set handler commands for different MIME types."""
@ -1107,8 +1105,7 @@ def main():
# Act on args # Act on args
if args.tls_cert: if args.tls_cert:
# If tls_key is None, python will attempt to load the key from tls_cert. # If tls_key is None, python will attempt to load the key from tls_cert.
gc.options["client_certfile"] = args.tls_cert gc.client_certs["active"] = (args.tls_cert, args.tls_key)
gc.options["client_keyfile"] = args.tls_key
gc.prompt = gc.cert_prompt gc.prompt = gc.cert_prompt
if args.bookmarks: if args.bookmarks:
gc.cmdqueue.append("bookmarks") gc.cmdqueue.append("bookmarks")