gabefromsimplebit's picture
Update app.py
82cf52c verified
Raw History Blame
10.1 kB
from flask import Flask, request, send_file, jsonify, redirect
from flask_cors import CORS
import subprocess
import plistlib
import zipfile
import shutil
import uuid
from pathlib import Path
app = Flask(__name__)
CORS(app)
# CHANGE THIS TO YOUR REAL HF SPACE URL
BASE_URL = "https://gabefromsimplebit-simplebit-signer.hf.space"
@app.route("/")
def home():
return jsonify({
"status": "online",
"service": "SimpleBit Signer"
})
# ---------------------------------------------------------
# Extract REAL main app bundle identifier
# ---------------------------------------------------------
def get_actual_bundle_id(ipa_path):
try:
with zipfile.ZipFile(ipa_path, "r") as ipa:
app_plist = None
for name in ipa.namelist():
# ONLY match:
# Payload/App.app/Info.plist
if (
name.startswith("Payload/")
and name.count("/") == 2
and name.endswith(".app/Info.plist")
):
app_plist = name
break
if not app_plist:
print("Main app Info.plist not found")
return None
print("MAIN APP PLIST:", app_plist)
with ipa.open(app_plist) as plist_file:
plist_data = plistlib.load(plist_file)
bundle_id = plist_data.get("CFBundleIdentifier")
print("REAL BUNDLE ID:", bundle_id)
return bundle_id
except Exception as e:
print("Bundle extraction error:", str(e))
return None
# ---------------------------------------------------------
# Save bundle ID
# ---------------------------------------------------------
def save_bundle_id(job_dir, bundle_id):
try:
with open(job_dir / "bundle_id.txt", "w") as f:
f.write(bundle_id)
except Exception as e:
print("Save bundle ID error:", str(e))
# ---------------------------------------------------------
# Load bundle ID
# ---------------------------------------------------------
def load_bundle_id(job_dir):
try:
with open(job_dir / "bundle_id.txt", "r") as f:
return f.read().strip()
except:
return "com.simplebit.dynamic"
# ---------------------------------------------------------
# SIGN IPA
# ---------------------------------------------------------
@app.route("/sign", methods=["POST"])
def sign():
try:
# -------------------------------------------------
# Validate uploads
# -------------------------------------------------
if "ipa" not in request.files:
return jsonify({
"error": "Missing IPA file"
}), 400
if "p12" not in request.files:
return jsonify({
"error": "Missing P12 file"
}), 400
if "provision" not in request.files:
return jsonify({
"error": "Missing provisioning profile"
}), 400
# -------------------------------------------------
# Check zsign installation
# -------------------------------------------------
zsign_path = shutil.which("zsign")
print("ZSIGN PATH:", zsign_path)
if not zsign_path:
return jsonify({
"error": "zsign is not installed"
}), 500
# -------------------------------------------------
# Create unique job folder
# -------------------------------------------------
job_id = str(uuid.uuid4())[:8]
job_dir = Path(job_id)
job_dir.mkdir(exist_ok=True)
# -------------------------------------------------
# Paths
# -------------------------------------------------
ipa_path = job_dir / "app.ipa"
p12_path = job_dir / "cert.p12"
prov_path = job_dir / "profile.mobileprovision"
signed_path = job_dir / "signed.ipa"
# -------------------------------------------------
# Save uploaded files
# -------------------------------------------------
request.files["ipa"].save(ipa_path)
request.files["p12"].save(p12_path)
request.files["provision"].save(prov_path)
password = request.form.get("password", "")
# -------------------------------------------------
# Extract bundle ID
# -------------------------------------------------
bundle_id = get_actual_bundle_id(ipa_path)
if not bundle_id:
bundle_id = "com.simplebit.dynamic"
# -------------------------------------------------
# zsign command
# -------------------------------------------------
command = [
zsign_path,
"-k", str(p12_path),
"-p", password,
"-m", str(prov_path),
"-o", str(signed_path),
str(ipa_path)
]
print("RUNNING:")
print(" ".join(command))
# -------------------------------------------------
# Execute zsign
# -------------------------------------------------
result = subprocess.run(
command,
capture_output=True,
text=True
)
print("RETURN CODE:", result.returncode)
print("STDOUT:")
print(result.stdout)
print("STDERR:")
print(result.stderr)
# -------------------------------------------------
# Signing failed
# -------------------------------------------------
if result.returncode != 0:
return jsonify({
"error": "Signing failed",
"returncode": result.returncode,
"stdout": result.stdout,
"stderr": result.stderr
}), 500
# -------------------------------------------------
# Ensure IPA exists
# -------------------------------------------------
if not signed_path.exists():
return jsonify({
"error": "Signed IPA missing"
}), 500
# -------------------------------------------------
# Save bundle ID
# -------------------------------------------------
save_bundle_id(job_dir, bundle_id)
# -------------------------------------------------
# Generate install link
# -------------------------------------------------
plist_url = f"{BASE_URL}/plist/{job_id}"
install_url = (
"itms-services://?action=download-manifest"
f"&url={plist_url}"
)
print("INSTALL URL:")
print(install_url)
# -------------------------------------------------
# AUTO REDIRECT TO INSTALL
# -------------------------------------------------
return redirect(install_url)
except Exception as e:
print("FATAL ERROR:")
print(str(e))
return jsonify({
"error": str(e)
}), 500
# ---------------------------------------------------------
# OTA plist
# ---------------------------------------------------------
@app.route("/plist/<job_id>")
def serve_plist(job_id):
try:
job_dir = Path(job_id)
signed_ipa = job_dir / "signed.ipa"
if not signed_ipa.exists():
return jsonify({
"error": "Signed IPA not found"
}), 404
bundle_id = load_bundle_id(job_dir)
ipa_url = f"{BASE_URL}/download/{job_id}"
plist_content = f"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>{ipa_url}</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>{bundle_id}</string>
<key>bundle-version</key>
<string>1.0</string>
<key>kind</key>
<string>software</string>
<key>title</key>
<string>SimpleBit Signed App</string>
</dict>
</dict>
</array>
</dict>
</plist>
"""
plist_path = job_dir / "manifest.plist"
with open(plist_path, "w") as f:
f.write(plist_content)
return send_file(
plist_path,
mimetype="application/xml"
)
except Exception as e:
return jsonify({
"error": str(e)
}), 500
# ---------------------------------------------------------
# Download signed IPA
# ---------------------------------------------------------
@app.route("/download/<job_id>")
def download_ipa(job_id):
try:
ipa_path = Path(job_id) / "signed.ipa"
if not ipa_path.exists():
return jsonify({
"error": "IPA not found"
}), 404
return send_file(
ipa_path,
as_attachment=True,
download_name="signed.ipa"
)
except Exception as e:
return jsonify({
"error": str(e)
}), 500
# ---------------------------------------------------------
# Health endpoint
# ---------------------------------------------------------
@app.route("/health")
def health():
return jsonify({
"status": "healthy",
"zsign_installed": shutil.which("zsign") is not None
})
# ---------------------------------------------------------
# Run Flask
# ---------------------------------------------------------
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=7860,
debug=True
)