forked from Lunes-Hosting/controlpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
350 lines (266 loc) · 12 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
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
340
341
342
343
344
345
346
347
348
349
350
from flask import Flask
from flask_apscheduler import APScheduler
from flask_caching import Cache
from flask_limiter import Limiter
from flask_mail import Mail, Message
from config.config import *
from Routes.AuthenticationHandler import *
from Routes.Servers import *
from Routes.Store import *
from Routes.Admin import *
from flask_session import Session
from multiprocessing import Process
from bot import enable_bot
import asyncio
import random
#This imports the bot's code ONLY if the user wishes to use it
app = Flask(__name__, "/static")
def rate_limit_key():
# Replace 'user_id' with the actual key used to store the user identifier in the session
user_identifier = session.get('random_id', None)
print(user_identifier, 1)
return user_identifier
limiter = Limiter(rate_limit_key, app=app, default_limits=["200 per day", "50 per hour"])
limiter.limit("20/hour", key_func=rate_limit_key)(user)
limiter.limit("15/hour", key_func=rate_limit_key)(servers)
app.register_blueprint(user)
app.register_blueprint(servers, url_prefix="/servers")
app.register_blueprint(store, url_prefix="/store")
app.register_blueprint(admin, url_prefix="/admin")
class Config:
SCHEDULER_API_ENABLED = True
app.config["SESSION_PERMANENT"] = True
app.config["SESSION_TYPE"] = "filesystem"
app.config["SECRET_KEY"] = SECRET_KEY
# Initialize the session
Session(app)
app.config.from_object(Config())
# initialize scheduler
scheduler = APScheduler()
# if you don't want to use a config, you can set options here:
# scheduler.api_enabled = True
scheduler.init_app(app)
app.config['MAIL_SERVER'] = MAIL_SERVER
app.config['MAIL_PORT'] = MAIL_PORT
app.config['MAIL_USE_SSL'] = os.getenv('MAIL_USE_SSL', 'False').lower() in ['true', '1', 'yes']
app.config['MAIL_USERNAME'] = MAIL_USERNAME
app.config['MAIL_PASSWORD'] = MAIL_PASSWORD
app.config['MAIL_DEFAULT_SENDER'] = MAIL_DEFAULT_SENDER
app.config['TURNSTILE_PUBLIC_KEY'] = TURNSTILE_SITE_KEY
app.config['TURNSTILE_SECRET_KEY'] = TURNSTILE_SECRET_KEY
mail = Mail(app)
@app.route('/send-mail')
def send_mail():
msg = Message('Test Subject', recipients=['[email protected]'])
msg.body = 'This is a test email sent from a Flask application.'
mail.send(msg)
return 'Mail sent!'
# Configuration for Flask-Caching
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
# Placeholder for the password reset token expiration time (in seconds)
TOKEN_EXPIRATION_TIME = 1800 # 30 minutes
# A dictionary to store the reset tokens temporarily
reset_tokens = {}
# Function to generate a random reset token
def generate_reset_token():
return ''.join(random.choices(string.ascii_letters + string.digits, k=20))
def send_email(email: str, reset_token: str, inner_app):
with inner_app.app_context():
msg = Message('Password Reset Request', recipients=[email])
msg.body = f'Please click the link below to reset your password:\n\n {HOSTED_URL}reset_password/{reset_token}'
mail.send(msg)
# Route to request a password reset (via email)
@app.route('/reset_password', methods=['GET', 'POST'])
def reset_password():
if request.method == 'POST':
email = request.form.get('email')
# Check if the email exists in your user database
# Replace this with your own logic to validate the email
# Generate a reset token
reset_token = generate_reset_token()
# Store the reset token in the cache with the email as the key
cache.set(email, reset_token, timeout=TOKEN_EXPIRATION_TIME)
# Compose and send the reset email
email_thread = threading.Thread(target=send_email, args=(str(email), reset_token, app,), daemon=True)
email_thread.start()
flash('An email with instructions to reset your password has been sent.')
return redirect(url_for('user.login_user'))
return render_template('reset_password.html')
# Route to confirm the password reset using the token
@app.route('/reset_password/<token>', methods=['GET', 'POST'])
def reset_password_confirm(token):
print(token)
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
confirm_password = request.form.get('confirm_password')
tokendone = request.form.get("token")
# Check if the email exists in your user database
# Replace this with your own logic to validate the email
# Retrieve the reset token from the cache using the email as the key
reset_token = cache.get(email)
if reset_token and reset_token == tokendone:
if password == confirm_password:
# Update the password in your user database
# Replace this with your own logic to update the password
# Remove the reset token from the cache
cache.delete(email)
salt = bcrypt.gensalt(rounds=10)
cnx = mysql.connector.connect(
host=HOST,
user=USER,
password=PASSWORD,
database=DATABASE
)
password_hash = bcrypt.hashpw(password.encode('utf-8'), salt)
cursor = cnx.cursor()
query = f"UPDATE users SET password = %s WHERE email = %s"
ptero_id = get_ptero_id(email)
values = (password_hash.decode(), email)
print(password_hash, email, type(email))
cursor.execute(query, values)
info = requests.get(f"{PTERODACTYL_URL}api/application/users/{ptero_id[0]}", headers=HEADERS).json()[
'attributes']
body = {
"username": info['username'],
"email": info['email'],
"first_name": info['first_name'],
"last_name": info['last_name'],
"password": password
}
requests.patch(f"{PTERODACTYL_URL}api/application/users/{ptero_id[0]}", headers=HEADERS,
json=body)
cnx.commit()
cursor.close()
cnx.close()
flash('Your password has been successfully reset.')
return redirect(url_for('user.login_user'))
else:
flash('Passwords do not match.')
else:
flash('Invalid or expired reset token.')
return render_template('reset_password_confirm.html', token=token)
# Function to generate a verification token
def generate_verification_token():
return ''.join(random.choices(string.ascii_letters + string.digits, k=20))
# Function to send a verification email
def send_verification_email(email: str, verification_token: str, inner_app):
with inner_app.app_context():
print("started emails")
msg = Message('Email Verification', recipients=[email])
msg.body = f'Please click the link below to verify your email:\n\n {HOSTED_URL}verify_email/{verification_token}'
mail.send(msg)
print(f"sent email to {email}")
# Modified register_user function
@app.route('/register', methods=['POST', 'GET'])
def register_user():
if request.method == "POST":
turnstile_response = request.form.get('cf-turnstile-response')
data = {
'secret': TURNSTILE_SECRET_KEY,
'response': turnstile_response
}
response = requests.post('https://challenges.cloudflare.com/turnstile/v0/siteverify', data=data)
result = response.json()
if not result['success']:
flash("Failed captcha please try again")
return render_template("register.html", TURNSTILE_PUBLIC_KEY=TURNSTILE_SITE_KEY)
data = request.form
email = data.get('email')
password = data.get('password')
name = data.get('username')
ip = request.headers.get('Cf-Connecting-Ip', request.remote_addr)
res = register(email, password, name, ip)
if type(res) == str:
flash(res + " If this in error please contact support at [email protected]")
return render_template("register.html", TURNSTILE_PUBLIC_KEY=TURNSTILE_SITE_KEY)
# Generate a verification token
verification_token = generate_verification_token()
# Store the verification token in the cache with the email as the key
cache.set(email, verification_token, timeout=TOKEN_EXPIRATION_TIME)
# Compose and send the verification email
email_thread = threading.Thread(target=send_verification_email, args=(email, verification_token, app,),
daemon=True)
email_thread.start()
# TEMP REMOVE REQUIRED EMAIL VERIF
query = f"UPDATE users SET email_verified_at = '{datetime.datetime.now()}' where email = %s"
use_database(query, (email,))
# TEMP REMOVE REQUIRED EMAIL VERIF
flash(
'A verification email has been sent to your email address. Please check your inbox and spam to verify '
'your email.')
return redirect(url_for('index'))
else:
return render_template("register.html", TURNSTILE_PUBLIC_KEY=TURNSTILE_SITE_KEY)
@app.route("/resend_confirmation_email")
def resend_confirmation_email():
if 'email' not in session:
return redirect(url_for("user.login_user"))
after_request(session=session, request=request.environ, require_login=True)
verification_token = generate_verification_token()
# Store the verification token in the cache with the email as the key
cache.set(session['email'], verification_token, timeout=TOKEN_EXPIRATION_TIME)
# Compose and send the verification email
email_thread = threading.Thread(target=send_verification_email, args=(session['email'], verification_token, app,),
daemon=True)
email_thread.start()
flash("Sent a verification email")
return redirect(url_for('index'))
# Route to confirm the email using the token
@app.route('/verify_email/<token>', methods=['GET'])
def verify_email(token):
if 'email' not in session:
return redirect(url_for("user.login_user"))
after_request(session=session, request=request.environ, require_login=True)
email = session['email']
# Retrieve the stored verification token from the cache
stored_token = cache.get(email)
if stored_token and stored_token == token:
cnx = mysql.connector.connect(
host=HOST,
user=USER,
password=PASSWORD,
database=DATABASE
)
cursor = cnx.cursor(buffered=True)
query = f"UPDATE users SET email_verified_at = '{datetime.datetime.now()}' where email = %s"
cursor.execute(query, (email,))
cnx.commit()
# Remove the verification token from the cache
cache.delete(email)
flash('Your email has been successfully verified.')
else:
flash('Invalid or expired verification token.')
return redirect(url_for('index'))
@scheduler.task('interval', id='do_job_1', seconds=3600, misfire_grace_time=900)
def job1():
print("started job1")
use_credits()
print("finished job 2")
@scheduler.task('interval', id='do_job_2', seconds=120, misfire_grace_time=900)
def job2():
print("started job2")
check_to_unsuspend()
print("finished job 2")
job_has_run = False
@scheduler.task('interval', id='do_run_job', seconds=5, misfire_grace_time=900)
def run_job():
global job_has_run
print(job_has_run)
if not job_has_run:
asyncio.run(enable_bot())
job_has_run = True
@scheduler.task('interval', id='do_sync_users', seconds=30, misfire_grace_time=900)
def sync_users():
print("started users sync")
sync_users_script()
print("finished job 2")
scheduler.start()
@app.route('/')
def index():
if 'email' not in session:
return redirect(url_for("user.login_user"))
after_request(session=session, request=request.environ, require_login=True)
if __name__ == '__main__':
# Create separate processes for Flask and the Discord bot
app.run(debug=False, host="0.0.0.0", port=80)