From 93b51f9e9bfe53faeaa03d2754f201fad4c4ce02 Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Wed, 16 Feb 2022 22:02:57 -0800 Subject: [PATCH] Dump --- Sources/Secretive.xcodeproj/project.pbxproj | 24 +++------- .../xcschemes/SecretiveUpdater.xcscheme | 6 +-- Sources/Secretive/App.swift | 9 +--- .../UpdaterCommunicationController.swift | 17 +++---- Sources/Secretive/Helpers/BundleIDs.swift | 1 + Sources/SecretiveUpdater/AppDelegate.swift | 43 ------------------ Sources/SecretiveUpdater/Updater.swift | 45 ++++++++++++------- .../SecretiveUpdater/UpdaterProtocol.swift | 3 +- Sources/SecretiveUpdater/main.swift | 4 +- 9 files changed, 52 insertions(+), 100 deletions(-) delete mode 100644 Sources/SecretiveUpdater/AppDelegate.swift diff --git a/Sources/Secretive.xcodeproj/project.pbxproj b/Sources/Secretive.xcodeproj/project.pbxproj index a12bbee..afbf0ab 100644 --- a/Sources/Secretive.xcodeproj/project.pbxproj +++ b/Sources/Secretive.xcodeproj/project.pbxproj @@ -19,7 +19,6 @@ 5003EF632780081B00DF2006 /* SecureEnclaveSecretKit in Frameworks */ = {isa = PBXBuildFile; productRef = 5003EF622780081B00DF2006 /* SecureEnclaveSecretKit */; }; 5003EF652780081B00DF2006 /* SmartCardSecretKit in Frameworks */ = {isa = PBXBuildFile; productRef = 5003EF642780081B00DF2006 /* SmartCardSecretKit */; }; 500ED3D427B7934A00A6DC28 /* UpdaterProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5088065927B4A5E40090BD57 /* UpdaterProtocol.swift */; }; - 500ED3D527B796C800A6DC28 /* Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app in Resources */ = {isa = PBXBuildFile; fileRef = 5081F6D027B790DD0094B82D /* Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app */; }; 500ED3DA27B797EE00A6DC28 /* Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app in CopyFiles */ = {isa = PBXBuildFile; fileRef = 5081F6D027B790DD0094B82D /* Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 501421622781262300BBAA70 /* Brief in Frameworks */ = {isa = PBXBuildFile; productRef = 501421612781262300BBAA70 /* Brief */; }; 501421652781268000BBAA70 /* SecretAgent.app in CopyFiles */ = {isa = PBXBuildFile; fileRef = 50A3B78A24026B7500D209EA /* SecretAgent.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; @@ -40,7 +39,7 @@ 506772C72424784600034DED /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 506772C62424784600034DED /* Credits.rtf */; }; 506772C92425BB8500034DED /* NoStoresView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 506772C82425BB8500034DED /* NoStoresView.swift */; }; 5079BA0F250F29BF00EA86F4 /* StoreListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5079BA0E250F29BF00EA86F4 /* StoreListView.swift */; }; - 5081F6D327B790DD0094B82D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5081F6D227B790DD0094B82D /* AppDelegate.swift */; }; + 5081F6D327B790DD0094B82D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5081F6D227B790DD0094B82D /* main.swift */; }; 5081F6D727B790DE0094B82D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5081F6D627B790DE0094B82D /* Assets.xcassets */; }; 5081F6E027B791110094B82D /* UpdaterProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5088065927B4A5E40090BD57 /* UpdaterProtocol.swift */; }; 5081F6E127B791110094B82D /* Updater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5088065B27B4A6240090BD57 /* Updater.swift */; }; @@ -155,15 +154,13 @@ 506772C82425BB8500034DED /* NoStoresView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoStoresView.swift; sourceTree = ""; }; 5079BA0E250F29BF00EA86F4 /* StoreListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreListView.swift; sourceTree = ""; }; 5081F6D027B790DD0094B82D /* Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app; sourceTree = BUILT_PRODUCTS_DIR; }; - 5081F6D227B790DD0094B82D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + 5081F6D227B790DD0094B82D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 5081F6D627B790DE0094B82D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 5081F6DB27B790DE0094B82D /* SecretiveUpdater.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SecretiveUpdater.entitlements; sourceTree = ""; }; 5081F6FF27B792150094B82D /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = ""; }; - 5088065027B4A5BE0090BD57 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 5088065927B4A5E40090BD57 /* UpdaterProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdaterProtocol.swift; sourceTree = ""; }; 5088065B27B4A6240090BD57 /* Updater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Updater.swift; sourceTree = ""; }; - 5088065D27B4A6460090BD57 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; - 5088068E27B4A6FF0090BD57 /* UpdaterCommunicationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UpdaterCommunicationController.swift; sourceTree = ""; }; + 5088068E27B4A6FF0090BD57 /* UpdaterCommunicationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 5; lastKnownFileType = sourcecode.swift; path = UpdaterCommunicationController.swift; sourceTree = ""; }; 508A58A9241E06B40069DC07 /* PreviewUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewUpdater.swift; sourceTree = ""; }; 508A58AB241E121B0069DC07 /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = ""; }; 508A58B2241ED2180069DC07 /* AgentStatusChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgentStatusChecker.swift; sourceTree = ""; }; @@ -242,7 +239,6 @@ 50617D9723FCE48E0099B055 /* SecretiveTests */, 50A3B78B24026B7500D209EA /* SecretAgent */, 508A58AF241E144C0069DC07 /* Config */, - 5088064927B4A5BE0090BD57 /* SecretiveUpdater */, 5081F6D127B790DD0094B82D /* SecretiveUpdater */, 50617D8023FCE48E0099B055 /* Products */, 5099A08B240243730062B6F2 /* Frameworks */, @@ -300,7 +296,7 @@ 5081F6D127B790DD0094B82D /* SecretiveUpdater */ = { isa = PBXGroup; children = ( - 5081F6D227B790DD0094B82D /* AppDelegate.swift */, + 5081F6D227B790DD0094B82D /* main.swift */, 5088065927B4A5E40090BD57 /* UpdaterProtocol.swift */, 5088065B27B4A6240090BD57 /* Updater.swift */, 5081F6FF27B792150094B82D /* Main.storyboard */, @@ -310,15 +306,6 @@ path = SecretiveUpdater; sourceTree = ""; }; - 5088064927B4A5BE0090BD57 /* SecretiveUpdater */ = { - isa = PBXGroup; - children = ( - 5088065027B4A5BE0090BD57 /* Info.plist */, - 5088065D27B4A6460090BD57 /* main.swift */, - ); - path = SecretiveUpdater; - sourceTree = ""; - }; 508A58AF241E144C0069DC07 /* Config */ = { isa = PBXGroup; children = ( @@ -538,7 +525,6 @@ 50617D8A23FCE48E0099B055 /* Preview Assets.xcassets in Resources */, 50617D8723FCE48E0099B055 /* Assets.xcassets in Resources */, 506772C72424784600034DED /* Credits.rtf in Resources */, - 500ED3D527B796C800A6DC28 /* Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app in Resources */, 508BF28E25B4F005009EFB7E /* InternetAccessPolicy.plist in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -616,7 +602,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 5081F6D327B790DD0094B82D /* AppDelegate.swift in Sources */, + 5081F6D327B790DD0094B82D /* main.swift in Sources */, 5081F6E027B791110094B82D /* UpdaterProtocol.swift in Sources */, 5081F6E127B791110094B82D /* Updater.swift in Sources */, ); diff --git a/Sources/Secretive.xcodeproj/xcshareddata/xcschemes/SecretiveUpdater.xcscheme b/Sources/Secretive.xcodeproj/xcshareddata/xcschemes/SecretiveUpdater.xcscheme index 25476f1..98fd2bc 100644 --- a/Sources/Secretive.xcodeproj/xcshareddata/xcschemes/SecretiveUpdater.xcscheme +++ b/Sources/Secretive.xcodeproj/xcshareddata/xcschemes/SecretiveUpdater.xcscheme @@ -15,7 +15,7 @@ @@ -45,7 +45,7 @@ @@ -62,7 +62,7 @@ diff --git a/Sources/Secretive/App.swift b/Sources/Secretive/App.swift index 24b7fb3..c1505f5 100644 --- a/Sources/Secretive/App.swift +++ b/Sources/Secretive/App.swift @@ -29,19 +29,12 @@ struct Secretive: App { .environmentObject(UpdateChecker(checkOnLaunch: hasRunSetup)) .environmentObject(agentStatusChecker) .onAppear { - updaterController.configure() + updaterController.installUpdate(url: URL(string: "https://github.com/maxgoedjen/secretive/releases/download/v2.1.1/Secretive.zip")!) if !hasRunSetup { showingSetup = true } } .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in - Task { - do { - let path = try await updaterController.updater?.installUpdate(url: URL(string: "https://github.com/maxgoedjen/secretive/releases/download/v2.1.1/Secretive.zip")!) - } catch { - print(error) - } - } guard hasRunSetup else { return } agentStatusChecker.check() if agentStatusChecker.running && justUpdatedChecker.justUpdated { diff --git a/Sources/Secretive/Controllers/UpdaterCommunicationController.swift b/Sources/Secretive/Controllers/UpdaterCommunicationController.swift index 883d4c4..4703609 100644 --- a/Sources/Secretive/Controllers/UpdaterCommunicationController.swift +++ b/Sources/Secretive/Controllers/UpdaterCommunicationController.swift @@ -15,12 +15,11 @@ class UpdaterCommunicationController: ObservableObject { init() { } - func configure() { + func installUpdate(url: URL) { guard !running else { return } - // TODO: Set disabled on launch. Only enable when I have an update to install. - let x = SMLoginItemSetEnabled("Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater" as CFString, false) - let y = SMLoginItemSetEnabled("Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater" as CFString, true) - connection = NSXPCConnection(machServiceName: "Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater") + _ = SMLoginItemSetEnabled(Bundle.main.updaterBundleID as CFString, false) + SMLoginItemSetEnabled(Bundle.main.updaterBundleID as CFString, true) + connection = NSXPCConnection(machServiceName: Bundle.main.updaterBundleID) connection?.remoteObjectInterface = NSXPCInterface(with: UpdaterProtocol.self) connection?.invalidationHandler = { Logger().warning("XPC connection invalidated") @@ -29,10 +28,12 @@ class UpdaterCommunicationController: ObservableObject { updater = connection?.remoteObjectProxyWithErrorHandler({ error in Logger().error("\(String(describing: error))") }) as? UpdaterProtocol - Task { - print(try await updater?.installUpdate(url: URL(string: "https://google.com")!)) - } running = true + let existingURL = Bundle.main.bundleURL + Task { + let result = try await updater?.installUpdate(url: url, to: existingURL) + print(result) + } } } diff --git a/Sources/Secretive/Helpers/BundleIDs.swift b/Sources/Secretive/Helpers/BundleIDs.swift index de4967d..d1c5090 100644 --- a/Sources/Secretive/Helpers/BundleIDs.swift +++ b/Sources/Secretive/Helpers/BundleIDs.swift @@ -4,4 +4,5 @@ import Foundation extension Bundle { public var agentBundleID: String {(self.bundleIdentifier?.replacingOccurrences(of: "Host", with: "SecretAgent"))!} public var hostBundleID: String {(self.bundleIdentifier?.replacingOccurrences(of: "SecretAgent", with: "Host"))!} + public var updaterBundleID: String { "Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater" } } diff --git a/Sources/SecretiveUpdater/AppDelegate.swift b/Sources/SecretiveUpdater/AppDelegate.swift deleted file mode 100644 index ed2b424..0000000 --- a/Sources/SecretiveUpdater/AppDelegate.swift +++ /dev/null @@ -1,43 +0,0 @@ -import Cocoa - -@main -class AppDelegate: NSObject, NSApplicationDelegate { - - let delegate = ServiceDelegate(exportedObject: Updater()) - let listener = NSXPCListener(machServiceName: Bundle.main.bundleIdentifier!) - - func applicationDidFinishLaunching(_ aNotification: Notification) { - listener.delegate = delegate - listener.resume() - Task { - try! await delegate.exported.authorize() - } - } - - func applicationWillTerminate(_ aNotification: Notification) { - } - - func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { - return false - } - - -} - -class ServiceDelegate: NSObject, NSXPCListenerDelegate { - - let exported: UpdaterProtocol - - init(exportedObject: UpdaterProtocol) { - self.exported = exportedObject - } - - func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool { - newConnection.exportedInterface = NSXPCInterface(with: UpdaterProtocol.self) - newConnection.exportedObject = exported - newConnection.resume() - return true - } - -} - diff --git a/Sources/SecretiveUpdater/Updater.swift b/Sources/SecretiveUpdater/Updater.swift index d4cc72d..5d5e369 100644 --- a/Sources/SecretiveUpdater/Updater.swift +++ b/Sources/SecretiveUpdater/Updater.swift @@ -8,13 +8,18 @@ import Security.AuthorizationTags class Updater: UpdaterProtocol { - func installUpdate(url: URL) async throws -> String { -// try await authorize() + func installUpdate(url: URL, to destinationURL: URL) async throws -> String { // let (downloadedURL, _) = try await URLSession.shared.download(from: url) // let unzipped = try await decompress(url: downloadedURL) +// try await move(url: unzipped, to: destinationURL) // let config = NSWorkspace.OpenConfiguration() // config.activates = true + // TODO: clean + _ = try await authorize() +// if let host = NSRunningApplication.runningApplications(withBundleIdentifier: "com.maxgoedjen.Secretive.Host").first(where: { $0.bundleURL?.path.hasPrefix("/Applications") ?? false }) { +// host.terminate() // +// } return "OK" } @@ -37,43 +42,51 @@ class Updater: UpdaterProtocol { return appURL } - func move(url: URL) async throws { - try await authorize() - try await move(url: url) - try await revokeAuthorization() + func move(url: URL, to destinationURL: URL) async throws { + let auth = try await authorize() + try await move(url: url, to: destinationURL) + try await revokeAuthorization(auth) } - func authorize() async throws { + func authorize() async throws -> AuthorizationRef { let flags = AuthorizationFlags() var authorization: AuthorizationRef? = nil - let status = AuthorizationCreate(nil, nil, flags, &authorization) - print(status) - print("Hello") + AuthorizationCreate(nil, nil, flags, &authorization) let authFlags: AuthorizationFlags = [.interactionAllowed, .extendRights, .preAuthorize] + var result: OSStatus? kAuthorizationRightExecute.withCString { cString in var item = AuthorizationItem(name: cString, valueLength: 0, value: nil, flags: 0) withUnsafeMutablePointer(to: &item) { pointer in var rights = AuthorizationRights(count: 1, items: pointer) - let out = AuthorizationCopyRights(authorization!, &rights, nil, authFlags, nil) - print(out) + result = AuthorizationCopyRights(authorization!, &rights, nil, authFlags, nil) } } - } - - func revokeAuthorization() async throws { + guard result == errAuthorizationSuccess, let authorization = authorization else { + throw RightsNotAcquiredError() + } + return authorization } - func priveledgedMove(url: URL) async throws { + func revokeAuthorization(_ authorization: AuthorizationRef) async throws { + AuthorizationFree(authorization, .destroyRights) + } + func priveledgedMove(url: URL, to destination: URL) async throws { + try FileManager.default.replaceItemAt(destination, withItemAt: url) } } extension Updater { + struct DecompressionError: Error, LocalizedError { let reason: String } + + struct RightsNotAcquiredError: Error, LocalizedError { + } + } extension URLSession { diff --git a/Sources/SecretiveUpdater/UpdaterProtocol.swift b/Sources/SecretiveUpdater/UpdaterProtocol.swift index 0961b4c..4e0b942 100644 --- a/Sources/SecretiveUpdater/UpdaterProtocol.swift +++ b/Sources/SecretiveUpdater/UpdaterProtocol.swift @@ -3,7 +3,6 @@ import Brief @objc public protocol UpdaterProtocol { - func installUpdate(url: URL) async throws -> String - func authorize() async throws + func installUpdate(url: URL, to: URL) async throws -> String } diff --git a/Sources/SecretiveUpdater/main.swift b/Sources/SecretiveUpdater/main.swift index bad9242..ee18cf6 100644 --- a/Sources/SecretiveUpdater/main.swift +++ b/Sources/SecretiveUpdater/main.swift @@ -19,6 +19,8 @@ class ServiceDelegate: NSObject, NSXPCListenerDelegate { let updater = Updater() let delegate = ServiceDelegate(exportedObject: Updater()) -let listener = NSXPCListener.service() +let listener = NSXPCListener(machServiceName: Bundle.main.bundleIdentifier!) listener.delegate = delegate listener.resume() +try "Hello world".data(using: .utf8)?.write(to: URL(fileURLWithPath: "/Users/max/Downloads/\(UUID().uuidString).txt")) +RunLoop.current.run()