MsRewards-Reborn/Flask/app.py

476 lines
14 KiB
Python
Raw Normal View History

2023-06-17 16:52:47 +02:00
from time import sleep
import subprocess
2023-08-21 21:10:55 +02:00
import os
2023-08-21 19:35:27 +02:00
from flask import Flask, Response, redirect, url_for, request, session, abort, render_template, send_from_directory
2023-06-17 16:52:47 +02:00
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user
2023-08-21 21:07:54 +02:00
from werkzeug.utils import secure_filename
2023-06-17 16:52:47 +02:00
import json
2023-06-25 10:28:06 +02:00
import re
2023-08-21 21:10:55 +02:00
global password
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/flask.json", "r") as inFile:
2023-06-19 20:52:05 +02:00
data = json.load(inFile)
password = data["password"]
secret = data["secret"]
if secret == "":
import secrets
secret = secrets.token_hex()
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/flask.json", "w") as inFile:
data = {
2023-06-19 20:52:05 +02:00
"password": password,
"secret": secret
}
2023-06-19 20:52:05 +02:00
json.dump(data, inFile)
2023-06-17 16:52:47 +02:00
"""
#Automatic start of MsRewards
2023-06-17 16:52:47 +02:00
"""
2023-06-29 11:55:49 +02:00
def daily_command():
subprocess.Popen(["git",'pull'])
subprocess.Popen(["pkill -9 chrome"])
subprocess.Popen(["pkill -9 Xvfb"])
subprocess.Popen(["pkill -9 undetected_chromedriver"])
2023-06-29 11:55:49 +02:00
2023-06-17 16:52:47 +02:00
scheduler = BackgroundScheduler()
scheduler.start()
2023-06-29 11:55:49 +02:00
scheduler.add_job( # on relance le job
daily_command, # ---
trigger=CronTrigger(
year="*", month="*", day="*", hour="0", minute="0", second="0"
), # ---
name="Daily refresh", # ---
2023-07-01 14:06:48 +02:00
id="99" # ---
2023-06-29 11:55:49 +02:00
)
2023-06-17 16:52:47 +02:00
2023-06-18 18:32:06 +02:00
def start_ms(i):
print("\033[32m" + f"Starting config {i}" + "\033[0m")
2023-07-02 19:46:37 +02:00
log = open(f"/app/MsRewards-Reborn/user_data/logs/{i}.txt", 'a') # so that data written to it will be appended
subprocess.Popen([f"python3 -u /app/MsRewards-Reborn/V6.py -c {i}"], stdout=log, stderr=log, shell=True)
2023-07-02 19:26:24 +02:00
log.close()
2023-06-17 16:52:47 +02:00
2023-06-29 11:55:49 +02:00
2023-06-18 18:32:06 +02:00
TriggerDict = {}
def update_jobs():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile:
2023-06-18 18:32:06 +02:00
configs = json.load(inFile)
for i in configs:
h, m = configs[i]["time"].split(":")
print("\033[36m" + f"config {i} : {h}:{m}" + "\033[0m")
TriggerDict[i] = CronTrigger(
year="*", month="*", day="*", hour=h, minute=m, second="0"
)
if configs[i]["enabled"]:
2023-06-19 20:52:05 +02:00
try :
2023-06-18 18:32:06 +02:00
scheduler.remove_job(i) # on reset le job
except Exception as e:
print(f"\033[33merror with deleting config {i} : {e}\033[0m")
2023-06-19 20:52:05 +02:00
try :
2023-06-18 18:32:06 +02:00
scheduler.add_job( # on relance le job
start_ms, # ---
trigger=TriggerDict[i], # ---
args=[i], # ---
name="Daily start", # ---
id=i # ---
)
print("\033[36m" + f"successfully created config {i}" + "\033[0m")
except Exception as e:
print(f"\033[33merror with creating config {i} : {e}\033[0m")
else :
try :
scheduler.remove_job(i)
except Exception as e :
print(f"\033[33merror with deleting config {i} : {e}\033[0m")
2023-06-17 16:52:47 +02:00
2023-06-25 09:56:14 +02:00
def edit_version():
with open("/app/MsRewards-Reborn/version", "r") as f:
version = f.readline().replace("\n", '')
f = open("/app/MsRewards-Reborn/Flask/templates/base.html", "r")
txt = f.readlines()
f.close()
2023-06-25 10:24:03 +02:00
f = open("/app/MsRewards-Reborn/Flask/templates/base.html", "w")
2023-06-25 09:56:14 +02:00
for i in txt:
i = re.sub('<div class="footer">([^<]+)</div>', f'<div class="footer">{version}</div>', i)
f.write(i)
f.close()
2023-06-17 16:52:47 +02:00
"""
#Flask app
2023-06-17 16:52:47 +02:00
"""
app = Flask(__name__)
"""
#Login stuff
2023-06-17 16:52:47 +02:00
"""
# config
2023-06-26 21:51:47 +02:00
app.config["TEMPLATES_AUTO_RELOAD"] = True
2023-06-17 16:52:47 +02:00
app.config.update(
SECRET_KEY = secret
)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
# silly user model
class User(UserMixin):
def __init__(self, id):
self.id = id
self.name = "user" + str(id)
self.password = password
2023-06-19 20:52:05 +02:00
2023-06-17 16:52:47 +02:00
def __repr__(self):
return "%d/%s/%s" % (self.id, self.name, self.password)
users = [User(1)]
2023-06-17 16:52:47 +02:00
@app.route("/login/", methods=["GET", "POST"])
def login():
if request.method == 'POST':
if request.form['password'] == password:
user = User(id)
login_user(user)
if password == "ChangeMe":
return(render_template("change_password.html"))
2023-08-22 11:21:42 +02:00
return(redirect('override'))
2023-06-17 16:52:47 +02:00
else:
return abort(401)
else:
return(render_template("login.html"))
@app.route("/change_password/", methods=["GET", "POST"])
@login_required
def change_password():
global password
if request.method == 'POST':
password = request.form["password"]
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/flask.json", "w") as inFile:
data = {
"password": password,
"secret": secret
}
json.dump(data, inFile)
return(render_template("override.html"))
2023-06-18 15:57:41 +02:00
2023-06-17 16:52:47 +02:00
# handle login failed
@app.errorhandler(401)
2023-08-21 21:25:05 +02:00
def unauthorized(e):
2023-06-17 16:52:47 +02:00
return(render_template("login.html"))
2023-06-18 15:58:22 +02:00
2023-06-19 20:52:05 +02:00
# callback to reload the user object
2023-06-17 16:52:47 +02:00
@login_manager.user_loader
def load_user(userid):
return User(userid)
"""
#end of login stuff
2023-06-17 16:52:47 +02:00
"""
@app.route("/", methods=["post"])
def dev():
action = request.form
print(action)
if action == "dev":
print("dev action test")
return(f"<h1> TO IMPLEMENT - {action}</h1>")
@app.route("/")
def main():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile:
2023-06-18 19:21:02 +02:00
configs = json.load(inFile)
return(render_template("override.html", data=configs))
2023-06-17 16:52:47 +02:00
@app.route("/discord/")
def discord_get():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/discord.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
data = json.load(inFile)
2023-06-18 17:40:59 +02:00
return(render_template("discord.html", data=data, len=maxi(data)))
2023-06-17 16:52:47 +02:00
@app.route("/discord/", methods=["post"])
def discord_post():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/discord.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
data = json.load(inFile)
action = request.form
2023-06-18 17:40:59 +02:00
if action['DISCORD'] == "delete" :
data.pop(action["select"], None)
else :
config = action["select"]
successL = action["successL"]
try :
a = action["successT"]
successT = "True"
except:
successT = "False"
try :
a = action["errorsT"]
errorsT = "True"
except:
errorsT = "False"
errorsL = action["errorsL"]
name = action["name"] if action["name"] else f"unnamed{action['select']}"
data[config] = {"errorsL" : errorsL, "errorsT": errorsT, "successT": successT, "successL": successL, "name": name}
2023-06-17 16:52:47 +02:00
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/discord.json", "w") as outFile:
2023-06-17 16:52:47 +02:00
json.dump(data, outFile)
2023-06-18 17:40:59 +02:00
return(render_template("discord.html", data=data, len=maxi(data)))
2023-06-17 16:52:47 +02:00
@app.route("/dev/")
def dev2():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/proxy.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
j = json.load(inFile)
new_proxy = {"address": "ADDRESS", "port": "PORT", "name":"NAME"}
max_index = 0
for i in range(1, 50):
try :
print(j[str(i)])
except :
print(f"found {i - 1} proxys")
max_index = i
break
j[f"{max_index}"] = new_proxy
print(j)
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/proxy.json", "w") as outfile:
2023-06-17 16:52:47 +02:00
json.dump(j, outfile)
return(render_template("dev.html"))
@app.route("/settings/")
def settings_get():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/settings.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
settings = json.load(inFile)
return(render_template("settings.html", data=settings))
2023-06-18 17:40:59 +02:00
2023-06-17 16:52:47 +02:00
@app.route("/settings/", methods=["post"])
def settings_post():
settings = {}
action = request.form
settings['avatarlink'] = action["avatarlink"]
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/settings.json", "w") as inFile:
2023-06-17 16:52:47 +02:00
json.dump(settings, inFile)
return(render_template("settings.html", data=settings))
@app.route("/proxy/")
def proxy_get():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/proxy.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
j = json.load(inFile)
2023-06-18 17:40:59 +02:00
return(render_template("proxy.html", data=j, len=maxi(j)))
2023-06-17 16:52:47 +02:00
@app.route("/proxy/", methods=["post"])
def proxy_post():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/proxy.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
data = json.load(inFile)
action = request.form
2023-06-18 17:40:59 +02:00
print(action)
if action['PROXY'] == "delete" :
print(action)
data.pop(action["select"], None)
else :
try :
config = action["select"]
address = action["address"]
port = action["port"]
name = action["name"] if action["name"] else f"@unnamed{action['select']}"
data[config] = {"address" : address, "port": port, "name": name}
except :
print("error : probably bad config")
2023-06-17 16:52:47 +02:00
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/proxy.json", "w") as outFile:
2023-06-17 16:52:47 +02:00
json.dump(data, outFile)
2023-06-18 17:40:59 +02:00
return(render_template("proxy.html", data=data, len=maxi(data)))
2023-06-17 16:52:47 +02:00
@app.route("/override/")
def override_get():
2023-06-25 09:56:14 +02:00
edit_version()
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile:
2023-06-18 17:40:59 +02:00
configs = json.load(inFile)
return(render_template("override.html", data=configs))
2023-06-17 16:52:47 +02:00
@app.route("/override/", methods=["post"])
2023-06-18 18:32:06 +02:00
def override_post():
2023-06-25 09:56:14 +02:00
edit_version()
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile:
2023-06-18 17:40:59 +02:00
configs = json.load(inFile)
data = dict(request.form)
for i in configs:
try :
data[f'switch{i}']
except :
data[f'switch{i}'] = "off"
for i in configs:
configs[i]["time"] = data[f"time{i}"]
configs[i]["enabled"] = data[f"switch{i}"] == "on"
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/configs.json", "w") as inFile:
2023-06-18 17:40:59 +02:00
json.dump(configs, inFile)
2023-06-18 18:32:06 +02:00
update_jobs()
2023-06-18 17:40:59 +02:00
return(render_template("override.html", data=configs))
2023-06-17 16:52:47 +02:00
@app.route("/database/")
def database_get():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/database.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
database = json.load(inFile)
return(render_template("database.html", data = database))
@app.route("/database/", methods=["post"])
def database_post():
action = request.form
data = {
"host": action['address'],
"table": action['table'],
"usr": action['user'],
"pwd": action['password'],
"checked": ""
}
try :
if action["switch"] :
data['checked'] = "checked"
except:
pass
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/database.json", "w") as inFile:
2023-06-17 16:52:47 +02:00
json.dump(data, inFile)
return(render_template("database.html", data = data))
2023-06-18 17:40:59 +02:00
@app.route("/config/")
def config_get():
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/proxy.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
proxys = json.load(inFile)
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/discord.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
discords = json.load(inFile)
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
configs = json.load(inFile)
2023-06-18 17:40:59 +02:00
return(render_template("config.html", data=configs, discords=discords, proxys=proxys, configs=configs, len=maxi(configs)))
2023-06-17 16:52:47 +02:00
2023-06-18 17:40:59 +02:00
@app.route("/config/", methods=["POST"])
def config_post():
2023-06-17 16:52:47 +02:00
action = request.form
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/proxy.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
proxys = json.load(inFile)
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/discord.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
discords = json.load(inFile)
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile:
2023-06-17 16:52:47 +02:00
configs = json.load(inFile)
2023-07-01 14:28:04 +02:00
if action["data"] == "delete":
print(action["config"])
configs.pop(action["config"])
else :
comptes = {
"1":{"mail": action["mail1"], "pwd": action["pwd1"], "2fa": action["2fa1"]},
"2":{"mail": action["mail2"], "pwd": action["pwd2"], "2fa": action["2fa2"]},
"3":{"mail": action["mail3"], "pwd": action["pwd3"], "2fa": action["2fa3"]},
"4":{"mail": action["mail4"], "pwd": action["pwd4"], "2fa": action["2fa4"]},
"5":{"mail": action["mail5"], "pwd": action["pwd5"], "2fa": action["2fa5"]}
2023-06-17 16:52:47 +02:00
}
2023-07-01 14:28:04 +02:00
configs[action["config"]] = {
"name" : action["name"] if action["name"] != "" else f"unnamed{action['config']}",
"proxy": action["proxy"],
"discord": action["discord"],
"time":"",
"enabled":"False",
"accounts": comptes
}
2023-06-25 09:22:22 +02:00
with open("/app/MsRewards-Reborn/user_data/configs.json", "w") as outFile:
2023-06-17 16:52:47 +02:00
json.dump(configs, outFile)
2023-06-18 17:40:59 +02:00
return(render_template("config.html", data=configs, discords=discords, proxys=proxys, configs=configs, len=maxi(configs)))
2023-06-17 16:52:47 +02:00
2023-07-02 19:36:14 +02:00
@app.route("/logs/", methods=["GET", "POST"])
def logs():
return(render_template("logs.html"))
2023-06-17 16:52:47 +02:00
2023-08-21 19:30:49 +02:00
2023-08-21 19:34:04 +02:00
@app.route('/download/<path:filename>', methods=['GET', 'POST'])
2023-08-21 19:46:57 +02:00
@login_required
def download(filename):
print("file send !")
return send_from_directory(directory='/app/MsRewards-Reborn/user_data/', path=filename, as_attachment=True)
2023-08-21 19:30:49 +02:00
2023-08-21 21:25:05 +02:00
2023-08-21 19:59:49 +02:00
def allowed_file(filename):
2023-08-21 21:25:05 +02:00
ALLOWED_EXTENSIONS = ["json"]
2023-08-21 19:59:49 +02:00
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
2023-08-21 19:30:49 +02:00
2023-08-21 20:57:26 +02:00
@app.route('/upload_file/', methods=['POST'])
2023-08-21 19:59:49 +02:00
@login_required
def upload_file():
2023-08-21 20:57:26 +02:00
print(request.files)
2023-08-21 21:25:05 +02:00
i = 1
while f'file{i}' in request.files :
file = request.files[f'file{i}']
if file.filename == '':
print('end of files')
return redirect(url_for('settings_get'))
2023-08-21 21:28:18 +02:00
2023-08-21 21:25:05 +02:00
elif file and allowed_file(file.filename):
filename = secure_filename(file.filename)
print(os.path.join('/app/MsRewards-Reborn/user_data/', filename))
file.save(os.path.join('/app/MsRewards-Reborn/user_data/', filename))
i += 1
2023-08-21 21:28:18 +02:00
print(i)
2023-08-21 21:26:21 +02:00
print(f'file{i}' in request.files)
2023-08-21 21:25:05 +02:00
print("requete bizarre")
return redirect(url_for('settings_get'))
2023-08-21 21:03:26 +02:00
2023-08-21 21:25:05 +02:00
2023-08-21 21:00:03 +02:00
return redirect(url_for('settings_get'))
2023-08-21 19:30:49 +02:00
2023-06-18 17:40:59 +02:00
def maxi(dict):
m = 0
for i in dict :
if int(i) >= m:
m = int(i)
return(m+1)
2023-06-17 16:52:47 +02:00
2023-06-18 19:21:02 +02:00
2023-08-22 11:21:42 +02:00
from flask import Flask
from requests import get
2023-08-22 11:24:50 +02:00
SITE_NAME = 'http://localhost:3000'
2023-08-22 11:21:42 +02:00
@app.route('/proxytest', defaults={'path': ''})
@app.route('/proxytest')
2023-08-22 11:23:59 +02:00
def proxy():
2023-08-22 11:21:42 +02:00
return get(f'{SITE_NAME}').content
2023-06-17 16:52:47 +02:00
if __name__ == '__main__':
2023-06-25 09:22:22 +02:00
update_jobs()
2023-06-25 10:49:25 +02:00
edit_version()
2023-08-22 13:33:43 +02:00
app.run(host='0.0.0.0', port=1234, debug=True)