Skip to content

Commit

Permalink
更改ping,routing,订阅可编辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Nov 27, 2023
1 parent 864fe80 commit 68a3211
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 95 deletions.
8 changes: 4 additions & 4 deletions V2rayU.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 4.0.3;
CURRENT_PROJECT_VERSION = 4.1.0;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = RJYEH6TCJD;
ENABLE_ONLY_ACTIVE_RESOURCES = YES;
Expand All @@ -847,7 +847,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.14;
MARKETING_VERSION = 4.0.0;
MARKETING_VERSION = 4.1.0;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = net.yanue.V2rayU;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -869,7 +869,7 @@
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 4.0.3;
CURRENT_PROJECT_VERSION = 4.1.0;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = RJYEH6TCJD;
ENABLE_ONLY_ACTIVE_RESOURCES = YES;
Expand All @@ -880,7 +880,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.14;
MARKETING_VERSION = 4.0.0;
MARKETING_VERSION = 4.1.0;
ONLY_ACTIVE_ARCH = NO;
PRODUCT_BUNDLE_IDENTIFIER = net.yanue.V2rayU;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
2 changes: 1 addition & 1 deletion V2rayU/Base.lproj/ConfigWindow.xib
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,7 @@ Gw
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="VE0-0F-NoI">
<rect key="frame" x="17" y="211" width="150" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="subscribe" bezelStyle="rounded" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="H9t-q7-Sop">
<buttonCell key="cell" type="push" title="subscription" bezelStyle="rounded" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="H9t-q7-Sop">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<string key="keyEquivalent">s</string>
Expand Down
14 changes: 7 additions & 7 deletions V2rayU/Base.lproj/PreferenceSubscription.xib
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn identifier="remarkCell" editable="NO" width="99.5" minWidth="40" maxWidth="1000" id="PPw-B0-d3E">
<tableColumn identifier="remarkCell" editable="NO" width="146.5" minWidth="40" maxWidth="1000" id="PPw-B0-d3E">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Remark">
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
Expand All @@ -107,11 +107,11 @@
</textFieldCell>
<prototypeCellViews>
<tableCellView id="sbc-Q5-Cgb">
<rect key="frame" x="1" y="1" width="104" height="20"/>
<rect key="frame" x="1" y="1" width="151.5" height="20"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="uV2-VE-pu4">
<rect key="frame" x="0.0" y="3" width="104" height="17"/>
<rect key="frame" x="0.0" y="3" width="152" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" identifier="firstName" title="Table View Cell" id="2SR-49-K11">
<font key="font" metaFont="system"/>
Expand All @@ -127,7 +127,7 @@
</tableCellView>
</prototypeCellViews>
</tableColumn>
<tableColumn identifier="urlCell" editable="NO" width="312.5" minWidth="40" maxWidth="1000" id="kc4-bL-WBd" userLabel="Subscription Url">
<tableColumn identifier="urlCell" editable="NO" width="494.5" minWidth="40" maxWidth="1000" id="kc4-bL-WBd" userLabel="Subscription Url">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Subscription Url ">
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
Expand All @@ -139,11 +139,11 @@
</textFieldCell>
<prototypeCellViews>
<tableCellView id="FLk-I9-55S">
<rect key="frame" x="107" y="1" width="318" height="16"/>
<rect key="frame" x="154.5" y="1" width="499.5" height="16"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="9CU-oY-puz">
<rect key="frame" x="0.0" y="0.0" width="318" height="16"/>
<rect key="frame" x="0.0" y="0.0" width="499" height="16"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" alignment="justified" identifier="lastName" title="Table View Cell" id="OER-yL-Osv">
<font key="font" metaFont="system"/>
Expand All @@ -167,7 +167,7 @@
</subviews>
</clipView>
<scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="XOO-5h-H6p">
<rect key="frame" x="1" y="202" width="418" height="16"/>
<rect key="frame" x="1" y="202" width="658" height="16"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="DDy-ic-Fva">
Expand Down
55 changes: 37 additions & 18 deletions V2rayU/Ping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ let pingURL = URL(string: "http://www.gstatic.com/generate_204")!

