From fe4b8a01fc87d1a76fca8e8733270a260aa23cc0 Mon Sep 17 00:00:00 2001 From: ficus Date: Tue, 25 Sep 2012 11:46:08 +0200 Subject: basic tor status using python-torctl --- torouterui/templates/home.html | 16 ++++++++++++++-- torouterui/tor.py | 42 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 5 deletions(-) (limited to 'torouterui') diff --git a/torouterui/templates/home.html b/torouterui/templates/home.html index fa2ed2b..58ed2f2 100644 --- a/torouterui/templates/home.html +++ b/torouterui/templates/home.html @@ -57,13 +57,25 @@

Tor

- +
+ + + +
Status + {% if status.tor.state == 'ESTABLISHED' %}label-success{% elif status.tor.state == 'CONNECTING' %}label-info{% else %}label-important{% endif %}"> {{ status.tor.state }} +
Circuit Established? + {{ status.tor.circuit_established }} +
Version + {% if status.tor.version %}{{ status.tor.version }} ({{ status.tor.version_current }}){% endif %} +
Traffic TX + {% if status.tor.traffic_written_bytes %}{{ status.tor.traffic_written_bytes }} bytes{% endif %} +
Traffic RX + {% if status.tor.traffic_read_bytes %}{{ status.tor.traffic_read_bytes }} bytes{% endif %}
diff --git a/torouterui/tor.py b/torouterui/tor.py index ef585c5..cf18ce2 100644 --- a/torouterui/tor.py +++ b/torouterui/tor.py @@ -3,16 +3,52 @@ Helper code for interacting with Tor and modifying the Tor system configuration. """ +from TorCtl import TorCtl + +def tor_getinfo(conn, key): + return conn.get_info(key)[key] def get_tor_status(): + """Ask for status over torctl pipe""" d = dict() - d['state'] = 'DISABLED' + try: + f = open('/var/run/tor/tor.pid', 'r') + f.close() + except IOError, ioe: + if ioe.errno == 13: + # "Permission denied" + d['state'] = 'PERMISSION_DENIED' + return d + elif ioe.errno == 2: + # "No such file or directory" + d['state'] = 'DISABLED' + return d + else: + raise ioe + conn = TorCtl.connect() + if not conn: + d['state'] = 'DISABLED' + return d + d['version'] = tor_getinfo(conn, 'version') + d['traffic_read_bytes'] = int(tor_getinfo(conn, 'traffic/read')) + d['traffic_written_bytes'] = int(tor_getinfo(conn, 'traffic/written')) + d['version_current'] = tor_getinfo(conn, 'status/version/current') + d['circuit_established'] = bool( + tor_getinfo(conn, 'status/circuit-established')) + if d['circuit_established']: + d['state'] = 'ESTABLISHED' + else: + d['state'] = 'CONNECTING' + conn.close() return d - + def get_tor_settings(): - return dict() + """Ask for settings over torctl pipe; don't use augeas""" + d = dict() + return d def save_tor_settings(): + """Commit settings through torctl pipe, then send SAVECONF""" pass -- cgit v1.2.3