sites now support multi pages

This commit is contained in:
Kevin Froman 2019-11-11 23:32:18 -06:00
parent 57e750a15a
commit 0cce0f4318
6 changed files with 45 additions and 10 deletions

View File

@ -59,8 +59,8 @@ setup.setup_config()
setup.setup_default_plugins() setup.setup_default_plugins()
def onionr_main(): def onionr_main():
"""Onionr entrypoint, start command processor"""
parser.register() parser.register()
return
if ran_as_script: if ran_as_script:
onionr_main() onionr_main()

View File

@ -34,7 +34,7 @@ from . import sitefiles
site_api = Blueprint('siteapi', __name__) site_api = Blueprint('siteapi', __name__)
@site_api.route('/site/<name>', endpoint='site') @site_api.route('/site/<name>/', endpoint='site')
def site(name: str)->Response: def site(name: str)->Response:
"""Accept a site 'name', if pubkey then show multi-page site, if hash show single page site""" """Accept a site 'name', if pubkey then show multi-page site, if hash show single page site"""
resp: str = 'Not Found' resp: str = 'Not Found'
@ -63,3 +63,33 @@ def site(name: str)->Response:
if resp == 'Not Found' or not resp: if resp == 'Not Found' or not resp:
abort(404) abort(404)
return Response(resp) return Response(resp)
@site_api.route('/site/<name>/<file>', endpoint='siteFile')
def site_file(name: str, file: str)->Response:
"""Accept a site 'name', if pubkey then show multi-page site, if hash show single page site"""
resp: str = 'Not Found'
mime_type = 'text/html'
# If necessary convert the name to base32 from mnemonic
if mnemonickeys.DELIMITER in name:
name = mnemonickeys.get_base32(name)
# Now make sure the key is regardless a valid base32 format ed25519 key (readding padding if necessary)
if stringvalidators.validate_pub_key(name):
name = unpaddedbase32.repad(name)
resp = sitefiles.get_file(name, file)
elif stringvalidators.validate_hash(name):
try:
resp = onionrblockapi.Block(name).bcontent
except onionrexceptions.NoDataAvailable:
abort(404)
except TypeError:
pass
try:
resp = base64.b64decode(resp)
except binascii.Error:
pass
if resp == 'Not Found' or not resp:
abort(404)
return Response(resp)

View File

@ -3,6 +3,8 @@ import tarfile
import io import io
import os import os
import unpaddedbase32
from coredb import blockmetadb from coredb import blockmetadb
from onionrblocks import onionrblockapi from onionrblocks import onionrblockapi
from onionrblocks import insert from onionrblocks import insert
@ -12,9 +14,11 @@ from onionrtypes import UserID, DeterministicKeyPassphrase, BlockHash
from onionrcrypto import generate_deterministic from onionrcrypto import generate_deterministic
def find_site_gzip(user_id: str)->str: def find_site_gzip(user_id: str)->tarfile.TarFile:
"""Return verified site tar object"""
sites = blockmetadb.get_blocks_by_type('osite') sites = blockmetadb.get_blocks_by_type('osite')
user_site = None user_site = None
user_id = unpaddedbase32.repad(user_id)
for site in sites: for site in sites:
block = onionrblockapi.Block(site) block = onionrblockapi.Block(site)
if block.isSigner(user_id): if block.isSigner(user_id):
@ -24,12 +28,13 @@ def find_site_gzip(user_id: str)->str:
return None return None
def get_file(user_id, file)->Union[bytes, None]: def get_file(user_id, file)->Union[bytes, None]:
"""Get a site file content"""
ret_data = "" ret_data = ""
site = find_site_gzip(user_id) site = find_site_gzip(user_id)
if site is None: return None if site is None: return None
for file in site.getmembers(): for t_file in site.getmembers():
if file.name == file: if t_file.name.replace('./', '') == file:
return site.extractfile(file) return site.extractfile(t_file)
return None return None
def create_site(admin_pass: DeterministicKeyPassphrase, directory:str='.')->Tuple[UserID, BlockHash]: def create_site(admin_pass: DeterministicKeyPassphrase, directory:str='.')->Tuple[UserID, BlockHash]:
@ -43,6 +48,6 @@ def create_site(admin_pass: DeterministicKeyPassphrase, directory:str='.')->Tupl
raw_tar.seek(0) raw_tar.seek(0)
block_hash = insert(raw_tar.read(), signing_key=private_key) block_hash = insert(raw_tar.read(), header='osite', signing_key=private_key, sign=True)
return (public_key, block_hash) return (public_key, block_hash)

View File

@ -24,7 +24,7 @@ from . import pluginwhitelist
# Be extremely mindful of this. These are endpoints available without a password # Be extremely mindful of this. These are endpoints available without a password
whitelist_endpoints = ['www', 'staticfiles.homedata', 'staticfiles.sharedContent', whitelist_endpoints = ['www', 'staticfiles.homedata', 'staticfiles.sharedContent',
'staticfiles.friends', 'staticfiles.friendsindex', 'siteapi.site', 'staticfiles.onionrhome', 'staticfiles.friends', 'staticfiles.friendsindex', 'siteapi.site', 'siteapi.siteFile', 'staticfiles.onionrhome',
'themes.getTheme', 'staticfiles.onboarding', 'staticfiles.onboardingIndex'] 'themes.getTheme', 'staticfiles.onboarding', 'staticfiles.onboardingIndex']
class ClientAPISecurity: class ClientAPISecurity:

View File

@ -29,7 +29,7 @@ def insert_block(data: Union[str, bytes], header: str ='txt',
if signing_key != '': if signing_key != '':
# if it was specified to use an alternative private key # if it was specified to use an alternative private key
our_private_key = signing_key our_private_key = signing_key
our_pub_key = crypto.cryptoutils.get_pub_key_from_priv(our_private_key) our_pub_key = bytesconverter.bytes_to_str(crypto.cryptoutils.get_pub_key_from_priv(our_private_key))
use_subprocess = powchoice.use_subprocess(config) use_subprocess = powchoice.use_subprocess(config)
storage_counter = storagecounter.StorageCounter() storage_counter = storagecounter.StorageCounter()