File size: 2,793 Bytes
bb56f22 | 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 | import gradio as gr
import subprocess
import os
import shutil
import re
def fix_apk(apk_file):
if apk_file is None:
return "لطفاً یک فایل انتخاب کنید.", None
apk_path = apk_file.name
output_dir = "extracted_apk"
# پاکسازی فایلهای قدیمی
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
if os.path.exists("fixed.apk"):
os.remove("fixed.apk")
if os.path.exists("final_signed_game.apk"):
os.remove("final_signed_game.apk")
try:
# مرحله ۱: باز کردن فایل
subprocess.run(["apktool", "d", apk_path, "-o", output_dir, "-f"], check=True)
# مرحله ۲: ویرایش کدهای اندروید ۱۴ و ۱۵
manifest_path = os.path.join(output_dir, "AndroidManifest.xml")
with open(manifest_path, "r", encoding="utf-8") as f:
manifest = f.read()
manifest = re.sub(r'targetSdkVersion="[0-9]+"', 'targetSdkVersion="34"', manifest)
manifest = manifest.replace('<activity ', '<activity android:exported="true" ')
with open(manifest_path, "w", encoding="utf-8") as f:
f.write(manifest)
# مرحله ۳: بستهبندی مجدد
subprocess.run(["apktool", "b", output_dir, "-o", "fixed.apk"], check=True)
# مرحله ۴: ساخت کلید امنیتی (در صورت نیاز)
if not os.path.exists("mykey.keystore"):
subprocess.run(["keytool", "-genkey", "-v", "-keystore", "mykey.keystore", "-alias", "myalias", "-keyalg", "RSA", "-keysize", "2048", "-validity", "10000", "-storepass", "123456", "-keypass", "123456", "-dname", "CN=Android,O=Android,C=US"], check=True)
# مرحله ۵: امضای نهایی
subprocess.run(["apksigner", "sign", "--ks", "mykey.keystore", "--ks-pass", "pass:123456", "--out", "final_signed_game.apk", "fixed.apk"], check=True)
return "جراحی با موفقیت انجام شد! حالا میتوانید بازی را نصب کنید.", "final_signed_game.apk"
except Exception as e:
return f"خطا در پردازش: {str(e)}", None
# ظاهر برنامه
iface = gr.Interface(
fn=fix_apk,
inputs=gr.File(label="فایل APK قدیمی بازی را اینجا آپلود کنید"),
outputs=[
gr.Textbox(label="وضعیت عملیات"),
gr.File(label="دانلود فایل نهایی و سالم")
],
title="اتاق عمل بازی 🛠️",
description="فایل بازی خود را آپلود کنید تا کدهای آن برای اجرا روی دستگاههای جدید بازسازی شود."
)
iface.launch() |