improve: provider
This commit is contained in:
parent
bd072f7c09
commit
3d129bfa8a
@ -227,7 +227,7 @@ class ApiRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func healthCheck(proxy: ClashProxyName) {
|
static func healthCheck(proxy: ClashProviderName) {
|
||||||
Logger.log("HeathCheck for \(proxy) started")
|
Logger.log("HeathCheck for \(proxy) started")
|
||||||
req("/providers/proxies/\(proxy.encoded)/healthcheck").response { res in
|
req("/providers/proxies/\(proxy.encoded)/healthcheck").response { res in
|
||||||
if res.response?.statusCode == 204 {
|
if res.response?.statusCode == 204 {
|
||||||
|
@ -122,8 +122,6 @@ class MenuItemFactory {
|
|||||||
guard proxyGroup.speedtestAble.count > 0 else { return }
|
guard proxyGroup.speedtestAble.count > 0 else { return }
|
||||||
menus.addItem(NSMenuItem.separator())
|
menus.addItem(NSMenuItem.separator())
|
||||||
let speedTestItem = ProxyGroupSpeedTestMenuItem(group: proxyGroup)
|
let speedTestItem = ProxyGroupSpeedTestMenuItem(group: proxyGroup)
|
||||||
speedTestItem.target = MenuItemFactory.self
|
|
||||||
speedTestItem.action = #selector(actionSpeedTest)
|
|
||||||
menus.addItem(speedTestItem)
|
menus.addItem(speedTestItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,21 +155,6 @@ class MenuItemFactory {
|
|||||||
return menu
|
return menu
|
||||||
}
|
}
|
||||||
|
|
||||||
static func generateProviderMenuItems(_ provider: ClashProvider) -> NSMenuItem? {
|
|
||||||
let menu = NSMenuItem(title: provider.name, action: nil, keyEquivalent: "")
|
|
||||||
let submenu = NSMenu(title: provider.name)
|
|
||||||
|
|
||||||
for proxy in provider.proxies {
|
|
||||||
let proxyMenuItem = NSMenuItem(title: proxy.name, action: #selector(empty), keyEquivalent: "")
|
|
||||||
proxyMenuItem.target = MenuItemFactory.self
|
|
||||||
proxyMenuItem.submenu = generateHistoryMenu(proxy)
|
|
||||||
submenu.addItem(proxyMenuItem)
|
|
||||||
}
|
|
||||||
|
|
||||||
menu.submenu = submenu
|
|
||||||
return menu
|
|
||||||
}
|
|
||||||
|
|
||||||
static func generateSwitchConfigMenuItems() -> [NSMenuItem] {
|
static func generateSwitchConfigMenuItems() -> [NSMenuItem] {
|
||||||
var items = [NSMenuItem]()
|
var items = [NSMenuItem]()
|
||||||
for config in ConfigManager.getConfigFilesList() {
|
for config in ConfigManager.getConfigFilesList() {
|
||||||
@ -217,10 +200,5 @@ extension MenuItemFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc static func actionSpeedTest(sender: ProxyGroupSpeedTestMenuItem) {
|
|
||||||
guard sender.testType == .reTest else { return }
|
|
||||||
ApiRequest.healthCheck(proxy: sender.proxyGroup.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc static func empty() {}
|
@objc static func empty() {}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ class ClashProvider: Codable {
|
|||||||
case Unknown
|
case Unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
let name: String
|
let name: ClashProviderName
|
||||||
let proxies: [ClashProxy]
|
let proxies: [ClashProxy]
|
||||||
let type: ProviderType
|
let type: ProviderType
|
||||||
let vehicleType: ProviderVehicleType
|
let vehicleType: ProviderVehicleType
|
||||||
|
@ -40,6 +40,7 @@ enum ClashProxyType: String, Codable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
typealias ClashProxyName = String
|
typealias ClashProxyName = String
|
||||||
|
typealias ClashProviderName = String
|
||||||
|
|
||||||
class ClashProxySpeedHistory: Codable {
|
class ClashProxySpeedHistory: Codable {
|
||||||
let time: Date
|
let time: Date
|
||||||
@ -72,16 +73,23 @@ class ClashProxy: Codable {
|
|||||||
let all: [ClashProxyName]?
|
let all: [ClashProxyName]?
|
||||||
let history: [ClashProxySpeedHistory]
|
let history: [ClashProxySpeedHistory]
|
||||||
let now: ClashProxyName?
|
let now: ClashProxyName?
|
||||||
var providerProxy = false
|
|
||||||
weak var enclosingResp: ClashProxyResp? = nil
|
weak var enclosingResp: ClashProxyResp? = nil
|
||||||
|
weak var enclosingProvider: ClashProvider? = nil
|
||||||
|
|
||||||
lazy var speedtestAble: [ClashProxyName] = {
|
enum SpeedtestAbleItem {
|
||||||
guard let resp = enclosingResp, let allProxys = all else { return all ?? [] }
|
case proxy(name: ClashProxyName)
|
||||||
var proxys = [ClashProxyName]()
|
case provider(name: ClashProxyName, provider: ClashProviderName)
|
||||||
|
}
|
||||||
|
|
||||||
|
lazy var speedtestAble: [SpeedtestAbleItem] = {
|
||||||
|
guard let resp = enclosingResp, let allProxys = all else { return [] }
|
||||||
|
var proxys = [SpeedtestAbleItem]()
|
||||||
for proxy in allProxys {
|
for proxy in allProxys {
|
||||||
if let p = resp.proxiesMap[proxy], !ClashProxyType.isProxyGroup(p) {
|
if let p = resp.proxiesMap[proxy], !ClashProxyType.isProxyGroup(p) {
|
||||||
if !p.providerProxy {
|
if let provider = p.enclosingProvider {
|
||||||
proxys.append(proxy)
|
proxys.append(.provider(name: p.name, provider: provider.name))
|
||||||
|
} else {
|
||||||
|
proxys.append(.proxy(name: p.name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,6 +118,8 @@ class ClashProxyResp {
|
|||||||
let proxies: [ClashProxy]
|
let proxies: [ClashProxy]
|
||||||
var proxiesMap: [ClashProxyName: ClashProxy]
|
var proxiesMap: [ClashProxyName: ClashProxy]
|
||||||
|
|
||||||
|
private var enclosingProviderResp: ClashProviderResp?
|
||||||
|
|
||||||
init(_ data: Any?) {
|
init(_ data: Any?) {
|
||||||
guard
|
guard
|
||||||
let data = data as? [String: [String: Any]],
|
let data = data as? [String: [String: Any]],
|
||||||
@ -145,9 +155,10 @@ class ClashProxyResp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateProvider(_ providerResp: ClashProviderResp) {
|
func updateProvider(_ providerResp: ClashProviderResp) {
|
||||||
|
enclosingProviderResp = providerResp
|
||||||
for provider in providerResp.providers.values {
|
for provider in providerResp.providers.values {
|
||||||
for proxy in provider.proxies {
|
for proxy in provider.proxies {
|
||||||
proxy.providerProxy = true
|
proxy.enclosingProvider = provider
|
||||||
proxiesMap[proxy.name] = proxy
|
proxiesMap[proxy.name] = proxy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ class MenuItemBaseView: NSView {
|
|||||||
private let handleClick: Bool
|
private let handleClick: Bool
|
||||||
private let autolayout: Bool
|
private let autolayout: Bool
|
||||||
|
|
||||||
|
|
||||||
// MARK: Public
|
// MARK: Public
|
||||||
|
|
||||||
var isHighlighted: Bool {
|
var isHighlighted: Bool {
|
||||||
@ -79,7 +78,6 @@ class MenuItemBaseView: NSView {
|
|||||||
effectView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
effectView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// MARK: Override
|
// MARK: Override
|
||||||
|
|
||||||
override func draw(_ dirtyRect: NSRect) {
|
override func draw(_ dirtyRect: NSRect) {
|
||||||
|
@ -25,6 +25,8 @@ class ProxyGroupSpeedTestMenuItem: NSMenuItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
super.init(title: NSLocalizedString("Benchmark", comment: ""), action: nil, keyEquivalent: "")
|
super.init(title: NSLocalizedString("Benchmark", comment: ""), action: nil, keyEquivalent: "")
|
||||||
|
target = self
|
||||||
|
action = #selector(healthCheck)
|
||||||
|
|
||||||
switch testType {
|
switch testType {
|
||||||
case .benchmark:
|
case .benchmark:
|
||||||
@ -39,6 +41,12 @@ class ProxyGroupSpeedTestMenuItem: NSMenuItem {
|
|||||||
required init(coder: NSCoder) {
|
required init(coder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func healthCheck() {
|
||||||
|
guard testType == .reTest else { return }
|
||||||
|
ApiRequest.healthCheck(proxy: proxyGroup.name)
|
||||||
|
menu?.cancelTracking()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
|
fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
|
||||||
@ -71,10 +79,19 @@ fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
|
|||||||
guard let group = (enclosingMenuItem as? ProxyGroupSpeedTestMenuItem)?.proxyGroup
|
guard let group = (enclosingMenuItem as? ProxyGroupSpeedTestMenuItem)?.proxyGroup
|
||||||
else { return }
|
else { return }
|
||||||
let testGroup = DispatchGroup()
|
let testGroup = DispatchGroup()
|
||||||
label.stringValue = NSLocalizedString("Testing", comment: "")
|
|
||||||
enclosingMenuItem?.isEnabled = false
|
var proxies = [ClashProxyName]()
|
||||||
setNeedsDisplay(bounds)
|
var providers = Set<ClashProviderName>()
|
||||||
for proxyName in group.speedtestAble {
|
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 {
|
||||||
testGroup.enter()
|
testGroup.enter()
|
||||||
ApiRequest.getProxyDelay(proxyName: proxyName) { delay in
|
ApiRequest.getProxyDelay(proxyName: proxyName) { delay in
|
||||||
let delayStr = delay == 0 ? "fail" : "\(delay) ms"
|
let delayStr = delay == 0 ? "fail" : "\(delay) ms"
|
||||||
@ -85,6 +102,16 @@ fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
testGroup.notify(queue: .main) {
|
||||||
[weak self] in
|
[weak self] in
|
||||||
guard let self = self, let menu = self.enclosingMenuItem else { return }
|
guard let self = self, let menu = self.enclosingMenuItem else { return }
|
||||||
@ -93,6 +120,7 @@ fileprivate class ProxyGroupSpeedTestMenuItemView: MenuItemBaseView {
|
|||||||
self.setNeedsDisplay()
|
self.setNeedsDisplay()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override func draw(_ dirtyRect: NSRect) {
|
override func draw(_ dirtyRect: NSRect) {
|
||||||
super.draw(dirtyRect)
|
super.draw(dirtyRect)
|
||||||
|
Loading…
Reference in New Issue
Block a user