Skip to content

Commit 051a915

Browse files
committed
Feature: add fail at launch protect
1 parent 4abbc70 commit 051a915

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

ClashX/AppDelegate.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
3434

3535
func applicationDidFinishLaunching(_ aNotification: Notification) {
3636
signal(SIGPIPE, SIG_IGN)
37-
37+
fail_launch_protect()
3838
_ = ProxyConfigManager.install()
3939
PFMoveToApplicationsFolderIfNecessary()
4040
self.startProxy()
@@ -113,6 +113,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
113113

114114
}
115115

116+
func fail_launch_protect(){
117+
let x = UserDefaults.standard
118+
var launch_fail_times:Int = 0
119+
if let xx = x.object(forKey: "launch_fail_times") as? Int {launch_fail_times = xx }
120+
launch_fail_times += 1
121+
x.set(launch_fail_times, forKey: "launch_fail_times")
122+
if launch_fail_times > 1{
123+
//发生连续崩溃
124+
let path = (NSHomeDirectory() as NSString).appendingPathComponent("/.config/clash/")
125+
let documentDirectory = URL(fileURLWithPath: path)
126+
let originPath = documentDirectory.appendingPathComponent("config.ini")
127+
let destinationPath = documentDirectory.appendingPathComponent("config.ini.bak")
128+
try? FileManager.default.moveItem(at: originPath, to: destinationPath)
129+
try? FileManager.default.removeItem(at: documentDirectory.appendingPathComponent("Country.mmdb"))
130+
NSUserNotificationCenter.default.post(title: "Fail on launch protect", info: "You origin Config has been rename to config.ini.bak")
131+
132+
}
133+
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double(Int64(1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
134+
x.set(0, forKey: "launch_fail_times")
135+
});
136+
}
137+
116138
func updateProxyList() {
117139
ProxyMenuItemFactory.menuItems { [unowned self] (menus) in
118140
let startIndex = self.statusMenu.items.index(of: self.separatorLineTop)! + 1

0 commit comments

Comments
 (0)