From 406ad86833f44167cf3719d7d052bdca0c0da0d7 Mon Sep 17 00:00:00 2001 From: Arturo FilastoĢ€ Date: Fri, 19 Aug 2011 15:05:07 +0200 Subject: Add support for torrc file read and write --- packages/torouter-web/src/config.py | 5 +++-- packages/torouter-web/src/tui/controllers/tor.py | 16 +++++++++++++--- .../torouter-web/src/tui/utils/configuration.py | 3 +++ packages/torouter-web/src/tui/utils/fileio.py | 21 +++++++++++++++++++++ packages/torouter-web/src/tui/utils/parsing.py | 7 +++++++ packages/torouter-web/src/tui/views/torconfig.html | 12 +++++++++++- packages/torouter-web/src/tui/views/torstatus.html | 2 ++ 7 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 packages/torouter-web/src/tui/utils/fileio.py (limited to 'packages/torouter-web/src') diff --git a/packages/torouter-web/src/config.py b/packages/torouter-web/src/config.py index 15057df..f3f8dcc 100644 --- a/packages/torouter-web/src/config.py +++ b/packages/torouter-web/src/config.py @@ -11,6 +11,7 @@ globals = {} # Add your own (username, password) pair authinfo = ("test", "test") -interfaces_file = os.getcwd() + "/../torouter-prep/configs/interfaces" -torrc_file = os.getcwd() + "/../torouter-prep/configs/torrc" +interfaces_file = os.getcwd() + "/../../torouter-prep/configs/interfaces" +#torrc_file = os.getcwd() + "/../../torouter-prep/configs/torrc" +torrc_file = "/tmp/torrc" diff --git a/packages/torouter-web/src/tui/controllers/tor.py b/packages/torouter-web/src/tui/controllers/tor.py index af8678c..eaa587d 100644 --- a/packages/torouter-web/src/tui/controllers/tor.py +++ b/packages/torouter-web/src/tui/controllers/tor.py @@ -1,7 +1,7 @@ import web import view, config from view import render -from tui.utils import session, parsing +from tui.utils import session, parsing, fileio """ The main Tor status page @@ -24,12 +24,22 @@ Tor configuration page """ class torrc: def update_config(self, data): + # Now it will just write to /tmp/torrc + files = [('/tmp/torrc',data.torrc)] + fileio.write(files) + return True def GET(self): - return render.base(render.torconfig()) + trc = parsing.torrc(config.torrc_file) + output = trc.output() + return render.base(render.torconfig(output)) def POST(self): self.update_config(web.input()) - return render.base(render.torconfig()) + trc = parsing.torrc(config.torrc_file) + trc.parse() + output = trc.html_output() + return render.base(render.torstatus(output,config.torrc_file)) + diff --git a/packages/torouter-web/src/tui/utils/configuration.py b/packages/torouter-web/src/tui/utils/configuration.py index 88bbc05..ba3d251 100644 --- a/packages/torouter-web/src/tui/utils/configuration.py +++ b/packages/torouter-web/src/tui/utils/configuration.py @@ -8,6 +8,7 @@ def get(name): if name == "wireless": conf['essid'] = "Torouter" conf['encryption'] = "WPA2" + conf['mac'] = "00:66:66:66:66:66" conf['key'] = "ljdasjkbcuBH12389Ba" return conf elif name == "firewall": @@ -43,6 +44,8 @@ def get_form(name): return web.form.Form( web.form.Textbox(name='essid', description='Wireless ESSID', value=c['essid']), + web.form.Textbox(name='mac', + description='Wireless MAC address', value=c['mac']), web.form.Dropdown(name='enctype', args=['WPA2', 'WPA', 'WEP (not reccomended)', 'open'], description='Wireless encryption scheme', value=c['encryption']), web.form.Password(name='key', diff --git a/packages/torouter-web/src/tui/utils/fileio.py b/packages/torouter-web/src/tui/utils/fileio.py new file mode 100644 index 0000000..3234f1e --- /dev/null +++ b/packages/torouter-web/src/tui/utils/fileio.py @@ -0,0 +1,21 @@ +import os + +def write(files): + tmpfiles = [] + for file in files: + tmpfile = "/tmp/" + file[0].split("/")[-1:][0] + ".tmp" + tmpfiles.append((tmpfile,file[0])) + # open the tmp file for write + f = open(tmpfile, 'w') + # write the data (file[1]) to the temp file + f.write(file[1]) + f.close() + + # rename all the file. This operation is atomic + for file in tmpfiles: + os.rename(file[0], file[1]) + +files = [('/tmp/file1','blablbal'),('/tmp/file2','The content'),('/tmp/file3','The content 3')] + +write(files) + diff --git a/packages/torouter-web/src/tui/utils/parsing.py b/packages/torouter-web/src/tui/utils/parsing.py index beb45d3..daf4436 100644 --- a/packages/torouter-web/src/tui/utils/parsing.py +++ b/packages/torouter-web/src/tui/utils/parsing.py @@ -87,6 +87,13 @@ class torrc: else: self.parsed.append(line) + def output(self): + output = "" + for line in self.fp.readlines(): + print line + output += line + return output + def html_output(self): output = "