From 3aae76170978a7557b0d7d2cbf10b3b856871538 Mon Sep 17 00:00:00 2001 From: ficus Date: Sun, 25 Nov 2012 02:24:27 +0100 Subject: add tor (re)starts --- torouterui/templates/tor.html | 13 ++++++++++++- torouterui/tor.py | 26 ++++++++++++++++++-------- torouterui/util.py | 2 +- torouterui/views.py | 24 +++++++++++++++++++++--- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/torouterui/templates/tor.html b/torouterui/templates/tor.html index 327ac59..3c7e340 100644 --- a/torouterui/templates/tor.html +++ b/torouterui/templates/tor.html @@ -2,6 +2,17 @@ {% extends "base.html" %} {% block body %} +{% if form == None %} +
+Tor daemon should probably be restarted... are you certain? +
+
+ +
+ +{% else %} +
Tor Network Configuration @@ -38,5 +49,5 @@ Save
- +{% endif %} {% endblock %} diff --git a/torouterui/tor.py b/torouterui/tor.py index dd047ca..83ac866 100644 --- a/torouterui/tor.py +++ b/torouterui/tor.py @@ -88,18 +88,28 @@ def get_tor_settings(): conn.close() return d +def restart_tor(): + grepcode = os.system("grep '^DisableNetwork 1' /etc/tor/torrc") + if grepcode == 512: + # permission denied + raise IOError("Don't have permission to read /etc/tor/torrc") + elif grepcode == 0: + pass # found + else: + if os.system('echo "DisableNetwork 1" >> /etc/tor/torrc') != 0: + raise IOError("Don't have permission to write /etc/tor/torrc") + util.enable_service('tor') + time.sleep(3) + conn = TorCtl.connect() + if not conn: + raise Exception("Could not start tor daemon!") + conn.close() + def save_tor_settings(form): """Commit settings through torctl pipe, then send SAVECONF""" conn = TorCtl.connect() if not conn: - print "Warning: couldn't connect to tor daemon; need to boot up tor daemon in disabled state" - os.system('echo "DisableNetwork 1" >> /etc/tor/torrc') - # tor daemon should always be running, even if disabled - util.enable_service('tor') - time.sleep(3) - conn = TorCtl.connect() - if not conn: - raise Exception("Could not start tor daemon!") + raise Exception("couldn't connect to tor daemon; need to boot up tor daemon in disabled state") conn.set_option('RelayBandwidthRate', "%dKB" % int(form['tor_relaybandwidthrateKBps'])) diff --git a/torouterui/util.py b/torouterui/util.py index 16e7f57..78046da 100644 --- a/torouterui/util.py +++ b/torouterui/util.py @@ -20,7 +20,7 @@ def fs_read(path): def enable_service(name): #os.system('update-rc.d %s defaults &' % name) - subprocess.Popen(['service', name, 'start'], close_fds=True) + subprocess.Popen(['service', name, 'restart'], close_fds=True) def disable_service(name): """Currently, this is never actually called""" diff --git a/torouterui/views.py b/torouterui/views.py index f52a89b..9002348 100644 --- a/torouterui/views.py +++ b/torouterui/views.py @@ -199,14 +199,30 @@ def torpage(): msg = list() status = dict() status['tor'] = tor.get_tor_status() - if request.method == 'GET': + # Check for reset first + if request.method == 'POST' and \ + request.form.get('submit') == 'Restart': + try: + tor.restart_tor() + msg.append(("info", + "Reloading Tor daemon... please wait a minute then revisit this page (don't just reload)."),) + except Exception, err: + msg.append(("error", err),) + return render_template('tor.html', form=None, status=status, + formerr=None, messages=msg) + # Then regular GET + if request.method == 'GET' or (request.method == 'POST' and \ + request.form.get('submit') == 'Reset'): if status['tor']['state'] == 'DISABLED': msg.append(("warning", - "Could not connect to Tor daemon for control. Will try to restart daemon if settings are saved from this page."),) + "Could not connect to Tor daemon for control."),) + form = None + elif status['tor']['state'] == 'PERMISSION_DENIED': + msg.append(("error", + "Permission denied when connecting to Tor daemon for control."),) form = None else: form = tor.get_tor_settings() - print form return render_template('tor.html', form=form, status=status, formerr=None, messages=msg) # Got this far, need to validate form @@ -225,6 +241,8 @@ def torpage(): msg.append(("error", "Was unable to commit changes...\"%s\"" % err)) + return render_template('tor.html', settings=None, status=None, + form=request.form, formerr=None, messages=msg) return render_template('tor.html', settings=None, status=None, form=request.form, formerr=None, messages=msg) -- cgit v1.2.3