Compare commits

...

3 Commits

Author SHA1 Message Date
augustin64 37e8f6f61b Fix fake_popen 2024-04-12 16:20:25 +02:00
augustin64 db6fa9b6b0 Add env variables arguments to flask app
- NO_SUBPROCESS to fake subprocesses calls
- APP_ROOT to use the app outside of Docker
2024-04-12 15:55:14 +02:00
augustin64 d6988c03b4 Oops.. fixed nginx config 2024-04-12 15:51:20 +02:00
5 changed files with 62 additions and 43 deletions

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
**/.venv
user_data/*

4
.gitignore vendored
View File

@ -3,18 +3,16 @@ geckodriver.log
.vscode/ .vscode/
.idea .idea
venv venv
**/.venv
/Git /Git
page.html page.html
screenshot.png screenshot.png
login.csv
data data
**/__pycache__ **/__pycache__
user_data/* user_data/*
install.sh install.sh
nohup.out nohup.out
points.csv
file.png file.png
user_data/configs.json
*.ts *.ts
LICENSE LICENSE
README.md README.md

View File

@ -13,11 +13,27 @@ import re
from requests import get from requests import get
import redis import redis
APP_ROOT = os.getenv("APP_ROOT")
if APP_ROOT is None:
APP_ROOT = "/app/MsRewards-Reborn/"
NO_SUBPROCESS = os.getenv("NO_SUBPROCESS")
if NO_SUBPROCESS is not None:
def fake_popen(*args, **kwargs):
print("Calling subprocess.Popen with", args, kwargs)
subprocess.Popen = fake_popen
print("Faking subprocess calls")
# redis part for live update # redis part for live update
pool = redis.ConnectionPool(host='localhost', port=6379, db=0) pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool) r = redis.Redis(connection_pool=pool)
def get_path(path):
return os.path.join(APP_ROOT, path)
def generate_output(): def generate_output():
pubsub = r.pubsub() pubsub = r.pubsub()
pubsub.subscribe('console') pubsub.subscribe('console')
@ -32,7 +48,7 @@ def generate_output():
# the end # the end
global password global password
with open("/app/MsRewards-Reborn/user_data/flask.json", "r") as inFile: with open(get_path("user_data/flask.json"), "r") as inFile:
data = json.load(inFile) data = json.load(inFile)
password = data["password"] password = data["password"]
@ -40,7 +56,7 @@ secret = data["secret"]
if secret == "": if secret == "":
import secrets import secrets
secret = secrets.token_hex() secret = secrets.token_hex()
with open("/app/MsRewards-Reborn/user_data/flask.json", "w") as inFile: with open(get_path("user_data/flask.json"), "w") as inFile:
data = { data = {
"password": password, "password": password,
"secret": secret "secret": secret
@ -70,14 +86,14 @@ scheduler.add_job( # on relance le job
def start_ms(i): def start_ms(i):
print("\033[32m" + f"Starting config {i}" + "\033[0m") print("\033[32m" + f"Starting config {i}" + "\033[0m")
log = open(f"/app/MsRewards-Reborn/Flask/static/logs/{i}.txt", 'a') # so that data written to it will be appended log = open(get_path(f"Flask/static/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) subprocess.Popen([f"python3 -u {get_path('V6.py')} -c {i}"], stdout=log, stderr=log, shell=True)
log.close() log.close()
TriggerDict = {} TriggerDict = {}
def update_jobs(): def update_jobs():
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile: with open(get_path("user_data/configs.json"), "r") as inFile:
configs = json.load(inFile) configs = json.load(inFile)
for i in configs: for i in configs:
try : try :
@ -120,7 +136,7 @@ app = Flask(__name__)
@app.context_processor @app.context_processor
def inject_default_variables(): def inject_default_variables():
with open("/app/MsRewards-Reborn/version", "r") as f: with open(get_path("version"), "r") as f:
version = f.readline().replace("\n", '') version = f.readline().replace("\n", '')
return dict(version=version) return dict(version=version)
""" """
@ -175,7 +191,7 @@ def change_password():
if request.method == 'POST': if request.method == 'POST':
password = request.form["password"] password = request.form["password"]
subprocess.Popen(["grafana-cli", "admin", "reset-admin-password", password]) subprocess.Popen(["grafana-cli", "admin", "reset-admin-password", password])
with open("/app/MsRewards-Reborn/user_data/flask.json", "w") as inFile: with open(get_path("user_data/flask.json"), "w") as inFile:
data = { data = {
"password": password, "password": password,
"secret": secret "secret": secret
@ -201,21 +217,21 @@ def load_user(userid):
@app.route("/") @app.route("/")
def main(): def main():
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile: with open(get_path("user_data/configs.json"), "r") as inFile:
configs = json.load(inFile) configs = json.load(inFile)
return(render_template("schedule.html", data=configs)) return(render_template("schedule.html", data=configs))
@app.route("/discord/") @app.route("/discord/")
def discord_get(): def discord_get():
with open("/app/MsRewards-Reborn/user_data/discord.json", "r") as inFile: with open(get_path("user_data/discord.json"), "r") as inFile:
data = json.load(inFile) data = json.load(inFile)
return(render_template("discord.html", data=data, len=maxi(data))) return(render_template("discord.html", data=data, len=maxi(data)))
@app.route("/discord/", methods=["post"]) @app.route("/discord/", methods=["post"])
def discord_post(): def discord_post():
with open("/app/MsRewards-Reborn/user_data/discord.json", "r") as inFile: with open(get_path("user_data/discord.json"), "r") as inFile:
data = json.load(inFile) data = json.load(inFile)
action = request.form action = request.form
if action['DISCORD'] == "delete" : if action['DISCORD'] == "delete" :
@ -237,7 +253,7 @@ def discord_post():
name = action["name"] if action["name"] else f"unnamed{action['select']}" name = action["name"] if action["name"] else f"unnamed{action['select']}"
data[config] = {"errorsL" : errorsL, "errorsT": errorsT, "successT": successT, "successL": successL, "name": name} data[config] = {"errorsL" : errorsL, "errorsT": errorsT, "successT": successT, "successL": successL, "name": name}
with open("/app/MsRewards-Reborn/user_data/discord.json", "w") as outFile: with open(get_path("user_data/discord.json"), "w") as outFile:
json.dump(data, outFile) json.dump(data, outFile)
return(render_template("discord.html", data=data, len=maxi(data))) return(render_template("discord.html", data=data, len=maxi(data)))
@ -249,7 +265,7 @@ def dev2():
@app.route("/settings/") @app.route("/settings/")
def settings_get(): def settings_get():
with open("/app/MsRewards-Reborn/user_data/settings.json", "r") as inFile: with open(get_path("user_data/settings.json"), "r") as inFile:
settings = json.load(inFile) settings = json.load(inFile)
return(render_template("settings.html", data=settings)) return(render_template("settings.html", data=settings))
@ -259,21 +275,21 @@ def settings_post():
settings = {} settings = {}
action = request.form action = request.form
settings['avatarlink'] = action["avatarlink"] settings['avatarlink'] = action["avatarlink"]
with open("/app/MsRewards-Reborn/user_data/settings.json", "w") as inFile: with open(get_path("user_data/settings.json"), "w") as inFile:
json.dump(settings, inFile) json.dump(settings, inFile)
return(render_template("settings.html", data=settings)) return(render_template("settings.html", data=settings))
@app.route("/proxy/") @app.route("/proxy/")
def proxy_get(): def proxy_get():
with open("/app/MsRewards-Reborn/user_data/proxy.json", "r") as inFile: with open(get_path("user_data/proxy.json"), "r") as inFile:
j = json.load(inFile) j = json.load(inFile)
return(render_template("proxy.html", data=j, len=maxi(j))) return(render_template("proxy.html", data=j, len=maxi(j)))
@app.route("/proxy/", methods=["post"]) @app.route("/proxy/", methods=["post"])
def proxy_post(): def proxy_post():
with open("/app/MsRewards-Reborn/user_data/proxy.json", "r") as inFile: with open(get_path("user_data/proxy.json"), "r") as inFile:
data = json.load(inFile) data = json.load(inFile)
action = request.form action = request.form
print(action) print(action)
@ -290,21 +306,21 @@ def proxy_post():
except : except :
print("error : probably bad config") print("error : probably bad config")
with open("/app/MsRewards-Reborn/user_data/proxy.json", "w") as outFile: with open(get_path("user_data/proxy.json"), "w") as outFile:
json.dump(data, outFile) json.dump(data, outFile)
return(render_template("proxy.html", data=data, len=maxi(data))) return(render_template("proxy.html", data=data, len=maxi(data)))
@app.route("/schedule/") @app.route("/schedule/")
def schedule_get(): def schedule_get():
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile: with open(get_path("user_data/configs.json"), "r") as inFile:
configs = json.load(inFile) configs = json.load(inFile)
return(render_template("schedule.html", data=configs)) return(render_template("schedule.html", data=configs))
@app.route("/schedule/", methods=["post"]) @app.route("/schedule/", methods=["post"])
def schedule_post(): def schedule_post():
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile: with open(get_path("user_data/configs.json"), "r") as inFile:
configs = json.load(inFile) configs = json.load(inFile)
data = dict(request.form) data = dict(request.form)
@ -318,7 +334,7 @@ def schedule_post():
configs[i]["time"] = data[f"time{i}"] configs[i]["time"] = data[f"time{i}"]
configs[i]["enabled"] = data[f"switch{i}"] == "on" configs[i]["enabled"] = data[f"switch{i}"] == "on"
with open("/app/MsRewards-Reborn/user_data/configs.json", "w") as inFile: with open(get_path("user_data/configs.json"), "w") as inFile:
json.dump(configs, inFile) json.dump(configs, inFile)
update_jobs() update_jobs()
return(render_template("schedule.html", data=configs)) return(render_template("schedule.html", data=configs))
@ -326,11 +342,11 @@ def schedule_post():
@app.route("/config/") @app.route("/config/")
def config_get(): def config_get():
with open("/app/MsRewards-Reborn/user_data/proxy.json", "r") as inFile: with open(get_path("user_data/proxy.json"), "r") as inFile:
proxys = json.load(inFile) proxys = json.load(inFile)
with open("/app/MsRewards-Reborn/user_data/discord.json", "r") as inFile: with open(get_path("user_data/discord.json"), "r") as inFile:
discords = json.load(inFile) discords = json.load(inFile)
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile: with open(get_path("user_data/configs.json"), "r") as inFile:
configs = json.load(inFile) configs = json.load(inFile)
return(render_template("config.html", data=configs, discords=discords, proxys=proxys, configs=configs, len=maxi(configs))) return(render_template("config.html", data=configs, discords=discords, proxys=proxys, configs=configs, len=maxi(configs)))
@ -338,11 +354,11 @@ def config_get():
@app.route("/config/", methods=["POST"]) @app.route("/config/", methods=["POST"])
def config_post(): def config_post():
action = request.form action = request.form
with open("/app/MsRewards-Reborn/user_data/proxy.json", "r") as inFile: with open(get_path("user_data/proxy.json"), "r") as inFile:
proxys = json.load(inFile) proxys = json.load(inFile)
with open("/app/MsRewards-Reborn/user_data/discord.json", "r") as inFile: with open(get_path("user_data/discord.json"), "r") as inFile:
discords = json.load(inFile) discords = json.load(inFile)
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile: with open(get_path("user_data/configs.json"), "r") as inFile:
configs = json.load(inFile) configs = json.load(inFile)
if action["data"] == "delete": if action["data"] == "delete":
@ -365,18 +381,18 @@ def config_post():
"enabled":"False", "enabled":"False",
"accounts": comptes "accounts": comptes
} }
with open("/app/MsRewards-Reborn/user_data/configs.json", "w") as outFile: with open(get_path("user_data/configs.json"), "w") as outFile:
json.dump(configs, outFile) json.dump(configs, outFile)
return(render_template("config.html", data=configs, discords=discords, proxys=proxys, configs=configs, len=maxi(configs))) return(render_template("config.html", data=configs, discords=discords, proxys=proxys, configs=configs, len=maxi(configs)))
@app.route("/logs/", methods=["GET", "POST"]) @app.route("/logs/", methods=["GET", "POST"])
def logs(): def logs():
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile: with open(get_path("user_data/configs.json"), "r") as inFile:
configs = json.load(inFile) configs = json.load(inFile)
files = [(configs[i]["name"], i) for i in configs] files = [(configs[i]["name"], i) for i in configs]
config_files = [i[1] for i in files] config_files = [i[1] for i in files]
for f in os.listdir("/app/MsRewards-Reborn/Flask/static/logs"): for f in os.listdir(get_path("Flask/static/logs")):
fid = ".".join(f.split(".")[:-1]) # filename without .txt fid = ".".join(f.split(".")[:-1]) # filename without .txt
if f != ".gitignore" and fid not in config_files: if f != ".gitignore" and fid not in config_files:
files.append((f, fid)) files.append((f, fid))
@ -395,21 +411,21 @@ def stats():
@app.route("/override/", methods=["POST"]) @app.route("/override/", methods=["POST"])
def override_post(): def override_post():
json = request.form.to_dict(flat=False) json = request.form.to_dict(flat=False)
log = open(f"/app/MsRewards-Reborn/Flask/static/logs/custom.txt", 'w') # so that data written to it will be appended log = open(get_path("Flask/static/logs/custom.txt"), 'w') # so that data written to it will be appended
subprocess.Popen([f"python3 -u /app/MsRewards-Reborn/V6.py -c {json['config'][0]} --json \"{json}\""], stdout=log, stderr=log, shell=True) subprocess.Popen([f"python3 -u {get_path('V6.py')} -c {json['config'][0]} --json \"{json}\""], stdout=log, stderr=log, shell=True)
log.close() log.close()
return(render_template("vnc_post.html")) return(render_template("vnc_post.html"))
@app.route("/override/", methods=["GET"]) @app.route("/override/", methods=["GET"])
def override_get(): def override_get():
with open("/app/MsRewards-Reborn/user_data/configs.json", "r") as inFile: with open(get_path("user_data/configs.json"), "r") as inFile:
configs = json.load(inFile) configs = json.load(inFile)
return(render_template("vnc_get.html", configs=configs)) return(render_template("vnc_get.html", configs=configs))
@app.route('/download/<path:filename>', methods=['GET', 'POST']) @app.route('/download/<path:filename>', methods=['GET', 'POST'])
@login_required @login_required
def download(filename): def download(filename):
return send_from_directory(directory='/app/MsRewards-Reborn/user_data/', path=filename, as_attachment=True) return send_from_directory(directory=get_path("user_data/"), path=filename, as_attachment=True)
def allowed_file(filename): def allowed_file(filename):
@ -430,8 +446,8 @@ def upload_file():
elif file and allowed_file(file.filename): elif file and allowed_file(file.filename):
filename = secure_filename(file.filename) filename = secure_filename(file.filename)
print(os.path.join('/app/MsRewards-Reborn/user_data/', filename)) print(os.path.join(get_path("user_data/"), filename))
file.save(os.path.join('/app/MsRewards-Reborn/user_data/', filename)) file.save(os.path.join(get_path("user_data/"), filename))
i += 1 i += 1
print(i) print(i)
@ -449,4 +465,7 @@ def maxi(dict):
update_jobs() update_jobs()
subprocess.Popen(["bash",'/app/MsRewards-Reborn/config/request.sh']) subprocess.Popen(["bash", get_path("config/request.sh")])
if __name__ == "__main__":
app.run()

View File

@ -46,7 +46,7 @@ server {
proxy_pass "http://127.0.0.1:6666"; proxy_pass "http://127.0.0.1:6666";
chunked_transfer_encoding off; chunked_transfer_encoding off;
proxy_buffering off; proxy_buffering off;
set_header X-Accel-Buffering no; add_header X-Accel-Buffering no;
} }
} }

View File

@ -11,11 +11,11 @@ class Driver:
self.mobile_driver = mobile_driver self.mobile_driver = mobile_driver
def switch_to_driver(self, driver: str): def switch_to_driver(self, driver: str):
match driver: match driver.lower():
case "pc" | "PC" | "Pc": case "pc":
self.driver = self.pc_driver self.driver = self.pc_driver
case "mobile" | "Mobile": case "mobile":
self.driver = self.mobile_driver self.driver = self.mobile_driver
case _: case _: