2020-07-27 20:43:58 +00:00
|
|
|
import NetworkExtension
|
|
|
|
import MobileNebula
|
|
|
|
|
|
|
|
class SiteContainer {
|
|
|
|
var site: Site
|
|
|
|
var updater: SiteUpdater
|
|
|
|
|
|
|
|
init(site: Site, updater: SiteUpdater) {
|
|
|
|
self.site = site
|
|
|
|
self.updater = updater
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Sites {
|
2022-11-17 21:43:16 +00:00
|
|
|
private var containers = [String: SiteContainer]()
|
2020-07-27 20:43:58 +00:00
|
|
|
private var messenger: FlutterBinaryMessenger?
|
|
|
|
|
|
|
|
init(messenger: FlutterBinaryMessenger?) {
|
|
|
|
self.messenger = messenger
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadSites(completion: @escaping ([String: Site]?, Error?) -> ()) {
|
2022-11-17 21:43:16 +00:00
|
|
|
_ = SiteList { (sites, err) in
|
2020-07-27 20:43:58 +00:00
|
|
|
if (err != nil) {
|
|
|
|
return completion(nil, err)
|
|
|
|
}
|
2022-11-17 21:43:16 +00:00
|
|
|
|
|
|
|
sites?.values.forEach{ site in
|
2022-11-18 21:03:52 +00:00
|
|
|
var updater = self.containers[site.id]?.updater
|
|
|
|
if (updater != nil) {
|
|
|
|
updater!.setSite(site: site)
|
|
|
|
} else {
|
|
|
|
updater = SiteUpdater(messenger: self.messenger!, site: site)
|
|
|
|
}
|
|
|
|
self.containers[site.id] = SiteContainer(site: site, updater: updater!)
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
let justSites = self.containers.mapValues {
|
2020-07-27 20:43:58 +00:00
|
|
|
return $0.site
|
|
|
|
}
|
|
|
|
completion(justSites, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteSite(id: String, callback: @escaping (Error?) -> ()) {
|
2022-11-17 21:43:16 +00:00
|
|
|
if let site = self.containers.removeValue(forKey: id) {
|
|
|
|
_ = KeyChain.delete(key: "\(site.site.id).dnCredentials")
|
|
|
|
_ = KeyChain.delete(key: "\(site.site.id).key")
|
|
|
|
|
|
|
|
do {
|
|
|
|
let fileManager = FileManager.default
|
|
|
|
let siteDir = try SiteList.getSiteDir(id: site.site.id)
|
|
|
|
try fileManager.removeItem(at: siteDir)
|
|
|
|
} catch {
|
|
|
|
print("Failed to delete site from fs: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !targetEnvironment(simulator)
|
2021-04-27 15:29:28 +00:00
|
|
|
site.site.manager!.removeFromPreferences(completionHandler: callback)
|
2022-11-17 21:43:16 +00:00
|
|
|
return
|
2020-07-27 20:43:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to remove
|
|
|
|
callback(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSite(id: String) -> Site? {
|
2022-11-17 21:43:16 +00:00
|
|
|
return self.containers[id]?.site
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUpdater(id: String) -> SiteUpdater? {
|
2022-11-17 21:43:16 +00:00
|
|
|
return self.containers[id]?.updater
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
2021-04-27 15:29:28 +00:00
|
|
|
|
|
|
|
func getContainer(id: String) -> SiteContainer? {
|
2022-11-17 21:43:16 +00:00
|
|
|
return self.containers[id]
|
2021-04-27 15:29:28 +00:00
|
|
|
}
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class SiteUpdater: NSObject, FlutterStreamHandler {
|
|
|
|
private var eventSink: FlutterEventSink?;
|
|
|
|
private var eventChannel: FlutterEventChannel;
|
|
|
|
private var site: Site
|
|
|
|
private var notification: Any?
|
2021-04-27 15:29:28 +00:00
|
|
|
public var startFunc: (() -> Void)?
|
2022-11-17 21:43:16 +00:00
|
|
|
private var configFd: Int32? = nil
|
|
|
|
private var configObserver: DispatchSourceFileSystemObject? = nil
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
init(messenger: FlutterBinaryMessenger, site: Site) {
|
2022-11-17 21:43:16 +00:00
|
|
|
do {
|
|
|
|
let configPath = try SiteList.getSiteConfigFile(id: site.id, createDir: false)
|
|
|
|
self.configFd = open(configPath.path, O_EVTONLY)
|
|
|
|
self.configObserver = DispatchSource.makeFileSystemObjectSource(
|
|
|
|
fileDescriptor: self.configFd!,
|
|
|
|
eventMask: .write
|
|
|
|
)
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
// SiteList.getSiteConfigFile should never throw because we are not creating it here
|
|
|
|
self.configObserver = nil
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
eventChannel = FlutterEventChannel(name: "net.defined.nebula/\(site.id)", binaryMessenger: messenger)
|
|
|
|
self.site = site
|
|
|
|
super.init()
|
2022-11-17 21:43:16 +00:00
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
eventChannel.setStreamHandler(self)
|
2022-11-17 21:43:16 +00:00
|
|
|
|
|
|
|
self.configObserver?.setEventHandler(handler: self.configUpdated)
|
|
|
|
self.configObserver?.setCancelHandler {
|
|
|
|
if self.configFd != nil {
|
|
|
|
close(self.configFd!)
|
|
|
|
}
|
|
|
|
self.configObserver = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
self.configObserver?.resume()
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 21:03:52 +00:00
|
|
|
func setSite(site: Site) {
|
|
|
|
self.site = site
|
|
|
|
}
|
|
|
|
|
2021-04-27 15:29:28 +00:00
|
|
|
/// onListen is called when flutter code attaches an event listener
|
2020-07-27 20:43:58 +00:00
|
|
|
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
|
|
|
eventSink = events;
|
2022-11-17 21:43:16 +00:00
|
|
|
#if !targetEnvironment(simulator)
|
2021-04-27 15:29:28 +00:00
|
|
|
self.notification = NotificationCenter.default.addObserver(forName: NSNotification.Name.NEVPNStatusDidChange, object: site.manager!.connection , queue: nil) { n in
|
|
|
|
let connected = self.site.connected
|
|
|
|
self.site.status = statusString[self.site.manager!.connection.status]
|
|
|
|
self.site.connected = statusMap[self.site.manager!.connection.status]
|
2022-09-21 20:27:35 +00:00
|
|
|
|
2021-04-27 15:29:28 +00:00
|
|
|
// Check to see if we just moved to connected and if we have a start function to call when that happens
|
|
|
|
if self.site.connected! && connected != self.site.connected && self.startFunc != nil {
|
|
|
|
self.startFunc!()
|
|
|
|
self.startFunc = nil
|
|
|
|
}
|
2022-09-21 20:27:35 +00:00
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
self.update(connected: self.site.connected!)
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
2022-11-17 21:43:16 +00:00
|
|
|
#endif
|
2020-07-27 20:43:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 15:29:28 +00:00
|
|
|
/// onCancel is called when the flutter listener stops listening
|
2020-07-27 20:43:58 +00:00
|
|
|
func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
|
|
|
if (self.notification != nil) {
|
|
|
|
NotificationCenter.default.removeObserver(self.notification!)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 15:29:28 +00:00
|
|
|
/// update is a way to send information to the flutter listener and generally should not be used directly
|
2022-11-17 21:43:16 +00:00
|
|
|
func update(connected: Bool, replaceSite: Site? = nil) {
|
|
|
|
if (replaceSite != nil) {
|
|
|
|
site = replaceSite!
|
|
|
|
}
|
|
|
|
site.connected = connected
|
|
|
|
site.status = connected ? "Connected" : "Disconnected"
|
|
|
|
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
let data = try! encoder.encode(site)
|
|
|
|
self.eventSink?(String(data: data, encoding: .utf8))
|
|
|
|
}
|
|
|
|
|
|
|
|
private func configUpdated() {
|
|
|
|
if self.site.connected != true {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let newSite = try? Site(manager: self.site.manager!) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.update(connected: newSite.connected ?? false, replaceSite: newSite)
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|