diff --git a/actions/tor b/actions/tor index 1964d318f..b6f9c723c 100755 --- a/actions/tor +++ b/actions/tor @@ -47,10 +47,8 @@ def parse_arguments(): subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') subparsers.add_parser('setup', help='Setup Tor configuration') - subparsers.add_parser('get-ports', - help='Get Tor ports in JSON format') - subparsers.add_parser('get-hs', - help='Get hidden service information in JSON format') + subparsers.add_parser('get-status', + help='Get Tor status in JSON format') configure = subparsers.add_parser('configure', help='Configure Tor') configure.add_argument('--service', choices=['enable', 'disable'], @@ -115,14 +113,9 @@ def subcommand_setup(_): time.sleep(10) -def subcommand_get_ports(_): - """Get Tor ports in JSON format.""" - print(json.dumps(get_ports())) - - -def subcommand_get_hs(_): - """Get hidden service information in JSON format.""" - print(json.dumps(get_hidden_service())) +def subcommand_get_status(_): + """Get Tor status in JSON format.""" + print(json.dumps(get_status())) def subcommand_configure(arguments): @@ -145,6 +138,11 @@ def subcommand_configure(arguments): _disable_apt_transport_tor() +def get_status(): + """Return dict with Tor status.""" + return {'ports': get_ports(), 'hidden_service': get_hidden_service()} + + def get_ports(): """Return dict mapping port names to numbers.""" ports = {} diff --git a/plinth/modules/tor/__init__.py b/plinth/modules/tor/__init__.py index d1e58c942..248239d7f 100644 --- a/plinth/modules/tor/__init__.py +++ b/plinth/modules/tor/__init__.py @@ -73,12 +73,12 @@ def init(): is_running=utils.is_running) # Register hidden service name with Name Services module. - hs_info = utils.get_hs() - hostname = hs_info['hostname'] - hs_virtports = [port['virtport'] for port in hs_info['ports']] + status = utils.get_status() + hostname = status['hs_hostname'] + hs_virtports = [port['virtport'] for port in status['hs_ports']] if utils.is_enabled() and utils.is_running() and \ - hs_info['enabled'] and hs_info['hostname']: + status['hs_enabled'] and status['hs_hostname']: hs_services = [] for service_type in SERVICES: if str(service_type[2]) in hs_virtports: @@ -133,8 +133,8 @@ def diagnose(): results.extend(_diagnose_control_port()) - output = actions.superuser_run('tor', ['get-ports']) - ports = json.loads(output) + output = actions.superuser_run('tor', ['get-status']) + ports = json.loads(output)['ports'] results.append([_('Tor relay port available'), 'passed' if 'orport' in ports else 'failed']) diff --git a/plinth/modules/tor/tests/test_tor.py b/plinth/modules/tor/tests/test_tor.py index 390f2e7f8..bb4e4b23d 100644 --- a/plinth/modules/tor/tests/test_tor.py +++ b/plinth/modules/tor/tests/test_tor.py @@ -36,15 +36,6 @@ class TestTor(unittest.TestCase): """ utils._is_apt_transport_tor_enabled() - @unittest.skipUnless(euid == 0, 'Needs to be root') - def test_get_hs(self): - """Test that get_hs does not raise any unhandled exceptions. - - This should work regardless of whether tor is installed, or - /etc/tor/torrc exists. - """ - utils.get_hs() - @unittest.skipUnless(euid == 0, 'Needs to be root') def test_get_status(self): """Test that get_status does not raise any unhandled exceptions. diff --git a/plinth/modules/tor/utils.py b/plinth/modules/tor/utils.py index 76b41b048..b35ad4d90 100644 --- a/plinth/modules/tor/utils.py +++ b/plinth/modules/tor/utils.py @@ -44,18 +44,13 @@ def is_running(): return action_utils.service_is_running('tor') -def get_hs(): - """Return hidden service status.""" - output = actions.superuser_run('tor', ['get-hs']) - return json.loads(output) - - def get_status(): """Return current Tor status.""" - output = actions.superuser_run('tor', ['get-ports']) - ports = json.loads(output) + output = actions.superuser_run('tor', ['get-status']) + status = json.loads(output) + ports = status['ports'] - hs_info = get_hs() + hs_info = status['hidden_service'] hs_services = [] hs_virtports = [port['virtport'] for port in hs_info['ports']] for service_type in SERVICES: