Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding an identity backup and auto load if corruption #1198

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions pwnagotchi/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hashlib
import os
import logging
import shutil

DefaultPath = "/etc/pwnagotchi/"

Expand All @@ -25,9 +26,14 @@ def __init__(self, path=DefaultPath, view=None):
while True:
# first time, generate new keys
if not os.path.exists(self.priv_path) or not os.path.exists(self.pub_path):
self._view.on_keys_generation()
logging.info("generating %s ..." % self.priv_path)
os.system("pwngrid -generate -keys '%s'" % self.path)
if os.path.exists(f'{self.priv_path}.original') and os.path.exists(f'{self.pub_path}.original') and os.path.exists(f'{self.fingerprint_path}.original'):
logging.warning('loading backup')
shutil.copy(f'{self.priv_path}.original', self.priv_path)
shutil.copy(f'{self.pub_path}.original', self.pub_path)
else:
self._view.on_keys_generation()
logging.info("generating %s ..." % self.priv_path)
os.system("pwngrid -generate -keys '%s'" % self.path)

# load keys: they might be corrupted if the unit has been turned off during the generation, in this case
# the exception will remove the files and go back at the beginning of this loop.
Expand All @@ -52,6 +58,12 @@ def __init__(self, path=DefaultPath, view=None):

# no exception, keys loaded correctly.
self._view.on_starting()
if not os.path.exists(f'{self.priv_path}.original'):
shutil.copy(self.priv_path, f'{self.priv_path}.original')
if not os.path.exists(f'{self.pub_path}.original'):
shutil.copy(self.pub_path, f'{self.pub_path}.original')
if not os.path.exists(f'{self.fingerprint_path}.original'):
shutil.copy(self.fingerprint_path, f'{self.fingerprint_path}.original')
return

except Exception as e:
Expand Down