mirror of
https://github.com/DefinedNet/mobile_nebula.git
synced 2025-02-15 00:05:27 +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
|
import NetworkExtension
|
||||||
|
|
||||||
class SiteList {
|
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
|
/// 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 {
|
static func getRootDir() throws -> URL {
|
||||||
let fileManager = FileManager.default
|
let fileManager = FileManager.default
|
||||||
let rootDir = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.net.defined.mobileNebula")!
|
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)
|
try fileManager.createDirectory(at: rootDir, withIntermediateDirectories: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +17,7 @@ class SiteList {
|
||||||
static func getSitesDir() throws -> URL {
|
static func getSitesDir() throws -> URL {
|
||||||
let fileManager = FileManager.default
|
let fileManager = FileManager.default
|
||||||
let sitesDir = try getRootDir().appendingPathComponent("sites", isDirectory: true)
|
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)
|
try fileManager.createDirectory(at: sitesDir, withIntermediateDirectories: true)
|
||||||
}
|
}
|
||||||
return sitesDir
|
return sitesDir
|
||||||
|
@ -29,7 +27,7 @@ class SiteList {
|
||||||
static func getSiteDir(id: String, create: Bool = false) throws -> URL {
|
static func getSiteDir(id: String, create: Bool = false) throws -> URL {
|
||||||
let fileManager = FileManager.default
|
let fileManager = FileManager.default
|
||||||
let siteDir = try getSitesDir().appendingPathComponent(id, isDirectory: true)
|
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)
|
try fileManager.createDirectory(at: siteDir, withIntermediateDirectories: true)
|
||||||
}
|
}
|
||||||
return siteDir
|
return siteDir
|
||||||
|
@ -37,7 +35,8 @@ class SiteList {
|
||||||
|
|
||||||
/// Gets the file that represents the site configuration, $rootDir/sites/$siteID/config.json
|
/// Gets the file that represents the site configuration, $rootDir/sites/$siteID/config.json
|
||||||
static func getSiteConfigFile(id: String, createDir: Bool) throws -> URL {
|
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
|
/// 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)
|
return try getSiteDir(id: id, create: createDir).appendingPathComponent("logs", isDirectory: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
init(completion: @escaping ([String: Site]?, (any Error)?) -> ()) {
|
static func loadAll(completion: @escaping ([String: Site]?, (any Error)?) -> Void) {
|
||||||
#if targetEnvironment(simulator)
|
#if targetEnvironment(simulator)
|
||||||
SiteList.loadAllFromFS { sites, err in
|
SiteList.loadAllFromFS { sites, err in
|
||||||
if sites != nil {
|
|
||||||
self.sites = sites!
|
|
||||||
}
|
|
||||||
completion(sites, err)
|
completion(sites, err)
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
SiteList.loadAllFromNETPM { sites, err in
|
SiteList.loadAllFromNETPM { sites, err in
|
||||||
if sites != nil {
|
|
||||||
self.sites = sites!
|
|
||||||
}
|
|
||||||
completion(sites, err)
|
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
|
let fileManager = FileManager.default
|
||||||
var siteDirs: [URL]
|
var siteDirs: [URL]
|
||||||
var sites = [String: Site]()
|
var sites = [String: Site]()
|
||||||
|
@ -91,15 +86,15 @@ class SiteList {
|
||||||
completion(sites, nil)
|
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]()
|
var sites = [String: Site]()
|
||||||
|
|
||||||
// dispatchGroup is used to ensure we have migrated all sites before returning them
|
// 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
|
// If there are no sites to migrate, there are never any entrants
|
||||||
let dispatchGroup = DispatchGroup()
|
let dispatchGroup = DispatchGroup()
|
||||||
|
|
||||||
NETunnelProviderManager.loadAllFromPreferences() { newManagers, err in
|
NETunnelProviderManager.loadAllFromPreferences { newManagers, err in
|
||||||
if (err != nil) {
|
if err != nil {
|
||||||
return completion(nil, err)
|
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 timer = RepeatingTimer(timeInterval: 15 * 60) // 15 * 60 is 15 minutes
|
||||||
private let log = Logger(subsystem: "net.defined.mobileNebula", category: "DNUpdater")
|
private let log = Logger(subsystem: "net.defined.mobileNebula", category: "DNUpdater")
|
||||||
|
|
||||||
func updateAll(onUpdate: @escaping (Site) -> Void) {
|
private func updateAll(onUpdate: @escaping (Site) -> Void) {
|
||||||
_ = SiteList { (sites, _) -> Void in
|
SiteList.loadAll(completion: { (sites, _) -> Void in
|
||||||
if let unwrappedSites = sites?.values {
|
if let unwrappedSites = sites?.values {
|
||||||
// NEVPN seems to force us onto the main thread and we are about to make network calls that
|
// 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.
|
// 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) {
|
func updateAllLoop(onUpdate: @escaping (Site) -> Void) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import NetworkExtension
|
|
||||||
import MobileNebula
|
import MobileNebula
|
||||||
|
import NetworkExtension
|
||||||
|
|
||||||
class SiteContainer {
|
class SiteContainer {
|
||||||
var site: Site
|
var site: Site
|
||||||
|
@ -19,15 +19,15 @@ class Sites {
|
||||||
self.messenger = messenger
|
self.messenger = messenger
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSites(completion: @escaping ([String: Site]?, (any Error)?) -> ()) {
|
func loadSites(completion: @escaping ([String: Site]?, (any Error)?) -> Void) {
|
||||||
_ = SiteList { (sites, err) in
|
SiteList.loadAll(completion: { (sites, err) in
|
||||||
if (err != nil) {
|
if err != nil {
|
||||||
return completion(nil, err)
|
return completion(nil, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sites?.values.forEach { site in
|
sites?.values.forEach { site in
|
||||||
var updater = self.containers[site.id]?.updater
|
var updater = self.containers[site.id]?.updater
|
||||||
if (updater != nil) {
|
if updater != nil {
|
||||||
updater!.setSite(site: site)
|
updater!.setSite(site: site)
|
||||||
} else {
|
} else {
|
||||||
updater = SiteUpdater(messenger: self.messenger!, site: site)
|
updater = SiteUpdater(messenger: self.messenger!, site: site)
|
||||||
|
@ -39,10 +39,10 @@ class Sites {
|
||||||
return $0.site
|
return $0.site
|
||||||
}
|
}
|
||||||
completion(justSites, nil)
|
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) {
|
if let site = self.containers.removeValue(forKey: id) {
|
||||||
_ = KeyChain.delete(key: "\(site.site.id).dnCredentials")
|
_ = KeyChain.delete(key: "\(site.site.id).dnCredentials")
|
||||||
_ = KeyChain.delete(key: "\(site.site.id).key")
|
_ = KeyChain.delete(key: "\(site.site.id).key")
|
||||||
|
@ -79,8 +79,8 @@ class Sites {
|
||||||
}
|
}
|
||||||
|
|
||||||
class SiteUpdater: NSObject, FlutterStreamHandler {
|
class SiteUpdater: NSObject, FlutterStreamHandler {
|
||||||
private var eventSink: FlutterEventSink?;
|
private var eventSink: FlutterEventSink?
|
||||||
private var eventChannel: FlutterEventChannel;
|
private var eventChannel: FlutterEventChannel
|
||||||
private var site: Site
|
private var site: Site
|
||||||
private var notification: Any?
|
private var notification: Any?
|
||||||
public var startFunc: (() -> Void)?
|
public var startFunc: (() -> Void)?
|
||||||
|
@ -124,7 +124,7 @@ class SiteUpdater: NSObject, FlutterStreamHandler {
|
||||||
|
|
||||||
/// onListen is called when flutter code attaches an event listener
|
/// onListen is called when flutter code attaches an event listener
|
||||||
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
||||||
eventSink = events;
|
eventSink = events
|
||||||
|
|
||||||
#if !targetEnvironment(simulator)
|
#if !targetEnvironment(simulator)
|
||||||
if site.manager == nil {
|
if site.manager == nil {
|
||||||
|
@ -133,7 +133,9 @@ class SiteUpdater: NSObject, FlutterStreamHandler {
|
||||||
return FlutterError(code: "Internal Error", message: "Flutter manager was not present", details: nil)
|
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
|
let oldConnected = self.site.connected
|
||||||
self.site.status = statusString[self.site.manager!.connection.status]
|
self.site.status = statusString[self.site.manager!.connection.status]
|
||||||
self.site.connected = statusMap[self.site.manager!.connection.status]
|
self.site.connected = statusMap[self.site.manager!.connection.status]
|
||||||
|
@ -152,7 +154,7 @@ class SiteUpdater: NSObject, FlutterStreamHandler {
|
||||||
|
|
||||||
/// onCancel is called when the flutter listener stops listening
|
/// onCancel is called when the flutter listener stops listening
|
||||||
func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||||
if (self.notification != nil) {
|
if self.notification != nil {
|
||||||
NotificationCenter.default.removeObserver(self.notification!)
|
NotificationCenter.default.removeObserver(self.notification!)
|
||||||
}
|
}
|
||||||
return nil
|
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
|
/// 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) {
|
func update(connected: Bool, replaceSite: Site? = nil) {
|
||||||
if (replaceSite != nil) {
|
if replaceSite != nil {
|
||||||
site = replaceSite!
|
site = replaceSite!
|
||||||
}
|
}
|
||||||
site.connected = connected
|
site.connected = connected
|
||||||
|
|
Loading…
Reference in a new issue