chore: optimize quit produce

This commit is contained in:
yicheng 2020-10-24 19:54:01 +08:00
parent 0a305a6c3b
commit 2ffcbdcfc4
2 changed files with 45 additions and 6 deletions

View File

@ -127,7 +127,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let group = DispatchGroup()
var shouldWait = false
if ConfigManager.shared.proxyPortAutoSet && !ConfigManager.shared.isProxySetByOtherVariable.value {
if ConfigManager.shared.proxyPortAutoSet && !ConfigManager.shared.isProxySetByOtherVariable.value || NetworkChangeNotifier.isCurrentSystemSetToClash(looser: true) ||
NetworkChangeNotifier.hasInterfaceProxySetToClash() {
Logger.log("ClashX quit need clean proxy setting")
shouldWait = true
group.enter()
@ -151,10 +152,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
switch res {
case .success:
Logger.log("ClashX quit after clean up finish")
DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
NSApp.reply(toApplicationShouldTerminate: true)
}
case .timedOut:
Logger.log("ClashX quit after clean up timeout")
DispatchQueue.main.async {
NSApp.reply(toApplicationShouldTerminate: true)
}
}
NSApp.reply(toApplicationShouldTerminate: true)
}
Logger.log("ClashX quit wait for clean up")
@ -163,6 +169,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ aNotification: Notification) {
UserDefaults.standard.set(0, forKey: "launch_fail_times")
Logger.log("ClashX will terminate")
if NetworkChangeNotifier.isCurrentSystemSetToClash(looser: true) ||
NetworkChangeNotifier.hasInterfaceProxySetToClash() {
Logger.log("Need Reset Proxy Setting again",level: .error)
SystemProxyManager.shared.disableProxy()
}
}
func setupStatusMenuItemData() {
@ -276,7 +288,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
ConfigManager.shared.isProxySetByOtherVariable.accept(!proxySetted)
if !proxySetted && ConfigManager.shared.proxyPortAutoSet {
let proxiesSetting = NetworkChangeNotifier.getRawProxySetting()
Logger.log("Proxy changed by other process!, current:\(proxiesSetting)", level: .warning)
Logger.log("Proxy changed by other process!, current:\(proxiesSetting), is Interface Set: \(NetworkChangeNotifier.hasInterfaceProxySetToClash())", level: .warning)
}
}.disposed(by: disposeBag)

View File

@ -78,13 +78,40 @@ class NetworkChangeNotifier {
return (httpProxy, httpsProxy, socksProxy)
}
static func isCurrentSystemSetToClash() -> Bool {
static func isCurrentSystemSetToClash(looser:Bool = false) -> Bool {
let (http, https, socks) = NetworkChangeNotifier.currentSystemProxySetting()
let currentPort = ConfigManager.shared.currentConfig?.usedHttpPort ?? 0
let currentSocks = ConfigManager.shared.currentConfig?.usedSocksPort ?? 0
let proxySetted = http == currentPort && https == currentPort && socks == currentSocks
return proxySetted
if looser {
return http == currentPort || https == currentPort || socks == currentSocks
} else {
return http == currentPort && https == currentPort && socks == currentSocks
}
}
static func hasInterfaceProxySetToClash() -> Bool {
let currentPort = ConfigManager.shared.currentConfig?.usedHttpPort
let currentSocks = ConfigManager.shared.currentConfig?.usedSocksPort
if let prefRef = SCPreferencesCreate(nil, "ClashX" as CFString, nil),
let sets = SCPreferencesGetValue(prefRef, kSCPrefNetworkServices){
for key in sets.allKeys {
let dict = sets.object(forKey: key) as? NSDictionary
let proxySettings = dict?["Proxies"] as? [String:Any]
if currentPort != nil {
if proxySettings?[kCFNetworkProxiesHTTPPort as String] as? Int == currentPort ||
proxySettings?[kCFNetworkProxiesHTTPSPort as String] as? Int == currentPort {
return true
}
}
if currentSocks != nil {
if proxySettings?[kCFNetworkProxiesSOCKSPort as String] as? Int == currentSocks {
return true
}
}
}
}
return false
}
static func getPrimaryInterface() -> String? {