Skip to content

Commit

Permalink
fix: install after sign being fed an unsigned ipa
Browse files Browse the repository at this point in the history
  • Loading branch information
khcrysalis committed Oct 4, 2024
1 parent a3a866f commit f7f120e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 46 deletions.
72 changes: 37 additions & 35 deletions Shared/Signing/AppSigner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ struct AppSigningOptions {
var certificate: Certificate?
}

func signInitialApp(options: AppSigningOptions, appPath: URL, completion: @escaping (Bool) -> Void) {
func signInitialApp(options: AppSigningOptions, appPath: URL, completion: @escaping (Result<URL, Error>) -> Void) {
UIApplication.shared.isIdleTimerDisabled = true
DispatchQueue(label: "Signing").async {
let fileManager = FileManager.default
let tmpDir = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
DispatchQueue(label: "Signing").async {
let fileManager = FileManager.default
let tmpDir = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
let tmpDirApp = tmpDir.appendingPathComponent(appPath.lastPathComponent)
var iconURL = ""
do {

do {
Debug.shared.log(message: "============================================")
Debug.shared.log(message: "\(options)")
Debug.shared.log(message: "============================================")
try fileManager.createDirectory(at: tmpDir, withIntermediateDirectories: true)
try fileManager.copyItem(at: appPath, to: tmpDirApp)
try fileManager.copyItem(at: appPath, to: tmpDirApp)

if let info = NSDictionary(contentsOf: tmpDirApp.appendingPathComponent("Info.plist"))!.mutableCopy() as? NSMutableDictionary {
try updateInfoPlist(infoDict: info, options: options, icon: options.iconURL, app: tmpDirApp)
Expand All @@ -62,36 +62,36 @@ func signInitialApp(options: AppSigningOptions, appPath: URL, completion: @escap
iconURL = iconFileName
}
}

let handler = TweakHandler(urls: options.toInject ?? [], app: tmpDirApp)
try handler.getInputFiles()

if let removeInjectPaths = options.removeInjectPaths, !removeInjectPaths.isEmpty {
if let appexe = try TweakHandler.findExecutable(at: tmpDirApp) {
_ = uninstallDylibs(filePath: appexe.path, dylibPaths: removeInjectPaths)
}
}

try updatePlugIns(options: options, app: tmpDirApp)
try removeDumbAssPlaceHolderExtension(options: options, app: tmpDirApp)
try updateMobileProvision(app: tmpDirApp)
let certPath = try CoreDataManager.shared.getCertifcatePath(source: options.certificate)

let certPath = try CoreDataManager.shared.getCertifcatePath(source: options.certificate)
let provisionPath = certPath.appendingPathComponent("\(options.certificate?.provisionPath ?? "")").path
let p12Path = certPath.appendingPathComponent("\(options.certificate?.p12Path ?? "")").path

Debug.shared.log(message: "🦋 Start Signing 🦋")

try signAppWithZSign(tmpDirApp: tmpDirApp, certPaths: (provisionPath, p12Path), password: options.certificate?.password ?? "", options: options)

Debug.shared.log(message: "🦋 End Signing 🦋")
let signedUUID = UUID().uuidString
try fileManager.createDirectory(at: getDocumentsDirectory().appendingPathComponent("Apps/Signed"), withIntermediateDirectories: true)
let path = getDocumentsDirectory().appendingPathComponent("Apps/Signed").appendingPathComponent(signedUUID)
try fileManager.moveItem(at: tmpDir, to: path)
DispatchQueue.main.async {

let signedUUID = UUID().uuidString
try fileManager.createDirectory(at: getDocumentsDirectory().appendingPathComponent("Apps/Signed"), withIntermediateDirectories: true)
let signedPath = getDocumentsDirectory().appendingPathComponent("Apps/Signed").appendingPathComponent(signedUUID)
try fileManager.moveItem(at: tmpDir, to: signedPath)

DispatchQueue.main.async {
CoreDataManager.shared.addToSignedApps(
version: options.version!,
name: options.name!,
Expand All @@ -101,28 +101,30 @@ func signInitialApp(options: AppSigningOptions, appPath: URL, completion: @escap
appPath: appPath.lastPathComponent,
timeToLive: options.certificate?.certData?.expirationDate ?? Date(),
teamName: options.certificate?.certData?.name ?? ""
) {
error in
Debug.shared.log(message: "signApp: \(String(describing: error))", type: .error)
completion(false)
) { error in
if let error = error {
Debug.shared.log(message: "signApp: \(error)", type: .error)
completion(.failure(error))
}
}

Debug.shared.log(message: String.localized("SUCCESS_SIGNED", arguments: "\(options.name ?? String.localized("UNKNOWN"))"), type: .success)
Debug.shared.log(message: "============================================")
UIApplication.shared.isIdleTimerDisabled = false
completion(true)
}
} catch {
DispatchQueue.main.async {
completion(.success(signedPath.appendingPathComponent(appPath.lastPathComponent)))
}
} catch {
DispatchQueue.main.async {
UIApplication.shared.isIdleTimerDisabled = false
Debug.shared.log(message: "signApp: \(error)", type: .critical)
completion(false)
}
}
}
completion(.failure(error))
}
}
}
}


func resignApp(certificate: Certificate, appPath: URL, completion: @escaping (Bool) -> Void) {
UIApplication.shared.isIdleTimerDisabled = true
DispatchQueue(label: "Resigning").async {
Expand Down
6 changes: 3 additions & 3 deletions iOS/Views/Apps/LibraryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ extension LibraryViewController {
button1.onTap = { [weak self] in
guard let self = self else { return }
self.popupVC.dismiss(animated: true)
self.startSigning(meow: source!, filePath: filePath?.path ?? "")
self.startSigning(meow: source!)
}

let button2 = PopupViewControllerButton(title: "Install \((source!.value(forKey: "name") as? String ?? ""))", color: .quaternarySystemFill, titleColor: .tintColor)
Expand Down Expand Up @@ -251,9 +251,9 @@ extension LibraryViewController {
tableView.deselectRow(at: indexPath, animated: true)
}

@objc func startSigning(meow: NSManagedObject, filePath: String) {
@objc func startSigning(meow: NSManagedObject) {
if FileManager.default.fileExists(atPath: CoreDataManager.shared.getFilesForDownloadedApps(for:(meow as! DownloadedApps)).path) {
let ap = AppSigningViewController(app: meow, filePath: filePath, appsViewController: self)
let ap = AppSigningViewController(app: meow, appsViewController: self)
let navigationController = UINavigationController(rootViewController: ap)
navigationController.modalPresentationStyle = .fullScreen
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
Expand Down
20 changes: 12 additions & 8 deletions iOS/Views/Apps/Signing/AppSigningViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class AppSigningViewController: UITableViewController, UINavigationControllerDel
var removeInjectPaths: [String] = []

var app: NSManagedObject!
var filePath: String = ""
var name: String = "Unknown"
var bundleId: String = "unknown"
var version: String = "unknown"
Expand Down Expand Up @@ -56,10 +55,9 @@ class AppSigningViewController: UITableViewController, UINavigationControllerDel

var certs: Certificate?

init(app: NSManagedObject, filePath: String, appsViewController: LibraryViewController) {
init(app: NSManagedObject, appsViewController: LibraryViewController) {
self.appsViewController = appsViewController
self.app = app
self.filePath = filePath
super.init(style: .insetGrouped)

if let hasGotCert = CoreDataManager.shared.getCurrentCertificate() {
Expand Down Expand Up @@ -174,14 +172,20 @@ class AppSigningViewController: UITableViewController, UINavigationControllerDel
certificate: certs
),
appPath:getFilesForDownloadedApps(app: app as! DownloadedApps, getuuidonly: false)
) { success in
if success {
) { result in
switch result {
case .success(let signedPath):
self.appsViewController.fetchSources()
self.appsViewController.tableView.reloadData()
if Preferences.autoInstallAfterSign {
self.appsViewController.startInstallProcess(meow: self.app, filePath: self.filePath)
}
Debug.shared.log(message: signedPath.path)
if Preferences.autoInstallAfterSign {
self.appsViewController.startInstallProcess(meow: self.app, filePath: signedPath.path)
}

case .failure(let error):
Debug.shared.log(message: "Signing failed: \(error.localizedDescription)", type: .error)
}

self.dismiss(animated: true)
}
}
Expand Down

0 comments on commit f7f120e

Please sign in to comment.