File size: 7,339 Bytes
e4fbb64
7943970
7279a02
2d59936
7107715
 
7279a02
 
30f1d28
7279a02
7107715
7943970
 
7107715
2d59936
 
 
7107715
4dcf967
 
 
7107715
7943970
5c2d1aa
11c4812
7107715
30f1d28
5c2d1aa
30f1d28
5c2d1aa
 
 
 
b6b9155
 
7107715
30f1d28
5c2d1aa
30f1d28
5c2d1aa
 
 
 
 
e4fbb64
7279a02
5c2d1aa
 
 
7279a02
 
30f1d28
4dcf967
30f1d28
7107715
7943970
e4fbb64
2d59936
e4fbb64
7107715
30f1d28
7107715
30f1d28
7107715
30f1d28
7279a02
 
 
30f1d28
7107715
 
 
 
 
 
 
7279a02
e4fbb64
 
 
 
 
7279a02
e4fbb64
 
 
5c2d1aa
e4fbb64
30f1d28
 
 
 
 
4dcf967
30f1d28
4dcf967
30f1d28
5c2d1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30f1d28
5c2d1aa
7279a02
e4fbb64
5c2d1aa
e4fbb64
7107715
7279a02
30f1d28
7107715
 
 
4dcf967
7107715
 
5c2d1aa
7279a02
7107715
30f1d28
7107715
 
7279a02
 
 
 
e4fbb64
5c2d1aa
e4fbb64
5c2d1aa
 
e4fbb64
5c2d1aa
 
 
 
 
 
7279a02
30f1d28
9c20c41
5c2d1aa
 
7279a02
2d59936
30f1d28
e4fbb64
 
7279a02
7107715
30f1d28
e4fbb64
7279a02
30f1d28
5c2d1aa
30f1d28
7107715
30f1d28
e4fbb64
30f1d28
e4fbb64
5c2d1aa
4dcf967
 
 
7279a02
30f1d28
9c20c41
30f1d28
7107715
 
4dcf967
2d59936
 
30f1d28
 
 
 
 
 
 
 
 
 
2d59936
30f1d28
5c2d1aa
30f1d28
 
 
 
 
 
 
 
 
5c2d1aa
30f1d28
5c2d1aa
30f1d28
 
 
4dcf967
 
7107715
30f1d28
 
e4fbb64
30f1d28
7107715
 
30f1d28
5c2d1aa
30f1d28
7107715
30f1d28
e4fbb64
30f1d28
e4fbb64
30f1d28
 
7279a02
5c2d1aa
30f1d28
 
e4fbb64
7279a02
30f1d28
 
 
7279a02
 
5c2d1aa
7107715
7943970
4dcf967
 
 
7943970
30f1d28
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from flask import Flask, request, send_file, jsonify, redirect
from flask_cors import CORS

import subprocess
import plistlib
import zipfile
import shutil
import uuid
import tempfile

from pathlib import Path

app = Flask(__name__)
CORS(app)

BASE_URL = "https://gabefromsimplebit-simplebit-signer.hf.space"


# -------------------------------------------------
# HOME
# -------------------------------------------------
@app.route("/")
def home():
    return jsonify({"status": "online"})


# -------------------------------------------------
# FIND .APP BUNDLE
# -------------------------------------------------
def find_app_bundle(temp_dir):
    for item in Path(temp_dir).rglob("*.app"):
        if item.is_dir():
            return item
    return None


# -------------------------------------------------
# READ INFO.PLIST
# -------------------------------------------------
def read_info_plist(app_bundle):
    for f in Path(app_bundle).rglob("Info.plist"):
        with open(f, "rb") as fp:
            return f, plistlib.load(fp)
    return None, None


def write_info_plist(plist_path, data):
    with open(plist_path, "wb") as fp:
        plistlib.dump(data, fp)


