Log for iOS by @ Akuma

This commit is contained in:
realWLF
2024-02-04 02:50:54 +03:30
parent b1d6c56a3e
commit a0da2e52c2
5 changed files with 62 additions and 224 deletions

View File

@@ -1,160 +0,0 @@
import Foundation
import Libcore
public class CommandClient: ObservableObject {
public enum ConnectionType {
case status
case groups
case log
case clashMode
}
private let connectionType: ConnectionType
private let logMaxLines: Int
private var commandClient: LibboxCommandClient?
private var connectTask: Task<Void, Error>?
@Published public var isConnected: Bool
@Published public var status: LibboxStatusMessage?
@Published public var groups: [LibboxOutboundGroup]?
@Published public var logList: [String]
@Published public var clashModeList: [String]
@Published public var clashMode: String
public init(_ connectionType: ConnectionType, logMaxLines: Int = 300) {
self.connectionType = connectionType
self.logMaxLines = logMaxLines
logList = []
clashModeList = []
clashMode = ""
isConnected = false
}
public func connect() {
if isConnected {
return
}
if let connectTask {
connectTask.cancel()
}
connectTask = Task {
await connect0()
}
}
public func disconnect() {
if let connectTask {
connectTask.cancel()
self.connectTask = nil
}
if let commandClient {
try? commandClient.disconnect()
self.commandClient = nil
}
}
private nonisolated func connect0() async {
let clientOptions = LibboxCommandClientOptions()
switch connectionType {
case .status:
clientOptions.command = LibboxCommandStatus
case .groups:
clientOptions.command = LibboxCommandGroup
case .log:
clientOptions.command = LibboxCommandLog
case .clashMode:
clientOptions.command = LibboxCommandClashMode
}
clientOptions.statusInterval = Int64(2 * NSEC_PER_SEC)
let client = LibboxNewCommandClient(clientHandler(self), clientOptions)!
do {
for i in 0 ..< 10 {
try await Task.sleep(nanoseconds: UInt64(Double(100 + (i * 50)) * Double(NSEC_PER_MSEC)))
try Task.checkCancellation()
do {
try client.connect()
await MainActor.run {
commandClient = client
}
return
} catch {}
try Task.checkCancellation()
}
} catch {
try? client.disconnect()
}
}
private class clientHandler: NSObject, LibboxCommandClientHandlerProtocol {
private let commandClient: CommandClient
init(_ commandClient: CommandClient) {
self.commandClient = commandClient
}
func connected() {
DispatchQueue.main.async { [self] in
if commandClient.connectionType == .log {
commandClient.logList = []
}
commandClient.isConnected = true
}
}
func disconnected(_: String?) {
DispatchQueue.main.async { [self] in
commandClient.isConnected = false
}
}
func clearLog() {
DispatchQueue.main.async { [self] in
commandClient.logList.removeAll()
}
}
func writeLog(_ message: String?) {
guard let message else {
return
}
DispatchQueue.main.async { [self] in
if commandClient.logList.count > commandClient.logMaxLines {
commandClient.logList.removeFirst()
}
commandClient.logList.append(message)
}
}
func writeStatus(_ message: LibboxStatusMessage?) {
DispatchQueue.main.async { [self] in
commandClient.status = message
}
}
func writeGroups(_ groups: LibboxOutboundGroupIteratorProtocol?) {
guard let groups else {
return
}
var newGroups: [LibboxOutboundGroup] = []
while groups.hasNext() {
newGroups.append(groups.next()!)
}
DispatchQueue.main.async { [self] in
commandClient.groups = newGroups
}
}
func initializeClashMode(_ modeList: LibboxStringIteratorProtocol?, currentMode: String?) {
DispatchQueue.main.async { [self] in
commandClient.clashModeList = modeList!.toArray()
commandClient.clashMode = currentMode!
}
}
func updateClashMode(_ newMode: String?) {
DispatchQueue.main.async { [self] in
commandClient.clashMode = newMode!
}
}
}
}

