2019-06-10 15:03:47 -07:00
|
|
|
//
|
|
|
|
|
// InstallAppOperation.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
2019-06-21 11:20:03 -07:00
|
|
|
// Created by Riley Testut on 6/19/19.
|
2019-06-10 15:03:47 -07:00
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
import Foundation
|
|
|
|
|
import Network
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
import AltStoreCore
|
2019-07-28 15:08:13 -07:00
|
|
|
import AltSign
|
2019-06-21 11:20:03 -07:00
|
|
|
import Roxas
|
2023-04-01 16:02:12 -07:00
|
|
|
import minimuxer
|
2019-06-10 15:03:47 -07:00
|
|
|
|
|
|
|
|
@objc(InstallAppOperation)
|
2023-01-04 09:52:12 -05:00
|
|
|
final class InstallAppOperation: ResultOperation<InstalledApp>
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2020-03-06 17:08:35 -08:00
|
|
|
let context: InstallAppOperationContext
|
2019-06-10 15:03:47 -07:00
|
|
|
|
2019-12-11 12:26:48 -08:00
|
|
|
private var didCleanUp = false
|
|
|
|
|
|
2020-03-06 17:08:35 -08:00
|
|
|
init(context: InstallAppOperationContext)
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
self.context = context
|
|
|
|
|
|
2019-06-10 15:03:47 -07:00
|
|
|
super.init()
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
self.progress.totalUnitCount = 100
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func main()
|
|
|
|
|
{
|
|
|
|
|
super.main()
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
if let error = self.context.error
|
|
|
|
|
{
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-06-10 15:03:47 -07:00
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
guard
|
2020-03-06 17:08:35 -08:00
|
|
|
let certificate = self.context.certificate,
|
2022-11-02 17:58:59 -07:00
|
|
|
let resignedApp = self.context.resignedApp
|
2019-06-21 11:20:03 -07:00
|
|
|
else { return self.finish(.failure(OperationError.invalidParameters)) }
|
|
|
|
|
|
2019-07-28 15:08:13 -07:00
|
|
|
let backgroundContext = DatabaseManager.shared.persistentContainer.newBackgroundContext()
|
|
|
|
|
backgroundContext.perform {
|
2020-01-24 15:03:16 -08:00
|
|
|
|
|
|
|
|
/* App */
|
2019-07-28 15:08:13 -07:00
|
|
|
let installedApp: InstalledApp
|
|
|
|
|
|
|
|
|
|
// Fetch + update rather than insert + resolve merge conflicts to prevent potential context-level conflicts.
|
|
|
|
|
if let app = InstalledApp.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(InstalledApp.bundleIdentifier), self.context.bundleIdentifier), in: backgroundContext)
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-07-28 15:08:13 -07:00
|
|
|
installedApp = app
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-03-06 17:08:35 -08:00
|
|
|
installedApp = InstalledApp(resignedApp: resignedApp, originalBundleIdentifier: self.context.bundleIdentifier, certificateSerialNumber: certificate.serialNumber, context: backgroundContext)
|
2019-07-28 15:08:13 -07:00
|
|
|
}
|
|
|
|
|
|
2020-03-06 17:08:35 -08:00
|
|
|
installedApp.update(resignedApp: resignedApp, certificateSerialNumber: certificate.serialNumber)
|
2020-10-01 11:51:39 -07:00
|
|
|
installedApp.needsResign = false
|
|
|
|
|
|
2020-01-24 15:03:16 -08:00
|
|
|
if let team = DatabaseManager.shared.activeTeam(in: backgroundContext)
|
|
|
|
|
{
|
|
|
|
|
installedApp.team = team
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* App Extensions */
|
2020-01-21 16:53:34 -08:00
|
|
|
var installedExtensions = Set<InstalledExtension>()
|
|
|
|
|
|
|
|
|
|
if
|
|
|
|
|
let bundle = Bundle(url: resignedApp.fileURL),
|
|
|
|
|
let directory = bundle.builtInPlugInsURL,
|
|
|
|
|
let enumerator = FileManager.default.enumerator(at: directory, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants])
|
|
|
|
|
{
|
|
|
|
|
for case let fileURL as URL in enumerator
|
|
|
|
|
{
|
|
|
|
|
guard let appExtensionBundle = Bundle(url: fileURL) else { continue }
|
|
|
|
|
guard let appExtension = ALTApplication(fileURL: appExtensionBundle.bundleURL) else { continue }
|
|
|
|
|
|
2020-01-24 15:03:16 -08:00
|
|
|
let parentBundleID = self.context.bundleIdentifier
|
|
|
|
|
let resignedParentBundleID = resignedApp.bundleIdentifier
|
|
|
|
|
|
|
|
|
|
let resignedBundleID = appExtension.bundleIdentifier
|
|
|
|
|
let originalBundleID = resignedBundleID.replacingOccurrences(of: resignedParentBundleID, with: parentBundleID)
|
|
|
|
|
|
2022-12-03 17:25:15 -05:00
|
|
|
print("`parentBundleID`: \(parentBundleID)")
|
|
|
|
|
print("`resignedParentBundleID`: \(resignedParentBundleID)")
|
|
|
|
|
print("`resignedBundleID`: \(resignedBundleID)")
|
|
|
|
|
print("`originalBundleID`: \(originalBundleID)")
|
|
|
|
|
|
2020-01-21 16:53:34 -08:00
|
|
|
let installedExtension: InstalledExtension
|
|
|
|
|
|
2020-01-24 15:03:16 -08:00
|
|
|
if let appExtension = installedApp.appExtensions.first(where: { $0.bundleIdentifier == originalBundleID })
|
2020-01-21 16:53:34 -08:00
|
|
|
{
|
|
|
|
|
installedExtension = appExtension
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-24 15:03:16 -08:00
|
|
|
installedExtension = InstalledExtension(resignedAppExtension: appExtension, originalBundleIdentifier: originalBundleID, context: backgroundContext)
|
2020-01-21 16:53:34 -08:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 15:03:16 -08:00
|
|
|
installedExtension.update(resignedAppExtension: appExtension)
|
|
|
|
|
|
2020-01-21 16:53:34 -08:00
|
|
|
installedExtensions.insert(installedExtension)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
installedApp.appExtensions = installedExtensions
|
|
|
|
|
|
2021-10-25 22:27:30 -07:00
|
|
|
self.context.beginInstallationHandler?(installedApp)
|
|
|
|
|
|
2019-12-11 12:26:48 -08:00
|
|
|
// Temporary directory and resigned .ipa no longer needed, so delete them now to ensure AltStore doesn't quit before we get the chance to.
|
|
|
|
|
self.cleanUp()
|
|
|
|
|
|
2020-03-11 14:43:19 -07:00
|
|
|
var activeProfiles: Set<String>?
|
|
|
|
|
if let sideloadedAppsLimit = UserDefaults.standard.activeAppsLimit
|
|
|
|
|
{
|
|
|
|
|
// When installing these new profiles, AltServer will remove all non-active profiles to ensure we remain under limit.
|
|
|
|
|
|
|
|
|
|
let fetchRequest = InstalledApp.activeAppsFetchRequest()
|
|
|
|
|
fetchRequest.includesPendingChanges = false
|
|
|
|
|
|
|
|
|
|
var activeApps = InstalledApp.fetch(fetchRequest, in: backgroundContext)
|
|
|
|
|
if !activeApps.contains(installedApp)
|
|
|
|
|
{
|
2020-05-17 23:36:30 -07:00
|
|
|
let activeAppsCount = activeApps.map { $0.requiredActiveSlots }.reduce(0, +)
|
2020-03-11 14:43:19 -07:00
|
|
|
|
2020-05-17 23:36:30 -07:00
|
|
|
let availableActiveApps = max(sideloadedAppsLimit - activeAppsCount, 0)
|
|
|
|
|
if installedApp.requiredActiveSlots <= availableActiveApps
|
2020-03-11 14:43:19 -07:00
|
|
|
{
|
|
|
|
|
// This app has not been explicitly activated, but there are enough slots available,
|
|
|
|
|
// so implicitly activate it.
|
|
|
|
|
installedApp.isActive = true
|
|
|
|
|
activeApps.append(installedApp)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
installedApp.isActive = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
activeProfiles = Set(activeApps.flatMap { (installedApp) -> [String] in
|
|
|
|
|
let appExtensionProfiles = installedApp.appExtensions.map { $0.resignedBundleIdentifier }
|
|
|
|
|
return [installedApp.resignedBundleIdentifier] + appExtensionProfiles
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 07:20:36 -07:00
|
|
|
var installing = true
|
2023-07-24 16:58:09 -04:00
|
|
|
if installedApp.storeApp?.bundleIdentifier.range(of: Bundle.Info.appbundleIdentifier) != nil {
|
2023-05-06 19:25:37 -07:00
|
|
|
// Reinstalling ourself will hang until we leave the app, so we need to exit it without force closing
|
2023-04-13 07:20:36 -07:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
|
2023-05-06 19:25:37 -07:00
|
|
|
if UIApplication.shared.applicationState != .active {
|
|
|
|
|
print("We are not in the foreground, let's not do anything")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !installing {
|
2023-04-13 07:20:36 -07:00
|
|
|
print("Installing finished")
|
2023-05-06 19:25:37 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
print("We are still installing after 3 seconds")
|
2023-07-11 01:45:14 -04:00
|
|
|
UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
|
2023-04-13 07:20:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-11 01:44:11 -04:00
|
|
|
var attempts = 10
|
|
|
|
|
while (attempts != 0){
|
|
|
|
|
print("Install ipa attempts left: \(attempts)")
|
|
|
|
|
do {
|
|
|
|
|
try install_ipa(installedApp.bundleIdentifier)
|
|
|
|
|
installing = false
|
|
|
|
|
installedApp.refreshedDate = Date()
|
2023-07-24 16:58:09 -04:00
|
|
|
self.finish(.success(installedApp))
|
|
|
|
|
break
|
2023-07-11 01:44:11 -04:00
|
|
|
} catch {
|
|
|
|
|
if (attempts == 0){
|
|
|
|
|
installing = false
|
2023-07-24 16:58:09 -04:00
|
|
|
self.finish(.failure(MinimuxerError.InstallApp))
|
|
|
|
|
}
|
2023-07-11 01:44:11 -04:00
|
|
|
}
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 12:26:48 -08:00
|
|
|
override func finish(_ result: Result<InstalledApp, Error>)
|
|
|
|
|
{
|
|
|
|
|
self.cleanUp()
|
|
|
|
|
|
2020-06-04 19:53:10 -07:00
|
|
|
// Only remove refreshed IPA when finished.
|
|
|
|
|
if let app = self.context.app
|
|
|
|
|
{
|
|
|
|
|
let fileURL = InstalledApp.refreshedIPAURL(for: app)
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
try FileManager.default.removeItem(at: fileURL)
|
2023-04-01 16:02:12 -07:00
|
|
|
print("Removed refreshed IPA")
|
2020-06-04 19:53:10 -07:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2023-04-01 16:02:12 -07:00
|
|
|
print("Failed to remove refreshed .ipa: \(error)")
|
2020-06-04 19:53:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 12:26:48 -08:00
|
|
|
super.finish(result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension InstallAppOperation
|
|
|
|
|
{
|
|
|
|
|
func cleanUp()
|
|
|
|
|
{
|
|
|
|
|
guard !self.didCleanUp else { return }
|
|
|
|
|
self.didCleanUp = true
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
try FileManager.default.removeItem(at: self.context.temporaryDirectory)
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
print("Failed to remove temporary directory.", error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|