Add ios connection info
This commit is contained in:
49
ios/Runner/Handlers/ActiveGroupsEventHandler.swift
Normal file
49
ios/Runner/Handlers/ActiveGroupsEventHandler.swift
Normal file
@@ -0,0 +1,49 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
import Libcore
|
||||
|
||||
public class ActiveGroupsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler {
|
||||
|
||||
static let name = "\(Bundle.main.serviceIdentifier)/active-groups"
|
||||
private var commandClient: CommandClient?
|
||||
private var channel: FlutterEventChannel?
|
||||
private var events: FlutterEventSink?
|
||||
private var cancellable: AnyCancellable?
|
||||
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let instance = ActiveGroupsEventHandler()
|
||||
instance.channel = FlutterEventChannel(name: Self.name,
|
||||
binaryMessenger: registrar.messenger())
|
||||
instance.channel?.setStreamHandler(instance)
|
||||
}
|
||||
|
||||
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
||||
FileManager.default.changeCurrentDirectoryPath(FilePath.sharedDirectory.path)
|
||||
self.events = events
|
||||
commandClient = CommandClient(.groupsInfoOnly)
|
||||
commandClient?.connect()
|
||||
cancellable = commandClient?.$groups.sink{ [self] sbGroups in
|
||||
self.writeGroups(sbGroups)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
commandClient?.disconnect()
|
||||
cancellable?.cancel()
|
||||
events = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeGroups(_ sbGroups: [SBGroup]?) {
|
||||
guard let sbGroups else {return}
|
||||
if
|
||||
let groups = try? JSONEncoder().encode(sbGroups),
|
||||
let groups = String(data: groups, encoding: .utf8)
|
||||
{
|
||||
DispatchQueue.main.async { [events = self.events, groups] () in
|
||||
events?(groups)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,93 +1,50 @@
|
||||
//
|
||||
// GroupsEventHandler.swift
|
||||
// Runner
|
||||
//
|
||||
// Created by GFWFighter on 10/24/23.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
import Libcore
|
||||
|
||||
struct SBItem: Codable {
|
||||
let tag: String
|
||||
let type: String
|
||||
let urlTestDelay: Int
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case tag
|
||||
case type
|
||||
case urlTestDelay = "url-test-delay"
|
||||
}
|
||||
}
|
||||
|
||||
struct SBGroup: Codable {
|
||||
let tag: String
|
||||
let type: String
|
||||
let selected: String
|
||||
let items: [SBItem]
|
||||
}
|
||||
|
||||
public class GroupsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler, LibboxCommandClientHandlerProtocol {
|
||||
public class GroupsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler{
|
||||
|
||||
static let name = "\(Bundle.main.serviceIdentifier)/groups"
|
||||
|
||||
private var commandClient: CommandClient?
|
||||
private var channel: FlutterEventChannel?
|
||||
|
||||
private var commandClient: LibboxCommandClient?
|
||||
private var events: FlutterEventSink?
|
||||
private var cancellable: AnyCancellable?
|
||||
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let instance = GroupsEventHandler()
|
||||
instance.channel = FlutterEventChannel(name: Self.name, binaryMessenger: registrar.messenger())
|
||||
instance.channel = FlutterEventChannel(name: Self.name,
|
||||
binaryMessenger: registrar.messenger())
|
||||
instance.channel?.setStreamHandler(instance)
|
||||
}
|
||||
|
||||
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
||||
FileManager.default.changeCurrentDirectoryPath(FilePath.sharedDirectory.path)
|
||||
self.events = events
|
||||
let opts = LibboxCommandClientOptions()
|
||||
opts.command = LibboxCommandGroup
|
||||
opts.statusInterval = 3000
|
||||
commandClient = LibboxCommandClient(self, options: opts)
|
||||
try? commandClient?.connect()
|
||||
commandClient = CommandClient(.groups)
|
||||
commandClient?.connect()
|
||||
cancellable = commandClient?.$groups.sink{ [self] groups in
|
||||
self.writeGroups(groups)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
try? commandClient?.disconnect()
|
||||
commandClient?.disconnect()
|
||||
cancellable?.cancel()
|
||||
events = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
public func writeGroups(_ message: LibboxOutboundGroupIteratorProtocol?) {
|
||||
guard let message else { return }
|
||||
var groups = [SBGroup]()
|
||||
while message.hasNext() {
|
||||
let group = message.next()!
|
||||
var items = [SBItem]()
|
||||
var groupItems = group.getItems()
|
||||
while groupItems?.hasNext() ?? false {
|
||||
let item = groupItems?.next()!
|
||||
items.append(SBItem(tag: item!.tag, type: item!.type, urlTestDelay: Int(item!.urlTestDelay)))
|
||||
}
|
||||
groups.append(.init(tag: group.tag, type: group.type, selected: group.selected, items: items))
|
||||
}
|
||||
if
|
||||
let groups = try? JSONEncoder().encode(groups),
|
||||
func writeGroups(_ sbGroups: [SBGroup]?) {
|
||||
guard let sbGroups else {return}
|
||||
if
|
||||
let groups = try? JSONEncoder().encode(sbGroups),
|
||||
let groups = String(data: groups, encoding: .utf8)
|
||||
{
|
||||
DispatchQueue.main.async { [events = self.events, groups] () in
|
||||
DispatchQueue.main.async { [events = self.events, groups] in
|
||||
events?(groups)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension GroupsEventHandler {
|
||||
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 writeLog(_ message: String?) {}
|
||||
public func writeStatus(_ message: LibboxStatusMessage?) {}
|
||||
}
|
||||
|
||||
@@ -1,48 +1,46 @@
|
||||
//
|
||||
// StatsEventHandler.swift
|
||||
// Runner
|
||||
//
|
||||
// Created by Hiddify on 12/27/23.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Flutter
|
||||
import Combine
|
||||
import Libcore
|
||||
|
||||
public class StatsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler, LibboxCommandClientHandlerProtocol {
|
||||
public class StatsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler {
|
||||
|
||||
static let name = "\(Bundle.main.serviceIdentifier)/stats"
|
||||
|
||||
private var commandClient: CommandClient?
|
||||
private var channel: FlutterEventChannel?
|
||||
|
||||
private var commandClient: LibboxCommandClient?
|
||||
private var events: FlutterEventSink?
|
||||
private var cancellable: AnyCancellable?
|
||||
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let instance = StatsEventHandler()
|
||||
instance.channel = FlutterEventChannel(name: Self.name, binaryMessenger: registrar.messenger(), codec: FlutterJSONMethodCodec())
|
||||
instance.channel = FlutterEventChannel(name: Self.name,
|
||||
binaryMessenger: registrar.messenger(),
|
||||
codec: FlutterJSONMethodCodec())
|
||||
instance.channel?.setStreamHandler(instance)
|
||||
}
|
||||
|
||||
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
|
||||
FileManager.default.changeCurrentDirectoryPath(FilePath.sharedDirectory.path)
|
||||
self.events = events
|
||||
let opts = LibboxCommandClientOptions()
|
||||
opts.command = LibboxCommandStatus
|
||||
opts.statusInterval = Int64(NSEC_PER_SEC)
|
||||
commandClient = LibboxCommandClient(self, options: opts)
|
||||
try? commandClient?.connect()
|
||||
commandClient = CommandClient(.status)
|
||||
commandClient?.connect()
|
||||
cancellable = commandClient?.$status.sink{ [self] status in
|
||||
self.writeStatus(status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
try? commandClient?.disconnect()
|
||||
commandClient?.disconnect()
|
||||
cancellable?.cancel()
|
||||
events = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
public func writeStatus(_ message: LibboxStatusMessage?) {
|
||||
guard
|
||||
let message
|
||||
else { return }
|
||||
func writeStatus(_ message: LibboxStatusMessage?) {
|
||||
guard let message else { return }
|
||||
|
||||
let data = [
|
||||
"connections-in": message.connectionsIn,
|
||||
"connections-out": message.connectionsOut,
|
||||
@@ -54,13 +52,3 @@ public class StatsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler, L
|
||||
events?(data)
|
||||
}
|
||||
}
|
||||
|
||||
extension StatsEventHandler {
|
||||
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 writeLog(_ message: String?) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user