2021-06-30 11:21:17 +00:00
|
|
|
import AppKit
|
2021-05-14 17:07:44 +00:00
|
|
|
import Foundation
|
|
|
|
import NetworkExtension
|
2022-06-22 21:56:35 +00:00
|
|
|
import SystemExtensions
|
2021-05-14 17:07:44 +00:00
|
|
|
|
2021-06-30 11:21:17 +00:00
|
|
|
let app = NSApplication.shared
|
2021-05-14 17:07:44 +00:00
|
|
|
|
2022-04-22 13:00:24 +00:00
|
|
|
let START = "--start"
|
|
|
|
let STOP = "--stop"
|
|
|
|
|
2022-08-12 22:57:00 +00:00
|
|
|
let HELP_STRING = "usage: lokinet {--start|--stop}"
|
2022-04-22 13:00:24 +00:00
|
|
|
|
2021-06-30 11:21:17 +00:00
|
|
|
class LokinetMain: NSObject, NSApplicationDelegate {
|
|
|
|
var vpnManager = NETunnelProviderManager()
|
2022-06-22 21:56:35 +00:00
|
|
|
var mode = START
|
|
|
|
let netextBundleId = "org.lokinet.network-extension"
|
2021-05-14 17:07:44 +00:00
|
|
|
|
2021-06-30 11:21:17 +00:00
|
|
|
func applicationDidFinishLaunching(_: Notification) {
|
2022-07-25 15:23:05 +00:00
|
|
|
if mode == START {
|
2022-06-22 21:56:35 +00:00
|
|
|
startNetworkExtension()
|
2022-07-25 15:23:05 +00:00
|
|
|
} else if mode == STOP {
|
2022-04-22 13:00:24 +00:00
|
|
|
tearDownVPNTunnel()
|
|
|
|
} else {
|
2022-07-25 15:23:05 +00:00
|
|
|
result(msg: HELP_STRING)
|
2022-04-22 13:00:24 +00:00
|
|
|
}
|
2021-06-30 11:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func bail() {
|
|
|
|
app.terminate(self)
|
|
|
|
}
|
|
|
|
|
2022-04-22 13:00:24 +00:00
|
|
|
func result(msg: String) {
|
|
|
|
NSLog(msg)
|
|
|
|
// TODO: does lokinet continue after this?
|
2022-07-25 15:23:05 +00:00
|
|
|
bail()
|
2022-04-22 13:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func tearDownVPNTunnel() {
|
|
|
|
NSLog("Stopping Lokinet")
|
2021-06-30 11:21:17 +00:00
|
|
|
NETunnelProviderManager.loadAllFromPreferences { [self] (savedManagers: [NETunnelProviderManager]?, error: Error?) in
|
2021-05-14 17:07:44 +00:00
|
|
|
if let error = error {
|
2022-04-22 13:00:24 +00:00
|
|
|
self.result(msg: error.localizedDescription)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let savedManagers = savedManagers {
|
|
|
|
for manager in savedManagers {
|
2022-06-22 21:56:35 +00:00
|
|
|
if (manager.protocolConfiguration as? NETunnelProviderProtocol)?.providerBundleIdentifier == self.netextBundleId {
|
2022-08-12 22:57:00 +00:00
|
|
|
manager.connection.stopVPNTunnel()
|
2022-04-22 13:00:24 +00:00
|
|
|
self.result(msg: "Lokinet Down")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.result(msg: "Lokinet is not up")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 21:56:35 +00:00
|
|
|
func startNetworkExtension() {
|
2022-07-25 15:23:05 +00:00
|
|
|
#if MACOS_SYSTEM_EXTENSION
|
|
|
|
NSLog("Loading Lokinet network extension")
|
|
|
|
// Start by activating the system extension
|
|
|
|
let activationRequest = OSSystemExtensionRequest.activationRequest(forExtensionWithIdentifier: netextBundleId, queue: .main)
|
|
|
|
activationRequest.delegate = self
|
|
|
|
OSSystemExtensionManager.shared.submitRequest(activationRequest)
|
|
|
|
#else
|
|
|
|
setupVPNTunnel()
|
|
|
|
#endif
|
2022-06-22 21:56:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 13:00:24 +00:00
|
|
|
func setupVPNTunnel() {
|
2022-06-22 21:56:35 +00:00
|
|
|
NSLog("Starting up Lokinet tunnel")
|
2022-04-22 13:00:24 +00:00
|
|
|
NETunnelProviderManager.loadAllFromPreferences { [self] (savedManagers: [NETunnelProviderManager]?, error: Error?) in
|
|
|
|
if let error = error {
|
|
|
|
self.result(msg: error.localizedDescription)
|
2021-07-29 20:40:25 +00:00
|
|
|
return
|
2021-05-14 17:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let savedManagers = savedManagers {
|
|
|
|
for manager in savedManagers {
|
2022-06-22 21:56:35 +00:00
|
|
|
if (manager.protocolConfiguration as? NETunnelProviderProtocol)?.providerBundleIdentifier == self.netextBundleId {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("Found saved VPN Manager")
|
2021-05-14 17:07:44 +00:00
|
|
|
self.vpnManager = manager
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let providerProtocol = NETunnelProviderProtocol()
|
2021-07-29 17:09:18 +00:00
|
|
|
providerProtocol.serverAddress = "loki.loki" // Needs to be set to some non-null dummy value
|
2021-06-30 11:21:17 +00:00
|
|
|
providerProtocol.username = "anonymous"
|
2022-06-22 21:56:35 +00:00
|
|
|
providerProtocol.providerBundleIdentifier = self.netextBundleId
|
2022-08-29 18:53:16 +00:00
|
|
|
if #available(macOS 11, *) {
|
|
|
|
providerProtocol.enforceRoutes = true
|
|
|
|
}
|
2021-07-29 17:09:18 +00:00
|
|
|
// macos seems to have trouble when this is true, and reports are that this breaks and
|
|
|
|
// doesn't do what it says on the tin in the first place. Needs more testing.
|
|
|
|
providerProtocol.includeAllNetworks = false
|
2021-05-14 17:07:44 +00:00
|
|
|
self.vpnManager.protocolConfiguration = providerProtocol
|
|
|
|
self.vpnManager.isEnabled = true
|
2021-07-29 20:40:25 +00:00
|
|
|
// self.vpnManager.isOnDemandEnabled = true
|
2021-07-29 17:09:18 +00:00
|
|
|
self.vpnManager.localizedDescription = "lokinet"
|
2022-04-22 13:00:24 +00:00
|
|
|
self.vpnManager.saveToPreferences(completionHandler: { [self] error -> Void in
|
2021-05-14 17:07:44 +00:00
|
|
|
if error != nil {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("Error saving to preferences")
|
2022-04-22 13:00:24 +00:00
|
|
|
self.result(msg: error!.localizedDescription)
|
2021-05-14 17:07:44 +00:00
|
|
|
} else {
|
|
|
|
self.vpnManager.loadFromPreferences(completionHandler: { error in
|
|
|
|
if error != nil {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("Error loading from preferences")
|
2022-04-22 13:00:24 +00:00
|
|
|
self.result(msg: error!.localizedDescription)
|
2021-05-14 17:07:44 +00:00
|
|
|
} else {
|
|
|
|
do {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("Trying to start")
|
2021-05-14 17:07:44 +00:00
|
|
|
self.initializeConnectionObserver()
|
|
|
|
try self.vpnManager.connection.startVPNTunnel()
|
|
|
|
} catch let error as NSError {
|
2022-07-25 15:23:05 +00:00
|
|
|
self.result(msg: error.localizedDescription)
|
2021-05-14 17:07:44 +00:00
|
|
|
} catch {
|
2022-04-22 13:00:24 +00:00
|
|
|
self.result(msg: "There was a fatal error")
|
2021-05-14 17:07:44 +00:00
|
|
|
}
|
2022-09-29 20:15:35 +00:00
|
|
|
|
|
|
|
// Check if we are already connected because, if so, we won't get a
|
|
|
|
// status change and will just hang waiting for one.
|
|
|
|
if self.vpnManager.connection.status == .connected {
|
|
|
|
self.result(msg: "VPN already connected");
|
|
|
|
}
|
2021-05-14 17:07:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeConnectionObserver() {
|
2022-04-22 13:00:24 +00:00
|
|
|
NotificationCenter.default.addObserver(forName: NSNotification.Name.NEVPNStatusDidChange, object: vpnManager.connection, queue: OperationQueue.main) { [self] _ -> Void in
|
2021-05-14 17:07:44 +00:00
|
|
|
if self.vpnManager.connection.status == .invalid {
|
2022-04-22 13:00:24 +00:00
|
|
|
self.result(msg: "VPN configuration is invalid")
|
2021-05-14 17:07:44 +00:00
|
|
|
} else if self.vpnManager.connection.status == .disconnected {
|
2022-04-22 13:00:24 +00:00
|
|
|
self.result(msg: "VPN is disconnected.")
|
2021-05-14 17:07:44 +00:00
|
|
|
} else if self.vpnManager.connection.status == .connecting {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("VPN is connecting...")
|
2021-05-14 17:07:44 +00:00
|
|
|
} else if self.vpnManager.connection.status == .reasserting {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("VPN is reasserting...")
|
2021-05-14 17:07:44 +00:00
|
|
|
} else if self.vpnManager.connection.status == .disconnecting {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("VPN is disconnecting...")
|
2021-07-29 20:40:25 +00:00
|
|
|
} else if self.vpnManager.connection.status == .connected {
|
2022-04-22 13:00:24 +00:00
|
|
|
self.result(msg: "VPN Connected")
|
2021-05-14 17:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 21:56:35 +00:00
|
|
|
#if MACOS_SYSTEM_EXTENSION
|
|
|
|
|
2022-07-25 15:23:05 +00:00
|
|
|
extension LokinetMain: OSSystemExtensionRequestDelegate {
|
|
|
|
func request(_: OSSystemExtensionRequest, didFinishWithResult result: OSSystemExtensionRequest.Result) {
|
|
|
|
guard result == .completed else {
|
|
|
|
NSLog("Unexpected result %d for system extension request", result.rawValue)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
NSLog("Lokinet system extension loaded")
|
|
|
|
setupVPNTunnel()
|
2022-06-22 21:56:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 15:23:05 +00:00
|
|
|
func request(_: OSSystemExtensionRequest, didFailWithError error: Error) {
|
|
|
|
NSLog("System extension request failed: %@", error.localizedDescription)
|
2022-08-12 22:57:00 +00:00
|
|
|
self.bail()
|
2022-07-25 15:23:05 +00:00
|
|
|
}
|
2022-06-22 21:56:35 +00:00
|
|
|
|
2022-07-25 15:23:05 +00:00
|
|
|
func requestNeedsUserApproval(_ request: OSSystemExtensionRequest) {
|
|
|
|
NSLog("Extension %@ requires user approval", request.identifier)
|
|
|
|
}
|
2022-06-22 21:56:35 +00:00
|
|
|
|
2022-07-25 15:23:05 +00:00
|
|
|
func request(_ request: OSSystemExtensionRequest,
|
|
|
|
actionForReplacingExtension existing: OSSystemExtensionProperties,
|
|
|
|
withExtension extension: OSSystemExtensionProperties) -> OSSystemExtensionRequest.ReplacementAction
|
|
|
|
{
|
|
|
|
NSLog("Replacing extension %@ version %@ with version %@", request.identifier, existing.bundleShortVersion, `extension`.bundleShortVersion)
|
|
|
|
return .replace
|
|
|
|
}
|
2022-06-22 21:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-04-22 13:00:24 +00:00
|
|
|
let args = CommandLine.arguments
|
|
|
|
|
2022-08-12 22:57:00 +00:00
|
|
|
// If we are invoked with no arguments then exec the gui. This is dumb, but there doesn't seem to
|
|
|
|
// be a nicer way to do this on Apple's half-baked platform because:
|
|
|
|
// - we have three "bundles" we need to manage: the GUI app, the system extension, and the Lokinet
|
|
|
|
// app (this file) which loads the system extension.
|
|
|
|
// - if we embed the system extension directly inside the GUI then it fails to launch because the
|
|
|
|
// electron GUI's requirements (needed for JIT) conflict with the ability to load a system
|
|
|
|
// extensions.
|
|
|
|
// - if we embed Lokinet.app inside Lokinet-GUI.app and then the system extension inside Lokinet.app
|
|
|
|
// then it works, but macos loses track of the system extension and doesn't remove it when you
|
|
|
|
// remove the application. (It breaks your system, leaving an impossible-to-remove system
|
|
|
|
// extension, in just the same way it breaks if you don't use Finder to remove the Application.
|
|
|
|
// Apple used to say (around 2 years ago as of writing) that they would fix this situation "soon",
|
|
|
|
// but hasn't, and has stopped saying anything about it.)
|
|
|
|
// - if we try to use multiple executables (one to launch the system extension, one simple shell
|
|
|
|
// script to execs the embedded GUI app) inside the Lokinet.app and make the GUI the default for
|
|
|
|
// the application then Lokinet gets killed by gatekeeper because code signing only applies the
|
|
|
|
// (required-for-system-extensions) provisioningprofile to the main binary in the app.
|
|
|
|
//
|
|
|
|
// So we are left needing *one* single binary that isn't the GUI but has to do double-duty for both
|
|
|
|
// exec'ing the binary and loading lokinet, depending on how it is called.
|
|
|
|
//
|
|
|
|
// But of course there is no way to specify command-line arguments to the default binary macOS runs,
|
|
|
|
// so we can't use a `--gui` flag or anything so abhorrent to macos purity, thus this nasty
|
|
|
|
// solution:
|
|
|
|
// - no args -- exec the GUI
|
|
|
|
// - `--start` -- load the system extension and start lokinet
|
|
|
|
// - `--stop` -- stop lokinet
|
|
|
|
//
|
|
|
|
// macOS: land of half-baked implementations and nasty hacks to make anything work.
|
|
|
|
|
|
|
|
if args.count == 1 {
|
|
|
|
let gui_path = Bundle.main.resourcePath! + "/../Helpers/Lokinet-GUI.app"
|
|
|
|
if !FileManager.default.fileExists(atPath: gui_path) {
|
|
|
|
NSLog("Could not find gui app at %@", gui_path)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
let gui_url = URL(fileURLWithPath: gui_path, isDirectory: false)
|
|
|
|
let gui_app_conf = NSWorkspace.OpenConfiguration()
|
|
|
|
let group = DispatchGroup()
|
|
|
|
group.enter()
|
|
|
|
NSWorkspace.shared.openApplication(at: gui_url, configuration: gui_app_conf,
|
|
|
|
completionHandler: { (app, error) in
|
|
|
|
if error != nil {
|
|
|
|
NSLog("Error launching gui: %@", error!.localizedDescription)
|
|
|
|
} else {
|
|
|
|
NSLog("Lauched GUI");
|
|
|
|
}
|
|
|
|
group.leave()
|
|
|
|
})
|
|
|
|
group.wait()
|
|
|
|
|
|
|
|
} else if args.count == 2 {
|
2022-04-22 13:00:24 +00:00
|
|
|
let delegate = LokinetMain()
|
2022-08-12 22:57:00 +00:00
|
|
|
delegate.mode = args[1]
|
2022-04-22 13:00:24 +00:00
|
|
|
app.delegate = delegate
|
|
|
|
app.run()
|
|
|
|
} else {
|
|
|
|
NSLog(HELP_STRING)
|
|
|
|
}
|