dashboard.py 17 KB
Newer Older
Jeff MrBE4R's avatar
Jeff MrBE4R committed
1
2
3
4
5
6
7
8
9
from flask import Flask, redirect, url_for, session, request, render_template, render_template_string
from sqlalchemy import select, and_
import config
import hashlib
import database

app = Flask(__name__)
app.config.from_object(config.ProductionConfig)

Jeff MrBE4R's avatar
Jeff MrBE4R committed
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

def get_all_users():
    try:
        users = config.Config.conn.execute(
            select(
            [
                database.users.c.id,
                database.users.c.login,
                database.users.c.fullname,
                database.users.c.email,
                database.users.c.can_view_admin,
                database.users.c.enabled,
            ]
            )
        )
    except:
        users = []
    return users


def get_user_by_id( u_id ):
    try:
        user = config.Config.conn.execute(
            select(
                [
                    database.users.c.id,
                    database.users.c.login,
                    database.users.c.fullname,
                    database.users.c.email,
                    database.users.c.can_view_admin,
                    database.users.c.enabled,
                ]
            ).where(
                database.users.c.id == int(u_id)
            )
        )
    except:
        user = []
    return user


def get_all_vms():
    try:
        vms = config.Config.conn.execute(
            select(
Jeff MrBE4R's avatar
Jeff MrBE4R committed
55
56
57
58
59
60
                [
                    database.vms.c.id,
                    database.vms.c.token,
                    database.vms.c.ip,
                    database.vms.c.enabled,
                ]
Jeff MrBE4R's avatar
Jeff MrBE4R committed
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
            )
        )
    except:
        vms = []
    return vms


def get_vm_by_id( u_id ):
    try:
        vm = config.Config.conn.execute(
            select(
                [
                    database.vms.c.id,
                    database.vms.c.token,
                    database.vms.c.ip,
                    database.vms.c.enabled,
                ]
            ).where(
                database.vms.c.id == int(u_id)
            )
        )
    except:
        vm = []
    return vm


def get_all_links():
    try:
        links = config.Config.conn.execute(
            select(
            [
Jeff MrBE4R's avatar
Jeff MrBE4R committed
92
                database.users_vms.c.id,
Jeff MrBE4R's avatar
Jeff MrBE4R committed
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
                database.users.c.fullname,
                database.vms.c.token,
            ]
            ).where(
                and_(
                    database.users.c.id == database.users_vms.c.user,
                    database.vms.c.id == database.users_vms.c.vm
                )
            )
        )
    except:
        links = []
    return links


def get_link_by_id( u_id ):
    try:
        link = config.Config.conn.execute(
            select(
                [
                    database.users.c.fullname,
                    database.vms.c.token,
                ]
            ).where(
                and_(
                    database.users.c.id == database.users_vms.c.user,
                    database.vms.c.id == database.users_vms.c.vm,
                    database.users_vms.c.id == int(u_id)
                )
            )
        )
    except:
        link = []
    return link


Jeff MrBE4R's avatar
Jeff MrBE4R committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def get_link_for_user( u_id ):
    try:
        link = config.Config.conn.execute(
            select(
                [
                    database.vms.c.token,
                ]
            ).where(
                and_(
                    database.users_vms.c.user == int(u_id),
                    database.vms.c.id == database.users_vms.c.vm,
                    database.vms.c.enabled == True,
                )
            )
        )
    except:
        link = []
    return link

Jeff MrBE4R's avatar
Jeff MrBE4R committed
148
149
150
151
152
153
154
@app.route('/')
def home_page():
    if 'logged_in' in session:
        return render_template('home.html', APP_NAME=config.Config.APP_NAME, APP_VERSION=config.Config.APP_VERSION)
    else:
        return redirect(url_for('login'))

Jeff MrBE4R's avatar
Jeff MrBE4R committed
155

