Loading domain_substitution/_extraction.py +11 −51 Original line number Diff line number Diff line Loading @@ -14,7 +14,6 @@ import tarfile from pathlib import Path, PurePosixPath from _common import (USE_REGISTRY, PlatformEnum, ExtractorEnum, get_logger, get_running_platform) from prune_binaries import CONTINGENT_PATHS DEFAULT_EXTRACTORS = { ExtractorEnum.SEVENZIP: USE_REGISTRY, Loading Loading @@ -88,7 +87,7 @@ def _process_relative_to(unpack_root, relative_to): relative_root.rmdir() def _extract_tar_with_7z(binary, archive_path, output_dir, relative_to, skip_unused, sysroot): def _extract_tar_with_7z(binary, archive_path, output_dir, relative_to): get_logger().debug('Using 7-zip extractor') if not relative_to is None and (output_dir / relative_to).exists(): get_logger().error('Temporary unpacking directory already exists: %s', Loading @@ -96,11 +95,6 @@ def _extract_tar_with_7z(binary, archive_path, output_dir, relative_to, skip_unu raise Exception() cmd1 = (binary, 'x', str(archive_path), '-so') cmd2 = (binary, 'x', '-si', '-aoa', '-ttar', '-o{}'.format(str(output_dir))) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd2 += ('-x!%s/%s' % (str(relative_to), cpath[:-1]), ) get_logger().debug('7z command line: %s | %s', ' '.join(cmd1), ' '.join(cmd2)) proc1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE) Loading @@ -116,15 +110,10 @@ def _extract_tar_with_7z(binary, archive_path, output_dir, relative_to, skip_unu _process_relative_to(output_dir, relative_to) def _extract_tar_with_tar(binary, archive_path, output_dir, relative_to, skip_unused, sysroot): def _extract_tar_with_tar(binary, archive_path, output_dir, relative_to): get_logger().debug('Using BSD or GNU tar extractor') output_dir.mkdir(exist_ok=True) cmd = (binary, '-xf', str(archive_path), '-C', str(output_dir)) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd += ('--exclude=%s/%s' % (str(relative_to), cpath[:-1]), ) get_logger().debug('tar command line: %s', ' '.join(cmd)) result = subprocess.run(cmd, check=False) if result.returncode != 0: Loading @@ -136,15 +125,10 @@ def _extract_tar_with_tar(binary, archive_path, output_dir, relative_to, skip_un _process_relative_to(output_dir, relative_to) def _extract_tar_with_winrar(binary, archive_path, output_dir, relative_to, skip_unused, sysroot): def _extract_tar_with_winrar(binary, archive_path, output_dir, relative_to): get_logger().debug('Using WinRAR extractor') output_dir.mkdir(exist_ok=True) cmd = (binary, 'x', '-o+', str(archive_path), str(output_dir)) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd += ('-x%s%s%s' % (str(relative_to), os.sep, cpath[:-1].replace('/')), ) get_logger().debug('WinRAR command line: %s', ' '.join(cmd)) result = subprocess.run(cmd, check=False) if result.returncode != 0: Loading @@ -154,7 +138,7 @@ def _extract_tar_with_winrar(binary, archive_path, output_dir, relative_to, skip _process_relative_to(output_dir, relative_to) def _extract_tar_with_python(archive_path, output_dir, relative_to, skip_unused, sysroot): def _extract_tar_with_python(archive_path, output_dir, relative_to): get_logger().debug('Using pure Python tar extractor') class NoAppendList(list): Loading @@ -181,12 +165,6 @@ def _extract_tar_with_python(archive_path, output_dir, relative_to, skip_unused, tar_file_obj.members = NoAppendList() for tarinfo in tar_file_obj: try: if skip_unused and [ cpath for cpath in CONTINGENT_PATHS if tarinfo.name.startswith(str(relative_to) + '/' + cpath) and not (sysroot and f'{sysroot}-sysroot' in cpath) ]: continue if relative_to is None: destination = output_dir / PurePosixPath(tarinfo.name) else: Loading @@ -210,7 +188,7 @@ def _extract_tar_with_python(archive_path, output_dir, relative_to, skip_unused, raise def extract_tar_file(archive_path, output_dir, relative_to, skip_unused, sysroot, extractors=None): def extract_tar_file(archive_path, output_dir, relative_to, extractors=None): """ Extract regular or compressed tar archive into the output directory. Loading @@ -233,8 +211,7 @@ def extract_tar_file(archive_path, output_dir, relative_to, skip_unused, sysroot sevenzip_cmd = str(_find_7z_by_registry()) sevenzip_bin = _find_extractor_by_cmd(sevenzip_cmd) if sevenzip_bin is not None: _extract_tar_with_7z(sevenzip_bin, archive_path, output_dir, relative_to, skip_unused, sysroot) _extract_tar_with_7z(sevenzip_bin, archive_path, output_dir, relative_to) return # Use WinRAR if 7-zip is not found Loading @@ -243,8 +220,7 @@ def extract_tar_file(archive_path, output_dir, relative_to, skip_unused, sysroot winrar_cmd = str(_find_winrar_by_registry()) winrar_bin = _find_extractor_by_cmd(winrar_cmd) if winrar_bin is not None: _extract_tar_with_winrar(winrar_bin, archive_path, output_dir, relative_to, skip_unused, sysroot) _extract_tar_with_winrar(winrar_bin, archive_path, output_dir, relative_to) return get_logger().warning( 'Neither 7-zip nor WinRAR were found. Falling back to Python extractor...') Loading @@ -252,17 +228,16 @@ def extract_tar_file(archive_path, output_dir, relative_to, skip_unused, sysroot # NOTE: 7-zip isn't an option because it doesn't preserve file permissions tar_bin = _find_extractor_by_cmd(extractors.get(ExtractorEnum.TAR)) if not tar_bin is None: _extract_tar_with_tar(tar_bin, archive_path, output_dir, relative_to, skip_unused, sysroot) _extract_tar_with_tar(tar_bin, archive_path, output_dir, relative_to) return else: # This is not a normal code path, so make it clear. raise NotImplementedError(current_platform) # Fallback to Python-based extractor on all platforms _extract_tar_with_python(archive_path, output_dir, relative_to, skip_unused, sysroot) _extract_tar_with_python(archive_path, output_dir, relative_to) def extract_with_7z(archive_path, output_dir, relative_to, skip_unused, sysroot, extractors=None): def extract_with_7z(archive_path, output_dir, relative_to, extractors=None): """ Extract archives with 7-zip into the output directory. Only supports archives with one layer of unpacking, so compressed tar archives don't work. Loading Loading @@ -292,11 +267,6 @@ def extract_with_7z(archive_path, output_dir, relative_to, skip_unused, sysroot, output_dir / relative_to) raise Exception() cmd = (sevenzip_bin, 'x', str(archive_path), '-aoa', '-o{}'.format(str(output_dir))) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd += ('-x!%s/%s' % (str(relative_to), cpath[:-1]), ) get_logger().debug('7z command line: %s', ' '.join(cmd)) result = subprocess.run(cmd, check=False) Loading @@ -307,12 +277,7 @@ def extract_with_7z(archive_path, output_dir, relative_to, skip_unused, sysroot, _process_relative_to(output_dir, relative_to) def extract_with_winrar(archive_path, output_dir, relative_to, skip_unused, sysroot, extractors=None): def extract_with_winrar(archive_path, output_dir, relative_to, extractors=None): """ Extract archives with WinRAR into the output directory. Only supports archives with one layer of unpacking, so compressed tar archives don't work. Loading Loading @@ -340,11 +305,6 @@ def extract_with_winrar(archive_path, output_dir / relative_to) raise Exception() cmd = (winrar_bin, 'x', '-o+', str(archive_path), str(output_dir)) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd += ('-x%s%s%s' % (str(relative_to), os.sep, cpath[:-1].replace('/', os.sep)), ) get_logger().debug('WinRAR command line: %s', ' '.join(cmd)) result = subprocess.run(cmd, check=False) Loading domain_substitution/domain_regex.list +1 −1 Original line number Diff line number Diff line fonts(\\*?)\.googleapis(\\*?)\.com#f0ntz\g<1>.9oo91e8p1\g<2>.qjz9zk google([A-Za-z\-]*?\\*?)\.com(?!mon)#9oo91e\g<1>.qjz9zk gstatic([A-Za-z\-]*?\\*?)\.com#95tat1c\g<1>.qjz9zk chrome([A-Za-z\-]*?\\*?)\.com#ch40me\g<1>.qjz9zk chrome([A-Za-z\-]*?\\*?)\.com(?!ponent)#ch40me\g<1>.qjz9zk chromium([A-Za-z\-]*?\\*?)\.org#ch40m1um\g<1>.qjz9zk mozilla([A-Za-z\-]*?\\*?)\.org#m0z111a\g<1>.qjz9zk facebook([A-Za-z\-]*?\\*?)\.com#f8c3b00k\g<1>.qjz9zk Loading Loading
domain_substitution/_extraction.py +11 −51 Original line number Diff line number Diff line Loading @@ -14,7 +14,6 @@ import tarfile from pathlib import Path, PurePosixPath from _common import (USE_REGISTRY, PlatformEnum, ExtractorEnum, get_logger, get_running_platform) from prune_binaries import CONTINGENT_PATHS DEFAULT_EXTRACTORS = { ExtractorEnum.SEVENZIP: USE_REGISTRY, Loading Loading @@ -88,7 +87,7 @@ def _process_relative_to(unpack_root, relative_to): relative_root.rmdir() def _extract_tar_with_7z(binary, archive_path, output_dir, relative_to, skip_unused, sysroot): def _extract_tar_with_7z(binary, archive_path, output_dir, relative_to): get_logger().debug('Using 7-zip extractor') if not relative_to is None and (output_dir / relative_to).exists(): get_logger().error('Temporary unpacking directory already exists: %s', Loading @@ -96,11 +95,6 @@ def _extract_tar_with_7z(binary, archive_path, output_dir, relative_to, skip_unu raise Exception() cmd1 = (binary, 'x', str(archive_path), '-so') cmd2 = (binary, 'x', '-si', '-aoa', '-ttar', '-o{}'.format(str(output_dir))) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd2 += ('-x!%s/%s' % (str(relative_to), cpath[:-1]), ) get_logger().debug('7z command line: %s | %s', ' '.join(cmd1), ' '.join(cmd2)) proc1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE) Loading @@ -116,15 +110,10 @@ def _extract_tar_with_7z(binary, archive_path, output_dir, relative_to, skip_unu _process_relative_to(output_dir, relative_to) def _extract_tar_with_tar(binary, archive_path, output_dir, relative_to, skip_unused, sysroot): def _extract_tar_with_tar(binary, archive_path, output_dir, relative_to): get_logger().debug('Using BSD or GNU tar extractor') output_dir.mkdir(exist_ok=True) cmd = (binary, '-xf', str(archive_path), '-C', str(output_dir)) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd += ('--exclude=%s/%s' % (str(relative_to), cpath[:-1]), ) get_logger().debug('tar command line: %s', ' '.join(cmd)) result = subprocess.run(cmd, check=False) if result.returncode != 0: Loading @@ -136,15 +125,10 @@ def _extract_tar_with_tar(binary, archive_path, output_dir, relative_to, skip_un _process_relative_to(output_dir, relative_to) def _extract_tar_with_winrar(binary, archive_path, output_dir, relative_to, skip_unused, sysroot): def _extract_tar_with_winrar(binary, archive_path, output_dir, relative_to): get_logger().debug('Using WinRAR extractor') output_dir.mkdir(exist_ok=True) cmd = (binary, 'x', '-o+', str(archive_path), str(output_dir)) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd += ('-x%s%s%s' % (str(relative_to), os.sep, cpath[:-1].replace('/')), ) get_logger().debug('WinRAR command line: %s', ' '.join(cmd)) result = subprocess.run(cmd, check=False) if result.returncode != 0: Loading @@ -154,7 +138,7 @@ def _extract_tar_with_winrar(binary, archive_path, output_dir, relative_to, skip _process_relative_to(output_dir, relative_to) def _extract_tar_with_python(archive_path, output_dir, relative_to, skip_unused, sysroot): def _extract_tar_with_python(archive_path, output_dir, relative_to): get_logger().debug('Using pure Python tar extractor') class NoAppendList(list): Loading @@ -181,12 +165,6 @@ def _extract_tar_with_python(archive_path, output_dir, relative_to, skip_unused, tar_file_obj.members = NoAppendList() for tarinfo in tar_file_obj: try: if skip_unused and [ cpath for cpath in CONTINGENT_PATHS if tarinfo.name.startswith(str(relative_to) + '/' + cpath) and not (sysroot and f'{sysroot}-sysroot' in cpath) ]: continue if relative_to is None: destination = output_dir / PurePosixPath(tarinfo.name) else: Loading @@ -210,7 +188,7 @@ def _extract_tar_with_python(archive_path, output_dir, relative_to, skip_unused, raise def extract_tar_file(archive_path, output_dir, relative_to, skip_unused, sysroot, extractors=None): def extract_tar_file(archive_path, output_dir, relative_to, extractors=None): """ Extract regular or compressed tar archive into the output directory. Loading @@ -233,8 +211,7 @@ def extract_tar_file(archive_path, output_dir, relative_to, skip_unused, sysroot sevenzip_cmd = str(_find_7z_by_registry()) sevenzip_bin = _find_extractor_by_cmd(sevenzip_cmd) if sevenzip_bin is not None: _extract_tar_with_7z(sevenzip_bin, archive_path, output_dir, relative_to, skip_unused, sysroot) _extract_tar_with_7z(sevenzip_bin, archive_path, output_dir, relative_to) return # Use WinRAR if 7-zip is not found Loading @@ -243,8 +220,7 @@ def extract_tar_file(archive_path, output_dir, relative_to, skip_unused, sysroot winrar_cmd = str(_find_winrar_by_registry()) winrar_bin = _find_extractor_by_cmd(winrar_cmd) if winrar_bin is not None: _extract_tar_with_winrar(winrar_bin, archive_path, output_dir, relative_to, skip_unused, sysroot) _extract_tar_with_winrar(winrar_bin, archive_path, output_dir, relative_to) return get_logger().warning( 'Neither 7-zip nor WinRAR were found. Falling back to Python extractor...') Loading @@ -252,17 +228,16 @@ def extract_tar_file(archive_path, output_dir, relative_to, skip_unused, sysroot # NOTE: 7-zip isn't an option because it doesn't preserve file permissions tar_bin = _find_extractor_by_cmd(extractors.get(ExtractorEnum.TAR)) if not tar_bin is None: _extract_tar_with_tar(tar_bin, archive_path, output_dir, relative_to, skip_unused, sysroot) _extract_tar_with_tar(tar_bin, archive_path, output_dir, relative_to) return else: # This is not a normal code path, so make it clear. raise NotImplementedError(current_platform) # Fallback to Python-based extractor on all platforms _extract_tar_with_python(archive_path, output_dir, relative_to, skip_unused, sysroot) _extract_tar_with_python(archive_path, output_dir, relative_to) def extract_with_7z(archive_path, output_dir, relative_to, skip_unused, sysroot, extractors=None): def extract_with_7z(archive_path, output_dir, relative_to, extractors=None): """ Extract archives with 7-zip into the output directory. Only supports archives with one layer of unpacking, so compressed tar archives don't work. Loading Loading @@ -292,11 +267,6 @@ def extract_with_7z(archive_path, output_dir, relative_to, skip_unused, sysroot, output_dir / relative_to) raise Exception() cmd = (sevenzip_bin, 'x', str(archive_path), '-aoa', '-o{}'.format(str(output_dir))) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd += ('-x!%s/%s' % (str(relative_to), cpath[:-1]), ) get_logger().debug('7z command line: %s', ' '.join(cmd)) result = subprocess.run(cmd, check=False) Loading @@ -307,12 +277,7 @@ def extract_with_7z(archive_path, output_dir, relative_to, skip_unused, sysroot, _process_relative_to(output_dir, relative_to) def extract_with_winrar(archive_path, output_dir, relative_to, skip_unused, sysroot, extractors=None): def extract_with_winrar(archive_path, output_dir, relative_to, extractors=None): """ Extract archives with WinRAR into the output directory. Only supports archives with one layer of unpacking, so compressed tar archives don't work. Loading Loading @@ -340,11 +305,6 @@ def extract_with_winrar(archive_path, output_dir / relative_to) raise Exception() cmd = (winrar_bin, 'x', '-o+', str(archive_path), str(output_dir)) if skip_unused: for cpath in CONTINGENT_PATHS: if sysroot and f'{sysroot}-sysroot' in cpath: continue cmd += ('-x%s%s%s' % (str(relative_to), os.sep, cpath[:-1].replace('/', os.sep)), ) get_logger().debug('WinRAR command line: %s', ' '.join(cmd)) result = subprocess.run(cmd, check=False) Loading
domain_substitution/domain_regex.list +1 −1 Original line number Diff line number Diff line fonts(\\*?)\.googleapis(\\*?)\.com#f0ntz\g<1>.9oo91e8p1\g<2>.qjz9zk google([A-Za-z\-]*?\\*?)\.com(?!mon)#9oo91e\g<1>.qjz9zk gstatic([A-Za-z\-]*?\\*?)\.com#95tat1c\g<1>.qjz9zk chrome([A-Za-z\-]*?\\*?)\.com#ch40me\g<1>.qjz9zk chrome([A-Za-z\-]*?\\*?)\.com(?!ponent)#ch40me\g<1>.qjz9zk chromium([A-Za-z\-]*?\\*?)\.org#ch40m1um\g<1>.qjz9zk mozilla([A-Za-z\-]*?\\*?)\.org#m0z111a\g<1>.qjz9zk facebook([A-Za-z\-]*?\\*?)\.com#f8c3b00k\g<1>.qjz9zk Loading