From 04133035ba9284c6ee9e7f11a1bd2f70d10a7996 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 7 Mar 2020 18:51:39 -0600 Subject: [PATCH] fix existing tor daemon not having closed ephemeral onions and work on mail test --- src/filepaths/__init__.py | 2 + src/netcontroller/__init__.py | 4 +- src/netcontroller/cleanephemeral.py | 40 +++++++++++++++++++ .../torcontrol/onionservicecreator.py | 7 +++- src/onionrcommands/daemonlaunch/__init__.py | 3 ++ src/onionrcommands/onionrstatistics.py | 1 + src/utils/gethostname.py | 1 - tests/browser-tests/test-mail.py | 19 +++++++++ 8 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 src/netcontroller/cleanephemeral.py diff --git a/src/filepaths/__init__.py b/src/filepaths/__init__.py index c104f8be..2870f859 100644 --- a/src/filepaths/__init__.py +++ b/src/filepaths/__init__.py @@ -34,3 +34,5 @@ keys_file = home + 'keys.txt' onboarding_mark_file = home + 'onboarding-completed' log_file = home + 'onionr.log' + +ephemeral_services_file = home + 'ephemeral-services.list' diff --git a/src/netcontroller/__init__.py b/src/netcontroller/__init__.py index bf912b74..5ca3e5ac 100755 --- a/src/netcontroller/__init__.py +++ b/src/netcontroller/__init__.py @@ -1,5 +1,7 @@ from . import getopenport, torcontrol from . import torcontrol +from . import cleanephemeral tor_binary = torcontrol.torbinary.tor_binary get_open_port = getopenport.get_open_port -NetController = torcontrol.NetController \ No newline at end of file +NetController = torcontrol.NetController +clean_ephemeral_services = cleanephemeral.clean_ephemeral_services \ No newline at end of file diff --git a/src/netcontroller/cleanephemeral.py b/src/netcontroller/cleanephemeral.py new file mode 100644 index 00000000..48e05c03 --- /dev/null +++ b/src/netcontroller/cleanephemeral.py @@ -0,0 +1,40 @@ +"""Onionr - Private P2P Communication. + +Remove ephemeral services +""" +from filenuke.nuke import clean + +from onionrutils.stringvalidators import validate_transport +from filepaths import ephemeral_services_file + +from netcontroller.torcontrol.torcontroller import get_controller +""" + 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 . +""" + + +def clean_ephemeral_services(): + """Remove transport's ephemeral services from respective controllers""" + try: + with open(ephemeral_services_file, 'r') as services: + services = services.readlines() + with get_controller() as torcontroller: + for hs in services: + hs += '.onion' + if validate_transport(hs): + torcontroller.remove_ephemeral_hidden_service(hs) + except FileNotFoundError: + pass + else: + clean(ephemeral_services_file) diff --git a/src/netcontroller/torcontrol/onionservicecreator.py b/src/netcontroller/torcontrol/onionservicecreator.py index 0bbe5d3d..2c2a0ea2 100644 --- a/src/netcontroller/torcontrol/onionservicecreator.py +++ b/src/netcontroller/torcontrol/onionservicecreator.py @@ -3,6 +3,8 @@ Create an ephemeral onion service """ from .torcontroller import get_controller + +from filepaths import ephemeral_services_file """ 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 @@ -19,7 +21,7 @@ from .torcontroller import get_controller """ -def create_onion_service(port=80): +def create_onion_service(port=80, record_to_service_removal_file=True): controller = get_controller() hs = controller.create_ephemeral_hidden_service( {80: port}, @@ -27,4 +29,7 @@ def create_onion_service(port=80): key_content = 'ED25519-V3', await_publication=True, detached=True) + if record_to_service_removal_file: + with open(ephemeral_services_file, 'a') as service_file: + service_file.write(hs.service_id + '\n') return (hs.service_id, hs.private_key) diff --git a/src/onionrcommands/daemonlaunch/__init__.py b/src/onionrcommands/daemonlaunch/__init__.py index 10e58d98..954344fc 100755 --- a/src/onionrcommands/daemonlaunch/__init__.py +++ b/src/onionrcommands/daemonlaunch/__init__.py @@ -23,6 +23,7 @@ import communicator from onionrplugins import onionrevents as events from netcontroller import NetController from netcontroller import get_open_port +from netcontroller import clean_ephemeral_services from onionrutils import localcommand from utils import identifyhome import filepaths @@ -163,6 +164,8 @@ def daemon(): events.event('daemon_start') communicator.startCommunicator(shared_state) + clean_ephemeral_services() + if not offline_mode and not use_existing_tor: net.killTor() else: diff --git a/src/onionrcommands/onionrstatistics.py b/src/onionrcommands/onionrstatistics.py index fe734141..af9afbce 100755 --- a/src/onionrcommands/onionrstatistics.py +++ b/src/onionrcommands/onionrstatistics.py @@ -124,6 +124,7 @@ def show_details(): active user ID in mnemonic form """ details = { + 'Data directory': identifyhome.identify_home(), 'Node Address': gethostname.get_hostname(), 'Public Key': onionrcrypto.pub_key.replace('=', ''), 'Human-readable Public Key': mnemonickeys.get_human_readable_ID() diff --git a/src/utils/gethostname.py b/src/utils/gethostname.py index 42652fa1..4a294218 100644 --- a/src/utils/gethostname.py +++ b/src/utils/gethostname.py @@ -21,7 +21,6 @@ from . import identifyhome import filepaths def get_hostname(): try: - print(identifyhome.identify_home()) with open(identifyhome.identify_home() + '/hs/hostname', 'r') as hostname: return hostname.read().strip() except FileNotFoundError: diff --git a/tests/browser-tests/test-mail.py b/tests/browser-tests/test-mail.py index d2c35f90..17a7ddcd 100644 --- a/tests/browser-tests/test-mail.py +++ b/tests/browser-tests/test-mail.py @@ -47,4 +47,23 @@ class OnionrTests(unittest.TestCase): Popen(['./onionr.sh', 'stop']).wait() web_driver.quit() + def test_mail(self): + Popen(['./onionr.sh', 'start']) + while b'http' not in Popen(['./onionr.sh', 'url'], stdout=subprocess.PIPE).communicate()[0]: + sleep(1) + + url = 'http' + escapeansi.escape_ANSI(Popen(['./onionr.sh', 'url'], stdout=subprocess.PIPE).communicate()[0].decode().split('http')[1]) + web_driver = start_firefox(url=url, headless=BROWSER_HEADLESS) + if not Text('Mail').exists(): + click('Get Started') + sleep(2) + click('Mail') + sleep(5) + if not Text('Mail').exists(): + Popen(['./onionr.sh', 'stop']).wait() + web_driver.quit() + raise ValueError + Popen(['./onionr.sh', 'stop']).wait() + web_driver.quit() + unittest.main()