From 00ac83c929bdf357f9315fd5d19b9174664942b5 Mon Sep 17 00:00:00 2001 From: Khiem Ton Date: Sat, 25 Mar 2023 18:46:54 +0700 Subject: [PATCH] feat: download dev image, fixing PermissionError while opening another opening temp file in Windows --- ispoof/spoofer/device.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/ispoof/spoofer/device.py b/ispoof/spoofer/device.py index e5865b6..499e4b8 100644 --- a/ispoof/spoofer/device.py +++ b/ispoof/spoofer/device.py @@ -1,10 +1,14 @@ from pathlib import Path from pymobiledevice3.lockdown import LockdownClient -from pymobiledevice3.cli.mounter import MobileImageMounterService, download_developer_disk_image +from pymobiledevice3.cli.mounter import MobileImageMounterService from pymobiledevice3.cli.developer import DtSimulateLocation from pymobiledevice3.services.diagnostics import DiagnosticsService from pymobiledevice3.exceptions import PasscodeRequiredError import logging +import requests +import tempfile +import zipfile +from tqdm import tqdm from ispoof.utils import get_home_folder DEVELOPER_DISK_IMAGE_URL = 'https://github.com/pdso/DeveloperDiskImage/raw/master/{ios_version}/{ios_version}.zip' @@ -33,11 +37,11 @@ class Device: image_dir = f'{get_home_folder()}/DevDiskImage/' image_path = f'{image_dir}/DeveloperDiskImage.dmg' signature = f'{image_path}.signature' - developer_disk_image_dir = Path(image_dir) + developer_disk_image_dir = Path(image_path).parent if not developer_disk_image_dir.exists(): try: - download_developer_disk_image(version, developer_disk_image_dir) + self.download_developer_disk_image(version, developer_disk_image_dir) except PermissionError: logger.error( f'DeveloperDiskImage could not be saved to path ({developer_disk_image_dir}). ' @@ -65,8 +69,18 @@ class Device: self.location.clear() self.diagnostics.restart() - def get_ios_version(self): - pass + def download_developer_disk_image(self, ios_version, directory): + url = DEVELOPER_DISK_IMAGE_URL.format(ios_version=ios_version) + with requests.get(url, stream=True) as r: + r.raise_for_status() + total_size_in_bytes = int(r.headers.get('content-length', 0)) + with tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True, dynamic_ncols=True) as progress_bar: + with tempfile.NamedTemporaryFile('wb+') as f: + for chunk in r.iter_content(chunk_size=8192): + progress_bar.update(len(chunk)) + f.write(chunk) + zip_file = zipfile.ZipFile(f) + zip_file.extractall(directory)