tor: Run configuration update as background task

- Closes: #294.
This commit is contained in:
James Valleroy 2015-12-16 21:47:18 -05:00 committed by Sunil Mohan Adapa
parent a22595af77
commit 07130b6007
No known key found for this signature in database
GPG Key ID: 36C361440C9BC971
3 changed files with 124 additions and 76 deletions

View File

@ -309,8 +309,6 @@ def _update_ports():
# port information may not be available immediately after Tor started
while not ready:
time.sleep(5)
ports = get_ports()
ready = 'orport' in ports and 'obfs3' in ports and 'obfs4' in ports
if ready:
@ -319,6 +317,7 @@ def _update_ports():
tries += 1
if tries >= 12:
return
time.sleep(10)
lines = """<?xml version="1.0" encoding="utf-8"?>
<service>

View File

@ -21,6 +21,15 @@
{% load bootstrap %}
{% load i18n %}
{% block page_head %}
{% if config_running %}
<meta http-equiv="refresh" content="3" />
{% endif %}
{% endblock %}
{% block content %}
<h2>{% trans "Anonymity Network (Tor)" %}</h2>
@ -38,89 +47,100 @@
<h3>{% trans "Status" %}</h3>
<p class="running-status-parent">
{% if status.is_running %}
{% if config_running %}
<p class="running-status-parent">
<span class="running-status active"></span>
{% trans "Tor is running" %}
{% else %}
<span class="running-status inactive"></span>
{% trans "Tor is not running" %}
{% trans "Tor configuration is being updated" %}
</p>
{% else %}
<p class="running-status-parent">
{% if status.is_running %}
<span class="running-status active"></span>
{% trans "Tor is running" %}
{% else %}
<span class="running-status inactive"></span>
{% trans "Tor is not running" %}
{% endif %}
</p>
{% include "diagnostics_button.html" with module="tor" %}
{% if status.hs_enabled %}
<div class="row">
<div class="col-sm-3">
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>{% trans "Hidden Service" %}</th>
<th>{% trans "Port" %}</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ status.hs_hostname }}</td>
<td>{{ status.hs_ports }}</td>
</tr>
</tbody>
</table>
</div>
</div>
{% endif %}
</p>
{% include "diagnostics_button.html" with module="tor" %}
<h3>{% trans "Configuration" %}</h3>
<form class="form" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary btn-md"
value="{% trans "Update setup" %}"/>
</form>
<h3>{% trans "Bridge" %}</h3>
<p>
{% blocktrans trimmed %}
Your {{ box_name }} is configured as a Tor bridge with obfsproxy,
so it can help circumvent censorship. 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 %}
</p>
{% if status.hs_enabled %}
<div class="row">
<div class="col-sm-3">
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>{% trans "Hidden Service" %}</th>
<th>{% trans "Service" %}</th>
<th>{% trans "Port" %}</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ status.hs_hostname }}</td>
<td>{{ status.hs_ports }}</td>
</tr>
{% for name, port in status.ports.items %}
<tr>
<td>{{ name }}</td>
<td>{{ port }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<h3>{% trans "SOCKS" %}</h3>
<p>
{% blocktrans trimmed %}
A Tor SOCKS port is available on your {{ box_name }} on TCP port
9050.
{% endblocktrans %}
</p>
{% endif %}
<h3>{% trans "Configuration" %}</h3>
<form class="form" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary btn-md"
value="{% trans "Update setup" %}"/>
</form>
<h3>{% trans "Bridge" %}</h3>
<p>
{% blocktrans trimmed %}
Your {{ box_name }} is configured as a Tor bridge with obfsproxy,
so it can help circumvent censorship. 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 %}
</p>
<div class="row">
<div class="col-sm-3">
<table class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>{% trans "Service" %}</th>
<th>{% trans "Port" %}</th>
</tr>
</thead>
<tbody>
{% for name, port in status.ports.items %}
<tr>
<td>{{ name }}</td>
<td>{{ port }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<h3>{% trans "SOCKS" %}</h3>
<p>
{% blocktrans trimmed %}
A Tor SOCKS port is available on your {{ box_name }} on TCP port
9050.
{% endblocktrans %}
</p>
{% endblock %}

View File

@ -31,6 +31,8 @@ from plinth.modules import tor
from plinth.modules.names import SERVICES
from plinth.signals import domain_added, domain_removed
config_process = None
def on_install():
"""Setup Tor configuration as soon as it is installed."""
@ -48,6 +50,9 @@ def index(request):
"""Serve configuration page."""
status = tor.get_status()
if config_process:
_collect_config_result(request, status)
form = None
if request.method == 'POST':
@ -63,6 +68,7 @@ def index(request):
return TemplateResponse(request, 'tor.html',
{'title': _('Tor Control Panel'),
'status': status,
'config_running': bool(config_process),
'form': form})
@ -95,25 +101,48 @@ def __apply_changes(request, old_status, new_status):
arguments.extend(['--apt-transport-tor', arg_value])
if arguments:
actions.superuser_run('tor', ['configure'] + arguments)
tor.socks_service.notify_enabled(None, new_status['enabled'])
tor.bridge_service.notify_enabled(None, new_status['enabled'])
messages.success(request, _('Configuration updated'))
global config_process
if not config_process:
config_process = actions.superuser_run(
'tor', ['configure'] + arguments, async=True)
else:
messages.info(request, _('Setting unchanged'))
def _collect_config_result(request, status):
"""Handle config process completion."""
global config_process
if not config_process:
return
return_code = config_process.poll()
# Config process is not complete yet
if return_code == None:
return
tor.socks_service.notify_enabled(None, status['enabled'])
tor.bridge_service.notify_enabled(None, status['enabled'])
# Update hidden service name registered with Name Services module.
domain_removed.send_robust(
sender='tor', domain_type='hiddenservice')
(hs_enabled, hs_hostname, hs_ports) = tor.get_hs()
if tor.is_enabled() and tor.is_running() and hs_enabled and hs_hostname:
if status['enabled'] and status['is_running'] and \
status['hs_enabled'] and status['hs_hostname']:
hs_services = []
for service in SERVICES:
if str(service[2]) in hs_ports:
if str(service[2]) in status['hs_ports']:
hs_services.append(service[0])
domain_added.send_robust(
sender='tor', domain_type='hiddenservice',
name=hs_hostname, description=_('Tor Hidden Service'),
name=status['hs_hostname'], description=_('Tor Hidden Service'),
services=hs_services)
if not return_code:
messages.success(request, _('Configuration updated.'))
else:
messages.info(request, _('Error occurred during configuration.'))
config_process = None