Supports installing/refreshings apps w/o computer on jailbroken devices

AltStore will use AltDaemon as a local AltServer if it’s installed and running. AltStore remains a regular sandboxed app, but AltDaemon has private entitlements necessary to perform AltServer operations without a computer.
This commit is contained in:
Riley Testut
2020-06-04 19:53:10 -07:00
parent cb4656722a
commit 496aca642c
6 changed files with 131 additions and 78 deletions

View File

@@ -10,14 +10,12 @@ import Foundation
import AltKit
import Roxas
private extension Notification.Name
{
static let didReceiveWiredServerConnectionResponse = Notification.Name("io.altstore.didReceiveWiredServerConnectionResponse")
}
private let ReceivedWiredServerConnectionResponse: @convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFNotificationName?, UnsafeRawPointer?, CFDictionary?) -> Void =
private let ReceivedServerConnectionResponse: @convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFNotificationName?, UnsafeRawPointer?, CFDictionary?) -> Void =
{ (center, observer, name, object, userInfo) in
NotificationCenter.default.post(name: .didReceiveWiredServerConnectionResponse, object: nil)
guard let name = name, let observer = observer else { return }
let operation = unsafeBitCast(observer, to: FindServerOperation.self)
operation.handle(name)
}
@objc(FindServerOperation)
@@ -26,12 +24,13 @@ class FindServerOperation: ResultOperation<Server>
let context: OperationContext
private var isWiredServerConnectionAvailable = false
private var isLocalServerConnectionAvailable = false
init(context: OperationContext = OperationContext())
{
self.context = context
}
override func main()
{
super.main()
@@ -49,48 +48,68 @@ class FindServerOperation: ResultOperation<Server>
}
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
let observer = Unmanaged.passUnretained(self).toOpaque()
// Prepare observers to receive callback from wired server (if connected).
CFNotificationCenterAddObserver(notificationCenter, nil, ReceivedWiredServerConnectionResponse, CFNotificationName.wiredServerConnectionAvailableResponse.rawValue, nil, .deliverImmediately)
NotificationCenter.default.addObserver(self, selector: #selector(FindServerOperation.didReceiveWiredServerConnectionResponse(_:)), name: .didReceiveWiredServerConnectionResponse, object: nil)
// Prepare observers to receive callback from wired connection or background daemon (if available).
CFNotificationCenterAddObserver(notificationCenter, observer, ReceivedServerConnectionResponse, CFNotificationName.wiredServerConnectionAvailableResponse.rawValue, nil, .deliverImmediately)
CFNotificationCenterAddObserver(notificationCenter, observer, ReceivedServerConnectionResponse, CFNotificationName.localServerConnectionAvailableResponse.rawValue, nil, .deliverImmediately)
// Post notification.
// Post notifications.
CFNotificationCenterPostNotification(notificationCenter, .wiredServerConnectionAvailableRequest, nil, nil, true)
CFNotificationCenterPostNotification(notificationCenter, .localServerConnectionAvailableRequest, nil, nil, true)
// Wait for either callback or timeout.
DispatchQueue.global().asyncAfter(deadline: .now() + 1.0) {
if self.isWiredServerConnectionAvailable
if self.isLocalServerConnectionAvailable
{
let server = Server(isWiredConnection: true)
// Prefer background daemon, if it exists and is running.
let server = Server(connectionType: .local)
self.finish(.success(server))
}
else if self.isWiredServerConnectionAvailable
{
let server = Server(connectionType: .wired)
self.finish(.success(server))
}
else if let server = ServerManager.shared.discoveredServers.first(where: { $0.isPreferred })
{
// Preferred server.
self.finish(.success(server))
}
else if let server = ServerManager.shared.discoveredServers.first
{
// Any available server.
self.finish(.success(server))
}
else
{
if let server = ServerManager.shared.discoveredServers.first(where: { $0.isPreferred })
{
// Preferred server.
self.finish(.success(server))
}
else if let server = ServerManager.shared.discoveredServers.first
{
// Any available server.
self.finish(.success(server))
}
else
{
// No servers.
self.finish(.failure(ConnectionError.serverNotFound))
}
// No servers.
self.finish(.failure(ConnectionError.serverNotFound))
}
}
}
}
private extension FindServerOperation
{
@objc func didReceiveWiredServerConnectionResponse(_ notification: Notification)
override func finish(_ result: Result<Server, Error>)
{
self.isWiredServerConnectionAvailable = true
super.finish(result)
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
let observer = Unmanaged.passUnretained(self).toOpaque()
CFNotificationCenterRemoveObserver(notificationCenter, observer, .wiredServerConnectionAvailableResponse, nil)
CFNotificationCenterRemoveObserver(notificationCenter, observer, .localServerConnectionAvailableResponse, nil)
}
}
fileprivate extension FindServerOperation
{
func handle(_ notification: CFNotificationName)
{
switch notification
{
case .wiredServerConnectionAvailableResponse: self.isWiredServerConnectionAvailable = true
case .localServerConnectionAvailableResponse: self.isLocalServerConnectionAvailable = true
default: break
}
}
}

View File

@@ -144,7 +144,7 @@ class InstallAppOperation: ResultOperation<InstalledApp>
})
}
let request = BeginInstallationRequest(activeProfiles: activeProfiles)
let request = BeginInstallationRequest(activeProfiles: activeProfiles, bundleIdentifier: installedApp.resignedBundleIdentifier)
connection.send(request) { (result) in
switch result
{
@@ -173,6 +173,21 @@ class InstallAppOperation: ResultOperation<InstalledApp>
{
self.cleanUp()
// 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)
}
catch
{
print("Failed to remove refreshed .ipa:", error)
}
}
super.finish(result)
}
}
@@ -223,12 +238,6 @@ private extension InstallAppOperation
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
{

View File

@@ -76,26 +76,41 @@ private extension SendAppOperation
guard let appData = try? Data(contentsOf: fileURL) else { throw OperationError.invalidApp }
guard let udid = Bundle.main.object(forInfoDictionaryKey: Bundle.Info.deviceID) as? String else { throw OperationError.unknownUDID }
let request = PrepareAppRequest(udid: udid, contentSize: appData.count)
var request = PrepareAppRequest(udid: udid, contentSize: appData.count)
if connection.server.connectionType == .local
{
// Background daemons have low memory limit (~6MB as of 13.5),
// so send just the file URL rather than the app data itself.
request.fileURL = fileURL
}
print("Sending request \(request)")
connection.send(request) { (result) in
switch result
{
case .failure(let error): completionHandler(.failure(error))
case .success:
print("Sending app data (\(appData.count) bytes)")
connection.send(appData, prependSize: false) { (result) in
switch result
{
case .failure(let error):
print("Failed to send app data (\(appData.count) bytes)")
completionHandler(.failure(error))
case .success:
print("Successfully sent app data (\(appData.count) bytes)")
completionHandler(.success(()))
if connection.server.connectionType == .local
{
// Sent file URL, so don't need to send any more.
completionHandler(.success(()))
}
else
{
print("Sending app data (\(appData.count) bytes)...")
connection.send(appData, prependSize: false) { (result) in
switch result
{
case .failure(let error):
print("Failed to send app data (\(appData.count) bytes)")
completionHandler(.failure(error))
case .success:
print("Successfully sent app data (\(appData.count) bytes)")
completionHandler(.success(()))
}
}
}
}