diff --git a/V2rayU/ConfigWindow.swift b/V2rayU/ConfigWindow.swift index 39f1981..109eec1 100644 --- a/V2rayU/ConfigWindow.swift +++ b/V2rayU/ConfigWindow.swift @@ -182,6 +182,8 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSTabViewDel self.serversTableView.reloadData() // selected current row self.serversTableView.selectRowIndexes(NSIndexSet(index: V2rayServer.count() - 1) as IndexSet, byExtendingSelection: false) + + menuController.showServers() } break diff --git a/V2rayU/MainMenu.swift b/V2rayU/MainMenu.swift index 77828ca..6c85eba 100644 --- a/V2rayU/MainMenu.swift +++ b/V2rayU/MainMenu.swift @@ -36,6 +36,8 @@ class MenuController: NSObject, NSMenuDelegate { // hide new version newVersionItem.isHidden = true + + showRouting() // windowWillClose Notification NotificationCenter.default.addObserver(self, selector: #selector(configWindowWillClose(notification:)), name: NSWindow.willCloseNotification, object: nil) @@ -130,13 +132,12 @@ class MenuController: NSObject, NSMenuDelegate { configWindow.reloadData() } } - showRouting() } func showRouting() { DispatchQueue.global().async { let rules = V2rayRoutings.all() - print("showRouting", rules) +// print("showRouting", rules) let sumMenus = NSMenu() // add Routing... menu and click event is goRouting let routingMenuItem = NSMenuItem() @@ -165,7 +166,7 @@ class MenuController: NSObject, NSMenuDelegate { } sumMenus.addItem(menuItem) } - print("showRouting", sumMenus) +// print("showRouting", sumMenus) // 假设 routingMenu 已经连接并且有一个子菜单 DispatchQueue.main.async { self.routingMenu.submenu = sumMenus diff --git a/V2rayU/Ping.swift b/V2rayU/Ping.swift index 6398aea..aa55c29 100644 --- a/V2rayU/Ping.swift +++ b/V2rayU/Ping.swift @@ -6,6 +6,8 @@ // Copyright © 2019 yanue. All rights reserved. // +import Foundation + // ping and choose fastest v2ray var inPing = false var inPingCurrent = false @@ -14,50 +16,51 @@ var ping = PingSpeed() let second: Double = 1000000 let pingURL = URL(string: "http://www.gstatic.com/generate_204")! +func getRandomPort() -> UInt16 { + return UInt16.random(in: 49152...65535) +} + class PingSpeed: NSObject { let maxConcurrentTasks = 30 func pingAll() { NSLog("ping start") - if inPing { + guard !inPing else { NSLog("ping inPing") return } - - // make sure core file + V2rayLaunch.checkV2rayCore() - // in ping + inPing = true - killAllPing() - + let itemList = V2rayServer.all() - if itemList.count == 0 { + guard !itemList.isEmpty else { NSLog("no items") inPing = false return } - var pingTip: String = "" - if isMainland { - pingTip = "Ping Speed - In Testing " - } else { - pingTip = "Ping Speed - 测试中" - } + + let pingTip = isMainland ? "Ping Speed - In Testing" : "Ping Speed - 测试中" menuController.setStatusMenuTip(pingTip: pingTip) + Task { do { try await pingTaskGroup(items: itemList) - } catch let error { + } catch { NSLog("pingTaskGroup error: \(error)") } } + NSLog("pingAll") } - - func pingTaskGroup(items: [V2rayItem]) async throws { + + func pingTaskGroup(items: [V2rayItem]) async throws { let taskChunks = stride(from: 0, to: items.count, by: maxConcurrentTasks).map { Array(items[$0.. 1 { - var pingedSvrs: Dictionary = [String: Int]() - var allSvrs = [String]() - for svr in serverList { - if svr.name == item.name { - continue - } - allSvrs.append(svr.name) - if svr.isValid && svr.speed != "-1ms" { - var speed = svr.speed - // suffix substring or not - let suffixStr = "ms" - if speed.hasSuffix(suffixStr) { - // Find the index to stop deleting at - let LIndex = speed.index(speed.endIndex, offsetBy: -suffixStr.count) - // Removing the suffix substring - speed = String(speed[.. 0 { - // sort by ping seed - let sortPing = pingedSvrs.sorted(by: { $0.value < $1.value }) - newSvrName = sortPing[0].key - } else { - // 修复越界问题 - let idx = Int.random(in: 0 ... allSvrs.count-1) - newSvrName = allSvrs[idx] + + let serverList = V2rayServer.all() + guard serverList.count > 1 else { + inPingProcess = false + return + } + + var pingedSvrs = [String: Int]() + var allSvrs = [String]() + + for svr in serverList where svr.name != item.name { + allSvrs.append(svr.name) + if svr.isValid && svr.speed != "-1ms" { + let speed = svr.speed.replacingOccurrences(of: "ms", with: "") + if let speedInt = Int(speed) { + pingedSvrs[svr.name] = speedInt } - NSLog(" - choose new server: \(newSvrName)") - // set current - UserDefaults.set(forKey: .v2rayCurrentServerName, value: newSvrName) - // restart - V2rayLaunch.restartV2ray() } - inPingCurrent = false } + + let newSvrName: String + if let fastestSvr = pingedSvrs.sorted(by: { $0.value < $1.value }).first { + newSvrName = fastestSvr.key + } else if let randomSvr = allSvrs.randomElement() { + newSvrName = randomSvr + } else { + inPingProcess = false + return + } + + NSLog(" - choose new server: \(newSvrName)") + UserDefaults.set(forKey: .v2rayCurrentServerName, value: newSvrName) + V2rayLaunch.restartV2ray() + inPingProcess = false } } diff --git a/V2rayU/V2rayLaunch.swift b/V2rayU/V2rayLaunch.swift index dab6418..811b462 100644 --- a/V2rayU/V2rayLaunch.swift +++ b/V2rayU/V2rayLaunch.swift @@ -278,7 +278,7 @@ class V2rayLaunch: NSObject { menuController.showServers() // ping current - PingCurrent(item: v2ray).doPing() + PingCurrent.shared.startPing(with: v2ray) } static func stopV2rayCore() { diff --git a/V2rayU/V2rayRouting.swift b/V2rayU/V2rayRouting.swift index 445c3e0..39e6f24 100644 --- a/V2rayU/V2rayRouting.swift +++ b/V2rayU/V2rayRouting.swift @@ -437,7 +437,7 @@ class RoutingItem: NSObject, NSCoding { } } - print("ips", ips, "domains", domains) +// print("ips", ips, "domains", domains) return (domains, ips) }