Improve UI

This commit is contained in:
世界
2024-02-08 10:37:37 +08:00
parent 70d43abd52
commit 1f6e99f576
32 changed files with 1201 additions and 646 deletions
@@ -64,6 +64,16 @@ public extension LibboxProfileContent {
}
}
public extension String {
func generateShareFile(name: String) throws -> URL {
let shareDirectory = FilePath.cacheDirectory.appendingPathComponent("share", isDirectory: true)
try FileManager.default.createDirectory(at: shareDirectory, withIntermediateDirectories: true)
let shareFile = shareDirectory.appendingPathComponent(name)
try write(to: shareFile, atomically: true, encoding: .utf8)
return shareFile
}
}
@available(iOS 16.0, macOS 13.0, *)
public struct TypedProfile: Transferable, Codable {
public let content: LibboxProfileContent
@@ -27,7 +27,7 @@ extension SharedPreferences {
}
}
public nonisolated func set(_ newValue: T) async {
public nonisolated func set(_ newValue: T?) async {
do {
try await SharedPreferences.write(name, newValue)
} catch {
+50 -5
View File
@@ -4,25 +4,54 @@ public enum SharedPreferences {
public static let selectedProfileID = Preference<Int64>("selected_profile_id", defaultValue: -1)
#if os(macOS)
private static let disableMemoryLimitByDefault = true
private static let ignoreMemoryLimitByDefault = true
#else
private static let disableMemoryLimitByDefault = false
private static let ignoreMemoryLimitByDefault = false
#endif
public static let disableMemoryLimit = Preference<Bool>("disable_memory_limit", defaultValue: disableMemoryLimitByDefault)
public static let alwaysOn = Preference<Bool>("always_on", defaultValue: false)
public static let ignoreMemoryLimit = Preference<Bool>("ignore_memory_limit", defaultValue: ignoreMemoryLimitByDefault)
public static let ignoreDeviceSleep = Preference<Bool>("ignore_device_sleep", defaultValue: false)
#if os(iOS)
public static let excludeLocalNetworksByDefault = true
#elseif os(macOS)
public static let excludeLocalNetworksByDefault = false
#endif
#if !os(tvOS)
public static let includeAllNetworks = Preference<Bool>("include_all_networks", defaultValue: false)
public static let excludeAPNs = Preference<Bool>("exclude_apns", defaultValue: true)
public static let excludeLocalNetworks = Preference<Bool>("exclude_local_networks", defaultValue: excludeLocalNetworksByDefault)
public static let excludeCellularServices = Preference<Bool>("exclude_celluar_services", defaultValue: true)
public static let enforceRoutes = Preference<Bool>("enforce_routes", defaultValue: false)
#endif
public static func resetPacketTunnel() async {
await ignoreMemoryLimit.set(nil)
await ignoreDeviceSleep.set(nil)
#if !os(tvOS)
await includeAllNetworks.set(nil)
await excludeAPNs.set(nil)
await excludeLocalNetworks.set(nil)
await excludeCellularServices.set(nil)
await enforceRoutes.set(nil)
#endif
}
public static let maxLogLines = Preference<Int>("max_log_lines", defaultValue: 300)
public static let alwaysOn = Preference<Bool>("always_on", defaultValue: false)
public static let ignoreDeviceSleep = Preference<Bool>("ignore_device_sleep", defaultValue: false)
#if os(macOS)
public static let showMenuBarExtra = Preference<Bool>("show_menu_bar_extra", defaultValue: true)
public static let menuBarExtraInBackground = Preference<Bool>("menu_bar_extra_in_background", defaultValue: false)
public static let startedByUser = Preference<Bool>("started_by_user", defaultValue: false)
public static func resetMacOS() async {
await showMenuBarExtra.set(nil)
await menuBarExtraInBackground.set(nil)
}
#endif
#if os(iOS)
@@ -30,4 +59,20 @@ public enum SharedPreferences {
#endif
public static let systemProxyEnabled = Preference<Bool>("system_proxy_enabled", defaultValue: true)
// Profile Override
public static let excludeDefaultRoute = Preference<Bool>("exclude_default_route", defaultValue: false)
public static let autoRouteUseSubRangesByDefault = Preference<Bool>("auto_route_use_sub_ranges_by_default", defaultValue: false)
public static func resetProfileOverride() async {
await excludeDefaultRoute.set(nil)
await autoRouteUseSubRangesByDefault.set(nil)
}
#if DEBUG
public static let inDebug = true
#else
public static let inDebug = false
#endif
}
@@ -5,8 +5,10 @@ public class ExtensionEnvironments: ObservableObject {
@Published public var logClient = CommandClient(.log)
@Published public var extensionProfileLoading = true
@Published public var extensionProfile: ExtensionProfile?
public let profileUpdate = ObjectWillChangePublisher()
public let selectedProfileUpdate = ObjectWillChangePublisher()
public let openSettings = ObjectWillChangePublisher()
public init() {}
@@ -27,6 +27,8 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
throw NSError(domain: "nil return pointer", code: 0)
}
let autoRouteUseSubRangesByDefault = await SharedPreferences.autoRouteUseSubRangesByDefault.get()
let settings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "127.0.0.1")
if options.getAutoRoute() {
settings.mtu = NSNumber(value: options.getMTU())
@@ -57,6 +59,15 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
let ipv4RoutePrefix = inet4RouteAddressIterator.next()!
ipv4Routes.append(NEIPv4Route(destinationAddress: ipv4RoutePrefix.address(), subnetMask: ipv4RoutePrefix.mask()))
}
} else if autoRouteUseSubRangesByDefault {
ipv4Routes.append(NEIPv4Route(destinationAddress: "1.0.0.0", subnetMask: "255.0.0.0"))
ipv4Routes.append(NEIPv4Route(destinationAddress: "2.0.0.0", subnetMask: "254.0.0.0"))
ipv4Routes.append(NEIPv4Route(destinationAddress: "4.0.0.0", subnetMask: "252.0.0.0"))
ipv4Routes.append(NEIPv4Route(destinationAddress: "8.0.0.0", subnetMask: "248.0.0.0"))
ipv4Routes.append(NEIPv4Route(destinationAddress: "16.0.0.0", subnetMask: "240.0.0.0"))
ipv4Routes.append(NEIPv4Route(destinationAddress: "32.0.0.0", subnetMask: "224.0.0.0"))
ipv4Routes.append(NEIPv4Route(destinationAddress: "64.0.0.0", subnetMask: "192.0.0.0"))
ipv4Routes.append(NEIPv4Route(destinationAddress: "128.0.0.0", subnetMask: "128.0.0.0"))
} else {
ipv4Routes.append(NEIPv4Route.default())
}
@@ -66,6 +77,13 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
let ipv4RoutePrefix = inet4RouteExcludeAddressIterator.next()!
ipv4ExcludeRoutes.append(NEIPv4Route(destinationAddress: ipv4RoutePrefix.address(), subnetMask: ipv4RoutePrefix.mask()))
}
if await SharedPreferences.excludeDefaultRoute.get(), !ipv4Routes.isEmpty {
if !ipv4ExcludeRoutes.contains(where: { it in
it.destinationAddress == "0.0.0.0" && it.destinationSubnetMask == "255.255.255.254"
}) {
ipv4ExcludeRoutes.append(NEIPv4Route(destinationAddress: "0.0.0.0", subnetMask: "255.255.255.254"))
}
}
ipv4Settings.includedRoutes = ipv4Routes
ipv4Settings.excludedRoutes = ipv4ExcludeRoutes
@@ -89,6 +107,15 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
let ipv6RoutePrefix = inet6RouteAddressIterator.next()!
ipv6Routes.append(NEIPv6Route(destinationAddress: ipv6RoutePrefix.address(), networkPrefixLength: NSNumber(value: ipv6RoutePrefix.prefix())))
}
} else if autoRouteUseSubRangesByDefault {
ipv6Routes.append(NEIPv6Route(destinationAddress: "100::", networkPrefixLength: 8))
ipv6Routes.append(NEIPv6Route(destinationAddress: "200::", networkPrefixLength: 7))
ipv6Routes.append(NEIPv6Route(destinationAddress: "400::", networkPrefixLength: 6))
ipv6Routes.append(NEIPv6Route(destinationAddress: "800::", networkPrefixLength: 5))
ipv6Routes.append(NEIPv6Route(destinationAddress: "1000::", networkPrefixLength: 4))
ipv6Routes.append(NEIPv6Route(destinationAddress: "2000::", networkPrefixLength: 3))
ipv6Routes.append(NEIPv6Route(destinationAddress: "4000::", networkPrefixLength: 2))
ipv6Routes.append(NEIPv6Route(destinationAddress: "8000::", networkPrefixLength: 1))
} else {
ipv6Routes.append(NEIPv6Route.default())
}
+1 -1
View File
@@ -35,7 +35,7 @@ open class ExtensionProvider: NEPacketTunnelProvider {
writeError("(packet-tunnel) redirect stderr error: \(error.localizedDescription)")
}
await LibboxSetMemoryLimit(!SharedPreferences.disableMemoryLimit.get())
await LibboxSetMemoryLimit(!SharedPreferences.ignoreMemoryLimit.get())
ignoreDeviceSleep = await SharedPreferences.ignoreDeviceSleep.get()
if platformInterface == nil {
+2 -2
View File
@@ -111,13 +111,13 @@
return false
}
public static func install(forceUpdate: Bool = false, inBackground: Bool = false) async throws -> OSSystemExtensionRequest.Result? {
public nonisolated static func install(forceUpdate: Bool = false, inBackground: Bool = false) async throws -> OSSystemExtensionRequest.Result? {
try await Task.detached {
try SystemExtension(forceUpdate, inBackground).activation()
}.result.get()
}
public static func uninstall() async throws -> OSSystemExtensionRequest.Result? {
public nonisolated static func uninstall() async throws -> OSSystemExtensionRequest.Result? {
try await Task.detached {
try SystemExtension().deactivation()
}.result.get()
+25
View File
@@ -0,0 +1,25 @@
import Foundation
import SwiftUI
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif
public extension Color {
static var textColor: Color {
#if canImport(UIKit)
return Color(uiColor: .label)
#elseif canImport(AppKit)
return Color(nsColor: .textColor)
#endif
}
static var linkColor: Color {
#if canImport(UIKit)
return Color(uiColor: .link)
#elseif canImport(AppKit)
return Color(nsColor: .linkColor)
#endif
}
}