diff --git a/src/filepaths/__init__.py b/src/filepaths/__init__.py index 0942f7a2..55629ec8 100644 --- a/src/filepaths/__init__.py +++ b/src/filepaths/__init__.py @@ -22,6 +22,7 @@ lock_file = home + 'onionr.lock' site_cache = home + 'onionr-sites.txt' +tor_hs_loc = home + 'hs/' tor_hs_address_file = home + 'hs/hostname' run_check_file = home + '.runcheck' diff --git a/src/netcontroller/torcontrol/__init__.py b/src/netcontroller/torcontrol/__init__.py index ef9778cc..dc7ff370 100644 --- a/src/netcontroller/torcontrol/__init__.py +++ b/src/netcontroller/torcontrol/__init__.py @@ -12,7 +12,6 @@ import multiprocessing import platform # For windows sigkill workaround from onionrtypes import BooleanSuccessState -import config import logger from .. import getopenport from .. import watchdog @@ -36,7 +35,6 @@ from utils import identifyhome along with this program. If not, see . """ -config.reload() TOR_KILL_WAIT = 3 addbridges = addbridges.add_bridges diff --git a/src/netcontroller/torcontrol/onionservicecreator.py b/src/netcontroller/torcontrol/onionservicecreator.py index da1f3785..721da938 100644 --- a/src/netcontroller/torcontrol/onionservicecreator.py +++ b/src/netcontroller/torcontrol/onionservicecreator.py @@ -1,4 +1,30 @@ """Onionr - Private P2P Communication. Create an ephemeral onion service -""" \ No newline at end of file +""" +from .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 create_onion_service(port=80): + controller = get_controller() + hs = controller.create_ephemeral_hidden_service( + {80: port}, + key_type='NEW', + key_content='ED25519-V3', + await_publication=True, + detached=True) + return (hs.service_id, hs.private_key) diff --git a/src/netcontroller/torcontrol/torcontroller.py b/src/netcontroller/torcontrol/torcontroller.py index f3dba225..46149f4d 100644 --- a/src/netcontroller/torcontrol/torcontroller.py +++ b/src/netcontroller/torcontrol/torcontroller.py @@ -1,9 +1,16 @@ from stem.control import Controller import config +config.reload() -def get_controller(): - c = Controller.from_port(port=config.get('tor.controlPort')) - c.authenticate(config.get('tor.controlpassword')) +def get_controller() -> Controller: + """Create and return a Tor controller connection.""" + port = config.get('tor.controlPort', 0) + password = config.get('tor.controlpassword', '') + if config.get('tor.use_existing_tor', False): + port = config.get('tor.existing_control_port', 0) + password = config.get('tor.existing_control_password', '') + c = Controller.from_port(port=port) + c.authenticate(password) return c diff --git a/src/onionrcommands/daemonlaunch/__init__.py b/src/onionrcommands/daemonlaunch/__init__.py index 315e92c0..37b84930 100755 --- a/src/onionrcommands/daemonlaunch/__init__.py +++ b/src/onionrcommands/daemonlaunch/__init__.py @@ -20,6 +20,7 @@ import logger import communicator from onionrplugins import onionrevents as events from netcontroller import NetController +from netcontroller import get_open_port from onionrutils import localcommand import filepaths from etc import onionrvalues, cleanup @@ -29,7 +30,8 @@ import runtests from httpapi import daemoneventsapi from .. import version from .getapihost import get_api_host_until_available -from .bettersleep import better_sleep +from utils.bettersleep import better_sleep +from netcontroller.torcontrol.onionservicecreator import create_onion_service """ 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 @@ -105,14 +107,23 @@ def daemon(): shared_state.get(onionrstatistics.tor.TorStats) + security_level = config.get('general.security_level', 1) + use_existing_tor = config.get('tor.use_existing_tor', False) + if not offline_mode: - - logger.info('Tor is starting...', terminal=True) - if not net.startTor(): - localcommand.local_command('shutdown') - cleanup.delete_run_files() - sys.exit(1) - if len(net.myID) > 0 and config.get('general.security_level', 1) == 0: + + if use_existing_tor: + net.socksPort = config.get('tor.existing_socks_port') + net.myID = create_onion_service(port=get_open_port()) + with open(filepaths.tor_hs_address_file, 'w') as tor_file: + tor_file.write(net.myID) + else: + logger.info('Tor is starting...', terminal=True) + if not net.startTor(): + localcommand.local_command('shutdown') + cleanup.delete_run_files() + sys.exit(1) + if len(net.myID) > 0 and security_level == 0: logger.debug('Started .onion service: %s' % (logger.colors.underline + net.myID)) else: @@ -128,7 +139,7 @@ def daemon(): events.event('daemon_start') communicator.startCommunicator(shared_state) - if not offline_mode: + if not offline_mode and not use_existing_tor: net.killTor() better_sleep(5) diff --git a/src/onionrtypes/__init__.py b/src/onionrtypes/__init__.py index a8075498..f3ccf88f 100644 --- a/src/onionrtypes/__init__.py +++ b/src/onionrtypes/__init__.py @@ -15,3 +15,5 @@ JSONSerializable = NewType('JSONSerializable', str) # Return value of some functions or methods, denoting operation success # Do not use for new code BooleanSuccessState = NewType('BooleanSuccessState', bool) + +OnionAddressString = NewType('OnionAddressString', str) diff --git a/src/utils/createdirs.py b/src/utils/createdirs.py index 8f251763..d76f4842 100644 --- a/src/utils/createdirs.py +++ b/src/utils/createdirs.py @@ -22,10 +22,14 @@ from . import identifyhome import filepaths home = identifyhome.identify_home() + def create_dirs(): - """Creates onionr data-related directories in order of the hardcoded list below, + """Create onionr data-related directories in + order of the hardcoded list below, then trigger creation of DBs""" - gen_dirs = [home, filepaths.block_data_location, filepaths.contacts_location, filepaths.export_location] + gen_dirs = [home, filepaths.block_data_location, + filepaths.contacts_location, filepaths.export_location, + filepaths.tor_hs_loc] for path in gen_dirs: if not os.path.exists(path): os.mkdir(path) diff --git a/static-data/default_config.json b/static-data/default_config.json index 0c181b72..7b2589bb 100755 --- a/static-data/default_config.json +++ b/static-data/default_config.json @@ -55,9 +55,10 @@ "use_bridge": false, "bridge_ip": "", "bridge_fingerprint": "", + "use_existing_tor": false, "existing_control_port": 0, "existing_control_password": "", - "temp_transport": false + "existing_socks_port": 0 }, "allocations": {