mirror of https://github.com/oxen-io/lokinet
add lokinet.swift cli args
* add --start flag to start lokinet * add --stop flag to stop * by default lokinet on macos needs a flag or it will be a nop
This commit is contained in:
parent
c81e950d13
commit
5dd71995c4
|
@ -4,31 +4,70 @@ import NetworkExtension
|
||||||
|
|
||||||
let app = NSApplication.shared
|
let app = NSApplication.shared
|
||||||
|
|
||||||
|
let START = "--start"
|
||||||
|
let STOP = "--stop"
|
||||||
|
|
||||||
|
let HELP_STRING = "usage: lokinet [--start|--stop]"
|
||||||
|
|
||||||
class LokinetMain: NSObject, NSApplicationDelegate {
|
class LokinetMain: NSObject, NSApplicationDelegate {
|
||||||
var vpnManager = NETunnelProviderManager()
|
var vpnManager = NETunnelProviderManager()
|
||||||
let lokinetComponent = "com.loki-project.lokinet.network-extension"
|
let lokinetComponent = "com.loki-project.lokinet.network-extension"
|
||||||
|
var mode = ""
|
||||||
|
|
||||||
func applicationDidFinishLaunching(_: Notification) {
|
func applicationDidFinishLaunching(_: Notification) {
|
||||||
setupVPNJizz()
|
if self.mode == START {
|
||||||
|
setupVPNTunnel()
|
||||||
|
}
|
||||||
|
else if self.mode == STOP {
|
||||||
|
tearDownVPNTunnel()
|
||||||
|
} else {
|
||||||
|
self.result(msg: HELP_STRING)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func bail() {
|
func bail() {
|
||||||
app.terminate(self)
|
app.terminate(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupVPNJizz() {
|
func result(msg: String) {
|
||||||
NSLog("Starting up lokinet")
|
NSLog(msg)
|
||||||
|
// TODO: does lokinet continue after this?
|
||||||
|
self.bail()
|
||||||
|
}
|
||||||
|
|
||||||
|
func tearDownVPNTunnel() {
|
||||||
|
NSLog("Stopping Lokinet")
|
||||||
NETunnelProviderManager.loadAllFromPreferences { [self] (savedManagers: [NETunnelProviderManager]?, error: Error?) in
|
NETunnelProviderManager.loadAllFromPreferences { [self] (savedManagers: [NETunnelProviderManager]?, error: Error?) in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
NSLog(error.localizedDescription)
|
self.result(msg: error.localizedDescription)
|
||||||
bail()
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if let savedManagers = savedManagers {
|
||||||
|
for manager in savedManagers {
|
||||||
|
if (manager.protocolConfiguration as? NETunnelProviderProtocol)?.providerBundleIdentifier == self.lokinetComponent {
|
||||||
|
manager.isEnabled = false
|
||||||
|
self.result(msg: "Lokinet Down")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.result(msg: "Lokinet is not up")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupVPNTunnel() {
|
||||||
|
NSLog("Starting up Lokinet")
|
||||||
|
NETunnelProviderManager.loadAllFromPreferences { [self] (savedManagers: [NETunnelProviderManager]?, error: Error?) in
|
||||||
|
if let error = error {
|
||||||
|
self.result(msg: error.localizedDescription)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if let savedManagers = savedManagers {
|
if let savedManagers = savedManagers {
|
||||||
for manager in savedManagers {
|
for manager in savedManagers {
|
||||||
if (manager.protocolConfiguration as? NETunnelProviderProtocol)?.providerBundleIdentifier == self.lokinetComponent {
|
if (manager.protocolConfiguration as? NETunnelProviderProtocol)?.providerBundleIdentifier == self.lokinetComponent {
|
||||||
NSLog("%@", manager)
|
|
||||||
NSLog("Found saved VPN Manager")
|
NSLog("Found saved VPN Manager")
|
||||||
self.vpnManager = manager
|
self.vpnManager = manager
|
||||||
}
|
}
|
||||||
|
@ -46,28 +85,24 @@ class LokinetMain: NSObject, NSApplicationDelegate {
|
||||||
self.vpnManager.isEnabled = true
|
self.vpnManager.isEnabled = true
|
||||||
// self.vpnManager.isOnDemandEnabled = true
|
// self.vpnManager.isOnDemandEnabled = true
|
||||||
self.vpnManager.localizedDescription = "lokinet"
|
self.vpnManager.localizedDescription = "lokinet"
|
||||||
self.vpnManager.saveToPreferences(completionHandler: { error -> Void in
|
self.vpnManager.saveToPreferences(completionHandler: { [self] error -> Void in
|
||||||
if error != nil {
|
if error != nil {
|
||||||
NSLog("Error saving to preferences")
|
NSLog("Error saving to preferences")
|
||||||
NSLog(error!.localizedDescription)
|
self.result(msg: error!.localizedDescription)
|
||||||
bail()
|
|
||||||
} else {
|
} else {
|
||||||
self.vpnManager.loadFromPreferences(completionHandler: { error in
|
self.vpnManager.loadFromPreferences(completionHandler: { error in
|
||||||
if error != nil {
|
if error != nil {
|
||||||
NSLog("Error loading from preferences")
|
NSLog("Error loading from preferences")
|
||||||
NSLog(error!.localizedDescription)
|
self.result(msg: error!.localizedDescription)
|
||||||
bail()
|
|
||||||
} else {
|
} else {
|
||||||
do {
|
do {
|
||||||
NSLog("Trying to start")
|
NSLog("Trying to start")
|
||||||
self.initializeConnectionObserver()
|
self.initializeConnectionObserver()
|
||||||
try self.vpnManager.connection.startVPNTunnel()
|
try self.vpnManager.connection.startVPNTunnel()
|
||||||
} catch let error as NSError {
|
} catch let error as NSError {
|
||||||
NSLog(error.localizedDescription)
|
self.result(msg: error.localizedDescription)
|
||||||
bail()
|
|
||||||
} catch {
|
} catch {
|
||||||
NSLog("There was a fatal error")
|
self.result(msg: "There was a fatal error")
|
||||||
bail()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -77,11 +112,11 @@ class LokinetMain: NSObject, NSApplicationDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeConnectionObserver() {
|
func initializeConnectionObserver() {
|
||||||
NotificationCenter.default.addObserver(forName: NSNotification.Name.NEVPNStatusDidChange, object: vpnManager.connection, queue: OperationQueue.main) { _ -> Void in
|
NotificationCenter.default.addObserver(forName: NSNotification.Name.NEVPNStatusDidChange, object: vpnManager.connection, queue: OperationQueue.main) { [self] _ -> Void in
|
||||||
if self.vpnManager.connection.status == .invalid {
|
if self.vpnManager.connection.status == .invalid {
|
||||||
NSLog("VPN configuration is invalid")
|
self.result(msg: "VPN configuration is invalid")
|
||||||
} else if self.vpnManager.connection.status == .disconnected {
|
} else if self.vpnManager.connection.status == .disconnected {
|
||||||
NSLog("VPN is disconnected.")
|
self.result(msg: "VPN is disconnected.")
|
||||||
} else if self.vpnManager.connection.status == .connecting {
|
} else if self.vpnManager.connection.status == .connecting {
|
||||||
NSLog("VPN is connecting...")
|
NSLog("VPN is connecting...")
|
||||||
} else if self.vpnManager.connection.status == .reasserting {
|
} else if self.vpnManager.connection.status == .reasserting {
|
||||||
|
@ -89,12 +124,19 @@ class LokinetMain: NSObject, NSApplicationDelegate {
|
||||||
} else if self.vpnManager.connection.status == .disconnecting {
|
} else if self.vpnManager.connection.status == .disconnecting {
|
||||||
NSLog("VPN is disconnecting...")
|
NSLog("VPN is disconnecting...")
|
||||||
} else if self.vpnManager.connection.status == .connected {
|
} else if self.vpnManager.connection.status == .connected {
|
||||||
NSLog("VPN Connected")
|
self.result(msg: "VPN Connected")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let delegate = LokinetMain()
|
let args = CommandLine.arguments
|
||||||
app.delegate = delegate
|
|
||||||
app.run()
|
if args.count > 1 {
|
||||||
|
let delegate = LokinetMain()
|
||||||
|
delegate.mode = args[1]
|
||||||
|
app.delegate = delegate
|
||||||
|
app.run()
|
||||||
|
} else {
|
||||||
|
NSLog(HELP_STRING)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue