Skip to content

Commit

Permalink
Fix iOS Crash Due to Outdated BundleIdentifier suffix name
Browse files Browse the repository at this point in the history
  • Loading branch information
xuqian-git committed Oct 25, 2024
1 parent a4c4f6a commit 9e7d20a
Showing 1 changed file with 20 additions and 20 deletions.
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

0 comments on commit 9e7d20a

Please sign in to comment.