class PingSpeed: NSObject {
let lock = NSLock()
let semaphore = DispatchSemaphore(value: 20) // work pool
let group = DispatchGroup()
let semaphore = DispatchSemaphore(value: 30) // work pool
var group = DispatchGroup()

func pingAll() {
NSLog("ping start")
Expand All @@ -32,11 +32,12 @@ class PingSpeed: NSObject {
V2rayLaunch.checkV2rayCore()
// in ping
inPing = true

killAllPing()

let itemList = V2rayServer().all()
let itemList = V2rayServer.all()
if itemList.count == 0 {
NSLog("no items")
inPing = false
return
}
Expand All @@ -51,17 +52,18 @@ class PingSpeed: NSObject {
menuController.setStatusMenuTip(pingTip: pingTip)
}
let thread = Thread{
self.runTask()
self.runTask(items: itemList)
}
thread.start()
}

func runTask() {
func runTask(items: [V2rayItem]) {
self.group = DispatchGroup()
let pingQueue = DispatchQueue(label: "pingQueue", qos: .background, attributes: .concurrent)
for item in itemList {
for item in items {
self.group.enter() // 进入DispatchGroup
pingQueue.async {
// 信号量,限制最大并发
self.semaphore.wait()
// run ping by async queue
self.pingEachServer(item: item)
Expand All @@ -70,10 +72,20 @@ class PingSpeed: NSObject {
self.group.wait() // 等待所有任务完成
print("All tasks finished")
inPing = false
let langStr = Locale.current.languageCode
var pingTip: String = ""
if langStr == "en" {
pingTip = "Ping Speed"
} else {
pingTip = "Ping"
}
DispatchQueue.main.async {
menuController.setStatusMenuTip(pingTip: pingTip)
}
DispatchQueue.main.async {
menuController.showServers()
}
// kill all ping
// kill
killAllPing()
}

Expand Down Expand Up @@ -113,27 +125,34 @@ class PingServer: NSObject, URLSessionDataDelegate {

NSLog("doPing: \(item.name)-\(item.remark) - \(_bindPort)")
bindPort = _bindPort
jsonFile = AppHomePath + "/.\(item.name).json"

let _json_file = ".\(item.name).json"
jsonFile = AppHomePath + "/" + _json_file

// create v2ray config file
createV2rayJsonFileForPing()

// Create a Process instance with async launch
// can't use `/bin/bash -c cmd...` otherwize v2ray process will become a ghost process
process.launchPath = v2rayCoreFile
process.arguments = ["run", "-config", jsonFile] // 兼容 v2ray | xray
// use `/bin/bash -c cmd ...` and need kill subprocess
let pingCmd = "cd \(AppHomePath) && ./v2ray-core/v2ray run -config \(_json_file)"
NSLog("pingCmd: \(pingCmd)")
process.launchPath = "/bin/bash"
process.arguments = ["-c", pingCmd]
// process.standardError = nil
// process.standardOutput = nil
process.terminationHandler = { _process in
// 结束子进程
if _process.terminationStatus != EXIT_SUCCESS {
NSLog("process is not kill \(_process.description) - \(_process.processIdentifier) - \(_process.terminationStatus)")
NSLog("process is not kill \(_bindPort) - \(_process.description) - \(_process.processIdentifier) - \(_process.terminationStatus)")
_process.terminate()
_process.waitUntilExit()
}
}
// async launch and can't waitUntilExit
process.launch()

// sleep for wait v2ray process instanse
usleep(useconds_t(1 * second))
usleep(useconds_t(2 * second))

// url request
let session = URLSession(configuration: getProxyUrlSessionConfigure(httpProxyPort: bindPort), delegate: self, delegateQueue: nil)
Expand Down Expand Up @@ -225,10 +244,9 @@ class PingCurrent: NSObject, URLSessionDataDelegate {
func doPing() {
inPingCurrent = true
NSLog("PingCurrent start: try=\(tryPing),item=\(item.remark)")
usleep(useconds_t(1 * second))
// set URLSessionDataDelegate
let config = getProxyUrlSessionConfigure()
config.timeoutIntervalForRequest = 2
config.timeoutIntervalForRequest = 3
// url request
let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: URLRequest(url: pingURL))
Expand Down Expand Up @@ -262,6 +280,7 @@ class PingCurrent: NSObject, URLSessionDataDelegate {
// ping current fail
if item.speed == "-1ms" {
if tryPing < 3 {
usleep(useconds_t(3 * second))
doPing()
} else {
// choose next server
Expand All @@ -281,7 +300,7 @@ class PingCurrent: NSObject, URLSessionDataDelegate {
return
}
do {
let serverList = V2rayServer().all()
let serverList = V2rayServer.all()
if serverList.count > 1 {
var pingedSvrs: Dictionary = [String: Int]()
var allSvrs = [String]()
Expand Down Expand Up @@ -322,4 +341,4 @@ class PingCurrent: NSObject, URLSessionDataDelegate {
inPingCurrent = false
}
}
}
}
9 changes: 8 additions & 1 deletion V2rayU/Preference/PreferenceSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ extension PreferenceSubscribeViewController: NSTableViewDataSource {
extension PreferenceSubscribeViewController: NSTableViewDelegate {
// For NSTableViewDelegate
func tableViewSelectionDidChange(_ notification: Notification) {
print("selected row", self.tableView.selectedRow)
let idx = self.tableView.selectedRow
if idx > -1 {
if let item = V2raySubscription.loadSubItem(idx: idx) {
// choose
self.remark.stringValue = item.remark
self.url.stringValue = item.url
}
}
}
}
8 changes: 4 additions & 4 deletions V2rayU/V2rayLaunch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class V2rayLaunch: NSObject {

static func runAtStart(){
// clear not available
V2rayServer().clearItems()
V2rayServer.clearItems()

// install before launch
V2rayLaunch.install()
Expand Down Expand Up @@ -222,7 +222,7 @@ class V2rayLaunch: NSObject {
// start v2ray core
static func startV2rayCore() {
NSLog("start v2ray-core begin")
guard let v2ray = V2rayServer().loadSelectedItem() else {
guard let v2ray = V2rayServer.loadSelectedItem() else {
noticeTip(title: "start v2ray fail", informativeText: "v2ray config not found")
menuController.setStatusOff()
return
Expand Down Expand Up @@ -413,7 +413,7 @@ class V2rayLaunch: NSObject {
let jsonFilePath = URL.init(fileURLWithPath: JsonConfigFilePath)

// delete before config
if FileManager.default.fileExists(atPath: jsonFile) {
if FileManager.default.fileExists(atPath: JsonConfigFilePath) {
try? FileManager.default.removeItem(at: jsonFilePath)
}

Expand Down Expand Up @@ -476,4 +476,4 @@ func checkV2rayUVersion() {
}
}
}
}
}
27 changes: 27 additions & 0 deletions V2rayU/V2rayServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,34 @@ class V2rayServer: NSObject {
}
print("loadConfig", self.v2rayItemList.count)
}

static func all() -> [V2rayItem] {
// static reset
var items : [V2rayItem] = []

// load name list from UserDefaults
var list = UserDefaults.getArray(forKey: .v2rayServerList)

if list == nil {
list = ["default"]
// store default
let model = V2rayItem(name: self.defaultV2rayName, remark: "default", isValid: false)
model.store()
}

// load each V2rayItem
for item in list! {
guard let v2ray = V2rayItem.load(name: item) else {
// delete from UserDefaults
V2rayItem.remove(name: item)
continue
}
// append
items.append(v2ray)
}
return items
}

// clear old not valid or exist item
static func clearItems() {
// remove all
Expand Down
Loading

0 comments on commit 68a3211

Please sign in to comment.