mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-06-10 11:00:22 +00:00
tor: Use single command to get status
Merge get_ports and get_hs into get_status.
This commit is contained in:
parent
9e9693c990
commit
1c98d1272f
22
actions/tor
22
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 = {}
|
||||
|
||||
@ -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'])
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user