Use package framework for installing tor

This commit is contained in:
Sunil Mohan Adapa 2014-12-22 23:31:50 +05:30
parent 7b45ad1813
commit c7f27e493e
3 changed files with 5 additions and 42 deletions

View File

@ -34,10 +34,6 @@ def parse_arguments():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
# Get whether Tor is installed
subparsers.add_parser('get-installed',
help='Get whether Tor is installed')
# Get whether Tor is running # Get whether Tor is running
subparsers.add_parser('is-running', help='Get whether Tor is running') subparsers.add_parser('is-running', help='Get whether Tor is running')
@ -59,11 +55,6 @@ def parse_arguments():
return parser.parse_args() return parser.parse_args()
def subcommand_get_installed(_):
"""Get whether Tor is installed"""
print('installed' if get_installed() else 'not installed')
def subcommand_is_running(_): def subcommand_is_running(_):
"""Get whether Tor is running""" """Get whether Tor is running"""
try: try:
@ -101,7 +92,7 @@ def subcommand_get_hs(_):
def subcommand_enable_hs(_): def subcommand_enable_hs(_):
"""Enable Tor hidden service""" """Enable Tor hidden service"""
if not get_installed() or get_hidden_service(): if get_hidden_service():
return return
with open(TOR_CONFIG, 'r') as conffile: with open(TOR_CONFIG, 'r') as conffile:
@ -121,7 +112,7 @@ def subcommand_enable_hs(_):
def subcommand_disable_hs(_): def subcommand_disable_hs(_):
"""Disable Tor hidden service""" """Disable Tor hidden service"""
if not get_installed() or not get_hidden_service(): if not get_hidden_service():
return return
with open(TOR_CONFIG, 'r') as conffile: with open(TOR_CONFIG, 'r') as conffile:
@ -154,19 +145,8 @@ def subcommand_disable_hs(_):
subprocess.call(['service', 'tor', 'restart']) subprocess.call(['service', 'tor', 'restart'])
def get_installed():
"""Get whether Tor is installed"""
with open('/dev/null', 'w') as file_handle:
status = subprocess.call(['which', 'tor'], stdout=file_handle)
return not status
def set_tor_service(enable): def set_tor_service(enable):
"""Enable/disable Tor service; enable: boolean""" """Enable/disable Tor service; enable: boolean"""
if not get_installed():
return
newline = 'RUN_DAEMON="yes"\n' if enable else 'RUN_DAEMON="no"\n' newline = 'RUN_DAEMON="yes"\n' if enable else 'RUN_DAEMON="no"\n'
with open(SERVICE_CONFIG, 'r') as file: with open(SERVICE_CONFIG, 'r') as file:
@ -182,9 +162,6 @@ def set_tor_service(enable):
def get_hidden_service(): def get_hidden_service():
"""Return a string with configured Tor hidden service information""" """Return a string with configured Tor hidden service information"""
if not get_installed():
return ''
hs_dir = None hs_dir = None
hs_ports = [] hs_ports = []

View File

@ -24,8 +24,6 @@
<h2>Tor</h2> <h2>Tor</h2>
{% if is_installed %}
<h3>Status</h3> <h3>Status</h3>
<p> <p>
@ -80,15 +78,6 @@ port-forwarded, if necessary:</p>
<p>A Tor SOCKS port is available on your {{ cfg.box_name }} on TCP port <p>A Tor SOCKS port is available on your {{ cfg.box_name }} on TCP port
9050.</p> 9050.</p>
{% else %}
<p>Tor is not installed, please install it. Tor comes pre-installed
with {{ cfg.box_name }}. On any Debian-based system (such as
{{ cfg.box_name }}) you may install it using the command
<code>aptitude install tor</code>.</p>
{% endif %}
{% endblock %} {% endblock %}
{% block sidebar %} {% block sidebar %}

View File

@ -27,6 +27,7 @@ from gettext import gettext as _
from plinth import actions from plinth import actions
from plinth import cfg from plinth import cfg
from plinth import package
class TorForm(forms.Form): # pylint: disable=W0232 class TorForm(forms.Form): # pylint: disable=W0232
@ -43,6 +44,7 @@ def init():
@login_required @login_required
@package.required('tor')
def index(request): def index(request):
"""Service the index page""" """Service the index page"""
status = get_status() status = get_status()
@ -61,7 +63,6 @@ def index(request):
return TemplateResponse(request, 'tor.html', return TemplateResponse(request, 'tor.html',
{'title': _('Tor Control Panel'), {'title': _('Tor Control Panel'),
'is_installed': status['is_installed'],
'is_running': status['is_running'], 'is_running': status['is_running'],
'tor_ports': status['ports'], 'tor_ports': status['ports'],
'tor_hs_enabled': status['hs_enabled'], 'tor_hs_enabled': status['hs_enabled'],
@ -72,9 +73,6 @@ def index(request):
def get_status(): def get_status():
"""Return the current status""" """Return the current status"""
is_installed = actions.superuser_run(
'tor',
['get-installed']).strip() == 'installed'
is_running = actions.superuser_run('tor', ['is-running']).strip() == 'yes' is_running = actions.superuser_run('tor', ['is-running']).strip() == 'yes'
output = actions.superuser_run('tor-get-ports') output = actions.superuser_run('tor-get-ports')
@ -103,8 +101,7 @@ def get_status():
hs_hostname = hs_info[0] hs_hostname = hs_info[0]
hs_ports = hs_info[1] hs_ports = hs_info[1]
return {'is_installed': is_installed, return {'is_running': is_running,
'is_running': is_running,
'ports': ports, 'ports': ports,
'hs_enabled': hs_enabled, 'hs_enabled': hs_enabled,
'hs_hostname': hs_hostname, 'hs_hostname': hs_hostname,