From 70001c841ff324646438240ff5c8cb6d201dcbc2 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 8 Jul 2016 16:19:52 +0530 Subject: [PATCH] tor: Minor improvements - Minimize loading of Augeas since it takes time. - Make some methods private - PEP8 fixes --- actions/tor | 40 +++++++++++---------- plinth/modules/tor/__init__.py | 3 +- plinth/modules/tor/templates/tor.html | 50 +++++++++++++-------------- plinth/modules/tor/utils.py | 14 ++++---- 4 files changed, 55 insertions(+), 52 deletions(-) diff --git a/actions/tor b/actions/tor index 23e16ffdd..b41f899b7 100755 --- a/actions/tor +++ b/actions/tor @@ -107,7 +107,7 @@ def subcommand_setup(_): # wait until hidden service information is available tries = 0 - while not get_hidden_service()['enabled']: + while not _get_hidden_service()['enabled']: tries += 1 if tries >= 12: return @@ -131,7 +131,7 @@ def subcommand_configure(arguments): elif arguments.relay == 'disable': _disable_relay(restart=restart) - restart = arguments.service == None + restart = arguments.service is None if arguments.hidden_service == 'enable': _enable_hs(restart=restart) elif arguments.hidden_service == 'disable': @@ -148,20 +148,20 @@ def subcommand_configure(arguments): def get_status(): """Return dict with Tor status.""" - return {'relay_enabled': is_relay_enabled(), - 'ports': get_ports(), - 'hidden_service': get_hidden_service()} - - -def is_relay_enabled(): - """Return whether bridge relay is enabled.""" aug = augeas_load() + return {'relay_enabled': _is_relay_enabled(aug), + 'ports': _get_ports(), + 'hidden_service': _get_hidden_service(aug)} + + +def _is_relay_enabled(aug): + """Return whether bridge relay is enabled.""" orport = aug.get(TOR_CONFIG + '/ORPort') bridge = aug.get(TOR_CONFIG + '/BridgeRelay') return orport == 'auto' and bridge == '1' -def get_ports(): +def _get_ports(): """Return dict mapping port names to numbers.""" ports = {} try: @@ -203,14 +203,16 @@ QUIT return matches.group(1) -def get_hidden_service(): +def _get_hidden_service(aug=None): """Return a string with configured Tor hidden service information""" hs_enabled = False hs_status = 'Ok' hs_hostname = None hs_ports = [] - aug = augeas_load() + if not aug: + aug = augeas_load() + hs_dir = aug.get(TOR_CONFIG + '/HiddenServiceDir') hs_port_paths = aug.match(TOR_CONFIG + '/HiddenServicePort') @@ -271,10 +273,11 @@ def _disable_relay(restart=False): def _enable_hs(restart=True): """Enable Tor hidden service""" - if get_hidden_service()['enabled']: + aug = augeas_load() + + if _get_hidden_service(aug)['enabled']: return - aug = augeas_load() aug.set(TOR_CONFIG + '/HiddenServiceDir', '/var/lib/tor/hidden_service') aug.set(TOR_CONFIG + '/HiddenServicePort[1]', @@ -291,7 +294,7 @@ def _enable_hs(restart=True): # wait until hidden service information is available tries = 0 - while not get_hidden_service()['enabled']: + while not _get_hidden_service()['enabled']: tries += 1 if tries >= 12: return @@ -301,10 +304,11 @@ def _enable_hs(restart=True): def _disable_hs(restart=True): """Disable Tor hidden service""" - if not get_hidden_service()['enabled']: + aug = augeas_load() + + if not _get_hidden_service(aug)['enabled']: return - aug = augeas_load() aug.remove(TOR_CONFIG + '/HiddenServiceDir') aug.remove(TOR_CONFIG + '/HiddenServicePort') aug.save() @@ -356,7 +360,7 @@ def _update_ports(): # port information may not be available immediately after Tor started while not ready: - ports = get_ports() + ports = _get_ports() ready = 'orport' in ports and 'obfs3' in ports and 'obfs4' in ports if ready: break diff --git a/plinth/modules/tor/__init__.py b/plinth/modules/tor/__init__.py index 248239d7f..502eaa3cb 100644 --- a/plinth/modules/tor/__init__.py +++ b/plinth/modules/tor/__init__.py @@ -77,7 +77,7 @@ def init(): hostname = status['hs_hostname'] hs_virtports = [port['virtport'] for port in status['hs_ports']] - if utils.is_enabled() and utils.is_running() and \ + if status['enabled'] and status['is_running'] and \ status['hs_enabled'] and status['hs_hostname']: hs_services = [] for service_type in SERVICES: @@ -120,7 +120,6 @@ def update_hidden_service_domain(status=None): services=status['hs_services']) - def diagnose(): """Run diagnostics and return the results.""" results = [] diff --git a/plinth/modules/tor/templates/tor.html b/plinth/modules/tor/templates/tor.html index f6a08870c..f07de42af 100644 --- a/plinth/modules/tor/templates/tor.html +++ b/plinth/modules/tor/templates/tor.html @@ -94,34 +94,34 @@ {% if status.relay_enabled %} -

