CoolFace
Apppublic

eternal077/auto-patch-server

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py121 linesDownload Raw Back to root
1from flask import Flask, request, send_file, jsonify2import subprocess3import uuid4import os5from pathlib import Path6 7app = Flask(__name__)8 9# Resolve absolute path to lspatch.jar next to this app.py10HERE = Path(__file__).resolve().parent11JAR_PATH = HERE / "lspatch.jar"12 13@app.get("/health")14def health():15    return jsonify({"status": "ok"}), 20016 17@app.post("/patch")18def patch():19    # Validate inputs20    if "core" not in request.files or "apk" not in request.files:21        return "Missing form parts 'core' and/or 'apk'", 40022    if not JAR_PATH.exists():23        return f"lspatch.jar not found at {JAR_PATH}", 50024    core = request.files["core"]25    apk = request.files["apk"]26    temp_id = str(uuid.uuid4())27    temp_folder = Path("/tmp") / temp_id28    temp_folder.mkdir(parents=True, exist_ok=True)29    core_path = temp_folder / "core.apk"30    apk_path = temp_folder / "snapchat.apk"31    core.save(str(core_path))32    apk.save(str(apk_path))33    patch_cmd = [34        "java", "-jar", str(JAR_PATH),35        "-m", str(core_path),  # mapping/core (embed modules)36        "-f",37        "-l", "2",38        "-v",39        str(apk_path)40    ]41    proc = subprocess.run(42        patch_cmd,43        cwd=str(temp_folder),44        capture_output=True45    )46    result_apk = None47    for fname in os.listdir(str(temp_folder)):48        if fname.endswith("-lspatched.apk"):49            result_apk = temp_folder / fname50            break51    if proc.returncode != 0 or result_apk is None or not result_apk.exists():52        stdout = (proc.stdout or b"").decode("utf-8", errors="ignore")53        stderr = (proc.stderr or b"").decode("utf-8", errors="ignore")54        return (55            "<h3>Patching failed</h3>"56            f"<pre>{stdout[:5000]}</pre><br><pre>{stderr[:5000]}</pre>",57            400,58        )59    return send_file(60        str(result_apk),61        download_name="PatchedSnapchat.apk",62        as_attachment=True,63        mimetype="application/vnd.android.package-archive"64    )65 66@app.post("/patch_nomod")67def patch_nomod():68    # Validate inputs (only need apk)69    if "apk" not in request.files:70        return "Missing form part 'apk'", 40071    if not JAR_PATH.exists():72        return f"lspatch.jar not found at {JAR_PATH}", 50073    apk = request.files["apk"]74    temp_id = str(uuid.uuid4())75    temp_folder = Path("/tmp") / temp_id76    temp_folder.mkdir(parents=True, exist_ok=True)77    apk_path = temp_folder / "snapchat.apk"78    apk.save(str(apk_path))79    # No -m argument, just patch the APK with no modules embedded80    patch_cmd = [81        "java", "-jar", str(JAR_PATH),82        "-f",83        "-l", "2",84        "-v",85        str(apk_path)86    ]87    proc = subprocess.run(88        patch_cmd,89        cwd=str(temp_folder),90        capture_output=True91    )92    result_apk = None93    for fname in os.listdir(str(temp_folder)):94        if fname.endswith("-lspatched.apk"):95            result_apk = temp_folder / fname96            break97    if proc.returncode != 0 or result_apk is None or not result_apk.exists():98        stdout = (proc.stdout or b"").decode("utf-8", errors="ignore")99        stderr = (proc.stderr or b"").decode("utf-8", errors="ignore")100        return (101            "<h3>Patching without modules failed</h3>"102            f"<pre>{stdout[:5000]}</pre><br><pre>{stderr[:5000]}</pre>",103            400,104        )105    return send_file(106        str(result_apk),107        download_name="PatchedSnapchatNoMods.apk",108        as_attachment=True,109        mimetype="application/vnd.android.package-archive"110    )111 112@app.get("/")113def root():114    return "<b>Patch server is running!</b>", 200115 116application = app117 118if __name__ == "__main__":119    port = int(os.getenv("PORT", "7860"))120    app.run(host="0.0.0.0", port=port)121