Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 85abf25

Browse files
authored
perf(Injector): use semaphore and URLSession to download remote resource (#40)
1 parent ecf16b3 commit 85abf25

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

InjectGUI/Backend/Injector.swift

+39-5
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,24 @@ class Injector: ObservableObject {
440440
let entitlementDownloadURL = injectConfiguration.generateInjectToolDownloadURL(name: entitlements)
441441
let downloadIntoTmpPath = try? FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: URL(fileURLWithPath: "/"), create: true)
442442
let entitlementsPath = downloadIntoTmpPath?.appendingPathComponent(entitlements).path
443-
let downloadCommand = "curl -L -o \(entitlementsPath!) \(entitlementDownloadURL!)"
444-
shells.append((downloadCommand, false))
443+
// let downloadCommand = "curl -L -o \(entitlementsPath!) \(entitlementDownloadURL!)"
444+
445+
let semaphore = DispatchSemaphore(value: 0)
446+
447+
let task = URLSession.shared.downloadTask(with: entitlementDownloadURL!) { (location, response, error) in
448+
if let location = location {
449+
try? FileManager.default.moveItem(at: location, to: URL(fileURLWithPath: entitlementsPath!))
450+
print("[*] Download Entitlements Success: \(entitlementDownloadURL!)")
451+
} else {
452+
print("[*] Download Entitlements Failed: \(entitlementDownloadURL!)")
453+
shells.append(("echo Download Entitlements Failed: \(entitlementDownloadURL!) && exit 1", false))
454+
}
455+
semaphore.signal()
456+
}
457+
458+
task.resume()
459+
semaphore.wait()
460+
445461
sign_prefix_with_deep += " --entitlements \(entitlementsPath!)"
446462
}
447463

@@ -486,7 +502,24 @@ class Injector: ObservableObject {
486502
try? FileManager.default.createDirectory(at: extraShellDir, withIntermediateDirectories: true, attributes: nil)
487503
}
488504
let downloadPath = downloadIntoTmpPath.appendingPathComponent(extraShell).path
489-
let downloadCommand = "curl -L -o \(downloadPath) \(getToolDownloadURL)"
505+
// let downloadCommand = "curl -L -o \(downloadPath) \(getToolDownloadURL)"
506+
// 创建信号量,等待下载完成
507+
508+
let semaphore = DispatchSemaphore(value: 0)
509+
510+
let task = URLSession.shared.downloadTask(with: getToolDownloadURL) { (location, response, error) in
511+
if let location = location {
512+
try? FileManager.default.moveItem(at: location, to: URL(fileURLWithPath: downloadPath))
513+
print("[*] Download Extra Shell Success: \(getToolDownloadURL)")
514+
} else {
515+
print("[*] Download Extra Shell Failed: \(getToolDownloadURL)")
516+
shells.append(("echo Download Extra Shell Failed: \(getToolDownloadURL) && exit 1", false))
517+
}
518+
semaphore.signal()
519+
}
520+
521+
task.resume()
522+
semaphore.wait()
490523

491524
let dest = self.genSourcePath(for: .bash)
492525

@@ -519,8 +552,9 @@ class Injector: ObservableObject {
519552
"sed -i '' 's|\(from)|\"\(to)\"|g' \(downloadPath)"
520553
}
521554

522-
shells.append((downloadCommand, false))
523-
shells.append(("chmod +x \(downloadPath)", false))
555+
// shells.append((downloadCommand, false))
556+
shells.append(("sudo chmod -R 777 \(downloadPath)", true))
557+
shells.append(("chmod +x \(downloadPath)", true))
524558
if !replaceCommands.isEmpty {
525559
shells.append(contentsOf: replaceCommands.map { ($0, false) })
526560
}

0 commit comments

Comments
 (0)