tofu: add save_cert_stash

This commit is contained in:
dece 2021-03-13 16:33:04 +01:00
parent 0b79bd9e9e
commit e517df7836
3 changed files with 31 additions and 7 deletions

View file

@ -10,10 +10,10 @@ usage decently.
[gemini]: https://gemini.circumlunar.space/ [gemini]: https://gemini.circumlunar.space/
[amfora]: https://github.com/makeworld-the-better-one/amfora [amfora]: https://github.com/makeworld-the-better-one/amfora
If you are interested in Gemini and looking for a client, I recommend trying a If you are interested in Gemini and looking for a desktop/laptop client, I
graphical one like the excellent [Lagrange][lagrange] or [Kristall][kristall], recommend trying a graphical one like the excellent [Lagrange][lagrange] or
or Amfora if you're feeling more at home in the terminal. Bebop won't attempt [Kristall][kristall], or Amfora if you're feeling more at home in the terminal.
to support every feature other clients might have. Bebop won't attempt to support every feature other clients might have.
[lagrange]: https://git.skyjake.fi/skyjake/lagrange [lagrange]: https://git.skyjake.fi/skyjake/lagrange
[kristall]: https://kristall.random-projects.net/ [kristall]: https://kristall.random-projects.net/

View file

@ -1,7 +1,9 @@
import argparse import argparse
import os
from bebop.browser import Browser from bebop.browser import Browser
from bebop.tofu import load_cert_stash from bebop.fs import get_user_data_path
from bebop.tofu import load_cert_stash, save_cert_stash
def main(): def main():
@ -14,8 +16,16 @@ def main():
else: else:
start_url = None start_url = None
cert_stash = load_cert_stash("/tmp/stash") user_data_path = get_user_data_path()
Browser(cert_stash).run(start_url=start_url) if not user_data_path.exists():
user_data_path.mkdir()
cert_stash_path = user_data_path / "known_hosts.txt"
cert_stash = load_cert_stash(cert_stash_path)
try:
Browser(cert_stash).run(start_url=start_url)
finally:
save_cert_stash(cert_stash, cert_stash_path)
main() main()

View file

@ -42,6 +42,20 @@ def load_cert_stash(stash_path):
return stash return stash
def save_cert_stash(stash, stash_path):
"""Save the certificate stash."""
try:
with open(stash_path, "wt") as stash_file:
for name, entry in stash.values():
algo, fingerprint, timestamp, is_permanent = entry
if not is_permanent:
continue
entry_line = f"{name} {algo} {fingerprint} {timestamp}\n"
stash_path.write(entry_line)
except (OSError, ValueError):
pass
class CertStatus(Enum): class CertStatus(Enum):
"""Value returned by validate_cert.""" """Value returned by validate_cert."""
# Cert is valid: proceed. # Cert is valid: proceed.