Files
MeshPort/apps/meshport_lab/macos/Runner/MainFlutterWindow.swift
T

81 lines
3.9 KiB
Swift

import Cocoa
import Security
import FlutterMacOS
class MainFlutterWindow: NSWindow {
private var vpnChannel: FlutterMethodChannel?
override func awakeFromNib() {
let flutterViewController = FlutterViewController()
let windowFrame = self.frame
self.contentViewController = flutterViewController
self.setFrame(windowFrame, display: true)
RegisterGeneratedPlugins(registry: flutterViewController)
vpnChannel = FlutterMethodChannel(name: "meshport/system_vpn", binaryMessenger: flutterViewController.engine.binaryMessenger)
vpnChannel?.setMethodCallHandler { call, result in
let profileQuery: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: "MeshPort.Lab.NetworkProfile",
kSecAttrAccount as String: "last-network"
]
if call.method == "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 status == errSecSuccess { result(nil) }
else { result(FlutterError(code: "KEYCHAIN_SAVE", message: "Could not save network profile to Keychain", details: status)) }
return
}
if call.method == "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 call.method == "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],
let socket = args["socket"] as? String, let uid = args["uid"] as? Int,
uid == Int(getuid()), uid != 0,
socket.hasPrefix("/tmp/meshport-vpn-"), socket.hasSuffix("/control.sock"),
let helper = Bundle.main.path(forAuxiliaryExecutable: "meshport-vpn-helper") else {
result(FlutterError(code: "VPN_UNAVAILABLE", message: "VPN helper missing or invalid launch request", details: nil))
return
}
func quoteShell(_ value: String) -> String {
"'" + value.replacingOccurrences(of: "'", with: "'\"'\"'") + "'"
}
let command = quoteShell(helper) + " --socket " + quoteShell(socket) + " --uid " + String(uid) + " </dev/null >/dev/null 2>&1 &"
let literal = command.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"")
DispatchQueue.global(qos: .userInitiated).async {
var error: NSDictionary?
let script = NSAppleScript(source: "do shell script \"" + literal + "\" with administrator privileges")
let output = script?.executeAndReturnError(&error)
DispatchQueue.main.async {
if output == nil || error != nil {
result(FlutterError(code: "VPN_AUTHORIZATION", message: "Administrator authorization cancelled or helper launch failed", details: nil))
} else {
result(nil)
}
}
}
}
super.awakeFromNib()
}
}