-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
339 lines (262 loc) · 11.9 KB
/
main.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
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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# Copyright (C) -present by FlaskUtilityServer@Github, < https://github.com/StarkGang >.
#
# This file is part of < https://github.com/Starkgang/FlaskUtilityServer > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/Starkgang/FlaskUtilityServer/blob/main/LICENSE >
#
# All rights reserved.
import base64
from flask import Flask, render_template, request, make_response, send_file, jsonify, session, redirect, url_for
from os import path as os_path
from werkzeug.utils import secure_filename
from time import time_ns
from pyperclip import copy as copy_to_clipboard
from PIL import ImageGrab
from config import CONFIG
from flask_socketio import SocketIO
from term_utils import UTILS
from passlib.hash import sha256_crypt
from utils import *
app = Flask(CONFIG.APP_NAME, template_folder='templates', static_folder='static')
base_dl_path = CONFIG.DOWNLOAD_FOLDER
upload_to = CONFIG.UPLOAD_FOLDER
app.secret_key = CONFIG.SECRET_KEY
valid_username = CONFIG.SECRET_USERNAME
hashed_password = sha256_crypt.hash(CONFIG.SECRET_PASSWORD)
socketio = SocketIO(app)
excluded_paths = ['/static', '/favicon.ico', '/login', '/login_page', '/templates', '/static/img', '/matrix']
@app.errorhandler(Exception)
def handle_error(error):
"""
Handles the error raised by the Flask application.
The function prints the error message, retrieves the error code from the error object (defaulting to 500 if not available), and converts the error object to a string for error details. It then renders the 'error.html' template with the error code and error details.
Args:
error: The error object.
Returns:
The rendered template.
"""
error_code = getattr(error, 'code', 500)
error_details = str(error)
return render_template('error.html', error_code=error_code, error_details=error_details)
@app.before_request
def before_request():
"""
Executes the before_request function for a Flask application.
The function checks if the 'username' key is not present in the session and if the request endpoint is not one of the excluded paths. If both conditions are met, it redirects the user to the '/login_page' endpoint.
Args:
None
Returns:
Redirects to page
"""
if 'username' not in session and request.endpoint not in ['login', 'login_page'] and not any(request.path.startswith(path) for path in excluded_paths):
return redirect('/login_page')
@app.route('/matrix')
def matrix():
"""
Renders the 'matrix.html' template with a speed value of 50.
Args:
None
Returns:
The rendered template.
"""
return render_template('matrix.html', speed=50)
@app.route('/ss')
def ss():
"""
Renders the 'screenshot.html' template.
Args:
None
Returns:
The rendered template.
"""
return render_template('screenshot.html')
@app.route('/screenshot', methods=['POST'])
def capture_screenshot():
"""
Captures a screenshot and returns it as a base64-encoded image.
The function uses the `ImageGrab.grab()` function from the `PIL` library to capture a screenshot. If an exception occurs during the capture, it returns a JSON response with an error message. Otherwise, it saves the screenshot as a PNG image in memory, converts it to base64 encoding, and returns a JSON response with the base64-encoded image.
Args:
None
Returns:
A JSON response containing the base64-encoded image.
"""
try:
screenshot = ImageGrab.grab()
except Exception:
return jsonify(error='Unable to capture screenshot')
img_io = io.BytesIO()
screenshot.save(img_io, format='PNG')
img_io.seek(0)
img_base64 = base64.b64encode(img_io.read()).decode('utf-8')
return jsonify(img_base64=img_base64)
@app.route('/terminal')
def terminal():
"""
Renders the 'terminal.html' template.
Args:
None
Returns:
The rendered template.
"""
return render_template('terminal.html')
@app.route('/login_page/')
def login_page():
"""
Handles the login process for the '/login' endpoint.
The function checks if the 'username' key is already present in the session. If it is, the user is redirected to the root URL '/'. Otherwise, the function retrieves the 'username' and 'password' values from the request form. If the provided username matches the valid username and the provided password matches the hashed password, the user is authenticated by adding the 'username' key to the session and returning a JSON response with a 'success' value of True. Otherwise, a JSON response with an 'error' message is returned.
Args:
None
Returns:
A JSON response indicating the success or failure of the login process.
"""
if 'username' in session:
return redirect("/")
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login_p():
"""
Handles the login process for the '/login' endpoint.
The function checks if the 'username' key is already present in the session. If it is, the user is redirected to the root URL '/'. Otherwise, the function retrieves the 'username' and 'password' values from the request form. If the provided username matches the valid username and the provided password matches the hashed password, the user is authenticated by adding the 'username' key to the session and returning a JSON response with a 'success' value of True. Otherwise, a JSON response with an 'error' message is returned.
Args:
None
Returns:
A JSON response indicating the success or failure of the login process.
"""
if 'username' in session:
return redirect("/")
username = request.form.get('username')
password = request.form.get('password')
if username == valid_username and sha256_crypt.verify(password, hashed_password):
session['username'] = username
return jsonify({'success': True})
else:
return jsonify({'error': 'Invalid credentials'})
@socketio.on('execute_code')
def execute_code(data):
"""
Executes the code provided by the client.
The function first checks if the user is not logged in and the current endpoint is not 'login' or 'login_page', and the request path does not start with any of the excluded paths. If these conditions are met, the user is redirected to the '/login_page' endpoint. Otherwise, the function retrieves the code from the 'data' parameter and initializes a UTILS object. It then executes different commands based on the prefix of the code. If the code starts with 'weather', it calls the 'get_weather' method of the UTILS object. If the code starts with 'neofetch', it calls the 'neofetch' method of the UTILS object. Otherwise, it runs the command using the 'run_command' function and captures the output and error. The function then constructs a response string by decoding the output and error, and emits the response to the client using the 'socketio.emit' function.
Args:
data (dict): A dictionary containing the code provided by the client.
Returns:
None
"""
if 'username' not in session and request.endpoint not in ['login', 'login_page'] and not any(request.path.startswith(path) for path in excluded_paths):
return redirect('/login_page')
command = data['code']
utils = UTILS()
to_return = ""
if command.startswith("weather"):
to_return += utils.get_weather(command.split(" ")[1])
elif command.startswith("neofetch"):
to_return += utils.neofetch()
else:
output, error = run_command(command)
if not output:
to_return += error.decode('utf-8')
to_return += output.decode('utf-8')
socketio.emit('execution_result', {'result': to_return})
@app.route('/sys_info')
def si():
"""
Renders the 'system_info.html' template with system information.
The function retrieves various system information using helper functions and stores them in a context dictionary. The context dictionary includes information about the platform, power, user, memory, disks, and network. The function then renders the 'system_info.html' template with the context as a parameter.
Args:
None
Returns:
The rendered template with system information.
"""
context = {
'platform_info': get_platform_info(),
'power_info': get_power_info(),
'user_info': get_user_info(),
'memory_info': get_memory_info(),
'disk_info': get_disks_info(),
'network_info': get_network_info(),
}
return render_template("system_info.html", context=context)
@app.route('/favicon.ico')
def favicon():
"""
Serves the favicon.ico file.
The function returns the favicon.ico file located at './static/img/icon.png' with the mimetype 'image/png'.
Args:
None
Returns:
The favicon.ico file as a response.
"""
return send_file('./static/img/icon.png', mimetype='image/png')
@app.route('/')
@app.route('/index/')
def home():
return render_template('index.html')
@app.route("/sysreq/", methods=['POST'])
def sysreq():
password = int(request.form.get('password'))
if password == CONFIG.SYSREQ_PASSWORD:
handle_system_opp(1)
else:
return "Error"
return "shutting down"
@app.route('/copy', methods=['GET'])
def copy():
return render_template('copy.html')
@app.route('/copy', methods=['POST'])
def submit():
copy_text = request.form.get('copy_text')
copy_utils = COPY_UTILS()
if valid := URL_VALIDATOR(copy_text)():
copy_utils.open_and_write_url_to_file(copy_text, copy_utils.request_and_return_page_name(copy_text), valid)
app.logger.info(f'Copied! \nInput: {copy_text}')
copy_to_clipboard(copy_text)
return "Copied!"
@app.route('/upload')
def index():
return render_template('upload.html',
page_name='Main',
project_name="pydrop")
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
save_path = os_path.join(upload_to, secure_filename(file.filename))
current_chunk = int(request.form['dzchunkindex'])
if os_path.exists(save_path) and current_chunk == 0:
return make_response(('File already exists', 400))
try:
with open(save_path, 'ab') as f:
f.seek(int(request.form['dzchunkbyteoffset']))
f.write(file.stream.read())
except OSError:
app.logger.error('Could not write to file')
return make_response(("Not sure why,"
" but we couldn't write the file to disk", 500))
total_chunks = int(request.form['dztotalchunkcount'])
if current_chunk + 1 == total_chunks:
if os_path.getsize(save_path) != int(request.form['dztotalfilesize']):
app.logger.error(f"File {file.filename} was completed, "
f"but has a size mismatch."
f"Was {os_path.getsize(save_path)} but we"
f" expected {request.form['dztotalfilesize']} ")
return make_response(('Size mismatch', 500))
else:
app.logger.info(f'File {file.filename} has been uploaded successfully')
return "Chunk upload successful"
@app.route('/files/')
def files():
search_query = request.args.get('search', '').lower()
path = request.args.get('path', '')
files_and_folders = get_files_and_folders(base_dl_path, path, search_query)
return render_template('files.html', files=files_and_folders, search_query=search_query, current_path=path)
@app.route('/download/<path:filename>')
def download(filename):
file_path = os_path.join(base_dl_path, filename)
return send_file(file_path, as_attachment=True)
@app.route('/folder')
def load_folder_content():
folder_path = request.args.get('path', '')
files_and_folders = get_files_and_folders(base_dl_path, folder_path)
return render_template('folder_content.html', files=files_and_folders)
if __name__ == '__main__':
ipv4_add = CONFIG.FLASK_APP_HOST or get_ipv4_address(logger=app.logger)
app.logger.info(f'Server started! \nUsing Host : {ipv4_add} and Port : {CONFIG.FLASK_APP_PORT} \nTime : {CONVERT_TO_RD_TIME(time_ns())()}')
app.run(host=ipv4_add, port=CONFIG.FLASK_APP_PORT, debug=CONFIG.DEBUG)