Jeff MrBE4R's avatar
Jeff MrBE4R committed
156
157
158
159
160
161
162
163
164
165
166
167
168
@app.route('/login', methods=['GET', 'POST'])
def login():
    if 'logged_in' in session:
        return redirect(url_for('dashboard'))
    else:
        error = None
        if request.method == 'POST':
            s = select(
                [
                    database.users.c.id,
                    database.users.c.login,
                    database.users.c.fullname,
                    database.users.c.email,
Jeff MrBE4R's avatar
Jeff MrBE4R committed
169
                    database.users.c.can_view_admin,
Jeff MrBE4R's avatar
Jeff MrBE4R committed
170
171
172
173
                ]
            ).where(
                and_(
                    database.users.c.login == request.form['username'],
Jeff MrBE4R's avatar
Jeff MrBE4R committed
174
175
176
                    database.users.c.enabled == True,
                    database.users.c.password == hashlib.sha256(request.form['password'].encode()).hexdigest(),

Jeff MrBE4R's avatar
Jeff MrBE4R committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
                )
            )

            account = ''

            for row in config.Config.conn.execute(s):
                account = row

            if len(account) == 0:
                error = 'Invalid credentials'
            else:
                session['logged_in'] = True
                session['userid'] = str(account['id'])
                session['login'] = str(account['login'])
                session['fullname'] = str(account['fullname'])
Jeff MrBE4R's avatar
Jeff MrBE4R committed
192
                session['can_view_admin'] = str(account['can_view_admin'])
Jeff MrBE4R's avatar
Jeff MrBE4R committed
193
194
195
196
197
198
199
200
201
202
203
204
205
                session['email'] = str(account['email'])

                return redirect(url_for('home_page'))

        return render_template('login.html', APP_NAME=config.Config.APP_NAME, APP_VERSION=config.Config.APP_VERSION, error=error)


@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    session.clear()
    return redirect(url_for('home_page'))

Jeff MrBE4R's avatar
Jeff MrBE4R committed
206

Jeff MrBE4R's avatar
Jeff MrBE4R committed
207
208
209
@app.route('/classroom')
def classroom():
    if 'logged_in' in session:
Jeff MrBE4R's avatar
Jeff MrBE4R committed
210
211
        a = get_link_for_user(session['userid'])
        return render_template('console.html', APP_NAME=config.Config.APP_NAME, APP_VERSION=config.Config.APP_VERSION, instance_uid=get_link_for_user(session['userid']))
Jeff MrBE4R's avatar
Jeff MrBE4R committed
212
213
214
    else:
        return redirect(url_for('login'))

Jeff MrBE4R's avatar
Jeff MrBE4R committed
215

Jeff MrBE4R's avatar
Jeff MrBE4R committed
216
217
218
219
220
@app.route('/install')
def install():
    database.createTables()
    database.createBaseUsers()

Jeff MrBE4R's avatar
Jeff MrBE4R committed
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

@app.route('/admin/users', methods=['GET', 'POST'])
def admin_user():
    if 'logged_in' in session:
        if session['can_view_admin'] == str('True').decode('utf8'):
            if request.method == 'POST':
                if str(request.form['action']).decode('utf8') == str('Edit').decode('utf8'):
                    user = get_user_by_id(int(request.form['id']))
                    for u in user:
                        user = u
                    return render_template('Admin/Edit/adminEditUser.html', APP_NAME=config.Config.APP_NAME, APP_VERSION=config.Config.APP_VERSION, user=user)
                elif str(request.form['action']).decode('utf8') == str('Save').decode('utf8'):
                    if request.form.get('can_view_admin'):
                        can_view_admin = True
                    else:
                        can_view_admin = False

                    if request.form.get('enabled'):
                        enabled = True
                    else:
                        enabled = False

                    if str(request.form['login']).decode('utf8') and str(request.form['fullname']).decode('utf8') and str(request.form['email']).decode('utf8'):
                        if str(request.form['password']).decode('utf8') and str(request.form['password_confirm']).decode('utf8'):
                            if str(request.form['password']).decode('utf8') == str(request.form['password_confirm']).decode('utf8'):
                                try:
                                    config.Config.conn.execute(
                                        database.users.update().values(
                                            login=str(request.form['login']).decode('utf8'),
                                            fullname=str(request.form['fullname']).decode('utf8'),
                                            password=str(hashlib.sha256(str(request.form['password']).decode('utf8')).hexdigest()).decode('utf8'),
                                            email=str(request.form['email']).decode('utf8'),
                                            can_view_admin=can_view_admin,
                                            enabled=enabled,
                                        ).where(
                                            database.users.c.id == int(request.form['id'])
                                        )
                                    )
                                except:
                                    pass
                        try:
                            config.Config.conn.execute(
                                database.users.update().values(
                                    login=str(request.form['login']).decode('utf8'),
                                    fullname=str(request.form['fullname']).decode('utf8'),
                                    email=str(request.form['email']).decode('utf8'),
                                    can_view_admin=can_view_admin,
                                    enabled=enabled,
                                ).where(
                                    database.users.c.id == int(request.form['id'])
                                )
                            )
                        except:
                            pass
                elif request.form['action'] == str('Delete').decode('utf8'):
                    if str(request.form['id']).decode('utf8'):
                        try:
                            config.Config.conn.execute(
                                database.users.delete().where(
                                    database.users.c.id == int(request.form['id'])
                                )
                            )
                        except:
                            pass
                elif request.form['action'] == str('Add').decode('utf8'):
                    if str(request.form['login']).decode('utf8') \
                            and str(request.form['password']).decode('utf8') \
                            and str(request.form['password_confirm']).decode('utf8') \
                            and str(request.form['fullname']).decode('utf8') \
                            and str(request.form['email']).decode('utf8'):
                        try:
                            config.Config.conn.execute(
                                database.users.insert().values(
                                    login=str(request.form['login']).decode('utf8'),
                                    password=str(hashlib.sha256(str(request.form['password']).decode('utf8')).hexdigest()).decode('utf8'),
                                    fullname=str(request.form['fullname']).decode('utf8'),
                                    email=str(request.form['email']).decode('utf8')
                                )
                            )
                        except:
                            pass
                return redirect(url_for('admin_user'))
            elif request.method == 'GET':
                return render_template('Admin/View/adminListUsers.html', APP_NAME=config.Config.APP_NAME, APP_VERSION=config.Config.APP_VERSION, users=get_all_users())
        else:
            return redirect(url_for('home_page'))
    else:
        return redirect(url_for('login'))


