added site creator command

This commit is contained in:
Kevin Froman 2019-11-04 04:52:38 -06:00
parent ff9bdc690e
commit 9fa05d6e1f
7 changed files with 61 additions and 12 deletions

View File

@ -1,4 +1,4 @@
from typing import Union from typing import Union, Tuple
import tarfile import tarfile
import io import io
import os import os
@ -6,8 +6,11 @@ import os
from coredb import blockmetadb from coredb import blockmetadb
from onionrblocks import onionrblockapi from onionrblocks import onionrblockapi
from onionrblocks import insert from onionrblocks import insert
from onionrtypes import UserID, DeterministicKeyPassphrase # Import types. Just for type hiting
from onionrcrypto import generate # Import types. Just for type hiting
from onionrtypes import UserID, DeterministicKeyPassphrase, BlockHash
from onionrcrypto import generate_deterministic
def find_site_gzip(user_id: str)->str: def find_site_gzip(user_id: str)->str:
sites = blockmetadb.get_blocks_by_type('osite') sites = blockmetadb.get_blocks_by_type('osite')
@ -25,8 +28,8 @@ def get_file(user_id, file)->Union[bytes, None]:
return site.extractfile(file) return site.extractfile(file)
return None return None
def create_site(admin_pass: DeterministicKeyPassphrase, directory:str='.')->UserID: def create_site(admin_pass: DeterministicKeyPassphrase, directory:str='.')->Tuple[UserID, BlockHash]:
public_key, private_key = generate.generate_deterministic(admin_pass) public_key, private_key = generate_deterministic(admin_pass)
raw_tar = io.BytesIO() raw_tar = io.BytesIO()
@ -36,6 +39,6 @@ def create_site(admin_pass: DeterministicKeyPassphrase, directory:str='.')->User
raw_tar.seek(0) raw_tar.seek(0)
insert.insert(raw_tar.read()) block_hash = insert(raw_tar.read(), signing_key=private_key)
return public_key return (public_key, block_hash)

View File

@ -26,7 +26,8 @@ def insert_block(data: Union[str, bytes], header: str ='txt',
our_private_key = crypto.priv_key our_private_key = crypto.priv_key
our_pub_key = crypto.pub_key our_pub_key = crypto.pub_key
if signingKey != '': if signing_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 = crypto.cryptoutils.get_pub_key_from_priv(our_private_key)

View File

@ -74,7 +74,9 @@ def register():
try: try:
if cmd != 'start': os.chdir(os.environ['ORIG_ONIONR_RUN_DIR']) if cmd != 'start': os.chdir(os.environ['ORIG_ONIONR_RUN_DIR'])
except KeyError: pass except KeyError: pass
try:
arguments.get_func(cmd)() arguments.get_func(cmd)()
except KeyboardInterrupt: pass
except onionrexceptions.NotFound: except onionrexceptions.NotFound:
if not register_plugin_commands(cmd) and not is_help_cmd: if not register_plugin_commands(cmd) and not is_help_cmd:
recommend.recommend() recommend.recommend()

View File

@ -30,7 +30,7 @@ from .. import softreset # command to delete onionr blocks
from .. import restartonionr # command to restart Onionr from .. import restartonionr # command to restart Onionr
from .. import runtimetestcmd from .. import runtimetestcmd
from .. import motdcreator from .. import motdcreator
from httpapi import onionrsitesapi from .. import sitecreator
import onionrexceptions import onionrexceptions
from onionrutils import importnewblocks # func to import new blocks from onionrutils import importnewblocks # func to import new blocks
@ -50,7 +50,7 @@ def get_arguments()->dict:
('openhome', 'gui', 'openweb', 'open-home', 'open-web'): openwebinterface.open_home, ('openhome', 'gui', 'openweb', 'open-home', 'open-web'): openwebinterface.open_home,
('get-url', 'url', 'get-web'): openwebinterface.get_url, ('get-url', 'url', 'get-web'): openwebinterface.get_url,
('addhtml', 'add-html'): filecommands.add_html, ('addhtml', 'add-html'): filecommands.add_html,
('addsite', 'add-site'): onionrsitesapi.sitefiles.create_site, ('addsite', 'add-site', 'update-site', 'updatesite'): sitecreator.create_multipage_site,
('addfile', 'add-file'): filecommands.add_file, ('addfile', 'add-file'): filecommands.add_file,
('get-file', 'getfile'): filecommands.get_file, ('get-file', 'getfile'): filecommands.get_file,
('export-block', 'exportblock'): exportblocks.export_block, ('export-block', 'exportblock'): exportblocks.export_block,

View File

@ -0,0 +1,40 @@
import sys
import getpass
from httpapi import onionrsitesapi
import onionrexceptions
import logger
from etc import onionrvalues
def create_multipage_site():
error_encountered = False
try:
directory = sys.argv[2]
except IndexError:
directory = '.'
try:
passphrase = sys.argv[3]
except IndexError:
logger.warn('''It is critical that this passphrase is long.
If you want to update your site later you must remember the passphrase.''', terminal=True)
logger.info(f'Please enter a site passphrase of at least {onionrvalues.PASSWORD_LENGTH} characters.', terminal=True)
passphrase = getpass.getpass()
logger.info('Confirm:', terminal=True)
confirm = getpass.getpass()
if passphrase != confirm:
logger.error('Passphrases do not match', terminal=True)
error_encountered = True
if len(passphrase) < onionrvalues.PASSWORD_LENGTH:
error_encountered = True
logger.error(f'Passphrase must be at least {onionrvalues.PASSWORD_LENGTH} characters.', terminal=True)
if error_encountered:
sys.exit(1)
results = onionrsitesapi.sitefiles.create_site(passphrase, directory=directory)
results = (results[0].replace('=', ''), results[1])
logger.info(f'Site address {results[0]}', terminal=True)
logger.info(f'Block for this version {results[1]}', terminal=True)
create_multipage_site.onionr_help = "[directory path (default relative)] - packages a whole directory and makes it available as an Onionr site."

View File

@ -6,6 +6,7 @@ import nacl.encoding, nacl.signing, nacl.exceptions
from onionrutils import bytesconverter from onionrutils import bytesconverter
from onionrutils import mnemonickeys from onionrutils import mnemonickeys
import logger import logger
def ed_sign(data, key, encodeResult=False): def ed_sign(data, key, encodeResult=False):
'''Ed25519 sign data''' '''Ed25519 sign data'''
key = unpaddedbase32.repad(bytesconverter.str_to_bytes(key)) key = unpaddedbase32.repad(bytesconverter.str_to_bytes(key))

View File

@ -4,3 +4,5 @@ UserID = NewType('UserID', str)
UserIDSecretKey = NewType('UserIDSecretKey', str) UserIDSecretKey = NewType('UserIDSecretKey', str)
DeterministicKeyPassphrase = NewType('DeterministicKeyPassphrase', str) DeterministicKeyPassphrase = NewType('DeterministicKeyPassphrase', str)
BlockHash = NewType('BlockHash', str)