From 7794893947b41d6009c4ebb3cbc5bdb493767f99 Mon Sep 17 00:00:00 2001
From: fonfon
Date: Mon, 15 Sep 2014 12:51:57 +0200
Subject: [PATCH] added tor actions: is-running, start, stop; display tor
status at tor app page (enabling/disabling will be integrated later)
---
actions/tor | 100 ++++++++++++++++++++++++++
plinth/modules/tor/templates/tor.html | 7 ++
plinth/modules/tor/tor.py | 4 +-
static/themes/default/css/plinth.css | 14 ++++
4 files changed, 124 insertions(+), 1 deletion(-)
create mode 100755 actions/tor
diff --git a/actions/tor b/actions/tor
new file mode 100755
index 000000000..34d46f48a
--- /dev/null
+++ b/actions/tor
@@ -0,0 +1,100 @@
+#!/usr/bin/python
+# -*- mode: python -*-
+#
+# This file is part of Plinth.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see .
+#
+
+"""
+Configuration helper for the Tor service
+"""
+
+import argparse
+import subprocess
+
+SERVICE_CONFIG = '/etc/default/tor'
+
+
+def parse_arguments():
+ """Return parsed command line arguments as dictionary"""
+ parser = argparse.ArgumentParser()
+ subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
+
+ # Get whether Tor is running
+ subparsers.add_parser('is-running', help='Get whether Tor is running')
+
+ # Start Tor and enable service
+ subparsers.add_parser('start', help='Start Tor service')
+
+ # Stop Tor and disable service
+ subparsers.add_parser('stop', help='Stop Tor service')
+
+ return parser.parse_args()
+
+
+def subcommand_is_running(_):
+ """Get whether Tor is running"""
+ try:
+ output = subprocess.check_output(['service', 'tor', 'status'])
+ except subprocess.CalledProcessError:
+ # if tor is not running we get a status code != 0 and a
+ # CalledProcessError
+ print "no"
+ else:
+ running = False
+ for line in output.split('\n'):
+ if "Active" in line and "running" in line:
+ running = True
+ break
+ print "yes" if running else "no"
+
+
+def subcommand_start(_):
+ """Start Tor and enable it as service"""
+ set_tor_service(enable=True)
+ subprocess.call(['service', 'tor', 'start'])
+
+
+def subcommand_stop(_):
+ """Start Tor and enable it as service"""
+ set_tor_service(enable=False)
+ subprocess.call(['service', 'tor', 'stop'])
+
+
+def set_tor_service(enable):
+ """Enable/disable tor service; enable: boolean"""
+ newline = "RUN_DAEMON=\"yes\"\n" if enable else "RUN_DAEMON=\"no\"\n"
+
+ with open(SERVICE_CONFIG, 'r') as file:
+ lines = file.readlines()
+ for i, line in enumerate(lines):
+ if line.startswith('RUN_DAEMON'):
+ lines[i] = newline
+ break
+ with open(SERVICE_CONFIG, 'w') as file:
+ file.writelines(lines)
+
+
+def main():
+ """Parse arguments and perform all duties"""
+ arguments = parse_arguments()
+
+ subcommand = arguments.subcommand.replace('-', '_')
+ subcommand_method = globals()['subcommand_' + subcommand]
+ subcommand_method(arguments)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/plinth/modules/tor/templates/tor.html b/plinth/modules/tor/templates/tor.html
index c97aafded..2612565a4 100644
--- a/plinth/modules/tor/templates/tor.html
+++ b/plinth/modules/tor/templates/tor.html
@@ -44,4 +44,11 @@ port-forwarded, if necessary:
{% endfor %}
+
+{% if is_running %}
+ Tor is running
+{% else %}
+ Tor is not running
+{% endif %}
+
{% endblock %}
diff --git a/plinth/modules/tor/tor.py b/plinth/modules/tor/tor.py
index 37ffd76cf..0911e3122 100644
--- a/plinth/modules/tor/tor.py
+++ b/plinth/modules/tor/tor.py
@@ -47,6 +47,8 @@ def index(request):
except ValueError:
continue
+ is_running = actions.superuser_run("tor", ["is-running"]).strip() == "yes"
return TemplateResponse(request, 'tor.html',
{'title': _('Tor Control Panel'),
- 'tor_ports': tor_ports})
+ 'tor_ports': tor_ports,
+ 'is_running': is_running})
diff --git a/static/themes/default/css/plinth.css b/static/themes/default/css/plinth.css
index c983d7042..f09bf917c 100644
--- a/static/themes/default/css/plinth.css
+++ b/static/themes/default/css/plinth.css
@@ -55,3 +55,17 @@ body {
.table td.cell-indented {
padding-left: 3em;
}
+
+.circle {
+ border-radius: 50%/50%;
+ border: 1px solid black;
+ width: 8px;
+ height: 8px;
+ display: inline-block;
+}
+.circle.active {
+ background-color: rgb(0, 167, 0);
+}
+.circle.inactive {
+ background-color: rgb(228, 66, 66);
+}