This commit is contained in:
Max Goedjen
2022-01-02 22:52:53 -08:00
parent d80d2ca656
commit df4661a718
6 changed files with 57 additions and 33 deletions

View File

@@ -1,34 +1,35 @@
import Foundation
import Combine
import AppKit
import OSLog
import SecretKit
import SecretAgentKitProtocol
protocol AgentCommunicationControllerProtocol: ObservableObject {
var agent: AgentProtocol { get }
var agent: AgentProtocol? { get }
}
class AgentCommunicationController: ObservableObject, AgentCommunicationControllerProtocol {
let agent: AgentProtocol
private let connection: NSXPCConnection
private(set) var agent: AgentProtocol? = nil
private var connection: NSXPCConnection? = nil
private var running = false
init() {
connection = NSXPCConnection(machServiceName: Bundle.main.agentBundleID)
connection.remoteObjectInterface = NSXPCInterface(with: AgentProtocol.self)
connection.invalidationHandler = {
print("INVALID")
}
agent = connection.remoteObjectProxyWithErrorHandler({ x in
print(x)
}) as! AgentProtocol
}
func configure() {
guard !running else { return }
connection = NSXPCConnection(machServiceName: Bundle.main.agentBundleID)
connection?.remoteObjectInterface = NSXPCInterface(with: AgentProtocol.self)
connection?.invalidationHandler = {
Logger().warning("XPC connection invalidated")
}
connection?.resume()
agent = connection?.remoteObjectProxyWithErrorHandler({ error in
Logger().error("\(String(describing: error))")
}) as! AgentProtocol
running = true
connection.resume()
}
}

View File

@@ -6,15 +6,19 @@ import SecretKit
struct AgentLaunchController {
func install(completion: (() -> Void)? = nil) {
func install(uninstallFirst: Bool = true, completion: (() -> Void)? = nil) {
Logger().debug("Installing agent")
_ = setEnabled(false)
if uninstallFirst {
_ = setEnabled(false)
}
// This is definitely a bit of a "seems to work better" thing but:
// Seems to more reliably hit if these are on separate runloops, otherwise it seems like it sometimes doesn't kill old
// and start new?
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
_ = setEnabled(true)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
completion?()
}
}
}
@@ -36,6 +40,12 @@ struct AgentLaunchController {
}
}
func killNonInstanceAgents(agents: [NSRunningApplication]) {
for agent in agents {
agent.terminate()
}
}
private func setEnabled(_ enabled: Bool) -> Bool {
SMLoginItemSetEnabled(Bundle.main.agentBundleID as CFString, enabled)
}

View File

@@ -30,13 +30,18 @@ class AgentStatusChecker: ObservableObject, AgentStatusCheckerProtocol {
let agents = secretAgentProcesses
for agent in agents {
guard let url = agent.bundleURL else { continue }
if url.absoluteString.hasPrefix(Bundle.main.bundleURL.absoluteString) {
if url.absoluteString.contains(Bundle.main.bundleURL.absoluteString) {
return agent
}
}
return nil
}
// All processes, _NOT_ including one the instance agent.
var noninstanceSecretAgentProcesses: [NSRunningApplication] {
NSRunningApplication.runningApplications(withBundleIdentifier: Bundle.main.agentBundleID)
.filter({ !($0.bundleURL?.absoluteString.contains(Bundle.main.bundleURL.absoluteString) ?? false) })
}
// Whether Secretive is being run in an Xcode environment.
var developmentBuild: Bool {