Improve VPN video routing, profile storage and authorization reuse

This commit is contained in:
shen
2026-10-13 00:34:16 -07:00
parent c94143dfae
commit f94dab0555
30 changed files with 1303 additions and 139 deletions
@@ -13,6 +13,8 @@ class MainFlutterWindow: NSWindow {
private var vpnChannel: FlutterMethodChannel?
private var qrScanner: MeshQRScanner?
private var importing = false
private let profileStore = LocalProfileStore()
private let profileQueue = DispatchQueue(label: "com.meshport.local-profiles")
override func close() {
// Hiding keeps the Flutter engine, drafts and active network sessions alive.
@@ -33,40 +35,32 @@ class MainFlutterWindow: NSWindow {
guard let self = self else { result(FlutterError(code:"WINDOW_CLOSED",message:"Window closed",details:nil)); return }
if self.handleCapabilities(call, result:result) { return }
let profileMethod = call.method.replacingOccurrences(of: "_proxy_profile", with: "_profile")
let isProxy = call.method.contains("_proxy_profile")
let profileQuery: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: isProxy ? "MeshPort.Lab.ProxyProfile" : "MeshPort.Lab.NetworkProfile",
kSecAttrAccount as String: isProxy ? "last-proxy" : "last-network"
]
if profileMethod == "save_profile", let config = call.arguments as? String,
config.utf8.count <= 65536, let data = config.data(using: .utf8) {
let attributes = [kSecValueData as String: data]
var status = SecItemUpdate(profileQuery as CFDictionary, attributes as CFDictionary)
if status == errSecItemNotFound {
var item = profileQuery
item[kSecValueData as String] = data
status = SecItemAdd(item as CFDictionary, nil)
if ["save_profile", "load_profile", "delete_profile", "profile_storage_status", "migrate_profiles"].contains(profileMethod) {
let kind: StoredProfile = call.method.contains("_proxy_profile") ? .proxy : .network
self.profileQueue.async {
do {
let value: Any?
switch profileMethod {
case "save_profile":
guard let text = call.arguments as? String else {
throw NSError(domain: "MeshPort.LocalProfile", code: 1)
}
try self.profileStore.save(text, kind: kind)
value = nil
case "load_profile": value = try self.profileStore.load(kind)
case "delete_profile": try self.profileStore.delete(kind); value = nil
case "migrate_profiles": try self.profileStore.migratePending(); value = nil
default:
value = ["path": self.profileStore.root.path,
"migration_pending": try self.profileStore.migrationPending()]
}
DispatchQueue.main.async { result(value) }
} catch {
DispatchQueue.main.async {
result(FlutterError(code: "PROFILE_STORAGE", message: "Could not read, save or migrate local configuration", details: (error as NSError).code))
}
}
}
if status == errSecSuccess { result(nil) }
else { result(FlutterError(code: "KEYCHAIN_SAVE", message: "Could not save network profile to Keychain", details: status)) }
return
}
if profileMethod == "load_profile" {
var query = profileQuery
query[kSecReturnData as String] = true
query[kSecMatchLimit as String] = kSecMatchLimitOne
var value: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &value)
if status == errSecItemNotFound { result(nil) }
else if status == errSecSuccess, let data = value as? Data { result(String(data: data, encoding: .utf8)) }
else { result(FlutterError(code: "KEYCHAIN_LOAD", message: "Could not read network profile", details: status)) }
return
}
if profileMethod == "delete_profile" {
let status = SecItemDelete(profileQuery as CFDictionary)
if status == errSecSuccess || status == errSecItemNotFound { result(nil) }
else { result(FlutterError(code: "KEYCHAIN_DELETE", message: "Could not delete network profile", details: status)) }
return
}
guard call.method == "launch", let args = call.arguments as? [String: Any],