This commit is contained in:
Neil Alexander 2024-06-23 10:54:21 +01:00
parent 0bea3a1613
commit 1b9e84d056
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
9 changed files with 234 additions and 137 deletions

View file

@ -32,9 +32,14 @@ class PlatformItemSource: NSObject {}
class ConfigurationProxy: PlatformItemSource {
private var manager: NETunnelProviderManager?
private var json: Data? = nil
private var json: Data? = nil {
didSet {
summary = MobileSummaryFromConfig(json)
}
}
private var dict: [String: Any]? = [:]
private var timer: Timer?
public var summary: MobileParsedConfig?
init(manager: NETunnelProviderManager) {
self.manager = manager
@ -46,11 +51,11 @@ class ConfigurationProxy: PlatformItemSource {
} catch {
NSLog("ConfigurationProxy: Error deserialising JSON (\(error))")
}
#if os(iOS)
#if os(iOS)
self.set("name", inSection: "NodeInfo", to: UIDevice.current.name)
#elseif os(OSX)
#elseif os(OSX)
self.set("name", inSection: "NodeInfo", to: Host.current().localizedName ?? "")
#endif
#endif
self.fix()
}
@ -131,16 +136,16 @@ class ConfigurationProxy: PlatformItemSource {
self.saveSoon()
}
}
public var autoStartAny: Bool {
get {
return self.get("Any", inSection: "AutoStart") as? Bool ?? false
}
set {
self.set("Any", inSection: "AutoStart", to: newValue)
self.saveSoon()
}
}
public var autoStartAny: Bool {
get {
return self.get("Any", inSection: "AutoStart") as? Bool ?? false
}
set {
self.set("Any", inSection: "AutoStart", to: newValue)
self.saveSoon()
}
}
public var autoStartWiFi: Bool {
get {
@ -303,29 +308,29 @@ class ConfigurationProxy: PlatformItemSource {
wifirule.interfaceTypeMatch = .any
rules.insert(wifirule, at: 0)
}
#if os(macOS)
#if os(macOS)
if self.get("Ethernet", inSection: "AutoStart") as? Bool ?? false {
let wifirule = NEOnDemandRuleConnect()
wifirule.interfaceTypeMatch = .ethernet
rules.insert(wifirule, at: 0)
}
#endif
#endif
if self.get("WiFi", inSection: "AutoStart") as? Bool ?? false {
let wifirule = NEOnDemandRuleConnect()
wifirule.interfaceTypeMatch = .wiFi
rules.insert(wifirule, at: 0)
}
#if canImport(UIKit)
#if canImport(UIKit)
if self.get("Mobile", inSection: "AutoStart") as? Bool ?? false {
let mobilerule = NEOnDemandRuleConnect()
mobilerule.interfaceTypeMatch = .cellular
rules.insert(mobilerule, at: 0)
}
#endif
#endif
manager.onDemandRules = rules
manager.isOnDemandEnabled = rules.count > 1
providerProtocol.disconnectOnSleep = rules.count > 1
manager.protocolConfiguration = providerProtocol
manager.saveToPreferences(completionHandler: { error in
@ -345,8 +350,8 @@ class ConfigurationProxy: PlatformItemSource {
private func convertToJson() throws {
self.json = try JSONSerialization.data(withJSONObject: self.dict as Any, options: .prettyPrinted)
}
#if canImport(UIKit)
#if canImport(UIKit)
override func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return "yggdrasil.conf"
}
@ -361,5 +366,5 @@ class ConfigurationProxy: PlatformItemSource {
}
return "yggdrasil.conf.json"
}
#endif
#endif
}

View file

@ -72,7 +72,7 @@ class CrossPlatformAppDelegate: PlatformAppDelegate, ObservableObject {
@Published var yggdrasilIP: String = "N/A"
@Published var yggdrasilSubnet: String = "N/A"
@Published var yggdrasilCoords: String = "[]"
@Published var yggdrasilPeers: [YggdrasilPeer] = []
func yggdrasilVersion() -> String {
@ -108,11 +108,12 @@ class CrossPlatformAppDelegate: PlatformAppDelegate, ObservableObject {
func becameBackground() {}
func updateStatus(conn: NEVPNConnection) {
if conn.status == .connected {
if conn.status == .connected || conn.status == .connecting {
self.yggdrasilEnabled = true
self.requestSummaryIPC()
} else if conn.status == .disconnecting || conn.status == .disconnected {
self.clearStatus()
self.yggdrasilEnabled = false
self.clearStatus()
}
}
@ -188,6 +189,8 @@ class CrossPlatformAppDelegate: PlatformAppDelegate, ObservableObject {
self.yggdrasilPublicKey = summary.publicKey
self.yggdrasilPeers = summary.peers
self.yggdrasilConnected = summary.peers.filter { $0.up }.count > 0
print("Response: \(String(data: js, encoding: .utf8))")
}
}
}

View file

@ -32,6 +32,9 @@ struct YggdrasilPeer: Codable, Identifiable {
let key: String?
let priority: UInt8
let cost: UInt16?
let rxBytes: Double?
let txBytes: Double?
let uptime: Int64?
enum CodingKeys: String, CodingKey {
case remote = "URI"
@ -40,6 +43,9 @@ struct YggdrasilPeer: Codable, Identifiable {
case key = "Key"
case priority = "Priority"
case cost = "Cost"
case rxBytes = "RXBytes"
case txBytes = "TXBytes"
case uptime = "Uptime"
}
public func getStatusBadgeColor() -> SwiftUI.Color {