Feature: add fail at launch protect

This commit is contained in:
yicheng 2018-08-06 15:52:38 +08:00
parent 4abbc701f3
commit 051a915364

View File

@ -34,7 +34,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
signal(SIGPIPE, SIG_IGN)
fail_launch_protect()
_ = ProxyConfigManager.install()
PFMoveToApplicationsFolderIfNecessary()
self.startProxy()
@ -113,6 +113,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
func fail_launch_protect(){
let x = UserDefaults.standard
var launch_fail_times:Int = 0
if let xx = x.object(forKey: "launch_fail_times") as? Int {launch_fail_times = xx }
launch_fail_times += 1
x.set(launch_fail_times, forKey: "launch_fail_times")
if launch_fail_times > 1{
//
let path = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/")
let documentDirectory = URL(fileURLWithPath: path)
let originPath = documentDirectory.appendingPathComponent("config.ini")
let destinationPath = documentDirectory.appendingPathComponent("config.ini.bak")
try? FileManager.default.moveItem(at: originPath, to: destinationPath)
try? FileManager.default.removeItem(at: documentDirectory.appendingPathComponent("Country.mmdb"))
NSUserNotificationCenter.default.post(title: "Fail on launch protect", info: "You origin Config has been rename to config.ini.bak")
}
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double(Int64(1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
x.set(0, forKey: "launch_fail_times")
});
}
func updateProxyList() {
ProxyMenuItemFactory.menuItems { [unowned self] (menus) in
let startIndex = self.statusMenu.items.index(of: self.separatorLineTop)! + 1