feat: check require approve only when no found helper

This commit is contained in:
yicheng 2022-11-28 18:33:36 +08:00
parent 08f2b1120c
commit d1fb0d6315
2 changed files with 36 additions and 39 deletions

View File

@ -54,8 +54,7 @@ extension PrivilegedHelperManager {
""" """
return bash return bash
} }
func runScriptWithRootPermission(script: String) { func runScriptWithRootPermission(script: String) {
let tmpPath = FileManager.default.temporaryDirectory.appendingPathComponent(NSUUID().uuidString).appendingPathExtension("sh") let tmpPath = FileManager.default.temporaryDirectory.appendingPathComponent(NSUUID().uuidString).appendingPathExtension("sh")
do { do {
@ -82,14 +81,14 @@ extension PrivilegedHelperManager {
let script = getInstallScript() let script = getInstallScript()
runScriptWithRootPermission(script: script) runScriptWithRootPermission(script: script)
} }
func removeInstallHelper() { func removeInstallHelper() {
defer { defer {
resetConnection() resetConnection()
Thread.sleep(forTimeInterval: 1) Thread.sleep(forTimeInterval: 5)
} }
let script = """ let script = """
launchctl remove \(PrivilegedHelperManager.machServiceName) || true
rm -rf /Library/LaunchDaemons/\(PrivilegedHelperManager.machServiceName).plist rm -rf /Library/LaunchDaemons/\(PrivilegedHelperManager.machServiceName).plist
rm -rf /Library/PrivilegedHelperTools/\(PrivilegedHelperManager.machServiceName) rm -rf /Library/PrivilegedHelperTools/\(PrivilegedHelperManager.machServiceName)
""" """

View File

@ -30,37 +30,39 @@ class PrivilegedHelperManager {
func checkInstall() { func checkInstall() {
Logger.log("checkInstall", level: .debug) Logger.log("checkInstall", level: .debug)
if #available(macOS 13, *) {
let url = URL(string: "/Library/LaunchDaemons/\(PrivilegedHelperManager.machServiceName).plist")!
let status = SMAppService.statusForLegacyPlist(at: url)
if status == .requiresApproval {
let alert = NSAlert()
let notice = NSLocalizedString("ClashX use a daemon helper to setup your system proxy. Please enable ClashX in the Login Items under the Allow in the Background section and relaunch the app", comment: "")
let addition = NSLocalizedString("If you can not find ClashX in the settings, you can try reset daemon", comment: "")
alert.messageText = notice + "\n" + addition
alert.addButton(withTitle: NSLocalizedString("Open System Login Item Setting", comment: ""))
alert.addButton(withTitle: NSLocalizedString("Reset Daemon", comment: ""))
if alert.runModal() == .alertFirstButtonReturn {
SMAppService.openSystemSettingsLoginItems()
} else {
removeInstallHelper()
}
}
}
getHelperStatus { [weak self] status in getHelperStatus { [weak self] status in
Logger.log("check result: \(status)", level: .debug)
guard let self = self else {return} guard let self = self else {return}
if status != .installed { switch status {
let isUpdate = status == .needUpdate case .noFound:
Logger.log("need to install helper", level: .debug) if #available(macOS 13, *) {
if Thread.isMainThread { let url = URL(string: "/Library/LaunchDaemons/\(PrivilegedHelperManager.machServiceName).plist")!
self.notifyInstall(isUpdate: isUpdate) let status = SMAppService.statusForLegacyPlist(at: url)
} else { if status == .requiresApproval {
DispatchQueue.main.async { let alert = NSAlert()
self.notifyInstall(isUpdate: isUpdate) let notice = NSLocalizedString("ClashX use a daemon helper to setup your system proxy. Please enable ClashX in the Login Items under the Allow in the Background section and relaunch the app", comment: "")
let addition = NSLocalizedString("If you can not find ClashX in the settings, you can try reset daemon", comment: "")
alert.messageText = notice + "\n" + addition
alert.addButton(withTitle: NSLocalizedString("Open System Login Item Setting", comment: ""))
alert.addButton(withTitle: NSLocalizedString("Reset Daemon", comment: ""))
if alert.runModal() == .alertFirstButtonReturn {
SMAppService.openSystemSettingsLoginItems()
} else {
self.removeInstallHelper()
}
} }
} }
} else { fallthrough
case .needUpdate:
Logger.log("need to install helper", level: .debug)
if Thread.isMainThread {
self.notifyInstall()
} else {
DispatchQueue.main.async {
self.notifyInstall()
}
}
case .installed:
self.isHelperCheckFinished.accept(true) self.isHelperCheckFinished.accept(true)
} }
} }
@ -82,7 +84,7 @@ class PrivilegedHelperManager {
} }
/// Install new helper daemon /// Install new helper daemon
private func installHelperDaemon(isUpdate:Bool) -> DaemonInstallResult { private func installHelperDaemon() -> DaemonInstallResult {
Logger.log("installHelperDaemon", level: .info) Logger.log("installHelperDaemon", level: .info)
defer { defer {
@ -119,10 +121,6 @@ class PrivilegedHelperManager {
// Launch the privileged helper using SMJobBless tool // Launch the privileged helper using SMJobBless tool
var error: Unmanaged<CFError>? var error: Unmanaged<CFError>?
if isUpdate {
Logger.log("disable old daemon")
SMJobRemove(kSMDomainSystemLaunchd, PrivilegedHelperManager.machServiceName as CFString, authRef, true, &error)
}
if SMJobBless(kSMDomainSystemLaunchd, PrivilegedHelperManager.machServiceName as CFString, authRef, &error) == false { if SMJobBless(kSMDomainSystemLaunchd, PrivilegedHelperManager.machServiceName as CFString, authRef, &error) == false {
let blessError = error!.takeRetainedValue() as Error let blessError = error!.takeRetainedValue() as Error
Logger.log("Bless Error: \(blessError)", level: .error) Logger.log("Bless Error: \(blessError)", level: .error)
@ -198,7 +196,7 @@ class PrivilegedHelperManager {
} }
extension PrivilegedHelperManager { extension PrivilegedHelperManager {
private func notifyInstall(isUpdate: Bool) { private func notifyInstall() {
guard showInstallHelperAlert() else { exit(0) } guard showInstallHelperAlert() else { exit(0) }
if cancelInstallCheck { if cancelInstallCheck {
@ -214,7 +212,7 @@ extension PrivilegedHelperManager {
return return
} }
let result = installHelperDaemon(isUpdate: isUpdate) let result = installHelperDaemon()
if case .success = result { if case .success = result {
return return
} }