From 4df69b7842aa728d86deb27ed19693c4f8d54e0c Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 28 Sep 2019 00:37:24 -0500 Subject: [PATCH] work on sites api for finding user sites --- onionr/filepaths/__init__.py | 2 ++ onionr/httpapi/onionrsitesapi/__init__.py | 14 +++++++++++++- onionr/httpapi/onionrsitesapi/findsite.py | 10 ++++++++++ onionr/utils/safezip.py | 7 +++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 onionr/httpapi/onionrsitesapi/findsite.py create mode 100644 onionr/utils/safezip.py diff --git a/onionr/filepaths/__init__.py b/onionr/filepaths/__init__.py index ae35889c..22120621 100644 --- a/onionr/filepaths/__init__.py +++ b/onionr/filepaths/__init__.py @@ -19,6 +19,8 @@ upload_list = home + 'upload-list.json' config_file = home + 'config.json' daemon_mark_file = app_root + '/daemon-true.txt' +site_cache = home + 'onionr-sites.txt' + tor_hs_address_file = home + 'hs/hostname' run_check_file = home + '.runcheck' diff --git a/onionr/httpapi/onionrsitesapi/__init__.py b/onionr/httpapi/onionrsitesapi/__init__.py index c130587e..48e7c116 100644 --- a/onionr/httpapi/onionrsitesapi/__init__.py +++ b/onionr/httpapi/onionrsitesapi/__init__.py @@ -19,10 +19,16 @@ ''' import base64 import binascii + +import unpaddedbase32 + from flask import Blueprint, Response, request, abort + from onionrblocks import onionrblockapi import onionrexceptions from onionrutils import stringvalidators +from utils import safezip +from onionrutils import mnemonickeys site_api = Blueprint('siteapi', __name__) @@ -30,6 +36,12 @@ site_api = Blueprint('siteapi', __name__) def site(name): bHash = name resp = 'Not Found' + if '-' in name: + name = mnemonickeys.get_base32(name) + if stringvalidators.validate_pub_key(name): + name = unpaddedbase32.repad(name) + + if stringvalidators.validate_hash(bHash): try: resp = onionrblockapi.Block(bHash).bcontent @@ -43,4 +55,4 @@ def site(name): pass if resp == 'Not Found' or not resp: abort(404) - return Response(resp) \ No newline at end of file + return Response(resp) diff --git a/onionr/httpapi/onionrsitesapi/findsite.py b/onionr/httpapi/onionrsitesapi/findsite.py new file mode 100644 index 00000000..3a6303df --- /dev/null +++ b/onionr/httpapi/onionrsitesapi/findsite.py @@ -0,0 +1,10 @@ +import onionrexceptions +from onionrutils import mnemonickeys +from onionrutils import stringvalidators + +def find_site(user_id: str)->str: + if '-' in user_id: user_id = mnemonickeys.get_base32(user_id) + if not stringvalidators.validate_pub_key(user_id): raise onionrexceptions.InvalidPubkey + + #for + \ No newline at end of file diff --git a/onionr/utils/safezip.py b/onionr/utils/safezip.py new file mode 100644 index 00000000..8cc87600 --- /dev/null +++ b/onionr/utils/safezip.py @@ -0,0 +1,7 @@ +# safe unzip https://stackoverflow.com/a/36583849 +def safe_unzip(zip_file, extractpath='.'): + with zipfile.ZipFile(zip_file, 'r') as zf: + for member in zf.infolist(): + abspath = os.path.abspath(os.path.join(extractpath, member.filename)) + if abspath.startswith(os.path.abspath(extractpath)): + zf.extract(member, extractpath)