-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.py
More file actions
1294 lines (1055 loc) · 47.1 KB
/
handlers.py
File metadata and controls
1294 lines (1055 loc) · 47.1 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
import base64
from datetime import datetime
from aiogram import Router, F
from aiogram.types import Message, CallbackQuery, BufferedInputFile, InputMediaDocument
from aiogram.filters import Command, StateFilter
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from aiogram.utils.keyboard import InlineKeyboardBuilder, InlineKeyboardButton
from api_client import OneBotAPIClient
from keyboards import (
get_main_menu_keyboard,
get_plans_keyboard,
get_back_keyboard,
get_settings_keyboard,
get_certificate_keyboard,
get_udid_input_keyboard,
get_key_plans_keyboard,
get_ipa_management_keyboard,
get_ipa_actions_keyboard
)
from languages import lang_manager
from config import PLANS, is_admin
from ipa_manager import IPAManager
from r2_storage import R2Storage
from url_shortener import URLShortener
router = Router()
class BotStates(StatesGroup):
waiting_for_udid = State()
waiting_for_api_key = State()
waiting_for_search = State()
waiting_for_p12_thumbnail = State()
waiting_for_mp_thumbnail = State()
waiting_for_key_quantity = State()
waiting_for_key_code = State()
waiting_for_key_udid = State()
waiting_for_ipa_file = State()
def get_text(key: str, **kwargs) -> str:
"""Get localized text"""
return lang_manager.get_text(key, **kwargs)
def format_date(date_string: str) -> str:
"""Format date to DD/MM/YYYY"""
try:
# Try to parse different date formats
if 'T' in date_string:
dt = datetime.fromisoformat(date_string.replace('Z', '+00:00'))
else:
dt = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
return dt.strftime('%d/%m/%Y')
except:
return date_string
def determine_status(api_response: dict) -> str:
"""Determine status based on API response - improved logic"""
# Get mobileprovision and p12 data
mobileprovision = api_response.get('mobileprovision', '')
p12 = api_response.get('p12', '')
# Check if mobileprovision has actual data
has_mobileprovision = (
mobileprovision and
mobileprovision.strip() and
mobileprovision != 'null' and
mobileprovision != '' and
len(mobileprovision.strip()) > 10 # Base64 data should be longer
)
# Check if p12 has actual data
has_p12 = (
p12 and
p12.strip() and
p12 != 'null' and
p12 != '' and
len(p12.strip()) > 10 # Base64 data should be longer
)
# If both exist with actual data, it's active
if has_p12 and has_mobileprovision:
return 'active'
# If only p12 exists or mobileprovision is missing/empty, it's processing
elif has_p12 and not has_mobileprovision:
return 'processing'
# Default to processing for safety
else:
return 'processing'
@router.message(Command("start"))
async def start_command(message: Message, db, config):
"""Handle /start command"""
user = await db.get_user(message.from_user.id)
if not user:
await db.save_user(message.from_user.id, message.from_user.username)
user = {"api_key": None}
registrations = await db.get_user_registrations(message.from_user.id)
# Only show balance and device info to admin
if is_admin(message.from_user.id):
if user.get("api_key"):
try:
api_client = OneBotAPIClient(config.API_BASE_URL)
balance = await api_client.get_balance(user["api_key"])
# No need to close since we're using context managers now
text = get_text("welcome", balance=balance, devices=len(registrations))
except:
text = get_text("welcome_no_balance", devices=len(registrations))
else:
text = get_text("welcome_no_api", devices=len(registrations))
else:
# Normal users get simple welcome message without sensitive info
text = get_text("welcome_normal_user")
await message.answer(text, reply_markup=get_main_menu_keyboard(message.from_user.id))
@router.callback_query(F.data == "register")
async def start_registration(callback: CallbackQuery, state: FSMContext, db):
"""Start UDID registration - show plans first (admin only)"""
if not is_admin(callback.from_user.id):
await callback.answer("Access denied!", show_alert=True)
return
user = await db.get_user(callback.from_user.id)
if not user or not user.get("api_key"):
await callback.answer(get_text("api_key_required"), show_alert=True)
return
await callback.message.edit_text(
get_text("select_plan_first"),
reply_markup=get_plans_keyboard()
)
await callback.answer()
@router.callback_query(F.data.startswith("plan_"))
async def process_plan_selection_first(callback: CallbackQuery, state: FSMContext):
"""Process plan selection and ask for UDID (admin only)"""
if not is_admin(callback.from_user.id):
await callback.answer("Access denied!", show_alert=True)
return
plan = callback.data.replace("plan_", "")
await state.update_data(plan=plan)
await callback.message.edit_text(
get_text("register_udid_after_plan", plan=plan),
reply_markup=get_udid_input_keyboard()
)
await state.set_state(BotStates.waiting_for_udid)
await callback.answer()
@router.message(StateFilter(BotStates.waiting_for_udid))
async def process_udid(message: Message, state: FSMContext, db, config):
"""Process UDID input and register (admin only)"""
if not is_admin(message.from_user.id):
await message.answer("Access denied!")
return
udid = message.text.strip().upper()
if not re.match(r'^[0-9A-F]{8}-[0-9A-F]{16}$', udid):
await message.answer(
get_text("invalid_udid"),
reply_markup=get_back_keyboard()
)
return
data = await state.get_data()
plan = data.get("plan")
user = await db.get_user(message.from_user.id)
# Add electric reaction to the UDID message
await message.react([{"type": "emoji", "emoji": "⚡"}])
try:
api_client = OneBotAPIClient(config.API_BASE_URL)
result = await api_client.register_udid(user["api_key"], udid, plan)
# Always fetch the latest certificate data for accurate status
try:
certificates = await api_client.get_certificate(user["api_key"], udid=udid)
if certificates:
cert_data = certificates[0]
# Use certificate data for status determination (most accurate)
status = determine_status(cert_data)
# Save complete certificate data
await db.save_certificate(
message.from_user.id,
udid,
cert_data.get("id", ""),
cert_data
)
# Use name from certificate data if available
name = cert_data.get('name', result.get('name', 'N/A'))
else:
# Fallback to registration response
status = determine_status(result)
name = result.get('name', 'N/A')
except:
# Fallback to registration response
status = determine_status(result)
name = result.get('name', 'N/A')
await db.save_registration(
message.from_user.id,
udid,
result.get("certificate_id", ""),
plan,
result,
status
)
# No need to close since we're using context managers now
status_emoji = "🟡" if status == "processing" else "🟢"
# Format registration success with clean info
current_date = datetime.now().strftime('%d/%m/%Y')
success_text = get_text("registration_success",
udid=udid,
date=current_date,
status=get_text(f"status_{status.lower()}"),
emoji=status_emoji,
name=name
)
await message.answer(
success_text,
reply_markup=get_back_keyboard()
)
except Exception as e:
await message.answer(
get_text("registration_failed", error=str(e)),
reply_markup=get_back_keyboard()
)
await state.clear()
@router.callback_query(F.data == "search")
async def start_search(callback: CallbackQuery, state: FSMContext, db):
"""Start search"""
user = await db.get_user(callback.from_user.id)
if not user or not user.get("api_key"):
await callback.answer(get_text("api_key_required"), show_alert=True)
return
await callback.message.edit_text(
get_text("search"),
reply_markup=get_back_keyboard()
)
await state.set_state(BotStates.waiting_for_search)
await callback.answer()
@router.message(StateFilter(BotStates.waiting_for_search))
async def process_search(message: Message, state: FSMContext, db, config):
"""Process search query"""
query = message.text.strip()
user = await db.get_user(message.from_user.id)
try:
api_client = OneBotAPIClient(config.API_BASE_URL)
# Try search by UDID first, then by certificate ID
results = []
try:
certs = await api_client.get_certificate(user["api_key"], udid=query)
results.extend(certs)
except:
pass
if not results:
try:
certs = await api_client.get_certificate(user["api_key"], certificate_id=query)
results.extend(certs)
except:
pass
# No need to close since we're using context managers now
if results:
cert_data = results[0]
udid = cert_data.get('udid', 'N/A')
# Save certificate data to database
await db.save_certificate(
message.from_user.id,
udid,
cert_data.get("id", ""),
cert_data
)
# Check if this UDID is already registered in our database
registration = await db.get_registration_by_udid(message.from_user.id, udid)
# If not registered, create a registration entry from the search result
if not registration:
# Determine the plan from certificate data (fallback to 'unknown' if not available)
plan = cert_data.get('plan', 'unknown')
# Determine status from current API response
current_status = determine_status(cert_data)
# Save as registration so user can download certificates
await db.save_registration(
message.from_user.id,
udid,
cert_data.get("id", ""),
plan,
cert_data, # Use cert_data as api_response
current_status
)
enabled = True # New registrations are enabled by default
else:
enabled = registration['enabled']
# Determine status from current API response (most accurate)
current_status = determine_status(cert_data)
# Update database status if it exists and has changed
if registration['status'] != current_status:
await db.update_registration_status(udid, current_status)
status_emoji = "🟡" if current_status == "processing" else "🟢"
# Format clean search results
added_date = format_date(cert_data.get('created_at', cert_data.get('date', datetime.now().isoformat())))
search_result = get_text("search_result",
udid=udid,
date=added_date,
status=get_text(f"status_{current_status.lower()}"),
emoji=status_emoji,
name=cert_data.get('name', 'N/A')
)
await message.answer(
search_result,
reply_markup=get_certificate_keyboard(udid, enabled, current_status, message.from_user.id)
)
else:
await message.answer(
get_text("no_search_results", query=query),
reply_markup=get_back_keyboard()
)
except Exception as e:
await message.answer(
get_text("registration_failed", error=str(e)),
reply_markup=get_back_keyboard()
)
await state.clear()
@router.callback_query(F.data.startswith("toggle_"))
async def toggle_certificate(callback: CallbackQuery, db, config):
"""Toggle certificate enabled/disabled status (admin only)"""
if not is_admin(callback.from_user.id):
await callback.answer("Access denied!", show_alert=True)
return
udid = callback.data.replace("toggle_", "")
user = await db.get_user(callback.from_user.id)
# Toggle the status
new_enabled = await db.toggle_registration_enabled(callback.from_user.id, udid)
# Get updated certificate data
try:
api_client = OneBotAPIClient(config.API_BASE_URL)
certificates = await api_client.get_certificate(user["api_key"], udid=udid)
# No need to close since we're using context managers now
if certificates:
cert_data = certificates[0]
current_status = determine_status(cert_data)
status_emoji = "🟡" if current_status == "processing" else "🟢"
status_text = "Processing" if current_status == "processing" else "Active"
# Format search results
added_date = format_date(cert_data.get('created_at', cert_data.get('date', datetime.now().isoformat())))
search_result = f"""UDID : {udid}
Added on : {added_date}
Status: {status_text} {status_emoji}
Name : {cert_data.get('name', 'N/A')}"""
await callback.message.edit_text(
search_result,
reply_markup=get_certificate_keyboard(udid, new_enabled, current_status, callback.from_user.id)
)
except Exception as e:
await callback.answer(get_text("error_updating_status", error=str(e)), show_alert=True)
await callback.answer()
@router.callback_query(F.data.startswith("download_cert_"))
async def download_certificate(callback: CallbackQuery, db, config):
"""Download certificate files"""
udid = callback.data.replace("download_cert_", "")
user = await db.get_user(callback.from_user.id)
# Check if certificate is enabled
registration = await db.get_registration_by_udid(callback.from_user.id, udid)
if not registration or not registration['enabled']:
await callback.answer(get_text("certificate_disabled"), show_alert=True)
return
try:
api_client = OneBotAPIClient(config.API_BASE_URL)
certificates = await api_client.get_certificate(user["api_key"], udid=udid)
# No need to close since we're using context managers now
if not certificates:
await callback.answer(get_text("certificate_not_found"), show_alert=True)
return
cert_data = certificates[0]
# Double check status before allowing download
current_status = determine_status(cert_data)
if current_status == "processing":
await callback.answer(get_text("certificate_processing"), show_alert=True)
return
# Get global thumbnails (now base64 data)
thumbnails = await db.get_global_thumbnails()
# Prepare files to send together as media group
media_group = []
# Add P12 file (no caption, with thumbnail if available)
if cert_data.get('p12'):
try:
p12_data = base64.b64decode(cert_data['p12'])
p12_file = BufferedInputFile(p12_data, filename=f"{udid}.p12")
# Use base64 thumbnail data directly
if thumbnails and thumbnails.get('p12_thumbnail'):
thumbnail_data = base64.b64decode(thumbnails['p12_thumbnail'])
thumbnail_input = BufferedInputFile(thumbnail_data, filename="thumb.jpg")
media_group.append(InputMediaDocument(media=p12_file, thumbnail=thumbnail_input))
else:
media_group.append(InputMediaDocument(media=p12_file))
except Exception as e:
await callback.message.answer(get_text("error_preparing_p12", error=str(e)))
# Add mobileprovision file (password caption, with thumbnail if available)
if cert_data.get('mobileprovision') and cert_data.get('mobileprovision').strip():
try:
mp_data = base64.b64decode(cert_data['mobileprovision'])
mp_file = BufferedInputFile(mp_data, filename=f"{udid}.mobileprovision")
# Fix HTML parsing error - escape the password properly
password = cert_data.get('p12_password', '1')
caption = f"{get_text('certificate_password', password=password)}"
# Use base64 thumbnail data directly
if thumbnails and thumbnails.get('mobileprovision_thumbnail'):
thumbnail_data = base64.b64decode(thumbnails['mobileprovision_thumbnail'])
thumbnail_input = BufferedInputFile(thumbnail_data, filename="thumb.jpg")
media_group.append(InputMediaDocument(
media=mp_file,
caption=caption,
thumbnail=thumbnail_input
))
else:
media_group.append(InputMediaDocument(
media=mp_file,
caption=caption
))
except Exception as e:
await callback.message.answer(get_text("certificate_file_error", file_type="mobileprovision", error=str(e)))
# Send both files together as media group
if media_group:
try:
await callback.message.answer_media_group(media_group)
except Exception as e:
# Fallback: send files separately if media group fails
await callback.message.answer(get_text("certificate_group_error", error=str(e)))
# Send P12 file individually
if cert_data.get('p12'):
try:
p12_data = base64.b64decode(cert_data['p12'])
p12_file = BufferedInputFile(p12_data, filename=f"{udid}.p12")
await callback.message.answer_document(p12_file)
except Exception as e:
await callback.message.answer(get_text("certificate_file_error", file_type="P12", error=str(e)))
# Send mobileprovision file individually
if cert_data.get('mobileprovision') and cert_data.get('mobileprovision').strip():
try:
mp_data = base64.b64decode(cert_data['mobileprovision'])
mp_file = BufferedInputFile(mp_data, filename=f"{udid}.mobileprovision")
password = cert_data.get('p12_password', '1')
await callback.message.answer_document(
mp_file,
caption=get_text("certificate_password", password=password)
)
except Exception as e:
await callback.message.answer(get_text("certificate_file_error", file_type="mobileprovision", error=str(e)))
else:
await callback.answer(get_text("certificate_no_files"), show_alert=True)
except Exception as e:
await callback.answer(get_text("certificate_download_error", error=str(e)), show_alert=True)
# Sign all unsigned IPAs for this user and show install buttons
try:
unsigned_ipas = await db.get_unsigned_ipas(callback.from_user.id)
if unsigned_ipas:
ipa_manager = IPAManager()
# Get certificate data for signing
p12_data = base64.b64decode(cert_data['p12'])
mobileprovision_data = base64.b64decode(cert_data['mobileprovision'])
signed_count = await ipa_manager.sign_all_user_ipas(
unsigned_ipas, p12_data, mobileprovision_data, db
)
# Show install buttons for all signed IPAs
signed_ipas = await db.get_signed_ipas(callback.from_user.id)
if signed_ipas:
# Create install buttons with pre-shortened URLs
builder = InlineKeyboardBuilder()
url_shortener = URLShortener()
# Add install buttons in rows of 2
for i in range(0, len(signed_ipas), 2):
row_ipas = signed_ipas[i:i+2]
row_buttons = []
for ipa in row_ipas:
app_name = ipa['app_name'] if ipa['app_name'] != 'Unknown' else ipa['original_filename'].replace('.ipa', '')
# Pre-shorten the URL in the backend
short_url = await url_shortener.shorten_install_url(ipa['install_url'])
row_buttons.append(
InlineKeyboardButton(
text=f"📱 {app_name}",
url=short_url # Use pre-shortened URL directly
)
)
builder.row(*row_buttons)
# No back button as requested
install_text = get_text("certificate_signed_message", udid=udid)
await callback.message.answer(
install_text,
reply_markup=builder.as_markup()
)
except Exception as e:
pass
await callback.answer()
@router.callback_query(F.data.startswith("install_ipa_"))
async def get_install_link(callback: CallbackQuery, db):
"""Get install link for specific IPA - This handler is now unused since buttons go directly to URLs"""
ipa_id = int(callback.data.replace("install_ipa_", ""))
ipa = await db.get_ipa_by_id(ipa_id)
if not ipa or ipa['user_id'] != callback.from_user.id:
await callback.answer(get_text("ipa_not_found"), show_alert=True)
return
if not ipa['install_url']:
await callback.answer(get_text("ipa_not_signed"), show_alert=True)
return
app_name = ipa['app_name'] if ipa['app_name'] != 'Unknown' else ipa['original_filename'].replace('.ipa', '')
# Show processing message
processing_msg = await callback.message.edit_text(get_text("ipa_preparing_link"))
try:
# Shorten the install URL
url_shortener = URLShortener()
short_install_url = await url_shortener.shorten_install_url(ipa['install_url'])
# Create inline keyboard with only install button (no back button)
builder = InlineKeyboardBuilder()
builder.row(
InlineKeyboardButton(
text=get_text("ipa_install_button", app_name=app_name),
url=short_install_url
)
)
# Simple message with just the app name
text = get_text("ipa_install_message", app_name=app_name)
await processing_msg.edit_text(text, reply_markup=builder.as_markup())
except Exception as e:
# Fallback to original URL if shortening fails
builder = InlineKeyboardBuilder()
builder.row(
InlineKeyboardButton(
text=get_text("ipa_install_button", app_name=app_name),
url=ipa['install_url']
)
)
text = get_text("ipa_install_message", app_name=app_name)
await processing_msg.edit_text(text, reply_markup=builder.as_markup())
await callback.answer()
@router.callback_query(F.data == "settings")
async def show_settings(callback: CallbackQuery, db):
"""Show settings (admin only)"""
if not is_admin(callback.from_user.id):
await callback.answer("Access denied!", show_alert=True)
return
user = await db.get_user(callback.from_user.id)
# Get thumbnail status
thumbnails = await db.get_thumbnails(callback.from_user.id)
settings_text = get_text("settings",
api_status=get_text("status_set") if user and user.get("api_key") else get_text("status_not_set"),
p12_status=get_text("status_set") if thumbnails and thumbnails.get('p12_thumbnail') else get_text("status_not_set"),
mp_status=get_text("status_set") if thumbnails and thumbnails.get('mobileprovision_thumbnail') else get_text("status_not_set")
)
await callback.message.edit_text(
settings_text,
reply_markup=get_settings_keyboard()
)
await callback.answer()
@router.callback_query(F.data == "set_api_key")
async def set_api_key(callback: CallbackQuery, state: FSMContext):
"""Start API key setting (admin only)"""
if not is_admin(callback.from_user.id):
await callback.answer("Access denied!", show_alert=True)
return
await callback.message.edit_text(
get_text("set_api_key"),
reply_markup=get_back_keyboard()
)
await state.set_state(BotStates.waiting_for_api_key)
await callback.answer()
@router.message(StateFilter(BotStates.waiting_for_api_key))
async def process_api_key(message: Message, state: FSMContext, db, config):
"""Process API key (admin only)"""
if not is_admin(message.from_user.id):
await message.answer("Access denied!")
return
api_key = message.text.strip()
try:
api_client = OneBotAPIClient(config.API_BASE_URL)
balance = await api_client.get_balance(api_key)
# No need to close since we're using context managers now
await db.save_user(message.from_user.id, message.from_user.username, api_key)
await message.answer(
get_text("api_key_success", balance=balance),
reply_markup=get_back_keyboard()
)
except Exception as e:
await message.answer(
get_text("api_key_invalid", error=str(e)),
reply_markup=get_back_keyboard()
)
await state.clear()
@router.callback_query(F.data == "set_thumbnails")
async def set_thumbnails_start(callback: CallbackQuery, state: FSMContext):
"""Start thumbnail setting process (admin only)"""
if not is_admin(callback.from_user.id):
await callback.answer("Access denied!", show_alert=True)
return
await callback.message.edit_text(
get_text("set_thumbnails_start"),
reply_markup=get_back_keyboard()
)
await state.set_state(BotStates.waiting_for_p12_thumbnail)
await callback.answer()
@router.message(StateFilter(BotStates.waiting_for_p12_thumbnail))
async def process_p12_thumbnail(message: Message, state: FSMContext, db):
"""Process P12 thumbnail image (admin only)"""
if not is_admin(message.from_user.id):
await message.answer("Access denied!")
return
if not message.photo:
await message.answer(
get_text("thumbnail_error"),
reply_markup=get_back_keyboard()
)
return
try:
# Get the largest photo size and download it
photo = message.photo[-1]
file = await message.bot.get_file(photo.file_id)
file_data = await message.bot.download_file(file.file_path)
# Convert to base64
thumbnail_base64 = base64.b64encode(file_data.read()).decode('utf-8')
# Save P12 thumbnail as base64
await db.save_thumbnails(message.from_user.id, p12_thumbnail_data=thumbnail_base64)
# Now ask for mobileprovision thumbnail
await message.answer(
get_text("p12_thumbnail_success"),
reply_markup=get_back_keyboard()
)
await state.set_state(BotStates.waiting_for_mp_thumbnail)
except Exception as e:
await message.answer(
get_text("thumbnail_save_error", type="P12", error=str(e)),
reply_markup=get_back_keyboard()
)
await state.clear()
@router.message(StateFilter(BotStates.waiting_for_mp_thumbnail))
async def process_mp_thumbnail(message: Message, state: FSMContext, db):
"""Process mobileprovision thumbnail image (admin only)"""
if not is_admin(message.from_user.id):
await message.answer("Access denied!")
return
if not message.photo:
await message.answer(
get_text("thumbnail_error"),
reply_markup=get_back_keyboard()
)
return
try:
# Get the largest photo size and download it
photo = message.photo[-1]
file = await message.bot.get_file(photo.file_id)
file_data = await message.bot.download_file(file.file_path)
# Convert to base64
thumbnail_base64 = base64.b64encode(file_data.read()).decode('utf-8')
# Save mobileprovision thumbnail as base64
await db.save_thumbnails(message.from_user.id, mobileprovision_thumbnail_data=thumbnail_base64)
await message.answer(
get_text("thumbnails_success"),
reply_markup=get_back_keyboard()
)
except Exception as e:
await message.answer(
get_text("thumbnail_save_error", type="mobileprovision", error=str(e)),
reply_markup=get_back_keyboard()
)
await state.clear()
@router.callback_query(F.data == "back_to_main")
async def back_to_main(callback: CallbackQuery, state: FSMContext, db, config):
"""Go back to main menu"""
await state.clear()
user = await db.get_user(callback.from_user.id)
registrations = await db.get_user_registrations(callback.from_user.id)
# Only show balance and device info to admin
if is_admin(callback.from_user.id):
if user and user.get("api_key"):
try:
api_client = OneBotAPIClient(config.API_BASE_URL)
balance = await api_client.get_balance(user["api_key"])
# No need to close since we're using context managers now
text = get_text("welcome", balance=balance, devices=len(registrations))
except:
text = get_text("welcome_no_balance", devices=len(registrations))
else:
text = get_text("welcome_no_api", devices=len(registrations))
else:
# Normal users get simple welcome message without sensitive info
text = get_text("welcome_normal_user")
await callback.message.edit_text(text, reply_markup=get_main_menu_keyboard(callback.from_user.id))
await callback.answer()
@router.callback_query(F.data == "create_keys")
async def start_key_creation(callback: CallbackQuery, state: FSMContext, db):
"""Start key creation process (admin only)"""
if not is_admin(callback.from_user.id):
await callback.answer("Access denied!", show_alert=True)
return
user = await db.get_user(callback.from_user.id)
if not user or not user.get("api_key"):
await callback.answer(get_text("error_api_key_first"), show_alert=True)
return
# Show key statistics
key_stats = await db.get_key_stats(callback.from_user.id)
stats_text = get_text("create_keys_start", stats="")
if key_stats:
stats_lines = []
for plan, stats in key_stats.items():
plan_name = PLANS.get(plan, plan)
stats_lines.append(get_text("stats_unused_keys", plan=plan_name, unused=stats['unused'], total=stats['total']))
stats_text = get_text("create_keys_start", stats="\n".join(stats_lines))
else:
stats_text = get_text("create_keys_start", stats=get_text("no_keys_yet"))
await callback.message.edit_text(
stats_text,
reply_markup=get_key_plans_keyboard()
)
await callback.answer()
@router.callback_query(F.data.startswith("key_plan_"))
async def process_key_plan_selection(callback: CallbackQuery, state: FSMContext):
"""Process plan selection for key creation (admin only)"""
if not is_admin(callback.from_user.id):
await callback.answer("Access denied!", show_alert=True)
return
plan = callback.data.replace("key_plan_", "")
await state.update_data(key_plan=plan)
plan_name = PLANS.get(plan, plan)
await callback.message.edit_text(
get_text("key_plan_selected", plan=plan_name),
reply_markup=get_back_keyboard()
)
await state.set_state(BotStates.waiting_for_key_quantity)
await callback.answer()
@router.message(StateFilter(BotStates.waiting_for_key_quantity))
async def process_key_quantity(message: Message, state: FSMContext, db):
"""Process key quantity input (admin only)"""
if not is_admin(message.from_user.id):
await message.answer("Access denied!")
return
try:
quantity = int(message.text.strip())
if quantity < 1 or quantity > 100:
await message.answer(
get_text("key_quantity_invalid"),
reply_markup=get_back_keyboard()
)
return
data = await state.get_data()
plan = data.get("key_plan")
# Create keys and get the key codes
key_codes = await db.create_keys(message.from_user.id, plan, quantity)
plan_name = PLANS.get(plan, plan)
# Format the key codes nicely
keys_text = "\n".join([f"🔑 {code}" for code in key_codes])
success_message = get_text("keys_created", quantity=quantity, plan=plan_name, keys=keys_text)
await message.answer(
success_message,
reply_markup=get_back_keyboard()
)
except ValueError:
await message.answer(
get_text("key_quantity_error"),
reply_markup=get_back_keyboard()
)
return
await state.clear()
@router.callback_query(F.data == "use_key")
async def start_key_usage(callback: CallbackQuery, state: FSMContext, db):
"""Start key usage process"""
user = await db.get_user(callback.from_user.id)
if not user or not user.get("api_key"):
await callback.answer(get_text("error_api_key_first"), show_alert=True)
return
await callback.message.edit_text(
get_text("use_key_start"),
reply_markup=get_back_keyboard()
)
await state.set_state(BotStates.waiting_for_key_code)
await callback.answer()
@router.message(StateFilter(BotStates.waiting_for_key_code))
async def process_key_code(message: Message, state: FSMContext, db):
"""Process key code input"""
key_code = message.text.strip().upper()
# Validate key format (10 characters, alphanumeric)
if not re.match(r'^[A-Z0-9]{10}$', key_code):
await message.answer(
get_text("key_format_invalid"),
reply_markup=get_back_keyboard()
)
return
# Check if key exists and is unused
key = await db.get_key_by_code(key_code)
if not key:
await message.answer(
get_text("key_not_found"),
reply_markup=get_back_keyboard()
)
await state.clear()
return
if key['used']:
await message.answer(
get_text("key_already_used"),
reply_markup=get_back_keyboard()
)
await state.clear()
return
# Store key info and ask for UDID
await state.update_data(key_code=key_code, key_plan=key['plan'])
plan_name = PLANS.get(key['plan'], key['plan'])
await message.answer(
get_text("key_valid", plan=plan_name),
reply_markup=get_back_keyboard()
)
await state.set_state(BotStates.waiting_for_key_udid)
@router.message(StateFilter(BotStates.waiting_for_key_udid))
async def process_key_udid(message: Message, state: FSMContext, db, config):
"""Process UDID input for key usage"""
udid = message.text.strip().upper()
if not re.match(r'^[0-9A-F]{8}-[0-9A-F]{16}$', udid):
await message.answer(
get_text("invalid_udid"),
reply_markup=get_back_keyboard()
)
return
data = await state.get_data()
key_code = data.get("key_code")
plan = data.get("key_plan")
user = await db.get_user(message.from_user.id)
# Add electric reaction to the UDID message
await message.react([{"type": "emoji", "emoji": "⚡"}])
try:
api_client = OneBotAPIClient(config.API_BASE_URL)
result = await api_client.register_udid(user["api_key"], udid, plan)
# Always fetch the latest certificate data for accurate status
try:
certificates = await api_client.get_certificate(user["api_key"], udid=udid)
if certificates:
cert_data = certificates[0]
# Use certificate data for status determination (most accurate)
status = determine_status(cert_data)
# Save complete certificate data
await db.save_certificate(
message.from_user.id,
udid,
cert_data.get("id", ""),
cert_data