Files
umbrix/ios/Runner/Handlers/MethodHandler.swift

204 lines
8.0 KiB
Swift
Raw Normal View History

2023-10-24 18:29:53 +03:30
//
// MethodHandler.swift
// Runner
//
// Created by GFWFighter on 10/23/23.
//
import Flutter
import Combine
import Libcore
public class MethodHandler: NSObject, FlutterPlugin {
private var cancelBag: Set<AnyCancellable> = []
2023-12-26 22:26:06 +03:30
public static let name = "\(Bundle.main.serviceIdentifier)/method"
2023-10-24 18:29:53 +03:30
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: Self.name, binaryMessenger: registrar.messenger())
let instance = MethodHandler()
registrar.addMethodCallDelegate(instance, channel: channel)
2024-01-12 17:05:16 +03:30
instance.channel = channel
2023-10-24 18:29:53 +03:30
}
private var channel: FlutterMethodChannel?
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
2024-01-12 17:05:16 +03:30
@Sendable func mainResult(_ res: Any?) async -> Void {
await MainActor.run {
result(res)
}
}
2023-10-24 18:29:53 +03:30
switch call.method {
case "parse_config":
2024-01-12 17:05:16 +03:30
guard
let args = call.arguments as? [String:Any?],
let path = args["path"] as? String,
let tempPath = args["tempPath"] as? String,
let debug = (args["debug"] as? NSNumber)?.boolValue
else {
result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
return
}
var error: NSError?
MobileParse(path, tempPath, debug, &error)
if let error {
result(FlutterError(code: String(error.code), message: error.description, details: nil))
return
}
2024-01-12 22:45:52 +03:30
result("")
2023-10-24 18:29:53 +03:30
case "change_config_options":
2024-01-12 17:05:16 +03:30
guard let options = call.arguments as? String else {
result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
return
}
VPNConfig.shared.configOptions = options
result(true)
2023-12-26 22:26:06 +03:30
case "setup":
2024-01-12 17:05:16 +03:30
Task {
do {
try await VPNManager.shared.setup()
} catch {
await mainResult(FlutterError(code: "SETUP", message: error.localizedDescription, details: nil))
return
2023-12-26 22:26:06 +03:30
}
2024-01-12 17:05:16 +03:30
await mainResult(true)
2023-12-26 22:26:06 +03:30
}
2023-10-24 18:29:53 +03:30
case "start":
2024-01-12 17:05:16 +03:30
Task {
guard
let args = call.arguments as? [String:Any?],
let path = args["path"] as? String
else {
await mainResult(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
return
}
VPNConfig.shared.activeConfigPath = path
var error: NSError?
let config = MobileBuildConfig(path, VPNConfig.shared.configOptions, &error)
if let error {
await mainResult(FlutterError(code: String(error.code), message: error.description, details: nil))
return
}
do {
try await VPNManager.shared.setup()
try await VPNManager.shared.connect(with: config, disableMemoryLimit: VPNConfig.shared.disableMemoryLimit)
} catch {
await mainResult(FlutterError(code: "SETUP_CONNECTION", message: error.localizedDescription, details: nil))
return
2023-10-24 18:29:53 +03:30
}
2024-01-12 17:05:16 +03:30
await mainResult(true)
2023-10-24 18:29:53 +03:30
}
case "restart":
Task { [unowned self] in
2024-01-12 17:05:16 +03:30
guard
let args = call.arguments as? [String:Any?],
let path = args["path"] as? String
else {
await mainResult(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
return
2023-10-24 18:29:53 +03:30
}
2024-01-12 17:05:16 +03:30
VPNConfig.shared.activeConfigPath = path
VPNManager.shared.disconnect()
await waitForStop().value
var error: NSError?
let config = MobileBuildConfig(path, VPNConfig.shared.configOptions, &error)
if let error {
await mainResult(FlutterError(code: "BUILD_CONFIG", message: error.localizedDescription, details: nil))
return
}
do {
try await VPNManager.shared.setup()
try await VPNManager.shared.connect(with: config, disableMemoryLimit: VPNConfig.shared.disableMemoryLimit)
} catch {
await mainResult(FlutterError(code: "SETUP_CONNECTION", message: error.localizedDescription, details: nil))
return
}
await mainResult(true)
2023-10-24 18:29:53 +03:30
}
case "stop":
2024-01-12 17:05:16 +03:30
VPNManager.shared.disconnect()
result(true)
2024-01-18 22:53:17 +03:30
case "reset":
VPNManager.shared.reset()
result(true)
2023-10-24 18:29:53 +03:30
case "url_test":
2024-01-12 17:05:16 +03:30
guard
let args = call.arguments as? [String:Any?]
else {
result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
return
}
let group = args["groupTag"] as? String
FileManager.default.changeCurrentDirectoryPath(FilePath.sharedDirectory.path)
do {
try LibboxNewStandaloneCommandClient()?.urlTest(group)
} catch {
result(FlutterError(code: "URL_TEST", message: error.localizedDescription, details: nil))
return
}
result(true)
2023-10-24 18:29:53 +03:30
case "select_outbound":
2024-01-12 17:05:16 +03:30
guard
let args = call.arguments as? [String:Any?],
let group = args["groupTag"] as? String,
let outbound = args["outboundTag"] as? String
else {
result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
return
}
FileManager.default.changeCurrentDirectoryPath(FilePath.sharedDirectory.path)
do {
try LibboxNewStandaloneCommandClient()?.selectOutbound(group, outboundTag: outbound)
} catch {
result(FlutterError(code: "SELECT_OUTBOUND", message: error.localizedDescription, details: nil))
return
}
result(true)
2024-01-15 01:39:05 +03:30
case "generate_config":
guard
let args = call.arguments as? [String:Any?],
let path = args["path"] as? String
else {
result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
return
}
var error: NSError?
let config = MobileBuildConfig(path, VPNConfig.shared.configOptions, &error)
if let error {
result(FlutterError(code: "BUILD_CONFIG", message: error.localizedDescription, details: nil))
return
}
result(config)
case "generate_warp_config":
guard let args = call.arguments as? [String: Any],
let licenseKey = args["license-key"] as? String,
let accountId = args["previous-account-id"] as? String,
let accessToken = args["previous-access-token"] as? String else {
result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
return
}
let warpConfig = MobileGenerateWarpConfig(licenseKey, accountId, accessToken, nil)
result(warpConfig)
2023-10-24 18:29:53 +03:30
default:
result(FlutterMethodNotImplemented)
}
}
private func waitForStop() -> Future<Void, Never> {
return Future { promise in
var cancellable: AnyCancellable? = nil
cancellable = VPNManager.shared.$state
.filter { $0 == .disconnected }
.first()
.delay(for: 0.5, scheduler: RunLoop.current)
.sink(receiveValue: { _ in
promise(.success(()))
cancellable?.cancel()
})
}
}
}