diff options
author | ficus <ficus@robocracy.org> | 2012-11-25 02:24:27 +0100 |
---|---|---|
committer | ficus <ficus@robocracy.org> | 2012-11-25 02:25:30 +0100 |
commit | 3aae76170978a7557b0d7d2cbf10b3b856871538 (patch) | |
tree | ce2bd74d873a03db0177029c7abe7aae8e4d9c8e | |
parent | 17f9bf637866bd9dbf63cce097a2db85f92e15cc (diff) | |
download | tui-3aae76170978a7557b0d7d2cbf10b3b856871538.tar.gz tui-3aae76170978a7557b0d7d2cbf10b3b856871538.zip |
add tor (re)starts
-rw-r--r-- | torouterui/templates/tor.html | 13 | ||||
-rw-r--r-- | torouterui/tor.py | 26 | ||||
-rw-r--r-- | torouterui/util.py | 2 | ||||
-rw-r--r-- | 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 %} +<form action="/tor/" method="POST"> +Tor daemon should probably be restarted... are you certain? +<br> +<br> +<button type="submit" class="btn btn-large btn-primary btn-warning" type="button" name="submit" value="Restart"> +Restart</button> +</form> + +{% else %} + <form action="/tor/" method="POST" class="form-horizontal"> <legend>Tor Network Configuration</legend> @@ -38,5 +49,5 @@ Save</button> </div> </form> - +{% 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 <a href='/tor'>revisit this page</a> (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) |