mirror of
				https://gitea.augustin64.fr/piair/MsRewards-Reborn.git
				synced 2025-11-03 23:23:53 +01:00 
			
		
		
		
	Add env variables arguments to flask app
- NO_SUBPROCESS to fake subprocesses calls - APP_ROOT to use the app outside of Docker
This commit is contained in:
		
							
								
								
									
										2
									
								
								.dockerignore
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.dockerignore
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
**/.venv
 | 
			
		||||
user_data/*
 | 
			
		||||
							
								
								
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -3,18 +3,16 @@ geckodriver.log
 | 
			
		||||
.vscode/
 | 
			
		||||
.idea
 | 
			
		||||
venv
 | 
			
		||||
**/.venv
 | 
			
		||||
/Git
 | 
			
		||||
page.html
 | 
			
		||||
screenshot.png
 | 
			
		||||
login.csv
 | 
			
		||||
data
 | 
			
		||||
**/__pycache__
 | 
			
		||||
user_data/*
 | 
			
		||||
install.sh
 | 
			
		||||
nohup.out
 | 
			
		||||
points.csv
 | 
			
		||||
file.png
 | 
			
		||||
user_data/configs.json
 | 
			
		||||
*.ts
 | 
			
		||||
LICENSE
 | 
			
		||||
README.md
 | 
			
		||||
							
								
								
									
										86
									
								
								Flask/app.py
									
									
									
									
									
								
							
							
						
						
									
										86
									
								
								Flask/app.py
									
									
									
									
									
								
							@@ -13,11 +13,24 @@ import re
 | 
			
		||||
from requests import get
 | 
			
		||||
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:
 | 
			
		||||
    subprocess.Popen = lambda x: print("Calling subprocess.Popen with", x)
 | 
			
		||||
    print("Faking subprocess calls")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# redis part for live update
 | 
			
		||||
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
 | 
			
		||||
r = redis.Redis(connection_pool=pool)
 | 
			
		||||
 | 
			
		||||
def get_path(path):
 | 
			
		||||
    return os.path.join(APP_ROOT, path)
 | 
			
		||||
 | 
			
		||||
def generate_output():
 | 
			
		||||
    pubsub = r.pubsub()
 | 
			
		||||
    pubsub.subscribe('console')
 | 
			
		||||
@@ -32,7 +45,7 @@ def generate_output():
 | 
			
		||||
# the end
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
 | 
			
		||||
password = data["password"]
 | 
			
		||||
@@ -40,7 +53,7 @@ secret = data["secret"]
 | 
			
		||||
if secret == "":
 | 
			
		||||
    import secrets
 | 
			
		||||
    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 = {
 | 
			
		||||
            "password": password,
 | 
			
		||||
            "secret": secret
 | 
			
		||||
@@ -70,14 +83,14 @@ scheduler.add_job(                  # on relance le job
 | 
			
		||||
 | 
			
		||||
def start_ms(i):
 | 
			
		||||
    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
 | 
			
		||||
    subprocess.Popen([f"python3 -u /app/MsRewards-Reborn/V6.py -c {i}"], stdout=log, stderr=log, shell=True)
 | 
			
		||||
    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 {get_path('V6.py')} -c {i}"], stdout=log, stderr=log, shell=True)
 | 
			
		||||
    log.close()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
TriggerDict = {}
 | 
			
		||||
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)
 | 
			
		||||
    for i in configs:
 | 
			
		||||
        try : 
 | 
			
		||||
@@ -120,7 +133,7 @@ app = Flask(__name__)
 | 
			
		||||
 | 
			
		||||
@app.context_processor
 | 
			
		||||
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", '')
 | 
			
		||||
    return dict(version=version)
 | 
			
		||||
"""
 | 
			
		||||
@@ -175,7 +188,7 @@ def change_password():
 | 
			
		||||
    if request.method == 'POST':
 | 
			
		||||
        password = request.form["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 = {
 | 
			
		||||
                "password": password, 
 | 
			
		||||
                "secret": secret
 | 
			
		||||
@@ -201,21 +214,21 @@ def load_user(userid):
 | 
			
		||||
 | 
			
		||||
@app.route("/")
 | 
			
		||||
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)
 | 
			
		||||
    return(render_template("schedule.html", data=configs))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/discord/")
 | 
			
		||||
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)
 | 
			
		||||
    return(render_template("discord.html", data=data, len=maxi(data)))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/discord/", methods=["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)    
 | 
			
		||||
    action = request.form
 | 
			
		||||
    if action['DISCORD'] == "delete" :
 | 
			
		||||
@@ -237,7 +250,7 @@ def discord_post():
 | 
			
		||||
        name = action["name"] if action["name"] else f"unnamed{action['select']}"
 | 
			
		||||
        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)
 | 
			
		||||
    return(render_template("discord.html", data=data, len=maxi(data)))
 | 
			
		||||
 | 
			
		||||
@@ -249,7 +262,7 @@ def dev2():
 | 
			
		||||
 | 
			
		||||
@app.route("/settings/")
 | 
			
		||||
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)
 | 
			
		||||
    return(render_template("settings.html", data=settings))
 | 
			
		||||
 | 
			
		||||
@@ -259,21 +272,21 @@ def settings_post():
 | 
			
		||||
    settings = {}
 | 
			
		||||
    action = request.form
 | 
			
		||||
    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)
 | 
			
		||||
    return(render_template("settings.html", data=settings))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/proxy/")
 | 
			
		||||
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)
 | 
			
		||||
    return(render_template("proxy.html", data=j, len=maxi(j)))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/proxy/", methods=["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)    
 | 
			
		||||
    action = request.form
 | 
			
		||||
    print(action)
 | 
			
		||||
@@ -290,21 +303,21 @@ def proxy_post():
 | 
			
		||||
        except :
 | 
			
		||||
            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)
 | 
			
		||||
    return(render_template("proxy.html", data=data, len=maxi(data)))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/schedule/")
 | 
			
		||||
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)
 | 
			
		||||
    return(render_template("schedule.html", data=configs))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/schedule/", methods=["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)
 | 
			
		||||
 | 
			
		||||
    data = dict(request.form)
 | 
			
		||||
@@ -318,7 +331,7 @@ def schedule_post():
 | 
			
		||||
        configs[i]["time"] = data[f"time{i}"]
 | 
			
		||||
        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)
 | 
			
		||||
    update_jobs()
 | 
			
		||||
    return(render_template("schedule.html", data=configs))
 | 
			
		||||
@@ -326,11 +339,11 @@ def schedule_post():
 | 
			
		||||
 | 
			
		||||
@app.route("/config/")
 | 
			
		||||
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)
 | 
			
		||||
    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)
 | 
			
		||||
    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)
 | 
			
		||||
    return(render_template("config.html", data=configs, discords=discords, proxys=proxys, configs=configs, len=maxi(configs)))
 | 
			
		||||
 | 
			
		||||
@@ -338,11 +351,11 @@ def config_get():
 | 
			
		||||
@app.route("/config/", methods=["POST"])
 | 
			
		||||
def config_post():
 | 
			
		||||
    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)
 | 
			
		||||
    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)
 | 
			
		||||
    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)
 | 
			
		||||
 | 
			
		||||
    if action["data"] == "delete":
 | 
			
		||||
@@ -365,18 +378,18 @@ def config_post():
 | 
			
		||||
            "enabled":"False",
 | 
			
		||||
            "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)
 | 
			
		||||
    return(render_template("config.html", data=configs, discords=discords, proxys=proxys, configs=configs, len=maxi(configs)))
 | 
			
		||||
 | 
			
		||||
@app.route("/logs/", methods=["GET", "POST"])
 | 
			
		||||
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)
 | 
			
		||||
    
 | 
			
		||||
    files = [(configs[i]["name"], i) for i in configs]
 | 
			
		||||
    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
 | 
			
		||||
        if f != ".gitignore" and fid not in config_files:
 | 
			
		||||
            files.append((f, fid))
 | 
			
		||||
@@ -395,21 +408,21 @@ def stats():
 | 
			
		||||
@app.route("/override/", methods=["POST"])
 | 
			
		||||
def override_post():
 | 
			
		||||
    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
 | 
			
		||||
    subprocess.Popen([f"python3 -u /app/MsRewards-Reborn/V6.py -c {json['config'][0]} --json \"{json}\""], stdout=log, stderr=log, shell=True)
 | 
			
		||||
    log = open(get_path("Flask/static/logs/custom.txt"), 'w')  # so that data written to it will be appended
 | 
			
		||||
    subprocess.Popen([f"python3 -u {get_path('V6.py')} -c {json['config'][0]} --json \"{json}\""], stdout=log, stderr=log, shell=True)
 | 
			
		||||
    log.close()
 | 
			
		||||
    return(render_template("vnc_post.html"))
 | 
			
		||||
 | 
			
		||||
@app.route("/override/", methods=["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)
 | 
			
		||||
    return(render_template("vnc_get.html", configs=configs))
 | 
			
		||||
 | 
			
		||||
@app.route('/download/<path:filename>', methods=['GET', 'POST'])
 | 
			
		||||
@login_required
 | 
			
		||||
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):
 | 
			
		||||
@@ -430,8 +443,8 @@ def upload_file():
 | 
			
		||||
 | 
			
		||||
        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))
 | 
			
		||||
            print(os.path.join(get_path("user_data/"), filename))
 | 
			
		||||
            file.save(os.path.join(get_path("user_data/"), filename))
 | 
			
		||||
        
 | 
			
		||||
        i += 1
 | 
			
		||||
    print(i)
 | 
			
		||||
@@ -449,4 +462,7 @@ def maxi(dict):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
update_jobs()
 | 
			
		||||
subprocess.Popen(["bash",'/app/MsRewards-Reborn/config/request.sh'])
 | 
			
		||||
subprocess.Popen(["bash", get_path("config/request.sh")])
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    app.run()
 | 
			
		||||
 
 | 
			
		||||
@@ -11,11 +11,11 @@ class Driver:
 | 
			
		||||
        self.mobile_driver = mobile_driver
 | 
			
		||||
 | 
			
		||||
    def switch_to_driver(self, driver: str):
 | 
			
		||||
        match driver:
 | 
			
		||||
            case "pc" | "PC" | "Pc":
 | 
			
		||||
        match driver.lower():
 | 
			
		||||
            case "pc":
 | 
			
		||||
                self.driver = self.pc_driver
 | 
			
		||||
 | 
			
		||||
            case "mobile" | "Mobile":
 | 
			
		||||
            case "mobile":
 | 
			
		||||
                self.driver = self.mobile_driver
 | 
			
		||||
 | 
			
		||||
            case _:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user