View File

@@ -1,12 +0,0 @@
import Foundation
import Libcore
extension LibboxStringIteratorProtocol {
func toArray() -> [String] {
var array: [String] = []
while hasNext() {
array.append(next())
}
return array
}
}

View File

@@ -42,8 +42,6 @@
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; }; 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; }; 97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
D78B64E92B6EB41900C5D800 /* Extension+Iterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D78B64E82B6EB41900C5D800 /* Extension+Iterator.swift */; };
D7A729302B6E44F200FFFF41 /* CommandClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A7292F2B6E44F200FFFF41 /* CommandClient.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@@ -166,8 +164,6 @@
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; 97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9AC67B4DCF829F5B6F63AA7D /* Pods-Runner-SingBoxPacketTunnel.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner-SingBoxPacketTunnel.release.xcconfig"; path = "Target Support Files/Pods-Runner-SingBoxPacketTunnel/Pods-Runner-SingBoxPacketTunnel.release.xcconfig"; sourceTree = "<group>"; }; 9AC67B4DCF829F5B6F63AA7D /* Pods-Runner-SingBoxPacketTunnel.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner-SingBoxPacketTunnel.release.xcconfig"; path = "Target Support Files/Pods-Runner-SingBoxPacketTunnel/Pods-Runner-SingBoxPacketTunnel.release.xcconfig"; sourceTree = "<group>"; };
C20A211B58CE31B2738D133C /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; }; C20A211B58CE31B2738D133C /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
D78B64E82B6EB41900C5D800 /* Extension+Iterator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Extension+Iterator.swift"; sourceTree = "<group>"; };
D7A7292F2B6E44F200FFFF41 /* CommandClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandClient.swift; sourceTree = "<group>"; };
F3FFE1D9C2D5629FACC123EE /* Pods-Runner-SingBoxPacketTunnel.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner-SingBoxPacketTunnel.profile.xcconfig"; path = "Target Support Files/Pods-Runner-SingBoxPacketTunnel/Pods-Runner-SingBoxPacketTunnel.profile.xcconfig"; sourceTree = "<group>"; }; F3FFE1D9C2D5629FACC123EE /* Pods-Runner-SingBoxPacketTunnel.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner-SingBoxPacketTunnel.profile.xcconfig"; path = "Target Support Files/Pods-Runner-SingBoxPacketTunnel/Pods-Runner-SingBoxPacketTunnel.profile.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
@@ -321,7 +317,6 @@
97C146E51CF9000F007C117D = { 97C146E51CF9000F007C117D = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
D7A7292E2B6E44D500FFFF41 /* Network */,
6836D3FF2B57FECF00A79D75 /* Local Packages */, 6836D3FF2B57FECF00A79D75 /* Local Packages */,
03E392CD2ADDE103000ADF15 /* Shared */, 03E392CD2ADDE103000ADF15 /* Shared */,
9740EEB11CF90186004384FC /* Flutter */, 9740EEB11CF90186004384FC /* Flutter */,
@@ -399,15 +394,6 @@
name = Frameworks; name = Frameworks;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
D7A7292E2B6E44D500FFFF41 /* Network */ = {
isa = PBXGroup;
children = (
D7A7292F2B6E44F200FFFF41 /* CommandClient.swift */,
D78B64E82B6EB41900C5D800 /* Extension+Iterator.swift */,
);
path = Network;
sourceTree = "<group>";
};
/* End PBXGroup section */ /* End PBXGroup section */
/* Begin PBXNativeTarget section */ /* Begin PBXNativeTarget section */
@@ -687,14 +673,12 @@
03B516772AE7634400EA47E2 /* Logger.swift in Sources */, 03B516772AE7634400EA47E2 /* Logger.swift in Sources */,
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */, 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */,
03B516712AE74CCD00EA47E2 /* VPNConfig.swift in Sources */, 03B516712AE74CCD00EA47E2 /* VPNConfig.swift in Sources */,
D78B64E92B6EB41900C5D800 /* Extension+Iterator.swift in Sources */,
03B5166B2AE7315E00EA47E2 /* AlertsEventHandler.swift in Sources */, 03B5166B2AE7315E00EA47E2 /* AlertsEventHandler.swift in Sources */,
0736958B2B3AC96D007249BE /* Bundle+Properties.swift in Sources */, 0736958B2B3AC96D007249BE /* Bundle+Properties.swift in Sources */,
0736958D2B3B79E0007249BE /* StatsEventHandler.swift in Sources */, 0736958D2B3B79E0007249BE /* StatsEventHandler.swift in Sources */,
03B516692AE7306B00EA47E2 /* StatusEventHandler.swift in Sources */, 03B516692AE7306B00EA47E2 /* StatusEventHandler.swift in Sources */,
032158B82ADDF8BF008D943B /* VPNManager.swift in Sources */, 032158B82ADDF8BF008D943B /* VPNManager.swift in Sources */,
0736958F2B3B8048007249BE /* PlatformMethodHandler.swift in Sources */, 0736958F2B3B8048007249BE /* PlatformMethodHandler.swift in Sources */,
D7A729302B6E44F200FFFF41 /* CommandClient.swift in Sources */,
03B516672AE6B93A00EA47E2 /* MethodHandler.swift in Sources */, 03B516672AE6B93A00EA47E2 /* MethodHandler.swift in Sources */,
03B5166D2AE7325500EA47E2 /* LogsEventHandler.swift in Sources */, 03B5166D2AE7325500EA47E2 /* LogsEventHandler.swift in Sources */,
03E392D02ADDF1BD000ADF15 /* FilePath.swift in Sources */, 03E392D02ADDF1BD000ADF15 /* FilePath.swift in Sources */,

