-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
121 lines (102 loc) · 3.68 KB
/
main.py
File metadata and controls
121 lines (102 loc) · 3.68 KB
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
import secrets
import string
import yaml
from flask import Flask, Response, send_from_directory, request, session, abort, render_template, url_for, redirect
from os import environ, listdir
from os.path import getsize, isdir
with open("config/config.yaml", "r", encoding="utf-8") as file:
cfg = yaml.load(file, Loader=yaml.SafeLoader)
with open("tmp/path.tmp", "r", encoding="utf-8") as file:
path = file.read()
app = Flask(__name__)
app.secret_key = secrets.token_hex(64)
try:
secure = str(cfg["custom"]["passkey"])
except (KeyError, TypeError):
alphabet = string.ascii_letters + string.digits
secure = ''.join(secrets.choice(alphabet) for _ in range(64))
try:
provider = str(cfg["custom"]["username"])
except (KeyError, TypeError):
try:
provider = environ['USERNAME']
except KeyError:
provider = environ['USER']
if provider == "root":
provider = "admin"
with open("tmp/daemon.yaml.tmp", "w", encoding="utf-8") as file:
wordlist = {
"provider": f"{provider}",
"secure": f"{secure}"
}
yaml.safe_dump(wordlist, file, allow_unicode=True)
print(f" * Host: {provider}")
print(f" * Passkey: {secure}\n")
def sizer(size_bytes):
for unit in ['B', 'KB', 'MB', 'GB']:
if size_bytes < 1024:
return f"{size_bytes:.2f} {unit}"
size_bytes /= 1024
else:
return f"{size_bytes:.2f} TB"
def show(site):
with open(site, "r", encoding="utf-8") as file:
return file.read()
@app.errorhandler(404)
def page_not_found(error):
return render_template("404.html"), 404
@app.errorhandler(403)
def forbidden(error):
return render_template("403.html"), 403
@app.errorhandler(500)
def internal_error(error):
return render_template("500.html"), 500
@app.route('/')
def index():
return show("w3/index.html") + f"""<title>Filelink - {provider}</title>""" + f"""<h6>Hosted by: {provider}</h6></body></html>"""
@app.route('/success')
def success():
files = ""
num = 1
if not session.get('authorized'):
return abort(403)
if not listdir(path):
return show("w3/success.html") + f"""<h5>no files available</h5>""" + f"""<h6>Hosted by: {provider}</h6></body></html>"""
for i in listdir(path):
if isdir(f"{path}/{i}"):
pass
else:
size = sizer(getsize(f"{path}/{i}"))
files += f"""<h5>{num}. <a href="/download/{i}">{i}</a> ({size})</h5>"""
num += 1
return show("w3/success.html") + f"""<title>Files - {provider}</title>""" + files + f"""<h6>{num-1} files. Hosted by: {provider}</h6></body></html>"""
@app.route('/access')
def access():
key = request.args.get('passkey')
if key and key == secure:
session['authorized'] = True
if session.get('authorized'):
return redirect(url_for("success"))
session['allow_get_key'] = True
return show("w3/access.html")
@app.route('/get-key')
def get_key():
if not session.get('allow_get_key'):
return abort(403)
session.pop('allow_get_key')
return Response(secure, mimetype='text/plain')
@app.route('/check-key', methods=['POST'])
def check_password():
user_pass = request.form.get('password', '')
if user_pass == secure:
session['authorized'] = True
return "OK"
return "FAIL", 403
@app.route('/download/<name>', methods=['GET'])
def download_file(name):
key = request.args.get('passkey')
if key and key == secure:
session['authorized'] = True
if not session.get('authorized'):
return abort(403)
return send_from_directory(path, name, as_attachment=True)