From f4b284acd8a559af414b7a0336784ac2f4a60df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 11 Oct 2025 15:07:09 +0800 Subject: [PATCH] Update usages of libbox --- .../Views/Setting/SettingView.swift | 2 +- .../ShadredPreferences+Database.swift | 10 +- Library/Network/CommandClient.swift | 14 ++- .../Network/ExtensionPlatformInterface.swift | 18 ++-- Library/Network/ExtensionProvider.swift | 98 +++++++------------ SFT/ApplicationDelegate.swift | 1 - 6 files changed, 64 insertions(+), 79 deletions(-) diff --git a/ApplicationLibrary/Views/Setting/SettingView.swift b/ApplicationLibrary/Views/Setting/SettingView.swift index de965fe..8cc2172 100644 --- a/ApplicationLibrary/Views/Setting/SettingView.swift +++ b/ApplicationLibrary/Views/Setting/SettingView.swift @@ -4,7 +4,7 @@ import SwiftUI public struct SettingView: View { private enum Tabs: Int, CaseIterable, Identifiable { - public var id: Self { + var id: Self { self } diff --git a/Library/Database/ShadredPreferences+Database.swift b/Library/Database/ShadredPreferences+Database.swift index 3dc9b13..8013001 100644 --- a/Library/Database/ShadredPreferences+Database.swift +++ b/Library/Database/ShadredPreferences+Database.swift @@ -61,12 +61,12 @@ extension SharedPreferences { } private class Item: Record, Identifiable { - public var id: String { + var id: String { name } - public var name: String - public var data: Data + var name: String + var data: Data init(name: String, data: Data) { self.name = name @@ -74,7 +74,7 @@ private class Item: Record, Identifiable { super.init() } - override public class var databaseTableName: String { + override class var databaseTableName: String { "preferences" } @@ -88,7 +88,7 @@ private class Item: Record, Identifiable { try super.init(row: row) } - override public func encode(to container: inout PersistenceContainer) throws { + override func encode(to container: inout PersistenceContainer) throws { container[Columns.name] = name container[Columns.data] = data } diff --git a/Library/Network/CommandClient.swift b/Library/Network/CommandClient.swift index d8efa81..ac14f19 100644 --- a/Library/Network/CommandClient.swift +++ b/Library/Network/CommandClient.swift @@ -18,6 +18,7 @@ public class CommandClient: ObservableObject { @Published public var status: LibboxStatusMessage? @Published public var groups: [LibboxOutboundGroup]? @Published public var logList: [String] + @Published public var defaultLogLevel: Int32 = 0 @Published public var clashModeList: [String] @Published public var clashMode: String @@ -160,20 +161,29 @@ public class CommandClient: ObservableObject { } } + func setDefaultLogLevel(_ level: Int32) { + DispatchQueue.main.async { [self] in + commandClient.defaultLogLevel = level + } + } + func clearLogs() { DispatchQueue.main.async { [self] in commandClient.logList.removeAll() } } - func writeLogs(_ messageList: (any LibboxStringIteratorProtocol)?) { + func writeLogs(_ messageList: (any LibboxLogIteratorProtocol)?) { guard let messageList else { return } DispatchQueue.main.async { [self] in var newLogList = commandClient.logList while messageList.hasNext() { - newLogList.append(messageList.next()) + let logEntry = messageList.next()! + if logEntry.level <= commandClient.defaultLogLevel { + newLogList.append(logEntry.message) + } } if newLogList.count >= commandClient.logMaxLines { newLogList.removeSubrange(0 ... newLogList.count - commandClient.logMaxLines) diff --git a/Library/Network/ExtensionPlatformInterface.swift b/Library/Network/ExtensionPlatformInterface.swift index eeb3986..6f80ef4 100644 --- a/Library/Network/ExtensionPlatformInterface.swift +++ b/Library/Network/ExtensionPlatformInterface.swift @@ -356,18 +356,17 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc #endif } + public func serviceStop() throws { + reset() + } + public func serviceReload() throws { runBlocking { [self] in await tunnel.reloadService() } } - public func postServiceClose() { - reset() - tunnel.postServiceClose() - } - - public func getSystemProxyStatus() -> LibboxSystemProxyStatus? { + public func getSystemProxyStatus() throws -> LibboxSystemProxyStatus { let status = LibboxSystemProxyStatus() guard let networkSettings else { return status @@ -404,6 +403,13 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc } } + public func writeDebugMessage(_ message: String?) { + guard let message else { + return + } + tunnel.writeMessage(message) + } + func reset() { networkSettings = nil } diff --git a/Library/Network/ExtensionProvider.swift b/Library/Network/ExtensionProvider.swift index 6d83ed1..21cba3f 100644 --- a/Library/Network/ExtensionProvider.swift +++ b/Library/Network/ExtensionProvider.swift @@ -11,9 +11,6 @@ import NetworkExtension open class ExtensionProvider: NEPacketTunnelProvider { public var username: String? private var commandServer: LibboxCommandServer! - private var boxService: LibboxBoxService! - private var systemProxyAvailable = false - private var systemProxyEnabled = false private var platformInterface: ExtensionPlatformInterface! override open func startTunnel(options _: [String: NSObject]?) async throws { @@ -23,22 +20,17 @@ open class ExtensionProvider: NEPacketTunnelProvider { options.basePath = FilePath.sharedDirectory.relativePath options.workingPath = FilePath.workingDirectory.relativePath options.tempPath = FilePath.cacheDirectory.relativePath - var error: NSError? - #if os(tvOS) - options.isTVOS = true - #endif - if let username { - options.username = username - } - LibboxSetup(options, &error) - if let error { - writeFatalError("(packet-tunnel) error: setup service: \(error.localizedDescription)") + var setupError: NSError? + LibboxSetup(options, &setupError) + if let setupError { + writeFatalError("(packet-tunnel) error: setup service: \(setupError.localizedDescription)") return } - LibboxRedirectStderr(FilePath.cacheDirectory.appendingPathComponent("stderr.log").relativePath, &error) - if let error { - writeFatalError("(packet-tunnel) redirect stderr error: \(error.localizedDescription)") + var stderrError: NSError? + LibboxRedirectStderr(FilePath.cacheDirectory.appendingPathComponent("stderr.log").relativePath, &stderrError) + if let stderrError { + writeFatalError("(packet-tunnel) redirect stderr error: \(stderrError.localizedDescription)") return } @@ -47,11 +39,16 @@ open class ExtensionProvider: NEPacketTunnelProvider { if platformInterface == nil { platformInterface = ExtensionPlatformInterface(self) } - commandServer = await LibboxNewCommandServer(platformInterface, Int32(SharedPreferences.maxLogLines.get())) + var error: NSError? + commandServer = LibboxNewCommandServer(platformInterface, platformInterface, &error) + if let error { + writeFatalError("(packet-tunnel): create command server error: \(error.localizedDescription)") + return + } do { try commandServer.start() } catch { - writeFatalError("(packet-tunnel): log server start error: \(error.localizedDescription)") + writeFatalError("(packet-tunnel): start command server error: \(error.localizedDescription)") return } writeMessage("(packet-tunnel): Here I stand") @@ -65,7 +62,7 @@ open class ExtensionProvider: NEPacketTunnelProvider { func writeMessage(_ message: String) { if let commandServer { - commandServer.writeMessage(message) + commandServer.writeMessage(2, message: message) } } @@ -73,9 +70,9 @@ open class ExtensionProvider: NEPacketTunnelProvider { #if DEBUG NSLog(message) #endif - writeMessage(message) - var error: NSError? - LibboxWriteServiceError(message, &error) + if let commandServer { + commandServer.setError(message) + } cancelTunnelWithError(nil) } @@ -98,33 +95,23 @@ open class ExtensionProvider: NEPacketTunnelProvider { writeFatalError("(packet-tunnel) error: read config file \(profile.path): \(error.localizedDescription)") return } - var error: NSError? - let service = LibboxNewService(configContent, platformInterface, &error) - if let error { - writeFatalError("(packet-tunnel) error: create service: \(error.localizedDescription)") - return - } - guard let service else { - return - } + let options = LibboxOverrideOptions() do { - try service.start() + try commandServer.startOrReloadService(configContent, options: options) } catch { writeFatalError("(packet-tunnel) error: start service: \(error.localizedDescription)") return } - commandServer.setService(service) - boxService = service #if os(macOS) await SharedPreferences.startedByUser.set(true) - if service.needWIFIState() { + if commandServer.needWIFIState() { if !Variant.useSystemExtension { locationManager = CLLocationManager() - locationDelegate = stubLocationDelegate(boxService) + locationDelegate = stubLocationDelegate() locationManager?.delegate = locationDelegate locationManager?.requestLocation() } else { - commandServer.writeMessage("(packet-tunnel) WIFI SSID and BSSID information is not currently available in the standalone version of SFM. We are working on resolving this issue.") + writeMessage("(packet-tunnel) WIFI SSID and BSSID information is not currently available in the standalone version of SFM. We are working on resolving this issue.") } } #endif @@ -136,14 +123,7 @@ open class ExtensionProvider: NEPacketTunnelProvider { private var locationDelegate: stubLocationDelegate? class stubLocationDelegate: NSObject, CLLocationManagerDelegate { - private unowned let boxService: LibboxBoxService - init(_ boxService: LibboxBoxService) { - self.boxService = boxService - } - - func locationManagerDidChangeAuthorization(_: CLLocationManager) { - boxService.updateWIFIState() - } + func locationManagerDidChangeAuthorization(_: CLLocationManager) {} func locationManager(_: CLLocationManager, didUpdateLocations _: [CLLocation]) {} @@ -153,14 +133,10 @@ open class ExtensionProvider: NEPacketTunnelProvider { #endif private func stopService() { - if let service = boxService { - do { - try service.close() - } catch { - writeMessage("(packet-tunnel) error: stop service: \(error.localizedDescription)") - } - boxService = nil - commandServer.setService(nil) + do { + try commandServer.closeService() + } catch { + writeMessage("(packet-tunnel) error: stop service: \(error.localizedDescription)") } if let platformInterface { platformInterface.reset() @@ -173,21 +149,15 @@ open class ExtensionProvider: NEPacketTunnelProvider { defer { reasserting = false } - stopService() - commandServer.resetLog() await startService() } - func postServiceClose() { - boxService = nil - } - override open func stopTunnel(with reason: NEProviderStopReason) async { writeMessage("(packet-tunnel) stopping, reason: \(reason)") stopService() if let server = commandServer { try? await Task.sleep(nanoseconds: 100 * NSEC_PER_MSEC) - try? server.close() + server.close() commandServer = nil } #if os(macOS) @@ -207,14 +177,14 @@ open class ExtensionProvider: NEPacketTunnelProvider { } override open func sleep() async { - if let boxService { - boxService.pause() + if let commandServer { + commandServer.pause() } } override open func wake() { - if let boxService { - boxService.wake() + if let commandServer { + commandServer.wake() } } } diff --git a/SFT/ApplicationDelegate.swift b/SFT/ApplicationDelegate.swift index d5844a4..66b6c4f 100644 --- a/SFT/ApplicationDelegate.swift +++ b/SFT/ApplicationDelegate.swift @@ -11,7 +11,6 @@ class ApplicationDelegate: NSObject, UIApplicationDelegate { options.basePath = FilePath.sharedDirectory.relativePath options.workingPath = FilePath.workingDirectory.relativePath options.tempPath = FilePath.cacheDirectory.relativePath - options.isTVOS = true var error: NSError? LibboxSetup(options, &error) LibboxSetLocale(Locale.current.identifier)