Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iOS Crash Due to Outdated change_config_options Method Name #1439

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions ios/Runner/Handlers/MethodHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ import Combine
import Libcore

public class MethodHandler: NSObject, FlutterPlugin {

private var cancelBag: Set<AnyCancellable> = []

public static let name = "\(Bundle.main.serviceIdentifier)/method"

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: Self.name, binaryMessenger: registrar.messenger())
let instance = MethodHandler()
registrar.addMethodCallDelegate(instance, channel: channel)
instance.channel = channel
}

private var channel: FlutterMethodChannel?

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
@Sendable func mainResult(_ res: Any?) async -> Void {
await MainActor.run {
result(res)
}
}

switch call.method {
case "parse_config":
guard
Expand All @@ -49,7 +49,7 @@ public class MethodHandler: NSObject, FlutterPlugin {
return
}
result("")
case "change_config_options":
case "change_hiddify_options":
guard let options = call.arguments as? String else {
result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
return
Expand Down Expand Up @@ -186,7 +186,7 @@ public class MethodHandler: NSObject, FlutterPlugin {
result(FlutterMethodNotImplemented)
}
}

private func waitForStop() -> Future<Void, Never> {
return Future { promise in
var cancellable: AnyCancellable? = nil
Expand Down
40 changes: 20 additions & 20 deletions ios/Runner/VPN/VPNManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ struct VPNManagerAlert {

class VPNManager: ObservableObject {
private var cancelBag: Set<AnyCancellable> = []

private var observer: NSObjectProtocol?
private var manager = NEVPNManager.shared()
private var loaded: Bool = false
private var timer: Timer?

static let shared: VPNManager = VPNManager()

@Published private(set) var state: NEVPNStatus = .invalid
@Published private(set) var alert: VPNManagerAlert = .init(alert: nil, message: nil)

@Published private(set) var upload: Int64 = 0
@Published private(set) var download: Int64 = 0
@Published private(set) var elapsedTime: TimeInterval = 0

private var _connectTime: Date?
private var connectTime: Date? {
set {
Expand All @@ -57,29 +57,29 @@ class VPNManager: ObservableObject {
}
}
private var readingWS: Bool = false

@Published var isConnectedToAnyVPN: Bool = false

init() {
observer = NotificationCenter.default.addObserver(forName: .NEVPNStatusDidChange, object: nil, queue: nil) { [weak self] notification in
guard let connection = notification.object as? NEVPNConnection else { return }
self?.state = connection.status
}

timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
guard let self else { return }
updateStats()
elapsedTime = -1 * (connectTime?.timeIntervalSinceNow ?? 0)
}
}

deinit {
if let observer {
NotificationCenter.default.removeObserver(observer)
}
timer?.invalidate()
}

func setup() async throws {
// guard !loaded else { return }
loaded = true
Expand All @@ -89,7 +89,7 @@ class VPNManager: ObservableObject {
print(error.localizedDescription)
}
}

private func loadVPNPreference() async throws {
do {
let managers = try await NETunnelProviderManager.loadAllFromPreferences()
Expand All @@ -99,7 +99,7 @@ class VPNManager: ObservableObject {
}
let newManager = NETunnelProviderManager()
let `protocol` = NETunnelProviderProtocol()
`protocol`.providerBundleIdentifier = Bundle.main.baseBundleIdentifier + ".SingBoxPacketTunnel"
`protocol`.providerBundleIdentifier = Bundle.main.baseBundleIdentifier + ".HiddifyPacketTunnel"
`protocol`.serverAddress = "localhost"
newManager.protocolConfiguration = `protocol`
newManager.localizedDescription = "Hiddify"
Expand All @@ -110,7 +110,7 @@ class VPNManager: ObservableObject {
print(error.localizedDescription)
}
}

private func enableVPNManager() async throws {
manager.isEnabled = true
do {
Expand All @@ -120,12 +120,12 @@ class VPNManager: ObservableObject {
print(error.localizedDescription)
}
}

@MainActor private func set(upload: Int64, download: Int64) {
self.upload = upload
self.download = download
}

var isAnyVPNConnected: Bool {
guard let cfDict = CFNetworkCopySystemProxySettings() else { return false }
let nsDict = cfDict.takeRetainedValue() as NSDictionary
Expand All @@ -141,7 +141,7 @@ class VPNManager: ObservableObject {
}
return false
}

func reset() {
loaded = false
if state != .disconnected && state != .invalid {
Expand All @@ -161,9 +161,9 @@ class VPNManager: ObservableObject {
}
}
}.store(in: &cancelBag)

}

private func updateStats() {
let isAnyVPNConnected = self.isAnyVPNConnected
if isConnectedToAnyVPN != isAnyVPNConnected {
Expand Down Expand Up @@ -191,7 +191,7 @@ class VPNManager: ObservableObject {
print(error.localizedDescription)
}
}

func connect(with config: String, disableMemoryLimit: Bool = false) async throws {
await set(upload: 0, download: 0)
guard state == .disconnected else { return }
Expand All @@ -206,7 +206,7 @@ class VPNManager: ObservableObject {
}
connectTime = .now
}

func disconnect() {
guard state == .connected else { return }
manager.connection.stopVPNTunnel()
Expand Down