fix global speedtest no work for providers

This commit is contained in:
yicheng 2020-04-29 20:29:08 +08:00
parent 9e861e684c
commit f0810289d7
4 changed files with 55 additions and 49 deletions

View File

@ -520,19 +520,27 @@ extension AppDelegate {
NSUserNotificationCenter.default.postSpeedTestBeginNotice()
isSpeedTesting = true
ApiRequest.getAllProxyList { [weak self] proxies in
let testGroup = DispatchGroup()
for proxyName in proxies {
testGroup.enter()
ApiRequest.getProxyDelay(proxyName: proxyName) { delay in
testGroup.leave()
ApiRequest.getMergedProxyData { [weak self] resp in
let group = DispatchGroup()
for (name, _) in resp?.enclosingProviderResp?.providers ?? [:] {
group.enter()
ApiRequest.healthCheck(proxy: name) {
group.leave()
}
}
testGroup.notify(queue: DispatchQueue.main, execute: {
for p in resp?.proxiesMap["GLOBAL"]?.all ?? [] {
group.enter()
ApiRequest.getProxyDelay(proxyName: p) { _ in
group.leave()
}
}
group.notify(queue: DispatchQueue.main) {
NSUserNotificationCenter.default.postSpeedTestFinishNotice()
self?.isSpeedTesting = false
})
}
}
}

View File

@ -195,13 +195,42 @@ class ApiRequest {
static func getAllProxyList(callback: @escaping (([ClashProxyName]) -> Void)) {
requestProxyGroupList {
proxyInfo in
let lists: [ClashProxyName] = proxyInfo.proxies
.filter { $0.name == "GLOBAL" }
.first?.all ?? []
let lists: [ClashProxyName] = proxyInfo.proxiesMap["GLOBAL"]?.all ?? []
callback(lists)
}
}
static func getMergedProxyData(complete: ((ClashProxyResp?) -> Void)? = nil) {
let group = DispatchGroup()
group.enter()
group.enter()
var provider: ClashProviderResp?
var proxyInfo: ClashProxyResp?
group.notify(queue: .main) {
guard let proxyInfo = proxyInfo, let proxyprovider = provider else {
assertionFailure()
complete?(nil)
return
}
proxyInfo.updateProvider(proxyprovider)
complete?(proxyInfo)
}
ApiRequest.requestProxyProviderList {
proxyprovider in
provider = proxyprovider
group.leave()
}
ApiRequest.requestProxyGroupList {
proxy in
proxyInfo = proxy
group.leave()
}
}
static func getProxyDelay(proxyName: String, callback: @escaping ((Int) -> Void)) {
req("/proxies/\(proxyName.encoded)/delay",
method: .get,
@ -225,7 +254,7 @@ class ApiRequest {
}
}
static func healthCheck(proxy: ClashProviderName) {
static func healthCheck(proxy: ClashProviderName, completeHandler: (() -> Void)? = nil) {
Logger.log("HeathCheck for \(proxy) started")
req("/providers/proxies/\(proxy.encoded)/healthcheck").response { res in
if res.response?.statusCode == 204 {
@ -233,6 +262,7 @@ class ApiRequest {
} else {
Logger.log("HeathCheck for \(proxy) failed")
}
completeHandler?()
}
}
}

View File

@ -22,10 +22,9 @@ class MenuItemFactory {
// MARK: - Public
static func refreshExistingMenuItems() {
let previousInfo = cachedProxyData
getMergedProxyData {
ApiRequest.getMergedProxyData {
info in
if info?.proxiesMap.keys != previousInfo?.proxiesMap.keys {
if info?.proxiesMap.keys != cachedProxyData?.proxiesMap.keys {
// force update menu
refreshMenuItems(mergedData: info)
return
@ -38,8 +37,9 @@ class MenuItemFactory {
}
static func recreateProxyMenuItems() {
getMergedProxyData {
ApiRequest.getMergedProxyData {
proxyInfo in
cachedProxyData = proxyInfo
refreshMenuItems(mergedData: proxyInfo)
}
}
@ -81,38 +81,6 @@ class MenuItemFactory {
// MARK: - Private
private static func getMergedProxyData(complete: ((ClashProxyResp?) -> Void)? = nil) {
let group = DispatchGroup()
group.enter()
group.enter()
var provider: ClashProviderResp?
var proxyInfo: ClashProxyResp?
group.notify(queue: .main) {
guard let proxyInfo = proxyInfo, let proxyprovider = provider else {
assertionFailure()
complete?(nil)
return
}
proxyInfo.updateProvider(proxyprovider)
cachedProxyData = proxyInfo
complete?(proxyInfo)
}
ApiRequest.requestProxyProviderList {
proxyprovider in
provider = proxyprovider
group.leave()
}
ApiRequest.requestProxyGroupList {
proxy in
proxyInfo = proxy
group.leave()
}
}
// MARK: Updaters
static func updateProxyList(withMenus menus: [NSMenuItem]) {

View File

@ -150,7 +150,7 @@ class ClashProxyResp {
// proxiesmap with provider included
var proxiesMap: [ClashProxyName: ClashProxy]
private var enclosingProviderResp: ClashProviderResp?
var enclosingProviderResp: ClashProviderResp?
init(_ data: Any?) {
guard