This commit is contained in:
Max Goedjen 2022-02-16 22:02:57 -08:00
parent ba1c4c1563
commit 93b51f9e9b
No known key found for this signature in database
GPG Key ID: E58C21DD77B9B8E8
9 changed files with 52 additions and 100 deletions

View File

@ -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 = "<group>"; };
5079BA0E250F29BF00EA86F4 /* StoreListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreListView.swift; sourceTree = "<group>"; };
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 = "<group>"; };
5081F6D227B790DD0094B82D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
5081F6D627B790DE0094B82D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
5081F6DB27B790DE0094B82D /* SecretiveUpdater.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SecretiveUpdater.entitlements; sourceTree = "<group>"; };
5081F6FF27B792150094B82D /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
5088065027B4A5BE0090BD57 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5088065927B4A5E40090BD57 /* UpdaterProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdaterProtocol.swift; sourceTree = "<group>"; };
5088065B27B4A6240090BD57 /* Updater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Updater.swift; sourceTree = "<group>"; };
5088065D27B4A6460090BD57 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
5088068E27B4A6FF0090BD57 /* UpdaterCommunicationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UpdaterCommunicationController.swift; sourceTree = "<group>"; };
5088068E27B4A6FF0090BD57 /* UpdaterCommunicationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 5; lastKnownFileType = sourcecode.swift; path = UpdaterCommunicationController.swift; sourceTree = "<group>"; };
508A58A9241E06B40069DC07 /* PreviewUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewUpdater.swift; sourceTree = "<group>"; };
508A58AB241E121B0069DC07 /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = "<group>"; };
508A58B2241ED2180069DC07 /* AgentStatusChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgentStatusChecker.swift; sourceTree = "<group>"; };
@ -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 = "<group>";
};
5088064927B4A5BE0090BD57 /* SecretiveUpdater */ = {
isa = PBXGroup;
children = (
5088065027B4A5BE0090BD57 /* Info.plist */,
5088065D27B4A6460090BD57 /* main.swift */,
);
path = SecretiveUpdater;
sourceTree = "<group>";
};
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 */,
);

View File

@ -15,7 +15,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5081F6CF27B790DD0094B82D"
BuildableName = "Test.app"
BuildableName = "Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app"
BlueprintName = "SecretiveUpdater"
ReferencedContainer = "container:Secretive.xcodeproj">
</BuildableReference>
@ -45,7 +45,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5081F6CF27B790DD0094B82D"
BuildableName = "Test.app"
BuildableName = "Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app"
BlueprintName = "SecretiveUpdater"
ReferencedContainer = "container:Secretive.xcodeproj">
</BuildableReference>
@ -62,7 +62,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "5081F6CF27B790DD0094B82D"
BuildableName = "Test.app"
BuildableName = "Z72PRUAWF6.com.maxgoedjen.SecretiveUpdater.app"
BlueprintName = "SecretiveUpdater"
ReferencedContainer = "container:Secretive.xcodeproj">
</BuildableReference>

View File

@ -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 {

View File

@ -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)
}
}
}

View File

@ -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" }
}

View File

@ -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
}
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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()