This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sepfinderbot.py
executable file
·363 lines (291 loc) · 12.8 KB
/
sepfinderbot.py
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3
import argparse
import html
import logging
import plistlib
import tempfile
import urllib.parse
import zipfile
from enum import Enum
from io import BytesIO
from pathlib import Path
import requests
import telegram
import toml
from packaging import version
from remotezip import RemoteZip
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, InputMediaDocument, ReplyKeyboardMarkup, ReplyKeyboardRemove
from telegram.ext import CallbackQueryHandler, CommandHandler, Filters, MessageHandler, Updater
DEVICE_TYPES = {
'iPhone': 'iPhone',
'iPad': 'iPad',
'iPod touch': 'iPod',
'Apple TV': 'AppleTV',
}
class State(Enum):
NONE = 0
DEVICE_TYPE = 1
DEVICE_MODEL = 2
BOARD_CONFIG = 3
FIRMWARE = 4
def sepbb(update, ctx):
update.message.reply_text('Please select a device type.', reply_markup=ReplyKeyboardMarkup([
[
'iPhone',
'iPad',
],
[
'iPod touch',
'Apple TV',
],
]))
ctx.user_data.clear()
ctx.user_data['state'] = State.DEVICE_TYPE
def on_text(update, ctx):
ctx.user_data.setdefault('state', State.NONE)
text = update.message.text
if ctx.user_data['state'] == State.DEVICE_TYPE:
try:
device_type = DEVICE_TYPES[text]
except KeyError:
return update.message.reply_text('Invalid input.')
r = session.get('https://api.ipsw.me/v4/devices')
if not r.ok:
return update.message.reply_text('Unable to communicate with ipsw.me API, please try again later.')
ctx.bot_data['devices'] = r.json()
devices = [x for x in ctx.bot_data['devices'] if x['identifier'].startswith(device_type)]
if not devices:
return update.message.reply_text(
'No devices found. Please start over using /start.', reply_markup=ReplyKeyboardRemove(),
)
keyboard = []
for i, device in enumerate(devices):
if i % 2 == 0:
keyboard.append([])
keyboard[-1].append(device['name'])
update.message.reply_text('Please select a device.', reply_markup=ReplyKeyboardMarkup(keyboard))
ctx.user_data['state'] = State.DEVICE_MODEL
elif ctx.user_data['state'] == State.DEVICE_MODEL:
try:
device = next(x for x in ctx.bot_data['devices'] if x['name'] == text)
except StopIteration:
return update.message.reply_text('Invalid input.')
r = session.get(f'https://api.ipsw.me/v4/device/{device["identifier"]}')
rb = session.get(f'https://api.m1sta.xyz/betas/{device["identifier"]}')
if r.ok:
device = r.json()
ctx.user_data['ipswme_failed'] = False
else:
device = {
'name': device['name'],
'identifier': device['identifier'],
'boards': device['boards'],
'firmwares': [],
}
update.message.reply_text('Unable to communicate with ipsw.me API.')
ctx.user_data['ipswme_failed'] = True
if rb.ok:
device_beta = rb.json()
device['firmwares'] += [
d for d in device_beta if not any(x['buildid'] == d['buildid'] for x in device['firmwares'])
]
else:
update.message.reply_text('Unable to communicate with the beta API.')
if ctx.user_data['ipswme_failed']:
return update.message.reply_text('Please try again later.', reply_markup=ReplyKeyboardRemove())
# Filter out DEV boards
boards = [x['boardconfig'] for x in device['boards'] if x['boardconfig'].lower().endswith('ap')]
if not boards:
return update.message.reply_text('No boardconfigs found for this device.')
ctx.user_data['device'] = device
if len(boards) > 1:
keyboard = []
for i, board in enumerate(boards):
if i % 2 == 0:
keyboard.append([])
keyboard[-1].append(board)
update.message.reply_text(
'Please select your board config.\n\n'
'You can get this using the System Info tweak or AIDA64 from the App Store.',
reply_markup=ReplyKeyboardMarkup(keyboard),
)
ctx.user_data['state'] = State.BOARD_CONFIG
else:
ctx.user_data['boardconfig'] = boards[0]
show_firmware_menu(update, ctx)
elif ctx.user_data['state'] == State.BOARD_CONFIG:
if not text.lower().endswith('ap'):
return update.message.reply_text('Invalid input.')
ctx.user_data['boardconfig'] = text
show_firmware_menu(update, ctx)
elif ctx.user_data['state'] == State.FIRMWARE:
if 'device' not in ctx.user_data or 'boardconfig' not in ctx.user_data:
return update.message.reply_text(
'Invalid state. The bot was probably restarted. Please start over using /start.',
reply_markup=ReplyKeyboardRemove(),
)
try:
firmware = ctx.user_data['firmware'] = next(
x for x in ctx.user_data['device']['firmwares'] if x['version'] == text
)
except StopIteration:
return update.message.reply_text('Invalid input.')
p = urllib.parse.urlparse(firmware['url'])
if p.netloc == 'appldnld.apple.com':
ctx.user_data['buildmanifest'] = pzb(update, ctx, firmware, 'BuildManifest.plist', 'BuildManifest')
else:
buildmanifest_url = urllib.parse.urlunparse(
p._replace(path='/'.join([*p.path.split('/')[:-1], 'BuildManifest.plist']))
)
r = session.get(buildmanifest_url)
if r.ok:
ctx.user_data['buildmanifest'] = r.content
else:
ctx.user_data['buildmanifest'] = pzb(
update, ctx, ctx.user_data['firmware'], 'BuildManifest.plist', 'BuildManifest'
)
try:
buildmanifest = plistlib.loads(ctx.user_data['buildmanifest'])
except Exception:
update.message.reply_text('Unable to parse BuildManifest, please try again later.')
raise
try:
buildidentity = next(
x for x in buildmanifest['BuildIdentities']
if x['Info']['DeviceClass'].lower() == ctx.user_data['boardconfig'].lower()
)
if 'RestoreSEP' in buildidentity['Manifest']:
sep_path = ctx.user_data['sep_path'] = buildidentity['Manifest']['RestoreSEP']['Info']['Path']
else:
sep_path = ctx.user_data['sep_path'] = None
if 'BasebandFirmware' in buildidentity['Manifest']:
bb_path = ctx.user_data['bb_path'] = buildidentity['Manifest']['BasebandFirmware']['Info']['Path']
else:
bb_path = ctx.user_data['bb_path'] = None
except Exception:
update.message.reply_text('Unable to get data from BuildManifest, please try again later.')
raise
try:
update.message.reply_text(
'Removing keyboard... (ignore this message)',
reply_markup=ReplyKeyboardRemove(),
disable_notification=True,
).delete()
except Exception:
pass
custom_latest_msg = '⚠️ <b>Note</b>: It is recommended to use <code>--custom-latest'
if 'beta' in firmware['version'].lower() or 'rc' in firmware['version'].lower():
custom_latest_msg += f'-beta --custom-latest-buildid {firmware["buildid"]}'
else:
custom_latest_msg += f' {firmware["version"]}'
custom_latest_msg += (
'</code> instead of manually specifying SEP/BB. Manually specifying may lead to bootloops due to '
'incompatibility of other firmware components such as Rose, especially on A13+.'
)
update.message.reply_text(
('<b>{device} ({boardconfig}) - {firmware} ({buildid})</b>\n\n'
'<b>SEP</b>: {sep_path}\n'
'<b>Baseband</b>: {bb_path}\n\n'
'{custom_latest_msg}').format(
device=html.escape(ctx.user_data['device']['name']),
boardconfig=html.escape(ctx.user_data['boardconfig']),
firmware=html.escape(firmware['version']),
buildid=html.escape(firmware['buildid']),
sep_path=html.escape(str(sep_path)),
bb_path=html.escape(str(bb_path)),
custom_latest_msg=custom_latest_msg,
),
parse_mode='html',
reply_markup=InlineKeyboardMarkup([
[
InlineKeyboardButton("Download", callback_data="download"),
],
]),
)
else:
update.message.reply_text(
'Invalid state. The bot was probably restarted. Please start over using /start.',
reply_markup=ReplyKeyboardRemove(),
)
def on_callback_query(update, ctx):
if update.callback_query.data == 'download':
zf = BytesIO()
zf.name = f'sepbb_{ctx.user_data["boardconfig"]}_{ctx.user_data["firmware"]["buildid"]}.zip'
files = [InputMediaDocument(ctx.user_data['buildmanifest'], filename='BuildManifest.plist')]
if ctx.user_data['sep_path']:
sep = pzb(update, ctx, ctx.user_data['firmware'], ctx.user_data['sep_path'], 'SEP')
if sep:
files.append(InputMediaDocument(sep, filename=Path(ctx.user_data['sep_path']).name))
if ctx.user_data['bb_path']:
baseband = pzb(update, ctx, ctx.user_data['firmware'], ctx.user_data['bb_path'], 'baseband')
if baseband:
files.append(InputMediaDocument(baseband, filename=Path(ctx.user_data['bb_path']).name))
try:
update.message.reply_media_group(files)
except telegram.error.NetworkError as e:
if str(e).startswith('File too large'):
update.message.reply_text('Sorry, the files are too big for Telegram.')
else:
raise
ctx.bot.answer_callback_query(update.callback_query.id)
def show_firmware_menu(update, ctx):
if 'device' not in ctx.user_data:
return update.message.reply_text(
'Invalid state. The bot was probably restarted. Please start over using /start.',
reply_markup=ReplyKeyboardRemove(),
)
firmwares = [x for x in ctx.user_data['device']['firmwares'] if x.get('signed')]
if not firmwares:
return update.message.reply_text('No signed firmwares found for this device.')
for firmware in firmwares:
firmware['version'] = firmware['version'].replace('[', '').replace(']', '')
firmwares = sorted(
firmwares,
key=lambda x: version.parse(
x['version'].replace(' ', '').replace('Update', '+')
+ ('1' if x['version'].lower().endswith(('beta', 'rc', 'update')) else '')
),
)
keyboard = []
for i, firmware in enumerate(firmwares):
if i % 2 == 0:
keyboard.append([])
keyboard[-1].append(firmware['version'])
update.message.reply_text(
'Please select a version.\n(Only currently signed versions are shown.)',
reply_markup=ReplyKeyboardMarkup(keyboard),
)
ctx.user_data['state'] = State.FIRMWARE
def pzb(update, ctx, firmware, file, name):
update.message = update.message or update.callback_query.message
update.message.reply_text(f'Extracting {name}, please wait...')
with tempfile.TemporaryDirectory() as d:
try:
with RemoteZip(firmware['url']) as rzip:
rzip.extract(file, d)
except Exception as e:
update.message.reply_text(
f'Unable to extract {name} for the selected firmware, please try again later.'
)
log.exception(e)
return
return (Path(d) / file).read_bytes()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true', help='enable debug logging')
args = parser.parse_args()
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG if args.debug else logging.INFO,
)
log = logging.getLogger('sepfinderbot')
config = toml.load('config.toml')
updater = Updater(config['token'], base_url=config.get('base_url'))
dispatcher = updater.dispatcher
session = requests.Session()
dispatcher.add_handler(CommandHandler('start', sepbb))
dispatcher.add_handler(CommandHandler('sep', sepbb))
dispatcher.add_handler(CommandHandler('sepbb', sepbb))
dispatcher.add_handler(MessageHandler(Filters.text, on_text))
dispatcher.add_handler(CallbackQueryHandler(on_callback_query))
updater.start_polling()