View File

@@ -1,11 +1,18 @@
import Foundation import Foundation
import Combine import Combine
import Libcore
public class LogsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler { class LogsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler, LibboxCommandClientHandlerProtocol {
static let name = "\(Bundle.main.serviceIdentifier)/service.logs" static let name = "\(Bundle.main.serviceIdentifier)/service.logs"
private var channel: FlutterEventChannel? private var channel: FlutterEventChannel?
private var cancellable: AnyCancellable?
private var commandClient: LibboxCommandClient?
private var events: FlutterEventSink?
private var maxLines: Int
private var logList: [String] = []
private var lock: NSLock = NSLock()
public static func register(with registrar: FlutterPluginRegistrar) { public static func register(with registrar: FlutterPluginRegistrar) {
let instance = LogsEventHandler() let instance = LogsEventHandler()
@@ -13,16 +20,53 @@ public class LogsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler {
instance.channel?.setStreamHandler(instance) instance.channel?.setStreamHandler(instance)
} }
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? { init(maxLines: Int = 32) {
VPNManager.shared.logClient?.connect() self.maxLines = maxLines
cancellable = VPNManager.shared.logClient?.$logList.sink { [events] logsList in super.init()
events(logsList) let opts = LibboxCommandClientOptions()
opts.command = LibboxCommandLog
opts.statusInterval = Int64(2 * NSEC_PER_SEC)
commandClient = LibboxCommandClient(self, options: opts)
try? commandClient?.connect()
} }
deinit {
try? commandClient?.disconnect()
}
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
events(logList)
self.events = events
return nil return nil
} }
public func onCancel(withArguments arguments: Any?) -> FlutterError? { public func onCancel(withArguments arguments: Any?) -> FlutterError? {
cancellable?.cancel() events = nil
return nil return nil
} }
func writeLog(_ message: String?) {
guard let message else {
return
}
lock.withLock { [self] in
if logList.count > maxLines {
logList.removeFirst()
}
logList.append(message)
DispatchQueue.main.async { [self] () in
events?(logList)
}
}
}
}
extension LogsEventHandler {
public func clearLog() {}
public func connected() {}
public func disconnected(_ message: String?) {}
public func initializeClashMode(_ modeList: LibboxStringIteratorProtocol?, currentMode: String?) {}
public func updateClashMode(_ newMode: String?) {}
public func writeGroups(_ message: LibboxOutboundGroupIteratorProtocol?) {}
public func writeStatus(_ message: LibboxStatusMessage?) {}
} }

