2019-10-16 22:46:03 +08:00
|
|
|
//
|
|
|
|
// ProxyGroupSpeedTestMenuItem.swift
|
|
|
|
// ClashX
|
|
|
|
//
|
|
|
|
// Created by yicheng on 2019/10/15.
|
|
|
|
// Copyright © 2019 west2online. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2019-10-31 21:03:22 +08:00
|
|
|
import Carbon
|
2019-10-16 22:46:03 +08:00
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class ProxyGroupSpeedTestMenuItem: NSMenuItem {
|
2019-12-11 22:04:53 +08:00
|
|
|
let proxyGroup: ClashProxy
|
|
|
|
let testType: TestType
|
2019-10-31 15:38:18 +08:00
|
|
|
|
2019-10-20 13:40:50 +08:00
|
|
|
init(group: ClashProxy) {
|
2019-10-16 22:46:03 +08:00
|
|
|
proxyGroup = group
|
2019-10-28 15:02:34 +08:00
|
|
|
switch group.type {
|
|
|
|
case .urltest, .fallback:
|
2019-10-31 15:38:18 +08:00
|
|
|
testType = .reTest
|
2019-10-28 15:02:34 +08:00
|
|
|
case .select:
|
2019-10-31 15:38:18 +08:00
|
|
|
testType = .benchmark
|
2019-10-28 15:02:34 +08:00
|
|
|
default:
|
2019-10-31 15:38:18 +08:00
|
|
|
testType = .unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
super.init(title: NSLocalizedString("Benchmark", comment: ""), action: nil, keyEquivalent: "")
|
2019-12-29 13:50:10 +08:00
|
|
|
target = self
|
|
|
|
action = #selector(healthCheck)
|
2019-10-31 15:38:18 +08:00
|
|
|
|
|
|
|
switch testType {
|
|
|
|
case .benchmark:
|
|
|
|
view = ProxyGroupSpeedTestMenuItemView(testType.title)
|
|
|
|
case .reTest:
|
|
|
|
title = testType.title
|
|
|
|
case .unknown:
|
2019-10-28 15:02:34 +08:00
|
|
|
assertionFailure()
|
|
|
|
}
|
2019-10-16 22:46:03 +08:00
|
|
|
}
|
2019-10-20 13:40:50 +08:00
|
|
|
|
2019-10-16 22:46:03 +08:00
|
|
|
required init(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2019-12-29 13:50:10 +08:00
|
|
|
|
|
|
|
@objc func healthCheck() {
|
|
|
|
guard testType == .reTest else { return }
|
|
|
|
ApiRequest.healthCheck(proxy: proxyGroup.name)
|
|
|
|
menu?.cancelTracking()
|
|
|
|
}
|
2019-10-16 22:46:03 +08:00
|
|
|
}
|
2019-10-17 17:07:47 +08:00
|
|
|
|
2020-02-22 18:31:13 +08:00
|
|
|
extension ProxyGroupSpeedTestMenuItem: ProxyGroupMenuHighlightDelegate {
|
|
|
|
func highlight(item: NSMenuItem?) {
|
|
|
|
(view as? ProxyGroupSpeedTestMenuItemView)?.isHighlighted = item == self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:04:31 +08:00
|
|
|
fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
|
2019-10-31 21:03:22 +08:00
|
|
|
private let label: NSTextField
|
2019-10-28 15:02:34 +08:00
|
|
|
|
2019-10-31 15:38:18 +08:00
|
|
|
init(_ title: String) {
|
|
|
|
label = NSTextField(labelWithString: title)
|
2019-11-02 13:27:57 +08:00
|
|
|
label.font = type(of: self).labelFont
|
2019-10-19 11:44:39 +08:00
|
|
|
label.sizeToFit()
|
2019-11-02 11:55:18 +08:00
|
|
|
let rect = NSRect(x: 0, y: 0, width: label.bounds.width + 40, height: 20)
|
2020-02-22 18:31:13 +08:00
|
|
|
super.init(frame: rect, autolayout: false)
|
2019-10-17 17:07:47 +08:00
|
|
|
addSubview(label)
|
2019-10-19 11:44:39 +08:00
|
|
|
label.frame = NSRect(x: 20, y: 0, width: label.bounds.width, height: 20)
|
2019-11-02 00:04:31 +08:00
|
|
|
label.textColor = NSColor.labelColor
|
2019-10-17 17:07:47 +08:00
|
|
|
}
|
2019-10-20 13:40:50 +08:00
|
|
|
|
2019-10-17 17:07:47 +08:00
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2019-11-02 11:55:18 +08:00
|
|
|
|
2020-02-22 18:31:13 +08:00
|
|
|
override var cells: [NSCell?] {
|
|
|
|
return [label.cell]
|
|
|
|
}
|
|
|
|
|
2019-11-02 11:55:18 +08:00
|
|
|
override var labels: [NSTextField] {
|
|
|
|
return [label]
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:04:31 +08:00
|
|
|
override func didClickView() {
|
|
|
|
startBenchmark()
|
|
|
|
}
|
2019-10-20 13:40:50 +08:00
|
|
|
|
2019-10-17 17:07:47 +08:00
|
|
|
private func startBenchmark() {
|
2019-10-31 21:03:22 +08:00
|
|
|
guard let group = (enclosingMenuItem as? ProxyGroupSpeedTestMenuItem)?.proxyGroup
|
|
|
|
else { return }
|
2019-10-20 12:58:14 +08:00
|
|
|
let testGroup = DispatchGroup()
|
2019-12-29 13:50:10 +08:00
|
|
|
|
|
|
|
var proxies = [ClashProxyName]()
|
|
|
|
var providers = Set<ClashProviderName>()
|
|
|
|
for testable in group.speedtestAble {
|
|
|
|
switch testable {
|
|
|
|
case let .provider(_, provider):
|
|
|
|
providers.insert(provider)
|
|
|
|
case let .proxy(name):
|
|
|
|
proxies.append(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for proxyName in proxies {
|
2019-10-20 12:58:14 +08:00
|
|
|
testGroup.enter()
|
2019-10-17 17:07:47 +08:00
|
|
|
ApiRequest.getProxyDelay(proxyName: proxyName) { delay in
|
2020-02-24 22:20:34 +08:00
|
|
|
let delayStr = delay == 0 ? NSLocalizedString("fail", comment: "") : "\(delay) ms"
|
2019-10-17 17:07:47 +08:00
|
|
|
NotificationCenter.default.post(name: kSpeedTestFinishForProxy,
|
|
|
|
object: nil,
|
2019-10-20 13:40:50 +08:00
|
|
|
userInfo: ["proxyName": proxyName, "delay": delayStr])
|
2019-10-20 12:58:14 +08:00
|
|
|
testGroup.leave()
|
2019-10-17 17:07:47 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 13:40:50 +08:00
|
|
|
|
2019-12-29 13:50:10 +08:00
|
|
|
if providers.count > 0 {
|
|
|
|
for provider in providers {
|
|
|
|
ApiRequest.healthCheck(proxy: provider)
|
|
|
|
}
|
|
|
|
enclosingMenuItem?.menu?.cancelTracking()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
label.stringValue = NSLocalizedString("Testing", comment: "")
|
|
|
|
enclosingMenuItem?.isEnabled = false
|
|
|
|
setNeedsDisplay()
|
|
|
|
testGroup.notify(queue: .main) {
|
|
|
|
[weak self] in
|
|
|
|
guard let self = self, let menu = self.enclosingMenuItem else { return }
|
|
|
|
self.label.stringValue = menu.title
|
|
|
|
menu.isEnabled = true
|
|
|
|
self.setNeedsDisplay()
|
|
|
|
}
|
2019-10-20 12:58:14 +08:00
|
|
|
}
|
2019-10-17 17:07:47 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-28 15:02:34 +08:00
|
|
|
|
2019-10-31 15:38:18 +08:00
|
|
|
extension ProxyGroupSpeedTestMenuItem {
|
2019-10-28 15:02:34 +08:00
|
|
|
enum TestType {
|
|
|
|
case benchmark
|
|
|
|
case reTest
|
2019-10-31 15:38:18 +08:00
|
|
|
case unknown
|
2019-10-28 15:02:34 +08:00
|
|
|
|
|
|
|
var title: String {
|
|
|
|
switch self {
|
|
|
|
case .benchmark: return NSLocalizedString("Benchmark", comment: "")
|
|
|
|
case .reTest: return NSLocalizedString("ReTest", comment: "")
|
2019-10-31 15:38:18 +08:00
|
|
|
case .unknown: return ""
|
2019-10-28 15:02:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|