-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// Apps.swift | ||
// Pareto Security | ||
// | ||
// Created by Janez Troha on 14. 03. 24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct PublicApp: Codable { | ||
let name: String | ||
let bundle: String | ||
let version: String | ||
|
||
static var all: [PublicApp] { | ||
var detectedApps: [PublicApp] = [] | ||
let allApps = try! FileManager.default.contentsOfDirectory(at: URL(string: "/Applications")!, includingPropertiesForKeys: [.isApplicationKey]) | ||
for app in allApps { | ||
let plist = PublicApp.readPlistFile(fileURL: app.appendingPathComponent("Contents/Info.plist")) | ||
if let appName = plist?["CFBundleName"] as? String, | ||
let appBundle = plist?["CFBundleIdentifier"] as? String { | ||
let bundleApp = PublicApp( | ||
name: appName, | ||
bundle: appBundle, | ||
version: plist?["CFBundleShortVersionString"] as? String ?? "Unknown" | ||
) | ||
detectedApps.append(bundleApp) | ||
} | ||
} | ||
|
||
// user apps | ||
let homeDirURL = FileManager.default.homeDirectoryForCurrentUser | ||
let localPath = URL(fileURLWithPath: "\(homeDirURL.path)/Applications/") | ||
if (try? localPath.checkResourceIsReachable()) ?? false { | ||
let userApps = try! FileManager.default.contentsOfDirectory(at: localPath, includingPropertiesForKeys: [.isApplicationKey]) | ||
for app in userApps { | ||
let plist = PublicApp.readPlistFile(fileURL: app.appendingPathComponent("Contents/Info.plist")) | ||
if let appName = plist?["CFBundleName"] as? String, | ||
let appBundle = plist?["CFBundleIdentifier"] as? String { | ||
let bundleApp = PublicApp( | ||
name: appName, | ||
bundle: appBundle, | ||
version: plist?["CFBundleShortVersionString"] as? String ?? "Unknown" | ||
) | ||
detectedApps.append(bundleApp) | ||
} | ||
} | ||
} | ||
|
||
return detectedApps | ||
} | ||
|
||
static func readPlistFile(fileURL: URL) -> [String: Any]? { | ||
guard let data = try? Data(contentsOf: fileURL) else { | ||
return nil | ||
} | ||
guard let result = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] else { | ||
return nil | ||
} | ||
return result | ||
} | ||
|
||
static func asJSON() -> String? { | ||
var export: [PublicApp] = [] | ||
|
||
for app in PublicApp.all.sorted(by: { lha, rha in | ||
lha.name.lowercased() < rha.name.lowercased() | ||
}) { | ||
export.append(app) | ||
} | ||
|
||
let jsonEncoder = JSONEncoder() | ||
jsonEncoder.outputFormatting = .prettyPrinted | ||
let jsonData = try! jsonEncoder.encode(export) | ||
guard let json = String(data: jsonData, encoding: String.Encoding.utf8) else { return nil } | ||
|
||
return json | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters