|
| 1 | +// |
| 2 | +// UninstallerManager.swift |
| 3 | +// KeyChain Installer |
| 4 | +// |
| 5 | +// Created by Mikhail Lutskiy on 19/12/2018. |
| 6 | +// Copyright © 2018 Mikhail Lutskii. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import AHLaunchCtl |
| 11 | + |
| 12 | +enum UninstallerManagerSteps { |
| 13 | + case autoRunProcess |
| 14 | + case webSocketBinary |
| 15 | + case keyChainBinary |
| 16 | + case keyChainResources |
| 17 | + case keyChainExamples |
| 18 | + case keyChainLogs |
| 19 | + case keyChainKeys |
| 20 | + case keyChainDir |
| 21 | +} |
| 22 | + |
| 23 | +protocol UninstallerManagerDelegate { |
| 24 | + func endOfStep(_ step: UninstallerManagerSteps) |
| 25 | + func successfullyUninstall() |
| 26 | + func failOfStep(_ step: UninstallerManagerSteps) |
| 27 | +} |
| 28 | + |
| 29 | +extension UninstallerManagerDelegate { |
| 30 | + func endOfStep(_ step: UninstallerManagerSteps) {} |
| 31 | + func successfullyUninstall() {} |
| 32 | + func failOfStep(_ step: UninstallerManagerSteps) {} |
| 33 | +} |
| 34 | + |
| 35 | +class UninstallerManager { |
| 36 | + |
| 37 | + static let shared = UninstallerManager() |
| 38 | + |
| 39 | + var delegate: UninstallerManagerDelegate? |
| 40 | + |
| 41 | + let fileManager = FileManager.default |
| 42 | + |
| 43 | + var isDeleteKeys = false |
| 44 | + |
| 45 | + let ahLaunchCtl = AHLaunchCtl() |
| 46 | + |
| 47 | + func launch () { |
| 48 | + ahLaunchCtl.authorize() |
| 49 | + print(fileManager.currentDirectoryPath) |
| 50 | + predictKeychainPath(success: { (path) in |
| 51 | + self.deleteForKeyChainPath(path) |
| 52 | + }) { |
| 53 | + self.deleteForKeyChainPath(self.fileManager.currentDirectoryPath) |
| 54 | + } |
| 55 | + disableAutoRunProcess() |
| 56 | + deleteKeyChainKeys() |
| 57 | + delegate?.successfullyUninstall() |
| 58 | + } |
| 59 | + |
| 60 | + func deleteForKeyChainPath(_ path: String) { |
| 61 | + deleteWebSocketBinary(path: path) |
| 62 | + deleteKeyChainBinary(path: path) |
| 63 | + deleteKeyChainResources(path: path) |
| 64 | + deleteKeyChainExamples(path: path) |
| 65 | + deleteKeyChainLogs(path: path) |
| 66 | + deleteFile(path, step: .keyChainDir) |
| 67 | + } |
| 68 | + |
| 69 | + func predictKeychainPath (success: @escaping(_ path: String) -> Void, fail:@escaping() -> Void) { |
| 70 | + AppleScriptManager.runScriptWithBodyWithReturnString("ps -ax -o command | grep websocket | grep -v grep", success: { (result) in |
| 71 | + let subcmd = result.components(separatedBy: " ") |
| 72 | + let path = subcmd.first?.replacingOccurrences(of: "/websocketd", with: "") ?? "" |
| 73 | + if path != "" { |
| 74 | + success(path) |
| 75 | + } else { |
| 76 | + fail() |
| 77 | + } |
| 78 | + print(path) |
| 79 | + }) { (error) in |
| 80 | + print(error) |
| 81 | + fail() |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + func disableAutoRunProcess () { |
| 86 | + do { |
| 87 | + try ahLaunchCtl.stop(Consts.LABEL_JOB, in: .globalLaunchDaemon) |
| 88 | + } catch { |
| 89 | + print(error.localizedDescription) |
| 90 | + } |
| 91 | + do { |
| 92 | + try ahLaunchCtl.unload(Consts.LABEL_JOB, in: .globalLaunchDaemon) |
| 93 | + } catch { |
| 94 | + print(error.localizedDescription) |
| 95 | + } |
| 96 | + do { |
| 97 | + try ahLaunchCtl.remove(Consts.LABEL_JOB, from: .globalLaunchDaemon) |
| 98 | + } catch { |
| 99 | + print(error.localizedDescription) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + func deleteWebSocketBinary (path: String) { |
| 104 | + deleteFile(path + "/websocketd", step: .webSocketBinary) |
| 105 | + } |
| 106 | + |
| 107 | + func deleteKeyChainBinary (path: String) { |
| 108 | + deleteFile(path + "/keychain", step: .webSocketBinary) |
| 109 | + } |
| 110 | + |
| 111 | + func deleteKeyChainResources (path: String) { |
| 112 | + deleteFile(path + "/resources", step: .webSocketBinary) |
| 113 | + } |
| 114 | + |
| 115 | + func deleteKeyChainExamples (path: String) { |
| 116 | + deleteFile(path + "/examples", step: .webSocketBinary) |
| 117 | + } |
| 118 | + |
| 119 | + func deleteKeyChainLogs (path: String) { |
| 120 | + deleteFile(path + "/log.txt", step: .webSocketBinary) |
| 121 | + deleteFile(path + "/out.txt", step: .webSocketBinary) |
| 122 | + } |
| 123 | + |
| 124 | + func deleteKeyChainKeys () { |
| 125 | + if isDeleteKeys { |
| 126 | + AppleScriptManager.runScriptWithBody("rm -rf /var/keychain", isAdminRequired: true) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + func deleteFile (_ file: String, step: UninstallerManagerSteps) { |
| 131 | + if fileManager.fileExists(atPath: file) { |
| 132 | + do { |
| 133 | + try fileManager.removeItem(atPath: file) |
| 134 | + delegate?.endOfStep(step) |
| 135 | + } catch let error as NSError { |
| 136 | + print(error) |
| 137 | + delegate?.failOfStep(step) |
| 138 | + } |
| 139 | + } else { |
| 140 | + delegate?.failOfStep(step) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments