2021-06-30 11:21:17 +00:00
|
|
|
import AppKit
|
2021-05-14 17:07:44 +00:00
|
|
|
import Foundation
|
|
|
|
import LokinetExtension
|
|
|
|
import NetworkExtension
|
|
|
|
|
2021-06-30 11:21:17 +00:00
|
|
|
let app = NSApplication.shared
|
2021-05-14 17:07:44 +00:00
|
|
|
|
2021-06-30 11:21:17 +00:00
|
|
|
class LokinetMain: NSObject, NSApplicationDelegate {
|
|
|
|
var vpnManager = NETunnelProviderManager()
|
|
|
|
let lokinetComponent = "com.loki-project.lokinet.network-extension"
|
2021-05-14 17:07:44 +00:00
|
|
|
var lokinetAdminTimer: DispatchSourceTimer?
|
|
|
|
|
2021-06-30 11:21:17 +00:00
|
|
|
func applicationDidFinishLaunching(_: Notification) {
|
|
|
|
setupVPNJizz()
|
|
|
|
}
|
|
|
|
|
|
|
|
func bail() {
|
|
|
|
app.terminate(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupVPNJizz() {
|
|
|
|
NSLog("Starting up lokinet")
|
|
|
|
NETunnelProviderManager.loadAllFromPreferences { [self] (savedManagers: [NETunnelProviderManager]?, error: Error?) in
|
2021-05-14 17:07:44 +00:00
|
|
|
if let error = error {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog(error.localizedDescription)
|
|
|
|
bail()
|
2021-05-14 17:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let savedManagers = savedManagers {
|
|
|
|
for manager in savedManagers {
|
|
|
|
if (manager.protocolConfiguration as? NETunnelProviderProtocol)?.providerBundleIdentifier == self.lokinetComponent {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("%@", manager)
|
|
|
|
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"
|
2021-05-14 17:07:44 +00:00
|
|
|
providerProtocol.providerBundleIdentifier = self.lokinetComponent
|
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-06-30 11:21:17 +00:00
|
|
|
self.vpnManager.isOnDemandEnabled = true
|
2021-07-29 17:09:18 +00:00
|
|
|
self.vpnManager.localizedDescription = "lokinet"
|
2021-05-14 17:07:44 +00:00
|
|
|
self.vpnManager.saveToPreferences(completionHandler: { error -> Void in
|
|
|
|
if error != nil {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("Error saving to preferences")
|
|
|
|
NSLog(error!.localizedDescription)
|
|
|
|
bail()
|
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")
|
|
|
|
NSLog(error!.localizedDescription)
|
|
|
|
bail()
|
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 {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog(error.localizedDescription)
|
|
|
|
bail()
|
2021-05-14 17:07:44 +00:00
|
|
|
} catch {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("There was a fatal error")
|
|
|
|
bail()
|
2021-05-14 17:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeConnectionObserver() {
|
|
|
|
NotificationCenter.default.addObserver(forName: NSNotification.Name.NEVPNStatusDidChange, object: vpnManager.connection, queue: OperationQueue.main) { _ -> Void in
|
|
|
|
if self.vpnManager.connection.status == .invalid {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("VPN configuration is invalid")
|
2021-05-14 17:07:44 +00:00
|
|
|
} else if self.vpnManager.connection.status == .disconnected {
|
2021-06-30 11:21:17 +00:00
|
|
|
NSLog("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-05-14 17:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 11:21:17 +00:00
|
|
|
let delegate = LokinetMain()
|
|
|
|
app.delegate = delegate
|
|
|
|
app.run()
|