@app.route('/admin/vms', methods=['GET', 'POST'])
def admin_vm():
    if 'logged_in' in session:
        if session['can_view_admin'] == str('True').decode('utf8'):
            if request.method == 'POST':
                if str(request.form['action']).decode('utf8') == str('Edit').decode('utf8'):
                    vm = get_vm_by_id(int(request.form['id']))
                    for u in vm:
                        vm = u
Jeff MrBE4R's avatar
Jeff MrBE4R committed
320
                    return render_template('Admin/Edit/adminEditVM.html', APP_NAME=config.Config.APP_NAME, APP_VERSION=config.Config.APP_VERSION, vm=vm)
Jeff MrBE4R's avatar
Jeff MrBE4R committed
321
322
323
324
325
326
                elif str(request.form['action']).decode('utf8') == str('Save').decode('utf8'):
                    if request.form.get('enabled'):
                        enabled = True
                    else:
                        enabled = False

Jeff MrBE4R's avatar
Jeff MrBE4R committed
327
328
329
330
331
332
333
334
                    try:
                        config.Config.conn.execute(
                            database.vms.update().values(
                                token=str(request.form['token']).decode('utf8'),
                                ip=str(request.form['ip']).decode('utf8'),
                                enabled=enabled,
                            ).where(
                                database.vms.c.id == int(request.form['id'])
Jeff MrBE4R's avatar
Jeff MrBE4R committed
335
                            )
Jeff MrBE4R's avatar
Jeff MrBE4R committed
336
337
338
339
340
                        )
                    except:
                        import sys
                        print sys.exc_info()
                        pass
Jeff MrBE4R's avatar
Jeff MrBE4R committed
341
342
343
344
345
346
347
348
349
350
351
                elif request.form['action'] == str('Delete').decode('utf8'):
                    if str(request.form['id']).decode('utf8'):
                        try:
                            config.Config.conn.execute(
                                database.vms.delete().where(
                                    database.vms.c.id == int(request.form['id'])
                                )
                            )
                        except:
                            pass
                elif request.form['action'] == str('Add').decode('utf8'):
Jeff MrBE4R's avatar
Jeff MrBE4R committed
352
                    if str(request.form['token']).decode('utf8') and str(request.form['ip']).decode('utf8'):
Jeff MrBE4R's avatar
Jeff MrBE4R committed
353
354
355
                        try:
                            config.Config.conn.execute(
                                database.vms.insert().values(
Jeff MrBE4R's avatar
Jeff MrBE4R committed
356
357
                                    token=str(request.form['token']).decode('utf8'),
                                    ip=str(request.form['ip']).decode('utf8'),
Jeff MrBE4R's avatar
Jeff MrBE4R committed
358
359
360
361
                                )
                            )
                        except:
                            pass
Jeff MrBE4R's avatar
Jeff MrBE4R committed
362
363
364
                elif request.form['action'] == str('token.cfg').decode('utf8)'):
                    ### Backup token file
                    fobj_in = open('/opt/noVNC/token.cfg')
