mirror of
https://github.com/rwinkhart/sshyp.git
synced 2026-09-05 08:37:14 -04:00
Use tempfile.NamedTemporaryFile() for editing notes, removing the need for shm_gen()
Former-commit-id: 2d2e733945ba2748a24ed93f861145cf26ac1f13 Former-commit-id: e3b5add08675438e1a08607d081a9a44f426425e
This commit is contained in:
+25
-40
@@ -3,8 +3,7 @@ from configparser import ConfigParser, NoSectionError
|
|||||||
from os import chmod, environ, listdir, walk
|
from os import chmod, environ, listdir, walk
|
||||||
from os.path import expanduser, isdir, isfile, realpath
|
from os.path import expanduser, isdir, isfile, realpath
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from random import randint
|
from shutil import move
|
||||||
from shutil import move, rmtree
|
|
||||||
from sshync import delete as offline_delete, run_profile
|
from sshync import delete as offline_delete, run_profile
|
||||||
from subprocess import CalledProcessError, DEVNULL, PIPE, run
|
from subprocess import CalledProcessError, DEVNULL, PIPE, run
|
||||||
from sys import argv, exit as s_exit
|
from sys import argv, exit as s_exit
|
||||||
@@ -117,23 +116,27 @@ def pass_gen():
|
|||||||
return _gen
|
return _gen
|
||||||
|
|
||||||
|
|
||||||
# creates a temporary directory for entry editing
|
# creates a temporary file to allow notes to be edited by standard editors
|
||||||
def shm_gen(_tmp_dir=f"{home}/.config/sshyp/tmp/"):
|
def edit_note(_note_lines):
|
||||||
_shm_folder_gen = string_gen('f', randint(12, 48))
|
from tempfile import NamedTemporaryFile
|
||||||
_shm_entry_gen = string_gen('f', randint(12, 48))
|
with NamedTemporaryFile(mode='w+') as _tmp:
|
||||||
Path(_tmp_dir + _shm_folder_gen).mkdir(mode=0o700)
|
_tmp.write('\n'.join(_note_lines))
|
||||||
return _shm_folder_gen, _shm_entry_gen
|
_tmp.seek(0)
|
||||||
|
run((editor, _tmp.name))
|
||||||
|
_tmp.seek(0)
|
||||||
|
_new_note = _tmp.read().rstrip()
|
||||||
|
return _new_note
|
||||||
|
|
||||||
|
|
||||||
# encrypts an entry and cleans up the temporary files
|
# encrypts an entry and cleans up the temporary files
|
||||||
def encrypt(_entry_data, _entry_dir, _gpg_id, _tmp_dir=f"{home}/.config/sshyp/tmp/"):
|
def encrypt(_entry_data, _entry_dir, _gpg_id):
|
||||||
_bytes_data = '\n'.join(_entry_data).encode()
|
_bytes_data = '\n'.join(_entry_data).encode()
|
||||||
_encrypted_data = run(('gpg', '-qr', str(_gpg_id), '-e'), input=_bytes_data, stdout=PIPE).stdout
|
_encrypted_data = run(('gpg', '-qr', str(_gpg_id), '-e'), input=_bytes_data, stdout=PIPE).stdout
|
||||||
open(_entry_dir + '.gpg', 'wb').write(_encrypted_data)
|
open(_entry_dir + '.gpg', 'wb').write(_encrypted_data)
|
||||||
|
|
||||||
|
|
||||||
# decrypts an entry to a temporary directory
|
# decrypts an entry to a temporary directory
|
||||||
def decrypt(_entry_dir, _quick_verify=None, _quick_pass=None, _tmp_dir=f"{home}/.config/sshyp/tmp/"):
|
def decrypt(_entry_dir, _quick_verify=None, _quick_pass=None):
|
||||||
_contents = None
|
_contents = None
|
||||||
|
|
||||||
# check quick-unlock status, fetch passphrase
|
# check quick-unlock status, fetch passphrase
|
||||||
@@ -406,27 +409,19 @@ def add_entry():
|
|||||||
|
|
||||||
# note entry
|
# note entry
|
||||||
if arguments[2] in ('note', '-n'):
|
if arguments[2] in ('note', '-n'):
|
||||||
_shm_folder, _shm_entry = shm_gen()
|
_username, _password, _url, _note = '', '', '', edit_note([])
|
||||||
run((editor, f"{tmp_dir}{_shm_folder}/{_shm_entry}"))
|
|
||||||
_password, _username, _url = '', '', ''
|
|
||||||
_notes = open(f"{tmp_dir}{_shm_folder}/{_shm_entry}", 'r').read()
|
|
||||||
rmtree(f"{tmp_dir}{_shm_folder}")
|
|
||||||
else:
|
else:
|
||||||
# password entry
|
# password entry
|
||||||
_username = str(input('username: '))
|
_username = str(input('username: '))
|
||||||
_password = str(input('password: '))
|
_password = str(input('password: '))
|
||||||
_url = str(input('url: '))
|
_url = str(input('url: '))
|
||||||
_add_note = input('add a note to this entry? (y/N) ')
|
if input('add a note to this entry? (y/N) ').lower() == 'y':
|
||||||
if _add_note.lower() == 'y':
|
_note = edit_note([])
|
||||||
_shm_folder, _shm_entry = shm_gen() # TODO update notes to use new edit method
|
|
||||||
run((editor, f"{tmp_dir}{_shm_folder}/{_shm_entry}"))
|
|
||||||
_notes = open(f"{tmp_dir}{_shm_folder}/{_shm_entry}", 'r').read()
|
|
||||||
rmtree(f"{tmp_dir}{_shm_folder}")
|
|
||||||
else:
|
else:
|
||||||
_notes = ''
|
_note = []
|
||||||
print('\n\u001b[1mentry preview:\u001b[0m')
|
print('\n\u001b[1mentry preview:\u001b[0m')
|
||||||
entry_reader([_password, _username, _url, _notes])
|
entry_reader([_username, _password, _url, _note])
|
||||||
encrypt([_password, _username, _url, _notes], directory + entry_name, gpg_id)
|
encrypt([_username, _password, _url, _note], directory + entry_name, gpg_id)
|
||||||
|
|
||||||
|
|
||||||
# creates a new folder
|
# creates a new folder
|
||||||
@@ -487,13 +482,8 @@ def edit():
|
|||||||
elif arguments[2] in ('url', '-l'):
|
elif arguments[2] in ('url', '-l'):
|
||||||
_detail, _edit_line = str(input('url: ')), 2
|
_detail, _edit_line = str(input('url: ')), 2
|
||||||
if arguments[2] in ('note', '-n'):
|
if arguments[2] in ('note', '-n'):
|
||||||
_shm_folder, _shm_entry = shm_gen()
|
_old_lines = decrypt(directory + entry_name, _quick_verify=quick_unlock_enabled)
|
||||||
_all_lines = decrypt(directory + entry_name, _quick_verify=quick_unlock_enabled)
|
_new_lines = _old_lines[0:3] + edit_note(_old_lines[3:]).split('\n')
|
||||||
open(f"{tmp_dir}{_shm_folder}/{_shm_entry}", 'w').write('\n'.join(_all_lines[3:]))
|
|
||||||
run((editor, f"{tmp_dir}{_shm_folder}/{_shm_entry}"))
|
|
||||||
_new_notes = open(f"{tmp_dir}{_shm_folder}/{_shm_entry}").read().rstrip().split('\n')
|
|
||||||
_new_lines = _all_lines[0:3] + _new_notes
|
|
||||||
rmtree(f"{tmp_dir}{_shm_folder}")
|
|
||||||
else:
|
else:
|
||||||
_new_lines = optimized_edit(decrypt(directory + entry_name, _quick_verify=quick_unlock_enabled), _detail,
|
_new_lines = optimized_edit(decrypt(directory + entry_name, _quick_verify=quick_unlock_enabled), _detail,
|
||||||
_edit_line)
|
_edit_line)
|
||||||
@@ -518,15 +508,11 @@ def gen():
|
|||||||
_username = str(input('username: '))
|
_username = str(input('username: '))
|
||||||
_password = pass_gen()
|
_password = pass_gen()
|
||||||
_url = str(input('url: '))
|
_url = str(input('url: '))
|
||||||
_add_note = input('add a note to this entry? (y/N) ')
|
if input('add a note to this entry? (y/N) ').lower() == 'y':
|
||||||
if _add_note.lower() == 'y':
|
_note = edit_note([])
|
||||||
_shm_folder, _shm_entry = shm_gen()
|
|
||||||
run((editor, f"{tmp_dir}{_shm_folder}/{_shm_entry}"))
|
|
||||||
_notes = open(f"{tmp_dir}{_shm_folder}/{_shm_entry}", 'r').read()
|
|
||||||
rmtree(f"{tmp_dir}{_shm_folder}")
|
|
||||||
else:
|
else:
|
||||||
_notes = ''
|
_note = ''
|
||||||
_new_lines = [_password, _username, _url, _notes]
|
_new_lines = [_password, _username, _url, _note]
|
||||||
print('\n\u001b[1mentry preview:\u001b[0m')
|
print('\n\u001b[1mentry preview:\u001b[0m')
|
||||||
entry_reader(_new_lines)
|
entry_reader(_new_lines)
|
||||||
encrypt(_new_lines, directory + entry_name, gpg_id)
|
encrypt(_new_lines, directory + entry_name, gpg_id)
|
||||||
@@ -640,7 +626,6 @@ if __name__ == "__main__":
|
|||||||
arg_start = 0
|
arg_start = 0
|
||||||
|
|
||||||
# import saved userdata
|
# import saved userdata
|
||||||
tmp_dir = f"{home}/.config/sshyp/tmp/"
|
|
||||||
try:
|
try:
|
||||||
sshyp_data = ConfigParser()
|
sshyp_data = ConfigParser()
|
||||||
sshyp_data.read(f"{home}/.config/sshyp/sshyp.ini")
|
sshyp_data.read(f"{home}/.config/sshyp/sshyp.ini")
|
||||||
|
|||||||
+2
-13
@@ -1,7 +1,7 @@
|
|||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
from curses import A_REVERSE, echo, KEY_DOWN, KEY_UP, cbreak, curs_set, endwin, initscr, newwin, noecho, nocbreak
|
from curses import A_REVERSE, echo, KEY_DOWN, KEY_UP, cbreak, curs_set, endwin, initscr, newwin, noecho, nocbreak
|
||||||
from curses.textpad import rectangle, Textbox
|
from curses.textpad import rectangle, Textbox
|
||||||
from os import environ, listdir, remove, symlink
|
from os import environ, listdir, remove
|
||||||
from os.path import exists, expanduser, isfile
|
from os.path import exists, expanduser, isfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from random import randint
|
from random import randint
|
||||||
@@ -235,7 +235,7 @@ def quick_unlock_config(_default):
|
|||||||
|
|
||||||
# re-encrypt/optimize all entries
|
# re-encrypt/optimize all entries
|
||||||
def refresh_encryption():
|
def refresh_encryption():
|
||||||
_directory, _tmp_dir = sshyp_data.get('SSHYNC', 'local_dir').rstrip('/'), f"{home}/.config/sshyp/tmp/"
|
_directory = sshyp_data.get('SSHYNC', 'local_dir').rstrip('/')
|
||||||
|
|
||||||
# warn the user of potential data loss and prompt to continue
|
# warn the user of potential data loss and prompt to continue
|
||||||
_proceed = curses_radio(('yes', 'no'), "are you sure you wish to re-encrypt all entries with this key?"
|
_proceed = curses_radio(('yes', 'no'), "are you sure you wish to re-encrypt all entries with this key?"
|
||||||
@@ -441,17 +441,6 @@ def initial_setup():
|
|||||||
if _exists_flag:
|
if _exists_flag:
|
||||||
sshyp_data.clear()
|
sshyp_data.clear()
|
||||||
|
|
||||||
# temporary file symlink creation
|
|
||||||
if not exists(f"{home}/.config/sshyp/tmp"):
|
|
||||||
# PORT START UNAME-TMP
|
|
||||||
if uname()[0] in ('Haiku', 'FreeBSD', 'Darwin'):
|
|
||||||
symlink('/tmp', f"{home}/.config/sshyp/tmp")
|
|
||||||
elif exists('/data/data/com.termux'):
|
|
||||||
symlink('/data/data/com.termux/files/usr/tmp', f"{home}/.config/sshyp/tmp")
|
|
||||||
else:
|
|
||||||
symlink('/dev/shm', f"{home}/.config/sshyp/tmp")
|
|
||||||
# PORT END UNAME-TMP
|
|
||||||
|
|
||||||
# curses initialization
|
# curses initialization
|
||||||
noecho()
|
noecho()
|
||||||
cbreak()
|
cbreak()
|
||||||
|
|||||||
+2
-23
@@ -1,34 +1,13 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import re
|
import re
|
||||||
from sys import argv, exit as s_exit
|
|
||||||
|
|
||||||
# read arguments
|
targets = (('UNAME-IMPORT-SSHYP', 'sshyp.py', ''), ('UNAME-IMPORT-STWEAK', 'stweak.py', ''))
|
||||||
arguments, replacement = argv[1:], None
|
|
||||||
|
|
||||||
# define replacement text depending on arguments
|
|
||||||
if len(arguments) > 0:
|
|
||||||
if arguments[0] == 'TMP':
|
|
||||||
replacement = 'symlink("/tmp", f"{home}/.config/sshyp/tmp")'
|
|
||||||
elif arguments[0] == 'TERMUX':
|
|
||||||
replacement = 'symlink("/data/data/com.termux/files/usr/tmp", f"{home}/.config/sshyp/tmp")\n'
|
|
||||||
elif arguments[0] == 'LINUX':
|
|
||||||
replacement = 'symlink("/dev/shm", f"{home}/.config/sshyp/tmp")'
|
|
||||||
else:
|
|
||||||
s_exit()
|
|
||||||
|
|
||||||
targets = (('UNAME-IMPORT-SSHYP', 'sshyp.py', ''), ('UNAME-IMPORT-STWEAK', 'stweak.py', ''),
|
|
||||||
('UNAME-TMP', 'stweak.py', replacement))
|
|
||||||
|
|
||||||
for target in targets:
|
for target in targets:
|
||||||
# read text from target file
|
# read text from target file
|
||||||
text, special = open(f"working/{target[1]}", 'r').read(), ''
|
text, special = open(f"working/{target[1]}", 'r').read(), ''
|
||||||
# determine text to end regex with
|
|
||||||
if target[2] == '':
|
|
||||||
regend = '\n'
|
|
||||||
else:
|
|
||||||
regend = ''
|
|
||||||
# compile regex and modify text
|
# compile regex and modify text
|
||||||
regex = re.compile(f"# PORT START {target[0]}.*?# PORT END {target[0]}{regend}", re.DOTALL)
|
regex = re.compile(f"# PORT START {target[0]}.*?# PORT END {target[0]}\n", re.DOTALL)
|
||||||
new_text = re.sub(regex, target[2], text)
|
new_text = re.sub(regex, target[2], text)
|
||||||
# write updated text to target file
|
# write updated text to target file
|
||||||
open(f"working/{target[1]}", 'w').write(new_text)
|
open(f"working/{target[1]}", 'w').write(new_text)
|
||||||
|
|||||||
Reference in New Issue
Block a user