+ added check for Tor to be ready before openhome works

* fixed not being able to stop Onionr when awaiting onboarding survey to be finished
This commit is contained in:
Kevin Froman 2020-02-11 19:49:44 -06:00
parent 3bb51a16e1
commit 353b2f1c63
5 changed files with 44 additions and 12 deletions

View File

@ -73,6 +73,9 @@ Please note: endpoints that simply provide static web app files are not document
* /insertblock * /insertblock
- Methods: POST - Methods: POST
- Accepts JSON data for creating a new block. 'message' contains the block data, 'to' specifies the peer's public key to encrypt the data to, 'sign' is a boolean for signing the message. - Accepts JSON data for creating a new block. 'message' contains the block data, 'to' specifies the peer's public key to encrypt the data to, 'sign' is a boolean for signing the message.
* /torready
- Methods: POST
- Returns boolean if Tor is started or not
# Public API # Public API

View File

@ -244,7 +244,10 @@ class OnionrCommunicatorDaemon:
while not config.get('onboarding.done', True) and \ while not config.get('onboarding.done', True) and \
not self.shutdown: not self.shutdown:
try:
time.sleep(2) time.sleep(2)
except KeyboardInterrupt:
self.shutdown = True
# Main daemon loop, mainly for calling timers, # Main daemon loop, mainly for calling timers,
# don't do any complex operations here to avoid locking # don't do any complex operations here to avoid locking

View File

@ -7,8 +7,8 @@ import subprocess
import platform import platform
from flask import Response, Blueprint, request, send_from_directory, abort from flask import Response, Blueprint, request, send_from_directory, abort
from flask import g
from gevent import spawn from gevent import spawn
from gevent import sleep
import unpaddedbase32 import unpaddedbase32
from httpapi import apiutils from httpapi import apiutils
@ -126,7 +126,7 @@ class PrivateEndpoints:
@private_endpoints_bp.route('/gettorsocks') @private_endpoints_bp.route('/gettorsocks')
def get_tor_socks(): def get_tor_socks():
return Response(str(client_api._too_many.get(NetController).socksPort)) return Response(str(g.too_many.get(NetController).socksPort))
@private_endpoints_bp.route('/setonboarding', methods=['POST']) @private_endpoints_bp.route('/setonboarding', methods=['POST'])
def set_onboarding(): def set_onboarding():
@ -136,3 +136,7 @@ class PrivateEndpoints:
def get_os_system(): def get_os_system():
return Response(platform.system().lower()) return Response(platform.system().lower())
@private_endpoints_bp.route('/torready')
def is_tor_ready():
"""If Tor is starting up, the web UI is not ready to be used."""
return Response(str(g.too_many.get(NetController).readyState).lower())

View File

@ -13,7 +13,6 @@ import platform # For windows sigkill workaround
from onionrtypes import BooleanSuccessState from onionrtypes import BooleanSuccessState
import logger import logger
import filepaths
from .. import getopenport from .. import getopenport
from .. import watchdog from .. import watchdog
from . import customtorrc from . import customtorrc
@ -109,10 +108,6 @@ class NetController:
logger.fatal('Got keyboard interrupt. Onionr will exit soon.', timestamp = False, terminal=True) logger.fatal('Got keyboard interrupt. Onionr will exit soon.', timestamp = False, terminal=True)
return False return False
logger.info('Finished starting Tor.', terminal=True)
self.readyState = True
try: try:
myID = open(self.dataDir + 'hs/hostname', 'r') myID = open(self.dataDir + 'hs/hostname', 'r')
self.myID = myID.read().replace('\n', '') self.myID = myID.read().replace('\n', '')
@ -125,6 +120,10 @@ class NetController:
multiprocessing.Process(target=watchdog.watchdog, multiprocessing.Process(target=watchdog.watchdog,
args=[os.getpid(), tor.pid]).start() args=[os.getpid(), tor.pid]).start()
logger.info('Finished starting Tor.', terminal=True)
self.readyState = True
return True return True
def killTor(self): def killTor(self):

View File

@ -3,9 +3,12 @@
Open the web interface properly into a web browser Open the web interface properly into a web browser
""" """
import webbrowser import webbrowser
from time import sleep
import logger import logger
from onionrutils import getclientapiserver from onionrutils import getclientapiserver
import config import config
from onionrutils.localcommand import local_command
""" """
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -22,8 +25,27 @@ import config
""" """
def _tell_if_ui_not_ready():
if local_command('/torready') != 'true':
logger.warn('The UI is not ready yet, waiting on Tor to start.', terminal=True)
def _wait_for_ui_to_be_ready():
if config.get('general.offline_mode', False) or \
not config.get('transports.tor', True) or \
config.get('tor.use_existing_tor'):
return
_tell_if_ui_not_ready()
while local_command('/torready') != 'true':
sleep(0.5)
logger.info("Tor is ready, opening UI", terminal=True)
def get_url() -> str: def get_url() -> str:
"""Build UI URL string and return it.""" """Build UI URL string and return it."""
onboarding = ""
if not config.get('onboarding.done', False):
onboarding = "onboarding/"
try: try:
url = getclientapiserver.get_client_API_server() url = getclientapiserver.get_client_API_server()
except FileNotFoundError: except FileNotFoundError:
@ -32,7 +54,7 @@ def get_url() -> str:
'Onionr seems to not be running (could not get api host)', 'Onionr seems to not be running (could not get api host)',
terminal=True) terminal=True)
else: else:
url = 'http://%s/#%s' % (url, config.get('client.webpassword')) url = 'http://%s/%s#%s' % (url, onboarding, config.get('client.webpassword'))
logger.info('Onionr web interface URL: ' + url, terminal=True) logger.info('Onionr web interface URL: ' + url, terminal=True)
return url return url
@ -50,7 +72,8 @@ def open_home():
'Onionr seems to not be running (could not get api host)', 'Onionr seems to not be running (could not get api host)',
terminal=True) terminal=True)
else: else:
url = 'http://%s/#%s' % (url, config.get('client.webpassword')) _wait_for_ui_to_be_ready()
url = get_url()
logger.info( logger.info(
'If Onionr does not open automatically, use this URL: ' + url, 'If Onionr does not open automatically, use this URL: ' + url,
terminal=True) terminal=True)