Jeff MrBE4R's avatar
Jeff MrBE4R committed
365
                    #fobj_in = open('./token.cfg')
Jeff MrBE4R's avatar
Jeff MrBE4R committed
366
                    fobj_out = open('/opt/noVNC/token.cfg.bkp', "w")
Jeff MrBE4R's avatar
Jeff MrBE4R committed
367
                    #fobj_out = open('./token.cfg.bkp', "w")
Jeff MrBE4R's avatar
Jeff MrBE4R committed
368
369
370
371
372
373
                    fobj_out.truncate()
                    for line in fobj_in:
                        fobj_out.write(line)
                    fobj_in.close()
                    fobj_out.close()
                    fobj_out = open('/opt/noVNC/token.cfg', 'w')
Jeff MrBE4R's avatar
Jeff MrBE4R committed
374
                    #fobj_out = open('./token.cfg', 'w')
Jeff MrBE4R's avatar
Jeff MrBE4R committed
375
376
377
378
379
380
381
382
383
384
385
                    res = config.Config.conn.execute(
                        select(
                            [
                                database.vms.c.token,
                                database.vms.c.ip
                            ]
                        ).where(
                            database.vms.c.enabled == True
                        )
                    )
                    for row in res:
Jeff MrBE4R's avatar
Jeff MrBE4R committed
386
                        fobj_out.write(str(str(row['token']) + ': ' + str(row['ip']) + ':5901\n'))
Jeff MrBE4R's avatar
Jeff MrBE4R committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
                return redirect(url_for('admin_vm'))
            elif request.method == 'GET':
                return render_template('Admin/View/adminListVMs.html', APP_NAME=config.Config.APP_NAME, APP_VERSION=config.Config.APP_VERSION, vms=get_all_vms())
        else:
            return redirect(url_for('home_page'))
    else:
        return redirect(url_for('login'))


@app.route('/admin/links', methods=['GET', 'POST'])
def admin_link():
    if 'logged_in' in session:
        if session['can_view_admin'] == str('True').decode('utf8'):
            if request.method == 'POST':
Jeff MrBE4R's avatar
Jeff MrBE4R committed
401
                if request.form['action'] == str('Delete').decode('utf8'):
Jeff MrBE4R's avatar
Jeff MrBE4R committed
402
403
404
                    if str(request.form['id']).decode('utf8'):
                        try:
                            config.Config.conn.execute(
Jeff MrBE4R's avatar
Jeff MrBE4R committed
405
406
                                database.users_vms.delete().where(
                                    database.users_vms.c.id == int(request.form['id'])
Jeff MrBE4R's avatar
Jeff MrBE4R committed
407
408
409
410
411
                                )
                            )
                        except:
                            pass
                elif request.form['action'] == str('Add').decode('utf8'):
Jeff MrBE4R's avatar
Jeff MrBE4R committed
412
413
                    if str(request.form['user-to-link']).decode('utf8') \
                            and str(request.form['server-to-link']).decode('utf8'):
Jeff MrBE4R's avatar
Jeff MrBE4R committed
414
415
                        try:
                            config.Config.conn.execute(
Jeff MrBE4R's avatar
Jeff MrBE4R committed
416
417
418
                                database.users_vms.insert().values(
                                    user=int(request.form['user-to-link']),
                                    vm=int(request.form['server-to-link']),
Jeff MrBE4R's avatar
Jeff MrBE4R committed
419
420
421
                                )
                            )
                        except:
Jeff MrBE4R's avatar
Jeff MrBE4R committed
422
423
                            import sys,traceback
                            print sys.exc_info()
Jeff MrBE4R's avatar
Jeff MrBE4R committed
424
                            pass
Jeff MrBE4R's avatar
Jeff MrBE4R committed
425
                return redirect(url_for('admin_link'))
Jeff MrBE4R's avatar
Jeff MrBE4R committed
426
            elif request.method == 'GET':
Jeff MrBE4R's avatar
Jeff MrBE4R committed
427
                return render_template('Admin/View/adminListLinks.html', APP_NAME=config.Config.APP_NAME, APP_VERSION=config.Config.APP_VERSION, links=get_all_links(), users=get_all_users(), vms=get_all_vms())
Jeff MrBE4R's avatar
Jeff MrBE4R committed
428
429
430
431
432
        else:
            return redirect(url_for('home_page'))
    else:
        return redirect(url_for('login'))

Jeff MrBE4R's avatar
Jeff MrBE4R committed
433
434
if __name__=='__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)