View File

@@ -39,9 +39,6 @@ class VPNManager: ObservableObject {
@Published private(set) var upload: Int64 = 0 @Published private(set) var upload: Int64 = 0
@Published private(set) var download: Int64 = 0 @Published private(set) var download: Int64 = 0
@Published private(set) var elapsedTime: TimeInterval = 0 @Published private(set) var elapsedTime: TimeInterval = 0
@Published private(set) var logList: [String] = []
@Published private(set) var logCallback = false
private(set) var logClient: CommandClient?
private var _connectTime: Date? private var _connectTime: Date?
private var connectTime: Date? { private var connectTime: Date? {
@@ -74,8 +71,6 @@ class VPNManager: ObservableObject {
updateStats() updateStats()
elapsedTime = -1 * (connectTime?.timeIntervalSinceNow ?? 0) elapsedTime = -1 * (connectTime?.timeIntervalSinceNow ?? 0)
} }
logClient = CommandClient(.log)
} }
deinit { deinit {
@@ -91,7 +86,7 @@ class VPNManager: ObservableObject {
do { do {
try await loadVPNPreference() try await loadVPNPreference()
} catch { } catch {
onServiceWriteLog(message: error.localizedDescription) LogsEventHandler().writeLog(error.localizedDescription)
} }
} }
@@ -112,7 +107,7 @@ class VPNManager: ObservableObject {
try await newManager.loadFromPreferences() try await newManager.loadFromPreferences()
self.manager = newManager self.manager = newManager
} catch { } catch {
onServiceWriteLog(message: error.localizedDescription) LogsEventHandler().writeLog(error.localizedDescription)
} }
} }
@@ -122,7 +117,7 @@ class VPNManager: ObservableObject {
try await manager.saveToPreferences() try await manager.saveToPreferences()
try await manager.loadFromPreferences() try await manager.loadFromPreferences()
} catch { } catch {
onServiceWriteLog(message: error.localizedDescription) LogsEventHandler().writeLog(error.localizedDescription)
} }
} }
@@ -162,7 +157,7 @@ class VPNManager: ObservableObject {
} }
try await self?.loadVPNPreference() try await self?.loadVPNPreference()
} catch { } catch {
onServiceWriteLog(message: error.localizedDescription) LogsEventHandler().writeLog(error.localizedDescription)
} }
} }
}.store(in: &cancelBag) }.store(in: &cancelBag)
@@ -193,7 +188,7 @@ class VPNManager: ObservableObject {
} }
} }
} catch { } catch {
onServiceWriteLog(message: error.localizedDescription) LogsEventHandler().writeLog(error.localizedDescription)
} }
} }
@@ -207,7 +202,7 @@ class VPNManager: ObservableObject {
"DisableMemoryLimit": (disableMemoryLimit ? "YES" : "NO") as NSString, "DisableMemoryLimit": (disableMemoryLimit ? "YES" : "NO") as NSString,
]) ])
} catch { } catch {
onServiceWriteLog(message: error.localizedDescription) LogsEventHandler().writeLog(error.localizedDescription)
} }
connectTime = .now connectTime = .now
} }
@@ -216,17 +211,4 @@ class VPNManager: ObservableObject {
guard state == .connected else { return } guard state == .connected else { return }
manager.connection.stopVPNTunnel() manager.connection.stopVPNTunnel()
} }
func onServiceWriteLog(message: String) {
logCallback = true
if logList.count > 300 {
logList.removeFirst()
}
logList.append(message)
}
func onServiceResetLogs() {
logCallback = false
logList.removeAll()
}
} }