aboutsummaryrefslogtreecommitdiffstats
path: root/torouterui/views.py
blob: 418c391599398baf897e607dfc264c57f3d9e53a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
Main Flask web application code for the torouter user interface.  See also
README and TODO.

Run ``./torouterui.py --help`` for command line argument options; see bottom of
this file for that code.

The code that actually interacts with the operating system is the /helpers
directory.

This code under a BSD license; see LICENSE file included with this code.

<https://trac.torproject.org/projects/tor/wiki/doc/Torouter>
"""

from flask import Flask, render_template, send_from_directory, request
import os

from torouterui import app
import torouterui.sysstatus as sysstatus
import torouterui.netif as netif
import torouterui.tor as tor


@app.route('/')
def statuspage():
    status = dict()
    status['system'] = sysstatus.get_system_status()
    status['resources'] = sysstatus.get_resources_status()
    status['wan'] = netif.get_wan_status()
    status['lan'] = netif.get_lan_status()
    status['wifi'] = netif.get_wifi_status()
    status['tor'] = tor.get_tor_status()
    return render_template('home.html', settings=None, status=status)

@app.route('/about/')
def aboutpage():
    return render_template('about.html')

@app.route('/reboot/', methods=['GET', 'POST'])
def rebootpage():
    msg = list()
    if request.method == 'GET':
        return render_template('reboot.html', status=None)
    elif 'confirm' in request.form:
        # TODO: actually execute reboot
        #os.system('reboot &')
        return render_template('reboot.html', status='rebooting')
    else:
        msg.append(("error", "Didn't confirm, not rebooting",),)
        return render_template('reboot.html', status=None, messages=msg)

@app.route('/wan/', methods=['GET', 'POST'])
def wanpage():
    msg = list()
    status = dict(wan=netif.get_wan_status())
    if not status['wan']:
        msg.append(("error",
            "Interface not detected, can not be configured."),)
        return render_template('wan.html', form=None, status=status,
            messages=msg, formerr=None)
    if request.method == 'GET':
        form = netif.get_wan_settings()
        return render_template('wan.html', form=form, status=status,
            formerr=None)
    # Got this far, need to validate form
    formerr = dict()
    if request.form['ipv4method'] == 'disabled':
        pass    # no further validation
    elif request.form['ipv4method'] == 'dhcp':
        pass    # no further validation
    elif request.form['ipv4method'] == 'static':
        if not netif.is_valid_ipv4(request.form['ipv4addr']):
            formerr['ipv4addr'] = "Not a valid IPv4 address"
        if not netif.is_valid_ipv4mask(request.form['ipv4netmask']):
            formerr['ipv4netmask'] = "Not a valid IPv4 netmask"
        if not netif.is_valid_ipv4(request.form['ipv4gateway']):
            formerr['ipv4gateway'] = "Not a valid IPv4 address"
    else:
        ke = KeyError("Invalid net config method: %s" % form['ipv4method'])
        print ke
        raise ke
    if len(formerr.keys()) > 0:
        msg.append(("error",
            "Please correct the validation issues below"),)
    else:
        # Ok, we have a valid form, now to commit it
        try:
            netif.save_wan_settings(request.form)
            msg.append(("success",
                "Configuration saved! Check logs for any errors"),)
        except IOError, ioerr:
            msg.append(("error",
                "Was unable to commit changes... permissions problem? \"%s\""
                    % ioerr))
    return render_template('wan.html', form=request.form, status=status,
            formerr=formerr, messages=msg)

@app.route('/lan/', methods=['GET', 'POST'])
def lanpage():
    msg = list()
    status = dict()
    status['lan'] = netif.get_lan_status()
    if not status['lan']:
        msg.append(("error",
            "Interface not detected, can not be configured."),)
        return render_template('lan.html', form=None, status=status,
            messages=msg, formerr=None)
    if request.method == 'GET':
        form = netif.get_lan_settings()
        return render_template('lan.html', form=form, status=status,
            formerr=None)
    # Got this far, need to validate form
    formerr = dict()
    if request.form.get('ipv4enable') != 'true':
        pass    # no further validation
    else:
        if not netif.is_valid_ipv4(request.form['ipv4addr']):
            formerr['ipv4addr'] = "Not a valid IPv4 address"
        if not netif.is_valid_ipv4mask(request.form['ipv4netmask']):
            formerr['ipv4netmask'] = "Not a valid IPv4 netmask"
        if not netif.is_valid_ipv4(request.form['dhcpbase']):
            formerr['dhcpbase'] = "Not a valid IPv4 address"
        if not netif.is_valid_ipv4(request.form['dhcptop']):
            formerr['dhcptop'] = "Not a valid IPv4 address"
        if not netif.is_valid_ipv4mask(request.form['dhcpnetmask']):
            formerr['dhcpnetmask'] = "Not a valid IPv4 netmask"
    if len(formerr.keys()) > 0:
        msg.append(("error",
            "Please correct the validation issues below"),)
    else:
        # Ok, we have a valid form, now to commit it
        try:
            netif.save_lan_settings(request.form)
            msg.append(("success",
                "Configuration saved! Check logs for any errors"),)
        except IOError, ioerr:
            msg.append(("error",
                "Was unable to commit changes... permissions problem? \"%s\""
                    % ioerr))
        except Exception, err:
            msg.append(("error",
                "Was unable to commit changes... \"%s\"" % err))
            raise err
    return render_template('lan.html', form=request.form, status=status,
            formerr=formerr, messages=msg)

@app.route('/wifi/', methods=['GET', 'POST'])
def wifipage():
    msg = list()
    status = dict()
    status['wifi'] = netif.get_wifi_status()
    if not status['wifi']:
        msg.append(("error",
            "Interface not detected, can not be configured."),)
        return render_template('wifi.html', form=None, status=status,
            messages=msg, formerr=None)
    if request.method == 'GET':
        form = netif.get_wifi_settings()
        return render_template('wifi.html', form=form, status=status,
            formerr=None)
    # Got this far, need to validate form
    formerr = dict()
    if request.form['ipv4method'] == 'disabled':
        pass    # no further validation
    elif request.form['ipv4method'] == 'static':
        if not netif.is_valid_ipv4(request.form['ipv4addr']):
            formerr['ipv4addr'] = "Not a valid IPv4 address"
        if not netif.is_valid_ipv4mask(request.form['ipv4netmask']):
            formerr['ipv4netmask'] = "Not a valid IPv4 netmask"
        if not netif.is_valid_ipv4(request.form['ipv4gateway']):
            formerr['ipv4gateway'] = "Not a valid IPv4 address"
    else:
        ke = KeyError("Invalid method: %s" % form['ipv4method'])
        print ke
        raise ke
    if len(formerr.keys()) > 0:
        msg.append(("error",
            "Please correct the validation issues below"),)
    else:
        # Ok, we have a valid form, now to commit it
        try:
            netif.save_wifi_settings(request.form)
            msg.append(("success",
                "Configuration saved! Check logs for any errors"),)
        except IOError, ioerr:
            msg.append(("error",
                "Was unable to commit changes... permissions problem? \"%s\""
                    % ioerr))
        except Exception, e:
            msg.append(("error",
                "Was unable to commit changes... \"%s\"" % e))
    return render_template('wifi.html', form=request.form, status=status,
            formerr=formerr, messages=msg)
    return render_template('wifi.html', settings=None, status=None)

@app.route('/tor/', methods=['GET', 'POST'])
def torpage():
    msg = list()
    status = dict()
    status['tor'] = tor.get_tor_status()
    if request.method == 'GET':
        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."),)
            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
    formerr = dict()
    # TODO: form validation
    if len(formerr.keys()) > 0:
        msg.append(("error",
            "Please correct the validation issues below"),)
    else:
        # Ok, we have a valid form, now to commit it
        try:
            tor.save_tor_settings(request.form)
            msg.append(("success",
                "Configuration saved! Check logs for any errors"),)
        except Exception, err:
            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)

@app.route('/logs/', methods=['GET'])
def logspage():
    logs = dict()
    logs['dmesg'] = sysstatus.get_dmesg()
    logs['syslog'] = sysstatus.get_syslog()
    logs['authlog'] = sysstatus.get_authlog()
    logs['tor'] = sysstatus.get_torlog()
    return render_template('logs.html', logs=logs)

@app.route('/processes/', methods=['GET'])
def processespage():
    process_list = sysstatus.get_process_list()
    return render_template('processes.html', process_list=process_list)

@app.route('/favicon.ico')
def favicon():
    """ Simple static redirect """
    return send_from_directory(os.path.join(app.root_path, 'static'),
                               'favicon.ico',
                               mimetype='image/vnd.microsoft.icon')

@app.route('/robots.txt')
def robots():
    """ "Just in case?" """
    return send_from_directory(os.path.join(app.root_path, 'static'),
                               'robots.txt',
                               mimetype='text/plain')