From 1f067f614a91d5b3fb76be771ea6ef22e2899b6c Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 15 Sep 2020 17:08:50 +0000 Subject: [PATCH] Added tor restart button to index and tor restart command. Enabled tor restart daemon event --- src/communicator/daemoneventhooks/__init__.py | 4 +- src/communicatorutils/netcheck.py | 2 +- src/communicatorutils/restarttor.py | 4 +- src/netcontroller/torcontrol/rebuildtor.py | 3 ++ src/onionrcommands/parser/arguments.py | 2 + src/onionrcommands/togglebootstrap.py | 1 - .../transports/tor/__init__.py | 6 ++- static-data/www/private/index.html | 7 ++++ static-data/www/shared/panel.js | 40 +++++++++++++++++++ 9 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/communicator/daemoneventhooks/__init__.py b/src/communicator/daemoneventhooks/__init__.py index 16050202..f43ac54f 100644 --- a/src/communicator/daemoneventhooks/__init__.py +++ b/src/communicator/daemoneventhooks/__init__.py @@ -67,14 +67,16 @@ def daemon_event_handlers(shared_state: 'TooMany'): return "removed" def restart_tor(): - restarttor.restart(comm_inst) + restarttor.restart(shared_state) kv.put('offlinePeers', []) + kv.put('onlinePeers', []) def test_runtime(): Thread(target=comm_inst.shared_state.get_by_string( "OnionrRunTestManager").run_tests).start() events_api.register_listener(remove_from_insert_queue_wrapper) + events_api.register_listener(restart_tor) events_api.register_listener(print_test) events_api.register_listener(upload_event) events_api.register_listener(test_runtime) diff --git a/src/communicatorutils/netcheck.py b/src/communicatorutils/netcheck.py index b4a5ddf6..df3d8c0e 100755 --- a/src/communicatorutils/netcheck.py +++ b/src/communicatorutils/netcheck.py @@ -53,7 +53,7 @@ def net_check(comm_inst): 'the Internet, and is Tor working? ' + 'This is usually temporary, but bugs and censorship can cause this to persist, in which case you should report it to beardog [at] mailbox.org', # noqa terminal=True) - restarttor.restart(comm_inst) + restarttor.restart(comm_inst.shared_state) kv.put('offlinePeers', []) kv.put('isOnline', False) else: diff --git a/src/communicatorutils/restarttor.py b/src/communicatorutils/restarttor.py index 9ffde83d..35388d05 100644 --- a/src/communicatorutils/restarttor.py +++ b/src/communicatorutils/restarttor.py @@ -21,8 +21,8 @@ import config """ -def restart(comm_inst): +def restart(shared_state): if not config.get('tor.use_existing_tor', False): - net = comm_inst.shared_state.get(netcontroller.NetController) + net = shared_state.get(netcontroller.NetController) net.killTor() net.startTor() diff --git a/src/netcontroller/torcontrol/rebuildtor.py b/src/netcontroller/torcontrol/rebuildtor.py index 8e366ae0..2367530a 100644 --- a/src/netcontroller/torcontrol/rebuildtor.py +++ b/src/netcontroller/torcontrol/rebuildtor.py @@ -30,3 +30,6 @@ def rebuild(): is_json=True, post_data={} ).get(10) + + +rebuild.onionr_help = "If Onionr is running and is managing its own Tor daemon, restart that daemon." diff --git a/src/onionrcommands/parser/arguments.py b/src/onionrcommands/parser/arguments.py index eba3add1..a0126fb0 100644 --- a/src/onionrcommands/parser/arguments.py +++ b/src/onionrcommands/parser/arguments.py @@ -19,6 +19,7 @@ from .. import motdcreator # cmd to generate new Onionr MOTDs from .. import sitecreator # cmd to create multi-page sites from .. import togglebootstrap # cmd to toggle bootstrap file usage from ..listsites import print_site_list # cmd to list list ids +from netcontroller.torcontrol.rebuildtor import rebuild as tor_restart import onionrexceptions from onionrutils import importnewblocks # func to import new blocks @@ -53,6 +54,7 @@ def get_arguments() -> dict: ('start', 'daemon'): daemonlaunch.start, ('stop', 'kill'): daemonlaunch.kill_daemon, ('restart',): restartonionr.restart, + ('restart-tor', 'restarttor'): tor_restart, ('add-address', 'addaddress', 'addadder'): keyadders.add_address, ('openhome', 'gui', 'openweb', 'open-home', 'open-web'): openwebinterface.open_home, diff --git a/src/onionrcommands/togglebootstrap.py b/src/onionrcommands/togglebootstrap.py index 0e25bf43..3970f962 100644 --- a/src/onionrcommands/togglebootstrap.py +++ b/src/onionrcommands/togglebootstrap.py @@ -24,7 +24,6 @@ import logger def toggle_bootstrap_config(): """Toggles the bootstrap configuration.""" - print("test") if config.get('general.use_bootstrap_list') is None: logger.error('No general.bootstrap_list setting found') sys.exit(3) diff --git a/src/onionrstatistics/transports/tor/__init__.py b/src/onionrstatistics/transports/tor/__init__.py index cb98f91a..76446508 100644 --- a/src/onionrstatistics/transports/tor/__init__.py +++ b/src/onionrstatistics/transports/tor/__init__.py @@ -1,10 +1,11 @@ """Onionr - Private P2P Communication. - +tor stats info """ import ujson as json from stem import CircStatus +import logger from netcontroller.torcontrol.torcontroller import get_controller """ @@ -37,6 +38,9 @@ class TorStats: """ if self.controller is None: self.controller = get_controller() + if not self.controller.is_alive(): + logger.info(f'{__name__} reconnecting to tor control') + self.controller = get_controller() self.get_circuits() json_serialized = {} for circuit in self.circuits.keys(): diff --git a/static-data/www/private/index.html b/static-data/www/private/index.html index 0cb16ad8..031c893b 100755 --- a/static-data/www/private/index.html +++ b/static-data/www/private/index.html @@ -12,6 +12,7 @@ + @@ -274,6 +275,12 @@
Unable to get nodes

+
+

+ Restart Tor +

+
+

Blocks

diff --git a/static-data/www/shared/panel.js b/static-data/www/shared/panel.js index 4e80c0b8..8ab507cd 100755 --- a/static-data/www/shared/panel.js +++ b/static-data/www/shared/panel.js @@ -20,6 +20,46 @@ shutdownBtn = document.getElementById('shutdownNode') restartBtn = document.getElementById('restartNode') + +let restartTorBtnControl = function(){ + if (typeof config == "undefined" || typeof config.tor == "undefined"){ + setTimeout(function(){restartTorBtnControl()}, 10) + return + } + var restartTor = document.getElementsByClassName('restartTor')[0] + + if (config.tor.use_existing_tor){ + restartTor.classList.add('is-hidden') + return + } + + restartTor.onclick = function(){ + if (restartTor.disabled){ + console.debug("Tor still restarting (or restart_tor event failed)") + return + } + restartTor.disabled = true + PNotify.success({ + text: 'Initializing Tor restart...' + }) + fetch('/daemon-event/restart_tor', { + method: 'POST', + body: {}, + headers: { + "content-type": "application/json", + "token": webpass + }}) + .then((resp) => resp.text()) + .then(function(data) { + PNotify.success({ + text: 'Tor restarting...' + }) + restartTor.disabled = false + }) + } +} +restartTorBtnControl() + shutdownBtn.onclick = function(){ if (! nowebpass){ if (confirm("Really shutdown Onionr?")){