mirror of
https://github.com/DefinedNet/mobile_nebula.git
synced 2025-02-14 16:05:25 +00:00
Make SiteList.loadAll
a static method
This commit is contained in:
parent
8ff1da215c
commit
6564a18d3d
3 changed files with 106 additions and 113 deletions
|
@ -1,14 +1,12 @@
|
|||
import NetworkExtension
|
||||
|
||||
class SiteList {
|
||||
private var sites = [String: Site]()
|
||||
|
||||
/// Gets the root directory that can be used to share files between the UI and VPN process. Does ensure the directory exists
|
||||
static func getRootDir() throws -> URL {
|
||||
let fileManager = FileManager.default
|
||||
let rootDir = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.net.defined.mobileNebula")!
|
||||
|
||||
if (!fileManager.fileExists(atPath: rootDir.absoluteString)) {
|
||||
if !fileManager.fileExists(atPath: rootDir.absoluteString) {
|
||||
try fileManager.createDirectory(at: rootDir, withIntermediateDirectories: true)
|
||||
}
|
||||
|
||||
|
@ -19,7 +17,7 @@ class SiteList {
|
|||
static func getSitesDir() throws -> URL {
|
||||
let fileManager = FileManager.default
|
||||
let sitesDir = try getRootDir().appendingPathComponent("sites", isDirectory: true)
|
||||
if (!fileManager.fileExists(atPath: sitesDir.absoluteString)) {
|
||||
if !fileManager.fileExists(atPath: sitesDir.absoluteString) {
|
||||
try fileManager.createDirectory(at: sitesDir, withIntermediateDirectories: true)
|
||||
}
|
||||
return sitesDir
|
||||
|
@ -29,7 +27,7 @@ class SiteList {
|
|||
static func getSiteDir(id: String, create: Bool = false) throws -> URL {
|
||||
let fileManager = FileManager.default
|
||||
let siteDir = try getSitesDir().appendingPathComponent(id, isDirectory: true)
|
||||
if (create && !fileManager.fileExists(atPath: siteDir.absoluteString)) {
|
||||
if create && !fileManager.fileExists(atPath: siteDir.absoluteString) {
|
||||
try fileManager.createDirectory(at: siteDir, withIntermediateDirectories: true)
|
||||
}
|
||||
return siteDir
|
||||
|
@ -37,7 +35,8 @@ class SiteList {
|
|||
|
||||
/// Gets the file that represents the site configuration, $rootDir/sites/$siteID/config.json
|
||||
static func getSiteConfigFile(id: String, createDir: Bool) throws -> URL {
|
||||
return try getSiteDir(id: id, create: createDir).appendingPathComponent("config", isDirectory: false).appendingPathExtension("json")
|
||||
return try getSiteDir(id: id, create: createDir).appendingPathComponent("config", isDirectory: false)
|
||||
.appendingPathExtension("json")
|
||||
}
|
||||
|
||||
/// Gets the file that represents the site log output, $rootDir/sites/$siteID/log
|
||||
|
@ -45,25 +44,21 @@ class SiteList {
|
|||
return try getSiteDir(id: id, create: createDir).appendingPathComponent("logs", isDirectory: false)
|
||||
}
|
||||
|
||||
init(completion: @escaping ([String: Site]?, (any Error)?) -> ()) {
|
||||
#if targetEnvironment(simulator)
|
||||
static func loadAll(completion: @escaping ([String: Site]?, (any Error)?) -> Void) {
|
||||
#if targetEnvironment(simulator)
|
||||
SiteList.loadAllFromFS { sites, err in
|
||||
if sites != nil {
|
||||
self.sites = sites!
|
||||
}
|
||||
|
||||
completion(sites, err)
|
||||
}
|
||||
#else
|
||||
#else
|
||||
SiteList.loadAllFromNETPM { sites, err in
|
||||
if sites != nil {
|
||||
self.sites = sites!
|
||||
}
|
||||
|
||||
completion(sites, err)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
private static func loadAllFromFS(completion: @escaping ([String: Site]?, (any Error)?) -> ()) {
|
||||
private static func loadAllFromFS(completion: @escaping ([String: Site]?, (any Error)?) -> Void) {
|
||||
let fileManager = FileManager.default
|
||||
var siteDirs: [URL]
|
||||
var sites = [String: Site]()
|
||||
|
@ -91,15 +86,15 @@ class SiteList {
|
|||
completion(sites, nil)
|
||||
}
|
||||
|
||||
private static func loadAllFromNETPM(completion: @escaping ([String: Site]?, (any Error)?) -> ()) {
|
||||
private static func loadAllFromNETPM(completion: @escaping ([String: Site]?, (any Error)?) -> Void) {
|
||||
var sites = [String: Site]()
|
||||
|
||||
// dispatchGroup is used to ensure we have migrated all sites before returning them
|
||||
// If there are no sites to migrate, there are never any entrants
|
||||
let dispatchGroup = DispatchGroup()
|
||||
|
||||
NETunnelProviderManager.loadAllFromPreferences() { newManagers, err in
|
||||
if (err != nil) {
|
||||
NETunnelProviderManager.loadAllFromPreferences { newManagers, err in
|
||||
if err != nil {
|
||||
return completion(nil, err)
|
||||
}
|
||||
|
||||
|
@ -133,8 +128,4 @@ class SiteList {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getSites() -> [String: Site] {
|
||||
return sites
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@ actor DNUpdater {
|
|||
private let timer = RepeatingTimer(timeInterval: 15 * 60) // 15 * 60 is 15 minutes
|
||||
private let log = Logger(subsystem: "net.defined.mobileNebula", category: "DNUpdater")
|
||||
|
||||
func updateAll(onUpdate: @escaping (Site) -> Void) {
|
||||
_ = SiteList { (sites, _) -> Void in
|
||||
private func updateAll(onUpdate: @escaping (Site) -> Void) {
|
||||
SiteList.loadAll(completion: { (sites, _) -> Void in
|
||||
if let unwrappedSites = sites?.values {
|
||||
// NEVPN seems to force us onto the main thread and we are about to make network calls that
|
||||
// could block for a while. Push ourselves onto another thread to avoid blocking the UI.
|
||||
|
@ -25,7 +25,7 @@ actor DNUpdater {
|
|||
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func updateAllLoop(onUpdate: @escaping (Site) -> Void) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import NetworkExtension
|
||||
import MobileNebula
|
||||
import NetworkExtension
|
||||
|
||||
class SiteContainer {
|
||||
var site: Site
|
||||
|
@ -19,15 +19,15 @@ class Sites {
|
|||
self.messenger = messenger
|
||||
}
|
||||
|
||||
func loadSites(completion: @escaping ([String: Site]?, (any Error)?) -> ()) {
|
||||
_ = SiteList { (sites, err) in
|
||||
if (err != nil) {
|
||||
func loadSites(completion: @escaping ([String: Site]?, (any Error)?) -> Void) {
|
||||
SiteList.loadAll(completion: { (sites, err) in
|
||||
if err != nil {
|
||||
return completion(nil, err)
|
||||
}
|
||||
|
||||
sites?.values.forEach{ site in
|
||||
sites?.values.forEach { site in
|
||||
var updater = self.containers[site.id]?.updater
|
||||
if (updater != nil) {
|
||||
if updater != nil {
|
||||
updater!.setSite(site: site)
|
||||
} else {
|
||||
updater = SiteUpdater(messenger: self.messenger!, site: site)
|
||||
|
@ -39,10 +39,10 @@ class Sites {
|
|||
return $0.site
|
||||
}
|
||||
completion(justSites, nil)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func deleteSite(id: String, callback: @escaping ((any Error)?) -> ()) {
|
||||
func deleteSite(id: String, callback: @escaping ((any Error)?) -> Void) {
|
||||
if let site = self.containers.removeValue(forKey: id) {
|
||||
_ = KeyChain.delete(key: "\(site.site.id).dnCredentials")
|
||||
_ = KeyChain.delete(key: "\(site.site.id).key")
|
||||
|
@ -55,10 +55,10 @@ class Sites {
|
|||
print("Failed to delete site from fs: \(error.localizedDescription)")
|
||||
}
|
||||
|
||||
#if !targetEnvironment(simulator)
|
||||
#if !targetEnvironment(simulator)
|
||||
site.site.manager!.removeFromPreferences(completionHandler: callback)
|
||||
return
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
// Nothing to remove
|
||||
|
@ -79,8 +79,8 @@ class Sites {
|
|||
}
|
||||
|
||||
class SiteUpdater: NSObject, FlutterStreamHandler {
|
||||
private var eventSink: FlutterEventSink?;
|
||||
private var eventChannel: FlutterEventChannel;
|
||||
private var eventSink: FlutterEventSink?
|
||||
private var eventChannel: FlutterEventChannel
|
||||
private var site: Site
|
||||
private var notification: Any?
|
||||
public var startFunc: (() -> Void)?
|
||||
|
@ -124,16 +124,18 @@ class SiteUpdater: NSObject, FlutterStreamHandler {
|
|||
|
||||
/// onListen is called when flutter code attaches an event listener
|
||||
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
||||
eventSink = events;
|
||||
eventSink = events
|
||||
|
||||
#if !targetEnvironment(simulator)
|
||||
#if !targetEnvironment(simulator)
|
||||
if site.manager == nil {
|
||||
//TODO: The dn updater path seems to race to build a site that lacks a manager. The UI does not display this error
|
||||
// and a another listen should occur and succeed.
|
||||
return FlutterError(code: "Internal Error", message: "Flutter manager was not present", details: nil)
|
||||
}
|
||||
|
||||
self.notification = NotificationCenter.default.addObserver(forName: NSNotification.Name.NEVPNStatusDidChange, object: site.manager!.connection , queue: nil) { n in
|
||||
self.notification = NotificationCenter.default.addObserver(
|
||||
forName: NSNotification.Name.NEVPNStatusDidChange, object: site.manager!.connection, queue: nil
|
||||
) { n in
|
||||
let oldConnected = self.site.connected
|
||||
self.site.status = statusString[self.site.manager!.connection.status]
|
||||
self.site.connected = statusMap[self.site.manager!.connection.status]
|
||||
|
@ -146,13 +148,13 @@ class SiteUpdater: NSObject, FlutterStreamHandler {
|
|||
|
||||
self.update(connected: self.site.connected!)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
return nil
|
||||
}
|
||||
|
||||
/// onCancel is called when the flutter listener stops listening
|
||||
func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
if (self.notification != nil) {
|
||||
if self.notification != nil {
|
||||
NotificationCenter.default.removeObserver(self.notification!)
|
||||
}
|
||||
return nil
|
||||
|
@ -160,7 +162,7 @@ class SiteUpdater: NSObject, FlutterStreamHandler {
|
|||
|
||||
/// update is a way to send information to the flutter listener and generally should not be used directly
|
||||
func update(connected: Bool, replaceSite: Site? = nil) {
|
||||
if (replaceSite != nil) {
|
||||
if replaceSite != nil {
|
||||
site = replaceSite!
|
||||
}
|
||||
site.connected = connected
|
||||
|
|
Loading…
Reference in a new issue