mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-27 10:44:33 +00:00
Add Tor form to enable/disable hidden service.
This commit is contained in:
parent
50797f9a92
commit
4e493c1ac5
30
actions/tor
30
actions/tor
@ -94,16 +94,16 @@ def subcommand_enable_hs(_):
|
|||||||
if get_hidden_service() != "":
|
if get_hidden_service() != "":
|
||||||
return
|
return
|
||||||
|
|
||||||
with open(TOR_CONFIG, 'r') as file:
|
with open(TOR_CONFIG, 'r') as conffile:
|
||||||
lines = file.readlines()
|
lines = conffile.readlines()
|
||||||
|
|
||||||
lines.append("# Hidden Service configured by Plinth\n")
|
lines.append("# Hidden Service configured by Plinth\n")
|
||||||
lines.append("HiddenServiceDir /var/lib/tor/hidden_service/\n")
|
lines.append("HiddenServiceDir /var/lib/tor/hidden_service/\n")
|
||||||
lines.append("HiddenServicePort 80 127.0.0.1:80\n")
|
lines.append("HiddenServicePort 80 127.0.0.1:80\n")
|
||||||
lines.append("HiddenServicePort 443 127.0.0.1:443\n")
|
lines.append("HiddenServicePort 443 127.0.0.1:443\n")
|
||||||
|
|
||||||
with open(TOR_CONFIG, 'w') as file:
|
with open(TOR_CONFIG, 'w') as conffile:
|
||||||
file.writelines(lines)
|
conffile.writelines(lines)
|
||||||
|
|
||||||
subprocess.call(['service', 'tor', 'restart'])
|
subprocess.call(['service', 'tor', 'restart'])
|
||||||
|
|
||||||
@ -114,26 +114,28 @@ def subcommand_disable_hs(_):
|
|||||||
if not get_hidden_service():
|
if not get_hidden_service():
|
||||||
return
|
return
|
||||||
|
|
||||||
with open(TOR_CONFIG, 'r') as file:
|
with open(TOR_CONFIG, 'r') as conffile:
|
||||||
lines = file.readlines()
|
lines = conffile.readlines()
|
||||||
|
|
||||||
filtered_lines = []
|
filtered_lines = []
|
||||||
removing = False
|
removing = False
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if removing:
|
if removing:
|
||||||
if not line.startswith("HiddenService"):
|
if not line.startswith("HiddenService"):
|
||||||
# end of Plinth hidden service block -- stop removing lines
|
# end of Plinth hidden service block
|
||||||
|
# stop removing lines
|
||||||
removing = False
|
removing = False
|
||||||
filtered_lines.append(line)
|
filtered_lines.append(line)
|
||||||
else:
|
else:
|
||||||
if line.startswith("# Hidden Service configured by Plinth"):
|
if line.startswith("# Hidden Service configured by Plinth"):
|
||||||
# start of Plinth hidden service block -- remove following HiddenService lines
|
# start of Plinth hidden service block
|
||||||
|
# remove following HiddenService lines
|
||||||
removing = True
|
removing = True
|
||||||
else:
|
else:
|
||||||
filtered_lines.append(line)
|
filtered_lines.append(line)
|
||||||
|
|
||||||
with open(TOR_CONFIG, 'w') as file:
|
with open(TOR_CONFIG, 'w') as conffile:
|
||||||
file.writelines(filtered_lines)
|
conffile.writelines(filtered_lines)
|
||||||
|
|
||||||
subprocess.call(['service', 'tor', 'restart'])
|
subprocess.call(['service', 'tor', 'restart'])
|
||||||
|
|
||||||
@ -157,8 +159,8 @@ def get_hidden_service():
|
|||||||
hs_dir = ""
|
hs_dir = ""
|
||||||
hs_ports = []
|
hs_ports = []
|
||||||
|
|
||||||
with open(TOR_CONFIG, 'r') as file:
|
with open(TOR_CONFIG, 'r') as conffile:
|
||||||
lines = file.readlines()
|
lines = conffile.readlines()
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if line.startswith('HiddenServiceDir'):
|
if line.startswith('HiddenServiceDir'):
|
||||||
hs_dir = line.split()[1]
|
hs_dir = line.split()[1]
|
||||||
@ -168,8 +170,8 @@ def get_hidden_service():
|
|||||||
if hs_dir == "":
|
if hs_dir == "":
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
with open(hs_dir + 'hostname', 'r') as file:
|
with open(hs_dir + 'hostname', 'r') as conffile:
|
||||||
hs_hostname = file.read().strip()
|
hs_hostname = conffile.read().strip()
|
||||||
|
|
||||||
return hs_hostname + ' ' + ','.join(hs_ports)
|
return hs_hostname + ' ' + ','.join(hs_ports)
|
||||||
|
|
||||||
|
|||||||
@ -18,6 +18,8 @@
|
|||||||
#
|
#
|
||||||
{% endcomment %}
|
{% endcomment %}
|
||||||
|
|
||||||
|
{% load bootstrap %}
|
||||||
|
|
||||||
{% block main_block %}
|
{% block main_block %}
|
||||||
|
|
||||||
<h3>Status</h3>
|
<h3>Status</h3>
|
||||||
@ -38,6 +40,15 @@ Here is the current configuration:
|
|||||||
<li>Hostname: {{ tor_hs_hostname }}</li>
|
<li>Hostname: {{ tor_hs_hostname }}</li>
|
||||||
<li>Ports: {{ tor_hs_ports }}</li>
|
<li>Ports: {{ tor_hs_ports }}</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
<form class="form" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
|
||||||
|
{{ form|bootstrap }}
|
||||||
|
|
||||||
|
<input type="submit" class="btn-primary" value="Update setup"/>
|
||||||
|
</form>
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p><h3>Bridge</h3>
|
<p><h3>Bridge</h3>
|
||||||
|
|||||||
@ -19,6 +19,8 @@
|
|||||||
Plinth module for configuring Tor
|
Plinth module for configuring Tor
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from django import forms
|
||||||
|
from django.contrib import messages
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
@ -27,6 +29,13 @@ from plinth import actions
|
|||||||
from plinth import cfg
|
from plinth import cfg
|
||||||
|
|
||||||
|
|
||||||
|
class TorForm(forms.Form): # pylint: disable=W0232
|
||||||
|
"""Tor configuration form"""
|
||||||
|
hs_enabled = forms.BooleanField(
|
||||||
|
label=_('Enable Hidden Service'),
|
||||||
|
required=False)
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
"""Initialize the Tor module"""
|
"""Initialize the Tor module"""
|
||||||
menu = cfg.main_menu.get('apps:index')
|
menu = cfg.main_menu.get('apps:index')
|
||||||
@ -36,30 +45,74 @@ def init():
|
|||||||
@login_required
|
@login_required
|
||||||
def index(request):
|
def index(request):
|
||||||
"""Service the index page"""
|
"""Service the index page"""
|
||||||
output = actions.superuser_run("tor-get-ports")
|
status = get_status()
|
||||||
|
|
||||||
|
form = None
|
||||||
|
|
||||||
|
if request.method == 'POST':
|
||||||
|
form = TorForm(request.POST, prefix='tor')
|
||||||
|
# pylint: disable=E1101
|
||||||
|
if form.is_valid():
|
||||||
|
_apply_changes(request, status, form.cleaned_data)
|
||||||
|
status = get_status()
|
||||||
|
form = TorForm(initial=status, prefix='tor')
|
||||||
|
else:
|
||||||
|
form = TorForm(initial=status, prefix='tor')
|
||||||
|
|
||||||
|
return TemplateResponse(request, 'tor.html',
|
||||||
|
{'title': _('Tor Control Panel'),
|
||||||
|
'is_running': status['is_running'],
|
||||||
|
'tor_ports': status['ports'],
|
||||||
|
'tor_hs_enabled': status['hs_enabled'],
|
||||||
|
'tor_hs_hostname': status['hs_hostname'],
|
||||||
|
'tor_hs_ports': status['hs_ports'],
|
||||||
|
'form': form})
|
||||||
|
|
||||||
|
|
||||||
|
def get_status():
|
||||||
|
"""Return the current status"""
|
||||||
|
is_running = actions.superuser_run("tor", ["is-running"]).strip() == "yes"
|
||||||
|
|
||||||
|
output = actions.superuser_run("tor-get-ports")
|
||||||
port_info = output.split("\n")
|
port_info = output.split("\n")
|
||||||
tor_ports = {}
|
ports = {}
|
||||||
for line in port_info:
|
for line in port_info:
|
||||||
try:
|
try:
|
||||||
(key, val) = line.split()
|
(key, val) = line.split()
|
||||||
tor_ports[key] = val
|
ports[key] = val
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
output = actions.superuser_run("tor", ["get-hs"])
|
output = actions.superuser_run("tor", ["get-hs"])
|
||||||
|
output = output.strip()
|
||||||
if output == "":
|
if output == "":
|
||||||
tor_hs_hostname = "Not Configured"
|
hs_enabled = False
|
||||||
tor_hs_ports = ""
|
hs_hostname = "Not Configured"
|
||||||
|
hs_ports = ""
|
||||||
else:
|
else:
|
||||||
|
hs_enabled = True
|
||||||
hs_info = output.split()
|
hs_info = output.split()
|
||||||
tor_hs_hostname = hs_info[0]
|
hs_hostname = hs_info[0]
|
||||||
tor_hs_ports = hs_info[1]
|
hs_ports = hs_info[1]
|
||||||
|
|
||||||
is_running = actions.superuser_run("tor", ["is-running"]).strip() == "yes"
|
return {'is_running': is_running,
|
||||||
return TemplateResponse(request, 'tor.html',
|
'ports': ports,
|
||||||
{'title': _('Tor Control Panel'),
|
'hs_enabled': hs_enabled,
|
||||||
'tor_hs_hostname': tor_hs_hostname,
|
'hs_hostname': hs_hostname,
|
||||||
'tor_hs_ports': tor_hs_ports,
|
'hs_ports': hs_ports}
|
||||||
'tor_ports': tor_ports,
|
|
||||||
'is_running': is_running})
|
|
||||||
|
def _apply_changes(request, old_status, new_status):
|
||||||
|
"""Apply the changes"""
|
||||||
|
if old_status['hs_enabled'] == new_status['hs_enabled']:
|
||||||
|
messages.info(request, _('Setting unchanged'))
|
||||||
|
return
|
||||||
|
|
||||||
|
if new_status['hs_enabled']:
|
||||||
|
messages.success(request, _('Tor hidden service enabled'))
|
||||||
|
command = 'enable-hs'
|
||||||
|
else:
|
||||||
|
messages.success(request, _('Tor hidden service disabled'))
|
||||||
|
command = 'disable-hs'
|
||||||
|
|
||||||
|
actions.superuser_run('tor', [command])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user