From ce19c5202ff19d35aa0877ad4af872908253484b Mon Sep 17 00:00:00 2001 From: yicheng Date: Sun, 5 Aug 2018 18:45:58 +0800 Subject: [PATCH] Optimize: Only add auto group when has more than one proxies . --- ClashX/General/ConfigFileFactory.swift | 5 +++++ ClashX/Support Files/sampleConfig.ini | 2 +- .../PreferencesWindowController.swift | 20 ++++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ClashX/General/ConfigFileFactory.swift b/ClashX/General/ConfigFileFactory.swift index 3bf365c..161a18c 100644 --- a/ClashX/General/ConfigFileFactory.swift +++ b/ClashX/General/ConfigFileFactory.swift @@ -22,6 +22,11 @@ class ConfigFileFactory { sampleConfigStr = sampleConfigStr?.replacingOccurrences(of: "{{ProxyPlaceHolder}}", with: proxyStr) sampleConfigStr = sampleConfigStr?.replacingOccurrences(of: "{{ProxyGroupPlaceHolder}}", with: proxyGroupStr) + + if proxies.count > 1 { + let autoGroupStr = "ProxyAuto = url-test, \(proxyGroupStr), http://www.google.com/generate_204, 300" + sampleConfigStr = sampleConfigStr?.replacingOccurrences(of: "{{ProxyAutoGroupPlaceHolder}}", with: autoGroupStr) + } return sampleConfigStr! } diff --git a/ClashX/Support Files/sampleConfig.ini b/ClashX/Support Files/sampleConfig.ini index 0dc854e..2e4091c 100644 --- a/ClashX/Support Files/sampleConfig.ini +++ b/ClashX/Support Files/sampleConfig.ini @@ -13,7 +13,7 @@ Proxy = select, {{ProxyGroupPlaceHolder}} # url-test select which proxy will be used by benchmarking speed to a URL. # name = url-test, [proxys], url, interval(second) -ProxyAuto = url-test, {{ProxyGroupPlaceHolder}}, http://www.google.com/generate_204, 300 +{{ProxyAutoGroupPlaceHolder}} [Rule] diff --git a/ClashX/ViewControllers/PreferencesWindowController.swift b/ClashX/ViewControllers/PreferencesWindowController.swift index 9b81a95..042f8f6 100755 --- a/ClashX/ViewControllers/PreferencesWindowController.swift +++ b/ClashX/ViewControllers/PreferencesWindowController.swift @@ -132,8 +132,26 @@ class PreferencesWindowController: NSWindowController shakeWindows() return } + var vaild = true + var remarkSet = Set() + remarkSet.insert("Proxy") + remarkSet.insert("ProxyAuto") + for config in serverConfigs { + if remarkSet.contains(config.remark) || !config.isValid() { + vaild = false + break + } else { + remarkSet.insert(config.remark) + } + } + + if (!vaild) { + self.shakeWindows() + return + } + let str = ConfigFileFactory.configFile(proxies: serverConfigs) - ConfigFileFactory.saveToClashConfigFile(str) + ConfigFileFactory.saveToClashConfigFile(str: str) window?.performClose(nil) }