# -------------------------------------------------
# SIGN ROUTE
# -------------------------------------------------
@app.route("/sign", methods=["POST"])
def sign():

    try:

        if "ipa" not in request.files:
            return jsonify({"error": "Missing IPA"}), 400
        if "p12" not in request.files:
            return jsonify({"error": "Missing P12"}), 400
        if "provision" not in request.files:
            return jsonify({"error": "Missing provisioning profile"}), 400

        zsign_path = shutil.which("zsign")
        if not zsign_path:
            return jsonify({"error": "zsign not installed"}), 500

        job_id = str(uuid.uuid4())[:8]
        job_dir = Path(job_id)
        job_dir.mkdir(exist_ok=True)

        ipa_path = job_dir / "app.ipa"
        p12_path = job_dir / "cert.p12"
        prov_path = job_dir / "profile.mobileprovision"
        signed_path = job_dir / "signed.ipa"

        request.files["ipa"].save(ipa_path)
        request.files["p12"].save(p12_path)
        request.files["provision"].save(prov_path)

        password = request.form.get("password", "")

        # -------------------------------------------------
        # EXTRACT IPA
        # -------------------------------------------------
        temp_dir = tempfile.mkdtemp()

        with zipfile.ZipFile(ipa_path, "r") as zip_ref:
            zip_ref.extractall(temp_dir)

        app_bundle = find_app_bundle(temp_dir)

        if not app_bundle:
            shutil.rmtree(temp_dir)
            return jsonify({"error": "No .app found"}), 500

        # -------------------------------------------------
        # READ ORIGINAL INFO
        # -------------------------------------------------
        plist_path, plist_data = read_info_plist(app_bundle)

        if not plist_data:
            shutil.rmtree(temp_dir)
            return jsonify({"error": "Info.plist missing"}), 500

        original_bundle_id = plist_data.get("CFBundleIdentifier", "unknown.bundle")
        original_name = (
            plist_data.get("CFBundleDisplayName")
            or plist_data.get("CFBundleName")
            or "App"
        )

        # -------------------------------------------------
        # DO NOT CHANGE BUNDLE ID (ENTERPRISE MODE)
        # -------------------------------------------------
        target_bundle_id = original_bundle_id

        plist_data["CFBundleIdentifier"] = target_bundle_id
        plist_data["CFBundleDisplayName"] = original_name
        plist_data["CFBundleName"] = original_name

        write_info_plist(plist_path, plist_data)

        # -------------------------------------------------
        # SIGN
        # -------------------------------------------------
        command = [
            zsign_path,
            "-f",
            "-k", str(p12_path),
            "-p", password,
            "-m", str(prov_path),
            str(app_bundle)
        ]

        result = subprocess.run(command, capture_output=True, text=True)

        if result.returncode != 0:
            shutil.rmtree(temp_dir)
            return jsonify({
                "error": "Signing failed",
                "stdout": result.stdout,
                "stderr": result.stderr
            }), 500

        # -------------------------------------------------
        # BUILD CLEAN IPA
        # -------------------------------------------------
        payload_dir = Path(job_dir) / "Payload"
        payload_dir.mkdir(exist_ok=True)

        shutil.copytree(app_bundle, payload_dir / app_bundle.name)

        zip_path = Path(job_dir) / "signed"
        shutil.make_archive(str(zip_path), "zip", job_dir, "Payload")

        shutil.move(str(zip_path) + ".zip", signed_path)

        shutil.rmtree(temp_dir)

        # save ORIGINAL bundle id for manifest
        (job_dir / "bundle_id.txt").write_text(original_bundle_id)

        plist_url = f"{BASE_URL}/plist/{job_id}"
        install_url = f"itms-services://?action=download-manifest&url={plist_url}"

        return redirect(install_url)

    except Exception as e:
        return jsonify({"error": str(e)}), 500


# -------------------------------------------------
# MANIFEST
# -------------------------------------------------
@app.route("/plist/<job_id>")
def plist(job_id):

    job_dir = Path(job_id)

    bundle_id = "unknown"

    if (job_dir / "bundle_id.txt").exists():
        bundle_id = (job_dir / "bundle_id.txt").read_text().strip()

    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>Signed App</string>
</dict>

</dict>
</array>
</dict>
</plist>
"""

    plist_path = job_dir / "manifest.plist"
    plist_path.write_text(plist_content)

    return send_file(plist_path, mimetype="application/xml")


# -------------------------------------------------
# DOWNLOAD IPA
# -------------------------------------------------
@app.route("/download/<job_id>")
def download(job_id):

    ipa_path = Path(job_id) / "signed.ipa"

    if not ipa_path.exists():
        return jsonify({"error": "IPA not found"}), 404

    response = send_file(ipa_path, mimetype="application/octet-stream")
    response.headers["Content-Disposition"] = "inline; filename=signed.ipa"
    return response


# -------------------------------------------------
# HEALTH
# -------------------------------------------------
@app.route("/health")
def health():
    return jsonify({"ok": True, "zsign": shutil.which("zsign") is not None})


# -------------------------------------------------
# RUN
# -------------------------------------------------
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=True)