-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
94 lines (72 loc) · 2.16 KB
/
app.py
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
# flask_web/app.py
import flask_login
import os
import pyotp
import flask
app = flask.Flask(__name__)
app.secret_key = 'thisisnotastring'
login_manager = flask_login.LoginManager()
login_manager.init_app(app)
class User(flask_login.UserMixin):
pass
@login_manager.user_loader
def user_loader(email):
user = User()
user.id = "gt"
return user
@login_manager.request_loader
def request_loader(request):
otp = request.form.get('password')
if otp == None:
return
user = User()
user.id = "gt"
user.is_authenticated = True
return user
@app.route('/login', methods=['GET', 'POST'])
def login():
if flask.request.method == 'GET':
return flask.render_template("form.html")
if flask.request.form['password'] == pyotp.TOTP('thisisasecret').now():
user = User()
user.id = "gt"
flask_login.login_user(user)
return flask.redirect('/')
return flask.redirect('/login')
@flask_login.login_required
@app.route('/', defaults={'req_path': ''})
@app.route('/<path:req_path>')
def dir_listing(req_path):
if not hasattr(flask_login.current_user, "id"):
return flask.redirect("/login")
BASE_DIR = '/var/lib/deluge/Downloads'
# Joining the base and the requested path
abs_path = os.path.join(BASE_DIR, req_path)
# Return 404 if path doesn't exist
if not os.path.exists(abs_path):
return flask.abort(404)
# Check if path is a file and serve
if os.path.isfile(abs_path):
return flask.send_file(abs_path)
# Show directory contents
files = os.listdir(abs_path)
return flask.render_template('files.html', files=files)
@app.route('/logout')
def logout():
flask_login.logout_user()
return 'Logged out'
@login_manager.unauthorized_handler
def unauthorized_handler():
return 'Unauthorized'
#@app.route('/')
#def my_form():
# return render_template('form.html')
#@app.route('/', methods=['POST'])
#def my_form_post():
# totp = pyotp.TOTP('thisisnotasecret')
# text = request.form['otp']
# if text==totp.now():
# return "authenticated"
# return "nah!"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')