Skip to content

Commit

Permalink
更改
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Jan 2, 2025
1 parent 2a0678f commit 0453780
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 129 deletions.
33 changes: 6 additions & 27 deletions V2rayU.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
660330042D0D5504009FEB56 /* GRDB in Frameworks */ = {isa = PBXBuildFile; productRef = 660330032D0D5504009FEB56 /* GRDB */; };
660330092D0D5540009FEB56 /* GRDBSQLite in Frameworks */ = {isa = PBXBuildFile; productRef = 660330082D0D5540009FEB56 /* GRDBSQLite */; };
6603305A2D11BF28009FEB56 /* KeyboardShortcuts in Frameworks */ = {isa = PBXBuildFile; productRef = 660330592D11BF28009FEB56 /* KeyboardShortcuts */; };
662CC3382D1AE8CA006E8450 /* Tun2SocksKit in Frameworks */ = {isa = PBXBuildFile; productRef = 662CC3372D1AE8CA006E8450 /* Tun2SocksKit */; };
662CC33A2D1AE8CA006E8450 /* Tun2SocksKitC in Frameworks */ = {isa = PBXBuildFile; productRef = 662CC3392D1AE8CA006E8450 /* Tun2SocksKitC */; };
662CC4012D1BED9B006E8450 /* RouingList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3FE2D1BED9B006E8450 /* RouingList.swift */; };
662CC4022D1BED9B006E8450 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3C02D1BED9B006E8450 /* AppDelegate.swift */; };
662CC4032D1BED9B006E8450 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3C52D1BED9B006E8450 /* String+Extension.swift */; };
Expand Down Expand Up @@ -42,7 +40,7 @@
662CC41E2D1BED9B006E8450 /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3F12D1BED9B006E8450 /* ProfileView.swift */; };
662CC41F2D1BED9B006E8450 /* JsonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3FA2D1BED9B006E8450 /* JsonView.swift */; };
662CC4202D1BED9B006E8450 /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3C82D1BED9B006E8450 /* Util.swift */; };
662CC4212D1BED9B006E8450 /* MenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3FC2D1BED9B006E8450 /* MenuView.swift */; };
662CC4212D1BED9B006E8450 /* AppMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3FC2D1BED9B006E8450 /* AppMenuView.swift */; };
662CC4222D1BED9B006E8450 /* V2rayInbound.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3E42D1BED9B006E8450 /* V2rayInbound.swift */; };
662CC4232D1BED9B006E8450 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3BF2D1BED9B006E8450 /* App.swift */; };
662CC4252D1BED9B006E8450 /* Url+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662CC3C62D1BED9B006E8450 /* Url+Extension.swift */; };
Expand Down Expand Up @@ -138,7 +136,7 @@
662CC3F92D1BED9B006E8450 /* Common.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Common.swift; sourceTree = "<group>"; };
662CC3FA2D1BED9B006E8450 /* JsonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JsonView.swift; sourceTree = "<group>"; };
662CC3FB2D1BED9B006E8450 /* Main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Main.swift; sourceTree = "<group>"; };
662CC3FC2D1BED9B006E8450 /* MenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuView.swift; sourceTree = "<group>"; };
662CC3FC2D1BED9B006E8450 /* AppMenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppMenuView.swift; sourceTree = "<group>"; };
662CC3FD2D1BED9B006E8450 /* Overview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Overview.swift; sourceTree = "<group>"; };
662CC3FE2D1BED9B006E8450 /* RouingList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouingList.swift; sourceTree = "<group>"; };
662CC3FF2D1BED9B006E8450 /* SubList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubList.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -173,10 +171,8 @@
files = (
66A078BC2D008A3700490469 /* SwiftyBeaver in Frameworks */,
6603305A2D11BF28009FEB56 /* KeyboardShortcuts in Frameworks */,
662CC33A2D1AE8CA006E8450 /* Tun2SocksKitC in Frameworks */,
660330042D0D5504009FEB56 /* GRDB in Frameworks */,
66F66F8B2D1E7DE300E77C64 /* Yams in Frameworks */,
662CC3382D1AE8CA006E8450 /* Tun2SocksKit in Frameworks */,
660330092D0D5540009FEB56 /* GRDBSQLite in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -329,7 +325,7 @@
662CC3F92D1BED9B006E8450 /* Common.swift */,
662CC3FA2D1BED9B006E8450 /* JsonView.swift */,
662CC3FB2D1BED9B006E8450 /* Main.swift */,
662CC3FC2D1BED9B006E8450 /* MenuView.swift */,
662CC3FC2D1BED9B006E8450 /* AppMenuView.swift */,
662CC3FD2D1BED9B006E8450 /* Overview.swift */,
662CC3FE2D1BED9B006E8450 /* RouingList.swift */,
662CC3FF2D1BED9B006E8450 /* SubList.swift */,
Expand Down Expand Up @@ -460,7 +456,6 @@
66A078BA2D008A3700490469 /* XCRemoteSwiftPackageReference "SwiftyBeaver" */,
660330022D0D5504009FEB56 /* XCRemoteSwiftPackageReference "GRDB" */,
660330582D11BF28009FEB56 /* XCRemoteSwiftPackageReference "KeyboardShortcuts" */,
662CC3362D1AE8CA006E8450 /* XCRemoteSwiftPackageReference "Tun2SocksKit" */,
662CC4322D1BEE94006E8450 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */,
662CC4332D1BEEDB006E8450 /* XCRemoteSwiftPackageReference "appcenter-sdk-apple" */,
66F66F892D1E7DE300E77C64 /* XCRemoteSwiftPackageReference "Yams" */,
Expand Down Expand Up @@ -535,7 +530,7 @@
662CC41E2D1BED9B006E8450 /* ProfileView.swift in Sources */,
662CC41F2D1BED9B006E8450 /* JsonView.swift in Sources */,
662CC4202D1BED9B006E8450 /* Util.swift in Sources */,
662CC4212D1BED9B006E8450 /* MenuView.swift in Sources */,
662CC4212D1BED9B006E8450 /* AppMenuView.swift in Sources */,
662CC4222D1BED9B006E8450 /* V2rayInbound.swift in Sources */,
662CC4232D1BED9B006E8450 /* App.swift in Sources */,
662CC4252D1BED9B006E8450 /* Url+Extension.swift in Sources */,
Expand Down Expand Up @@ -708,6 +703,7 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = V2rayU/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = V2rayU;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_LSUIElement = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 yanue. All rights reserved.";
Expand Down Expand Up @@ -741,6 +737,7 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = V2rayU/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = V2rayU;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_LSUIElement = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 yanue. All rights reserved.";
Expand Down Expand Up @@ -798,14 +795,6 @@
minimumVersion = 2.2.4;
};
};
662CC3362D1AE8CA006E8450 /* XCRemoteSwiftPackageReference "Tun2SocksKit" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/EbrahimTahernejad/Tun2SocksKit.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 4.7.6;
};
};
662CC4322D1BEE94006E8450 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/firebase/firebase-ios-sdk.git";
Expand Down Expand Up @@ -856,16 +845,6 @@
package = 660330582D11BF28009FEB56 /* XCRemoteSwiftPackageReference "KeyboardShortcuts" */;
productName = KeyboardShortcuts;
};
662CC3372D1AE8CA006E8450 /* Tun2SocksKit */ = {
isa = XCSwiftPackageProductDependency;
package = 662CC3362D1AE8CA006E8450 /* XCRemoteSwiftPackageReference "Tun2SocksKit" */;
productName = Tun2SocksKit;
};
662CC3392D1AE8CA006E8450 /* Tun2SocksKitC */ = {
isa = XCSwiftPackageProductDependency;
package = 662CC3362D1AE8CA006E8450 /* XCRemoteSwiftPackageReference "Tun2SocksKit" */;
productName = Tun2SocksKitC;
};
66A078BB2D008A3700490469 /* SwiftyBeaver */ = {
isa = XCSwiftPackageProductDependency;
package = 66A078BA2D008A3700490469 /* XCRemoteSwiftPackageReference "SwiftyBeaver" */;
Expand Down
163 changes: 140 additions & 23 deletions V2rayU/AppState.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import Combine

enum RunMode: String {
case global
Expand All @@ -24,34 +25,150 @@ enum RunMode: String {
final class AppState: ObservableObject {
static let shared = AppState() // 单例实例

@Published var runMode: RunMode = .off
@Published var pingURL: URL = URL(string: "http://www.gstatic.com/generate_204")!

@Published var runMode: RunMode = .off

@Published var launchAtLogin: Bool = true
@Published var checkForUpdates: Bool = true
@Published var autoUpdateServers: Bool = true
@Published var selectFastestServer: Bool = true

func setRunMode(mode: RunMode) async {
self.runMode = mode
}
@Published var logLevel = "info"
@Published var socksPort = 1080
@Published var socksHost = "127.0.0.1"
@Published var httpPort = 1087
@Published var httpHost = "127.0.0.1"
@Published var enableSocks = true
@Published var enableUdp = false
@Published var enableMux = false
@Published var enableSniffing = false
@Published var mux = 8
@Published var dnsJson = ""

private var cancellables = Set<AnyCancellable>()

private var _runningProfile: String = UserDefaults.get(forKey: .runningProfile) ?? ""
var runningProfile: String {
get {
_runningProfile
}
set {
_runningProfile = newValue
UserDefaults.standard.set(newValue, forKey: "runningProfile")
objectWillChange.send() // 通知更新
init() {
self.runMode = RunMode(rawValue: UserDefaults.get(forKey: .runMode) ?? "off") ?? .off
self.enableMux = UserDefaults.getBool(forKey: .enableMux)
self.enableUdp = UserDefaults.getBool(forKey: .enableUdp)
self.enableSniffing = UserDefaults.getBool(forKey: .enableSniffing)

self.httpPort = UserDefaults.getInt(forKey: .localHttpPort)
if self.httpPort == 0 {
self.httpPort = 1080
}
self.httpHost = UserDefaults.get(forKey: .localHttpHost) ?? "127.0.0.1"
self.socksPort = UserDefaults.getInt(forKey: .localSockPort,defaultValue: 1080)
self.socksHost = UserDefaults.get(forKey: .localSockHost) ?? "127.0.0.1"
self.mux = Int(UserDefaults.get(forKey: .muxConcurrent) ?? "8") ?? 8

self.logLevel = UserDefaults.get(forKey: .v2rayLogLevel) ?? "info"

self.launchAtLogin = UserDefaults.getBool(forKey: .autoLaunch)
self.autoUpdateServers = UserDefaults.getBool(forKey: .autoUpdateServers)
self.checkForUpdates = UserDefaults.getBool(forKey: .autoCheckVersion)
self.selectFastestServer = UserDefaults.getBool(forKey: .autoSelectFastestServer)
print("AppState init", self.launchAtLogin, self.autoUpdateServers, self.checkForUpdates, self.selectFastestServer)

self.setupBindings()
}

private func setupBindings() {
$runMode
.sink { mode in
UserDefaults.set(forKey: .runMode, value: mode.rawValue)
}
.store(in: &cancellables)

private var _runningRouting: String = UserDefaults.get(forKey: .runningRouting) ?? ""
var runningRouting: String {
get {
_runningRouting
}
set {
_runningRouting = newValue
UserDefaults.standard.set(newValue, forKey: "runningRouting")
objectWillChange.send() // 通知更新
}
$launchAtLogin
.sink { launch in
UserDefaults.setBool(forKey: .autoLaunch, value: launch)
}
.store(in: &cancellables)

$autoUpdateServers
.sink { _bool in
UserDefaults.setBool(forKey: .autoUpdateServers, value: _bool)
}
.store(in: &cancellables)

$checkForUpdates
.sink { _bool in
UserDefaults.setBool(forKey: .autoCheckVersion, value: _bool)
}
.store(in: &cancellables)

$selectFastestServer
.sink { _bool in
UserDefaults.setBool(forKey: .autoSelectFastestServer, value: _bool)
}
.store(in: &cancellables)

$logLevel
.sink { level in
UserDefaults.set(forKey: .v2rayLogLevel, value: level)
}
.store(in: &cancellables)

$socksPort
.sink { port in
UserDefaults.setInt(forKey: .localSockPort, value: port)
}
.store(in: &cancellables)

$socksHost
.sink { host in
UserDefaults.set(forKey: .localSockHost, value: host)
}
.store(in: &cancellables)

$httpPort
.sink { port in
UserDefaults.setInt(forKey: .localHttpPort, value: port)
}
.store(in: &cancellables)

$httpHost
.sink { host in
UserDefaults.set(forKey: .localHttpHost, value: host)
}
.store(in: &cancellables)

$enableMux
.sink { enabled in
UserDefaults.setBool(forKey: .enableMux, value: enabled)
}
.store(in: &cancellables)

$enableUdp
.sink { enabled in
UserDefaults.setBool(forKey: .enableUdp, value: enabled)
}
.store(in: &cancellables)

$enableSniffing
.sink { enabled in
UserDefaults.setBool(forKey: .enableSniffing, value: enabled)
}
.store(in: &cancellables)

$mux
.sink { value in
UserDefaults.set(forKey: .muxConcurrent, value: "\(value)")
}
.store(in: &cancellables)

$dnsJson
.sink { json in
UserDefaults.set(forKey: .v2rayDnsJson, value: json)
}
.store(in: &cancellables)
}


func setRunMode(mode: RunMode) async {
self.runMode = mode
}

}
20 changes: 16 additions & 4 deletions V2rayU/Base/UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ extension UserDefaults {
case runningProfile
}

static func del(forKey key: KEY) {
UserDefaults.standard.removeObject(forKey: key.rawValue)
}

static func setInt(forKey key: KEY, value: Int) {
UserDefaults.standard.set(value, forKey: key.rawValue)
}

static func getInt(forKey key: KEY, defaultValue: Int = 0) -> Int {
let num = UserDefaults.standard.integer(forKey: key.rawValue)
if num != 0 {
return num
}
return defaultValue
}

static func setBool(forKey key: KEY, value: Bool) {
UserDefaults.standard.set(value, forKey: key.rawValue)
}
Expand All @@ -75,10 +91,6 @@ extension UserDefaults {
return UserDefaults.standard.string(forKey: key.rawValue)
}

static func del(forKey key: KEY) {
UserDefaults.standard.removeObject(forKey: key.rawValue)
}

static func setArray(forKey key: KEY, value: [String]) {
UserDefaults.standard.set(value, forKey: key.rawValue)
}
Expand Down
25 changes: 12 additions & 13 deletions V2rayU/Service/Tunnel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import Tun2SocksKit

extension V2rayLaunch {
static let tunUpScriptPath: String = AppHomePath + "/tun_up.sh"
Expand Down Expand Up @@ -67,14 +66,14 @@ extension V2rayLaunch {
.replacingOccurrences(of: "{{ socksPort }}", with: "1080") // Example port
print("Tun2Socks config: \(stringConfigContent)")
// Run Tun2Socks
Socks5Tunnel.run(withConfig: .string(content: stringConfigContent)) { code in
print("Tun2Socks exited with code: \(code)")
logStats()
}
// Socks5Tunnel.run(withConfig: .string(content: stringConfigContent)) { code in
// print("Tun2Socks exited with code: \(code)")
// logStats()
// }
}

static func stopTun2Socks() {
Socks5Tunnel.quit()
// Socks5Tunnel.quit()
}

// Create script and set executable permissions
Expand All @@ -92,12 +91,12 @@ extension V2rayLaunch {

// 打印统计信息
static func logStats() {
let stats = Socks5Tunnel.stats
print("Tun2Socks stats: \(stats)")

DispatchQueue.global().asyncAfter(deadline: .now() + 10) {
let updatedStats = Socks5Tunnel.stats
print("Tun2Socks updated stats: \(updatedStats)")
}
// let stats = Socks5Tunnel.stats
// print("Tun2Socks stats: \(stats)")
//
// DispatchQueue.global().asyncAfter(deadline: .now() + 10) {
// let updatedStats = Socks5Tunnel.stats
// print("Tun2Socks updated stats: \(updatedStats)")
// }
}
}
1 change: 0 additions & 1 deletion V2rayU/Service/V2rayLaunch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Cocoa
import SystemConfiguration
import Tun2SocksKit

// 高版本macos执行NSAppleScript会出现授权失败
func executeAppleScriptWithOsascript(script: String) {
Expand Down
Loading

0 comments on commit 0453780

Please sign in to comment.