Skip to content

Commit 7d6ee15

Browse files
committed
refactor: enhance framework Mach-O retrieval methods
1 parent cb3f0a8 commit 7d6ee15

3 files changed

Lines changed: 35 additions & 9 deletions

File tree

TrollFools/InjectorV3+Bundle.swift

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,44 @@ extension InjectorV3 {
3636

3737
// MARK: - Shared Methods
3838

39-
func frameworkMachOsInBundle(_ target: URL) throws -> OrderedSet<URL> {
39+
func allFrameworkMachOsInBundle(_ target: URL) -> OrderedSet<URL> {
4040
precondition(checkIsBundle(target), "Not a bundle: \(target.path)")
4141

42-
let executableURL = try locateExecutableInBundle(target)
43-
precondition(isMachO(executableURL), "Not a Mach-O: \(executableURL.path)")
44-
4542
let frameworksURL = target.appendingPathComponent("Frameworks")
46-
let linkedDylibs = try linkedDylibsRecursivelyOfMachO(executableURL)
4743

4844
var enumeratedURLs = OrderedSet<URL>()
4945
if let enumerator = FileManager.default.enumerator(
5046
at: frameworksURL,
51-
includingPropertiesForKeys: [.fileSizeKey],
47+
includingPropertiesForKeys: [.isRegularFileKey],
5248
options: [.skipsHiddenFiles]
5349
) {
5450
for case let itemURL as URL in enumerator {
5551
if checkIsInjectedBundle(itemURL) || enumerator.level > 2 {
5652
enumerator.skipDescendants()
5753
continue
5854
}
59-
if enumerator.level == 2 {
55+
if enumerator.level == 2,
56+
isMachO(itemURL),
57+
!itemURL.lastPathComponent.hasSuffix(".\(Self.injectedMarkerName).bak"),
58+
!itemURL.lastPathComponent.hasSuffix(".troll-fools.bak")
59+
{
6060
enumeratedURLs.append(itemURL)
6161
}
6262
}
6363
}
6464

65+
return enumeratedURLs
66+
}
67+
68+
func frameworkMachOsInBundle(_ target: URL) throws -> OrderedSet<URL> {
69+
precondition(checkIsBundle(target), "Not a bundle: \(target.path)")
70+
71+
let executableURL = try locateExecutableInBundle(target)
72+
precondition(isMachO(executableURL), "Not a Mach-O: \(executableURL.path)")
73+
74+
let linkedDylibs = try linkedDylibsRecursivelyOfMachO(executableURL)
75+
let enumeratedURLs = allFrameworkMachOsInBundle(target)
76+
6577
let machOs = linkedDylibs.intersection(enumeratedURLs)
6678
var sortedMachOs: [URL] =
6779
switch injectStrategy {

TrollFools/InjectorV3+Eject.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,14 @@ extension InjectorV3 {
9494
}
9595

9696
fileprivate func collectModifiedMachOs() throws -> [URL] {
97-
try frameworkMachOsInBundle(bundleURL)
97+
let preferredMachOs = try frameworkMachOsInBundle(bundleURL)
98+
.filter { hasAlternate($0) }.elements
99+
if !preferredMachOs.isEmpty {
100+
return preferredMachOs
101+
}
102+
103+
DDLogWarn("Fallback to traversal strategy", ddlog: logger)
104+
return allFrameworkMachOsInBundle(bundleURL)
98105
.filter { hasAlternate($0) }.elements
99106
}
100107

TrollFools/InjectorV3+Inject.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,14 @@ extension InjectorV3 {
205205
// MARK: - Path Finder
206206

207207
fileprivate func locateAvailableMachO() throws -> URL? {
208-
try frameworkMachOsInBundle(bundleURL)
208+
let preferredMachO = try frameworkMachOsInBundle(bundleURL)
209+
.first { try !isProtectedMachO($0) }
210+
if let preferredMachO {
211+
return preferredMachO
212+
}
213+
214+
DDLogWarn("Fallback to traversal strategy", ddlog: logger)
215+
return try allFrameworkMachOsInBundle(bundleURL)
209216
.first { try !isProtectedMachO($0) }
210217
}
211218

0 commit comments

Comments
 (0)