From d085d86a26360b8d4dc1e014fe1914163098fd2b Mon Sep 17 00:00:00 2001 From: MMXX Date: Wed, 29 Dec 2021 15:28:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=B9=E6=9E=9C=E8=B6=85=E7=BA=A7=E7=AD=BE?= =?UTF-8?q?=E5=90=8D=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fir_ser/api/utils/app/supersignutils.py | 127 +++++++++++++----------- fir_ser/api/utils/storage/storage.py | 2 +- fir_ser/api/views/download.py | 20 ++-- fir_ser/api/views/receiveudids.py | 3 +- fir_ser/config.py | 4 +- 5 files changed, 80 insertions(+), 76 deletions(-) diff --git a/fir_ser/api/utils/app/supersignutils.py b/fir_ser/api/utils/app/supersignutils.py index 86a7975..ddecc64 100644 --- a/fir_ser/api/utils/app/supersignutils.py +++ b/fir_ser/api/utils/app/supersignutils.py @@ -69,7 +69,7 @@ def resign_by_app_id_and_developer(app_id, developer_id, developer_app_id, need_ locker = { 'locker_key': f"run_sign_{app_obj.app_id}_{developer_obj.issuer_id}", "timeout": 60 * 5} - status, result = IosUtils.run_sign(app_obj.user_id, app_obj, developer_obj, d_time, [], locker=locker) + status, result = IosUtils.run_sign(app_obj.user_id, app_obj, developer_obj, d_time, locker=locker) return status, {'developer_id': developer_obj.issuer_id, 'result': (status, result)} @@ -375,6 +375,29 @@ def get_developer_obj_by_others(user_obj, udid, app_obj, read_only): return result +def check_sign_is_exists(user_obj, app_obj, udid, developer_obj): + d_result = {'code': 0, 'msg': 'success'} + app_udid_obj = AppUDID.objects.filter(app_id=app_obj, udid__udid=udid, udid__developerid=developer_obj).first() + if app_udid_obj and app_udid_obj.is_download: + if app_udid_obj.is_signed: + if check_ipa_is_latest_sign(app_obj, developer_obj): + d_result['msg'] = f'udid {udid} exists app_id {app_obj}' + logger.warning(d_result) + return True, d_result + else: + sign_flag = True + else: + sign_flag = True + if sign_flag: + locker = { + 'locker_key': f"run_sign_{app_obj.app_id}_{developer_obj.issuer_id}", + "timeout": 60 * 5 + } + logger.warning(f"udid {udid} is exist but app_id {app_obj} not sign. start run_sign ...") + return IosUtils.run_sign(user_obj, app_obj, developer_obj, time.time(), [udid], locker=locker) + logger.warning(f"udid {udid} not exist app_id {app_obj} . start find developer and sign") + + class IosUtils(object): def __init__(self, udid_info, user_obj, app_obj=None): self.developer_obj = None @@ -471,22 +494,23 @@ class IosUtils(object): def update_sign_file_name(user_obj, app_obj, developer_obj_id, release_obj, random_file_name): apptodev_obj = APPToDeveloper.objects.filter(developerid_id=developer_obj_id, app_id=app_obj).first() storage_obj = Storage(user_obj) - if apptodev_obj: - delete_local_files(apptodev_obj.binary_file + ".ipa") - storage_obj.delete_file(apptodev_obj.binary_file + ".ipa") - apptodev_obj.binary_file = random_file_name - old_release_file = apptodev_obj.release_file - apptodev_obj.release_file = release_obj.release_id - apptodev_obj.save(update_fields=['binary_file', 'release_file']) - if storage_obj.get_storage_type() in [1, 2] and old_release_file != release_obj.release_id: - logger.warning(f"update sign file , now clean ole {old_release_file}.ipa file") - delete_local_files(old_release_file + ".ipa") - else: - APPToDeveloper.objects.create(developerid_id=developer_obj_id, app_id=app_obj, - binary_file=random_file_name, release_file=release_obj.release_id) logger.info(f"update sign file end, now upload {storage_obj.storage} {random_file_name}.ipa file") - return storage_obj.upload_file(os.path.join(MEDIA_ROOT, random_file_name + ".ipa")) + if storage_obj.upload_file(os.path.join(MEDIA_ROOT, random_file_name + ".ipa")): + if apptodev_obj: + delete_local_files(apptodev_obj.binary_file + ".ipa") + storage_obj.delete_file(apptodev_obj.binary_file + ".ipa") + apptodev_obj.binary_file = random_file_name + old_release_file = apptodev_obj.release_file + apptodev_obj.release_file = release_obj.release_id + apptodev_obj.save(update_fields=['binary_file', 'release_file']) + if storage_obj.get_storage_type() in [1, 2] and old_release_file != release_obj.release_id: + logger.warning(f"update sign file , now clean ole {old_release_file}.ipa file") + delete_local_files(old_release_file + ".ipa") + else: + APPToDeveloper.objects.create(developerid_id=developer_obj_id, app_id=app_obj, + binary_file=random_file_name, release_file=release_obj.release_id) + return True @staticmethod def update_sign_data(user_obj, app_obj, developer_obj_id, random_file_name, release_obj, udid_list): @@ -497,9 +521,7 @@ class IosUtils(object): for udid in udid_list: udid_obj = UDIDsyncDeveloper.objects.filter(developerid_id=developer_obj_id, udid=udid).first() AppUDID.objects.filter(app_id=app_obj, udid=udid_obj).update(**{"is_signed": True}) - del_cache_response_by_short(app_obj.app_id, udid=udid) - else: - del_cache_response_by_short(app_obj.app_id) + del_cache_response_by_short(app_obj.app_id) return True def check_sign_permission(self): @@ -524,30 +546,6 @@ class IosUtils(object): return False, d_result return True, d_result - def check_sign_is_exists(self, client_ip): - d_result = {'code': 0, 'msg': 'success'} - app_udid_obj = AppUDID.objects.filter(app_id=self.app_obj, udid__udid=self.udid, - udid__developerid=self.developer_obj).first() - if app_udid_obj and app_udid_obj.is_download: - sign_flag = False - if app_udid_obj.is_signed: - if check_ipa_is_latest_sign(self.app_obj, self.developer_obj): - msg = "udid %s exists app_id %s" % (self.udid, self.app_obj) - d_result['msg'] = msg - logger.info(d_result) - return True, d_result - else: - sign_flag = True - else: - sign_flag = True - if sign_flag: - locker = { - 'locker_key': f"run_sign_{self.app_obj.app_id}_{self.developer_obj.issuer_id}", - "timeout": 60 * 5} - logger.info("start run_sign ...") - return IosUtils.run_sign(self.user_obj, self.app_obj, self.developer_obj, - time.time(), [self.udid], locker=locker) - @staticmethod @call_function_try_attempts() def check_or_register_devices(app_obj, user_obj, developer_obj, udid_info, client_ip, failed_call_prefix): @@ -653,10 +651,10 @@ class IosUtils(object): @staticmethod @call_function_try_attempts() def make_and_download_profile(app_obj, developer_obj, failed_call_prefix, developer_app_id_obj=None, - new_device_id_list=None, + new_did_list=None, failed_callback=None): - if new_device_id_list is None: - new_device_id_list = [] + if new_did_list is None: + new_did_list = [] if failed_callback is None: failed_callback = [] @@ -665,8 +663,8 @@ class IosUtils(object): device_id_list = DeveloperDevicesID.objects.filter(app_id=app_obj, developerid=developer_obj).values_list('did') device_id_lists = [did[0] for did in device_id_list] - if new_device_id_list: - device_id_lists.extend(new_device_id_list) + if new_did_list: + device_id_lists.extend(new_did_list) device_id_lists = list(set(device_id_lists)) if not developer_app_id_obj: developer_app_id_obj = DeveloperAppID.objects.filter(developerid=developer_obj, app_id=app_obj).first() @@ -704,7 +702,7 @@ class IosUtils(object): return status, msg d_result = {'code': 0, 'msg': 'success'} - result = self.check_sign_is_exists(client_ip) + result = check_sign_is_exists(self.user_obj, self.app_obj, self.udid, self.developer_obj) if result: return result @@ -767,17 +765,20 @@ class IosUtils(object): time.sleep(5) with cache.lock(download_profile_prefix, timeout=60 * 10): did_udid_lists = get_and_clean_udid_cache_queue(prefix_key) - if not did_udid_lists: + new_did_list = [] + new_udid_list = [] + for did_udid in did_udid_lists: + if not check_sign_is_exists(self.user_obj, self.app_obj, did_udid[1], self.developer_obj): + new_did_list.append(did_udid[0]) + new_udid_list.append(did_udid[1]) + if not new_did_list: return True, True logger.warning( - f'app {self.app_obj} receive {len(did_udid_lists)} did_udid_result: {did_udid_lists}') + f'app {self.app_obj} receive {len(new_did_list)} did_udid_result: {new_did_list}') status, download_profile_result = IosUtils.make_and_download_profile(self.app_obj, self.developer_obj, add_new_bundles_prefix, - new_device_id_list=[did_udid[0] - for - did_udid in - did_udid_lists]) + new_did_list=new_did_list) if not status: if 'UNEXPECTED_ERROR' in str(download_profile_result): return False, {} @@ -789,10 +790,10 @@ class IosUtils(object): return True, True locker = { 'locker_key': f"run_sign_{self.app_obj.app_id}_{self.developer_obj.issuer_id}", - "timeout": 60 * 5} + "timeout": 60 * 5 + } return IosUtils.run_sign(self.user_obj, self.app_obj, self.developer_obj, - start_time, - [did_udid[1] for did_udid in did_udid_lists], locker=locker) + start_time, new_udid_list, locker=locker) if not self.developer_obj: d_result['code'] = 1005 @@ -806,6 +807,16 @@ class IosUtils(object): def run_sign(user_obj, app_obj, developer_obj, d_time, udid_list=None): if udid_list is None: udid_list = [] + else: + new_did_list = [] + for did_udid in udid_list: + if not check_sign_is_exists(user_obj, app_obj, did_udid, developer_obj): + new_did_list.append(did_udid) + if not new_did_list: + return True, True + else: + udid_list = new_did_list + d_result = {'code': 0, 'msg': 'success'} AppUDID.objects.filter(app_id=app_obj, udid__udid__in=udid_list, udid__developerid=developer_obj).update(is_download=True) @@ -829,7 +840,7 @@ class IosUtils(object): return status, d_result msg = f"app_id {app_obj} developer {developer_obj} sign end... time:{time.time() - start_time}" - logger.info(msg) + logger.warning(msg) d_result['msg'] = msg return True, d_result diff --git a/fir_ser/api/utils/storage/storage.py b/fir_ser/api/utils/storage/storage.py index 5bcf74d..666475d 100644 --- a/fir_ser/api/utils/storage/storage.py +++ b/fir_ser/api/utils/storage/storage.py @@ -98,7 +98,7 @@ class Storage(object): else: filename = filename + '.ipa' try: - logger.info(f"storage {self.storage} delete file {filename}") + logger.warning(f"storage {self.storage} delete file {filename}") return self.storage.del_file(filename) except Exception as e: logger.error(f"delete file {filename} failed Exception {e}") diff --git a/fir_ser/api/views/download.py b/fir_ser/api/views/download.py index ebb2649..ac5fe0a 100644 --- a/fir_ser/api/views/download.py +++ b/fir_ser/api/views/download.py @@ -193,21 +193,13 @@ class ShortDownloadView(APIView): def calculate_cache_key(self, view_instance, view_method, request, args, kwargs): release_id = request.query_params.get("release_id", '') - udid = request.query_params.get("udid", None) - time = request.query_params.get("time", None) + udid = request.query_params.get("udid", '') short = kwargs.get("short", '') - origin_domain_name = get_origin_domain_name(request) - if not origin_domain_name: - origin_domain_name = 'default.site' - if udid and time: - udid = time - if not udid: - udid = "" - logging.info( - f"get or make cache_response short:{short} origin_domain_name:{origin_domain_name} release_id:{release_id} udid:{udid}") - return "_".join( - [settings.CACHE_KEY_TEMPLATE.get("download_short_key"), short, origin_domain_name, - release_id, udid]) + origin_name = get_origin_domain_name(request) + if not origin_name: + origin_name = 'default.site' + logging.info(f"cache_response short:{short} origin_domain_name:{origin_name} release_id:{release_id}") + return "_".join([settings.CACHE_KEY_TEMPLATE.get("download_short_key"), short, origin_name, release_id, udid]) class InstallView(APIView): diff --git a/fir_ser/api/views/receiveudids.py b/fir_ser/api/views/receiveudids.py index 7ced0c9..12144cb 100644 --- a/fir_ser/api/views/receiveudids.py +++ b/fir_ser/api/views/receiveudids.py @@ -54,8 +54,9 @@ class IosUDIDView(APIView): task_id = c_task.id logger.info(f"sign app {app_obj} task_id:{task_id}") try: - result = c_task.get(propagate=False, timeout=0.5) + result = c_task.get(propagate=False, timeout=3) except TimeoutError: + logger.error(f"get task task_id:{task_id} result timeout") result = '' if c_task.successful(): c_task.forget() diff --git a/fir_ser/config.py b/fir_ser/config.py index ed71dbe..423e439 100644 --- a/fir_ser/config.py +++ b/fir_ser/config.py @@ -58,8 +58,8 @@ class BASECONF(object): 'GetAuthC2': '300/h', 'InstallAccess1': '10/m', 'InstallAccess2': '20/h', - 'ReceiveUdid1': '10/h', - 'ReceiveUdid2': '20/h', + 'ReceiveUdid1': '20/h', + 'ReceiveUdid2': '30/h', }