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
|
|
|
|
|
|
|
|
|
|
import AltKit
|
2019-07-28 15:08:13 -07:00
|
|
|
import AltSign
|
2019-06-21 11:20:03 -07:00
|
|
|
import Roxas
|
2019-06-10 15:03:47 -07:00
|
|
|
|
|
|
|
|
@objc(InstallAppOperation)
|
2019-07-28 15:08:13 -07:00
|
|
|
class InstallAppOperation: ResultOperation<InstalledApp>
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
let context: AppOperationContext
|
2019-06-10 15:03:47 -07:00
|
|
|
|
2019-12-11 12:26:48 -08:00
|
|
|
private var didCleanUp = false
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
init(context: AppOperationContext)
|
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
|
2019-07-28 15:08:13 -07:00
|
|
|
let resignedApp = self.context.resignedApp,
|
2020-01-08 12:41:02 -08:00
|
|
|
let connection = self.context.installationConnection
|
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 {
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
installedApp = InstalledApp(resignedApp: resignedApp, originalBundleIdentifier: self.context.bundleIdentifier, context: backgroundContext)
|
2020-01-21 16:49:38 -08:00
|
|
|
installedApp.installedDate = Date()
|
2019-07-28 15:08:13 -07:00
|
|
|
}
|
|
|
|
|
|
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 }
|
|
|
|
|
|
|
|
|
|
let installedExtension: InstalledExtension
|
|
|
|
|
|
|
|
|
|
if let appExtension = installedApp.appExtensions.first(where: { $0.bundleIdentifier == appExtension.bundleIdentifier })
|
|
|
|
|
{
|
|
|
|
|
installedExtension = appExtension
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
installedExtension = InstalledExtension(resignedAppExtension: appExtension, originalBundleIdentifier: appExtension.bundleIdentifier, context: backgroundContext)
|
|
|
|
|
installedExtension.installedDate = Date()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
installedExtensions.insert(installedExtension)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
installedApp.appExtensions = installedExtensions
|
|
|
|
|
|
2019-07-31 15:19:27 -07:00
|
|
|
installedApp.version = resignedApp.version
|
|
|
|
|
|
2019-07-28 15:08:13 -07:00
|
|
|
if let profile = resignedApp.provisioningProfile
|
|
|
|
|
{
|
|
|
|
|
installedApp.refreshedDate = profile.creationDate
|
|
|
|
|
installedApp.expirationDate = profile.expirationDate
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-14 18:39:08 -08:00
|
|
|
if let team = Team.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(Team.isActiveTeam), NSNumber(value: true)), in: backgroundContext)
|
|
|
|
|
{
|
|
|
|
|
installedApp.team = team
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
|
2019-07-28 15:08:13 -07:00
|
|
|
self.context.group.beginInstallationHandler?(installedApp)
|
|
|
|
|
|
|
|
|
|
let request = BeginInstallationRequest()
|
2020-01-08 12:41:02 -08:00
|
|
|
connection.send(request) { (result) in
|
2019-07-28 15:08:13 -07:00
|
|
|
switch result
|
|
|
|
|
{
|
|
|
|
|
case .failure(let error): self.finish(.failure(error))
|
|
|
|
|
case .success:
|
|
|
|
|
|
2020-01-08 12:41:02 -08:00
|
|
|
self.receive(from: connection) { (result) in
|
2019-07-28 15:08:13 -07:00
|
|
|
switch result
|
|
|
|
|
{
|
|
|
|
|
case .success:
|
|
|
|
|
backgroundContext.perform {
|
|
|
|
|
installedApp.refreshedDate = Date()
|
|
|
|
|
self.finish(.success(installedApp))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
self.finish(.failure(error))
|
2019-07-19 16:42:40 -07: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()
|
|
|
|
|
|
|
|
|
|
super.finish(result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension InstallAppOperation
|
|
|
|
|
{
|
2020-01-08 12:41:02 -08:00
|
|
|
func receive(from connection: ServerConnection, completionHandler: @escaping (Result<Void, Error>) -> Void)
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2020-01-08 12:41:02 -08:00
|
|
|
connection.receiveResponse() { (result) in
|
2019-06-21 11:20:03 -07:00
|
|
|
do
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
let response = try result.get()
|
|
|
|
|
print(response)
|
2019-06-10 15:03:47 -07:00
|
|
|
|
2019-11-18 14:49:17 -08:00
|
|
|
switch response
|
2019-06-10 15:03:47 -07:00
|
|
|
{
|
2019-11-18 14:49:17 -08:00
|
|
|
case .installationProgress(let response):
|
|
|
|
|
if response.progress == 1.0
|
|
|
|
|
{
|
|
|
|
|
self.progress.completedUnitCount = self.progress.totalUnitCount
|
|
|
|
|
completionHandler(.success(()))
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self.progress.completedUnitCount = Int64(response.progress * 100)
|
2020-01-08 12:41:02 -08:00
|
|
|
self.receive(from: connection, completionHandler: completionHandler)
|
2019-11-18 14:49:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .error(let response):
|
|
|
|
|
completionHandler(.failure(response.error))
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
completionHandler(.failure(ALTServerError(.unknownRequest)))
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
completionHandler(.failure(ALTServerError(error)))
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-11 12:26:48 -08:00
|
|
|
|
|
|
|
|
func cleanUp()
|
|
|
|
|
{
|
|
|
|
|
guard !self.didCleanUp else { return }
|
|
|
|
|
self.didCleanUp = true
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
try FileManager.default.removeItem(at: self.context.temporaryDirectory)
|
|
|
|
|
|
|
|
|
|
if let app = self.context.app
|
|
|
|
|
{
|
|
|
|
|
let fileURL = InstalledApp.refreshedIPAURL(for: app)
|
|
|
|
|
try FileManager.default.removeItem(at: fileURL)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
print("Failed to remove temporary directory.", error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-10 15:03:47 -07:00
|
|
|
}
|