onionr/src/onionrcommands/sitecreator.py

73 lines
2.5 KiB
Python
Raw Normal View History

"""Onionr - Private P2P Communication.
Command to create Onionr mutli-page sites
"""
2019-11-04 10:52:38 +00:00
import sys
import os
2019-11-04 10:52:38 +00:00
import getpass
from niceware import generate_passphrase
2019-11-04 10:52:38 +00:00
from httpapi import onionrsitesapi
import logger
from etc import onionrvalues
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
2019-11-04 10:52:38 +00:00
def create_multipage_site():
"""Command to create mutlipage sites with specified dir and password."""
2019-11-04 10:52:38 +00:00
error_encountered = False
orig_dir = os.getcwd()
2019-11-04 10:52:38 +00:00
try:
directory = sys.argv[2]
os.chdir(directory)
directory = '.'
2019-11-04 10:52:38 +00:00
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)
passphrase = "-".join(generate_passphrase(32))
print("Site restore phrase:", passphrase)
2019-11-04 10:52:38 +00:00
if len(passphrase) < onionrvalues.PASSWORD_LENGTH:
error_encountered = True
logger.error(
f'Passphrase must be at least {onionrvalues.PASSWORD_LENGTH}' +
' characters.', terminal=True)
2019-11-04 10:52:38 +00:00
if error_encountered:
2019-11-04 10:52:38 +00:00
sys.exit(1)
results = onionrsitesapi.sitefiles.create_site(
passphrase, directory=directory)
2019-11-04 10:52:38 +00:00
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)
os.chdir(orig_dir)
2019-11-04 10:52:38 +00:00
create_multipage_site.onionr_help = "[directory path " # type: ignore
create_multipage_site.onionr_help += "(default relative)] " # type: ignore
create_multipage_site.onionr_help += "- packages a whole " # type: ignore
create_multipage_site.onionr_help += "directory and makes " # type: ignore
create_multipage_site.onionr_help += "it available as " # type: ignore
create_multipage_site.onionr_help += "an Onionr site." # type: ignore