{% trans "Bridge Relay" %}

-

- {% blocktrans trimmed %} - If your {{ box_name }} is behind a router or firewall, you should - make sure the following ports are open, and port-forwarded, if - necessary: - {% endblocktrans %} -

-
-
- - - - - - - - - {% for name, port in status.ports.items %} +

{% trans "Bridge Relay" %}

+

+ {% blocktrans trimmed %} + If your {{ box_name }} is behind a router or firewall, you should + make sure the following ports are open, and port-forwarded, if + necessary: + {% endblocktrans %} +

+
+
+
{% trans "Service" %}{% trans "Port" %}
+ - - + + - {% endfor %} - -
{{ name }}{{ port }}{% trans "Service" %}{% trans "Port" %}
+ + + {% for name, port in status.ports.items %} + + {{ name }} + {{ port }} + + {% endfor %} + + +
- {% endif %}

{% trans "SOCKS" %}

diff --git a/plinth/modules/tor/utils.py b/plinth/modules/tor/utils.py index dbc8afa5e..810376a5f 100644 --- a/plinth/modules/tor/utils.py +++ b/plinth/modules/tor/utils.py @@ -48,7 +48,6 @@ def get_status(): """Return current Tor status.""" output = actions.superuser_run('tor', ['get-status']) status = json.loads(output) - ports = status['ports'] hs_info = status['hidden_service'] hs_services = [] @@ -60,21 +59,21 @@ def get_status(): return {'enabled': is_enabled(), 'is_running': is_running(), 'relay_enabled': status['relay_enabled'], - 'ports': ports, + 'ports': status['ports'], 'hs_enabled': hs_info['enabled'], 'hs_status': hs_info['status'], 'hs_hostname': hs_info['hostname'], 'hs_ports': hs_info['ports'], 'hs_services': hs_services, - 'apt_transport_tor_enabled': \ - _is_apt_transport_tor_enabled() + 'apt_transport_tor_enabled': + _is_apt_transport_tor_enabled() } def iter_apt_uris(aug): """Iterate over all the APT source URIs.""" - return itertools.chain.from_iterable([aug.match(path) for \ - path in APT_SOURCES_URI_PATHS]) + return itertools.chain.from_iterable([aug.match(path) + for path in APT_SOURCES_URI_PATHS]) def get_real_apt_uri_path(aug, path): @@ -104,7 +103,8 @@ def get_augeas(): aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + augeas.Augeas.NO_MODL_AUTOLOAD) aug.set('/augeas/load/Aptsources/lens', 'Aptsources.lns') - aug.set('/augeas/load/Aptsources/incl[last() + 1]', '/etc/apt/sources.list') + aug.set('/augeas/load/Aptsources/incl[last() + 1]', + '/etc/apt/sources.list') aug.set('/augeas/load/Aptsources/incl[last() + 1]', '/etc/apt/sources.list.d/*.list') aug.load()