From 315c7f1e934d49b1a424468e908d0b272710348d Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Mon, 14 Dec 2015 18:15:05 -0500 Subject: [PATCH] tor: Notify firewall about socks service status --- plinth/modules/tor/__init__.py | 14 +++++++-- plinth/modules/tor/views.py | 55 ++++++++++++++-------------------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/plinth/modules/tor/__init__.py b/plinth/modules/tor/__init__.py index 611b0be25..42d47a6ac 100644 --- a/plinth/modules/tor/__init__.py +++ b/plinth/modules/tor/__init__.py @@ -27,12 +27,15 @@ import itertools from plinth import actions from plinth import action_utils from plinth import cfg +from plinth import service as service_module from plinth.modules.names import SERVICES from plinth.signals import domain_added depends = ['plinth.modules.apps', 'plinth.modules.names'] +service = None + APT_SOURCES_URI_PATHS = ('/files/etc/apt/sources.list/*/uri', '/files/etc/apt/sources.list.d/*/*/uri') APT_TOR_PREFIX = 'tor+' @@ -44,14 +47,19 @@ def init(): menu.add_urlname(_('Anonymity Network (Tor)'), 'glyphicon-eye-close', 'tor:index', 100) + global service + service = service_module.Service( + 'tor-socks', _('Tor Anonymity Network'), + is_external=False, enabled=is_enabled()) + # Register hidden service name with Name Services module. (hs_enabled, hs_hostname, hs_ports) = get_hs() if is_enabled() and is_running() and hs_enabled and hs_hostname: hs_services = [] - for service in SERVICES: - if str(service[2]) in hs_ports: - hs_services.append(service[0]) + for service_type in SERVICES: + if str(service_type[2]) in hs_ports: + hs_services.append(service_type[0]) else: hs_hostname = None hs_services = None diff --git a/plinth/modules/tor/views.py b/plinth/modules/tor/views.py index 15f51a1d2..394c82314 100644 --- a/plinth/modules/tor/views.py +++ b/plinth/modules/tor/views.py @@ -25,7 +25,6 @@ from django.utils.translation import ugettext_lazy as _ from .forms import TorForm from plinth import actions -from plinth import action_utils from plinth import package from plinth.errors import ActionError from plinth.modules import tor @@ -37,6 +36,7 @@ def on_install(): """Setup Tor configuration as soon as it is installed.""" actions.superuser_run('tor', ['setup']) actions.superuser_run('tor', ['enable-apt-transport-tor']) + tor.service.notify_enabled(None, True) @package.required(['tor', 'tor-geoipdb', 'torsocks', 'obfs4proxy', @@ -75,38 +75,38 @@ def _apply_changes(request, old_status, new_status): def __apply_changes(request, old_status, new_status): """Apply the changes.""" - if old_status['enabled'] == new_status['enabled'] and \ - old_status['hs_enabled'] == new_status['hs_enabled'] and \ - old_status['apt_transport_tor_enabled'] == \ - new_status['apt_transport_tor_enabled']: - messages.info(request, _('Setting unchanged')) - return + modified = False if old_status['enabled'] != new_status['enabled']: - if new_status['enabled']: - actions.superuser_run('tor', ['enable']) - messages.success(request, _('Tor enabled')) - else: - actions.superuser_run('tor', ['disable']) - messages.success(request, _('Tor disabled')) + sub_command = 'enable' if new_status['enabled'] else 'disable' + actions.superuser_run('tor', [sub_command]) + tor.service.notify_enabled(None, new_status['enabled']) + modified = True if old_status['hs_enabled'] != new_status['hs_enabled']: - if new_status['hs_enabled']: - actions.superuser_run('tor', ['enable-hs']) - messages.success(request, _('Tor hidden service enabled')) - else: - actions.superuser_run('tor', ['disable-hs']) - messages.success(request, _('Tor hidden service disabled')) + sub_command = 'enable-hs' if new_status['hs_enabled'] else 'disable-hs' + actions.superuser_run('tor', [sub_command]) + modified = True + + if old_status['apt_transport_tor_enabled'] != \ + new_status['apt_transport_tor_enabled']: + sub_command = 'enable-apt-transport-tor' \ + if new_status['apt_transport_tor_enabled'] \ + else 'disable-apt-transport-tor' + actions.superuser_run('tor', [sub_command]) + modified = True + + if modified: + messages.success(request, _('Configuration updated')) + else: + messages.info(request, _('Setting unchanged')) # Update hidden service name registered with Name Services module. domain_removed.send_robust( sender='tor', domain_type='hiddenservice') - enabled = action_utils.service_is_enabled('tor') - is_running = action_utils.service_is_running('tor') (hs_enabled, hs_hostname, hs_ports) = tor.get_hs() - - if enabled and is_running and hs_enabled and hs_hostname: + if tor.is_enabled() and tor.is_running() and hs_enabled and hs_hostname: hs_services = [] for service in SERVICES: if str(service[2]) in hs_ports: @@ -116,12 +116,3 @@ def __apply_changes(request, old_status, new_status): sender='tor', domain_type='hiddenservice', name=hs_hostname, description=_('Tor Hidden Service'), services=hs_services) - - if old_status['apt_transport_tor_enabled'] != \ - new_status['apt_transport_tor_enabled']: - if new_status['apt_transport_tor_enabled']: - actions.superuser_run('tor', ['enable-apt-transport-tor']) - messages.success(request, _('Enabled package download over Tor')) - else: - actions.superuser_run('tor', ['disable-apt-transport-tor']) - messages.success(request, _('Disabled package download over Tor'))