Change ios method error handling
This commit is contained in:
@@ -25,102 +25,138 @@ public class MethodHandler: NSObject, FlutterPlugin {
|
|||||||
private var channel: FlutterMethodChannel?
|
private var channel: FlutterMethodChannel?
|
||||||
|
|
||||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||||
switch call.method {
|
@Sendable func mainResult(_ res: Any?) async -> Void {
|
||||||
case "parse_config":
|
|
||||||
result(parseConfig(args: call.arguments))
|
|
||||||
case "change_config_options":
|
|
||||||
result(changeConfigOptions(args: call.arguments))
|
|
||||||
case "setup":
|
|
||||||
Task { [unowned self] in
|
|
||||||
let res = await setup(args: call.arguments)
|
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
result(res)
|
result(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "start":
|
|
||||||
Task { [unowned self] in
|
|
||||||
let res = await start(args: call.arguments)
|
|
||||||
await MainActor.run {
|
|
||||||
result(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "restart":
|
|
||||||
Task { [unowned self] in
|
|
||||||
let res = await restart(args: call.arguments)
|
|
||||||
await MainActor.run {
|
|
||||||
result(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "stop":
|
|
||||||
result(stop())
|
|
||||||
case "url_test":
|
|
||||||
result(urlTest(args: call.arguments))
|
|
||||||
case "select_outbound":
|
|
||||||
result(selectOutbound(args: call.arguments))
|
|
||||||
default:
|
|
||||||
result(FlutterMethodNotImplemented)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func parseConfig(args: Any?) -> String {
|
switch call.method {
|
||||||
var error: NSError?
|
case "parse_config":
|
||||||
guard
|
guard
|
||||||
let args = args as? [String:Any?],
|
let args = call.arguments as? [String:Any?],
|
||||||
let path = args["path"] as? String,
|
let path = args["path"] as? String,
|
||||||
let tempPath = args["tempPath"] as? String,
|
let tempPath = args["tempPath"] as? String,
|
||||||
let debug = (args["debug"] as? NSNumber)?.boolValue
|
let debug = (args["debug"] as? NSNumber)?.boolValue
|
||||||
else {
|
else {
|
||||||
return "bad method format"
|
result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
let res = MobileParse(path, tempPath, debug, &error)
|
var error: NSError?
|
||||||
|
MobileParse(path, tempPath, debug, &error)
|
||||||
if let error {
|
if let error {
|
||||||
return error.localizedDescription
|
result(FlutterError(code: String(error.code), message: error.description, details: nil))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return ""
|
result(true)
|
||||||
}
|
case "change_config_options":
|
||||||
|
guard let options = call.arguments as? String else {
|
||||||
public func changeConfigOptions(args: Any?) -> Bool {
|
result(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
|
||||||
guard let options = args as? String else {
|
return
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
VPNConfig.shared.configOptions = options
|
VPNConfig.shared.configOptions = options
|
||||||
return true
|
result(true)
|
||||||
}
|
case "setup":
|
||||||
|
Task {
|
||||||
public func setup(args: Any?) async -> Bool {
|
|
||||||
do {
|
do {
|
||||||
try await VPNManager.shared.setup()
|
try await VPNManager.shared.setup()
|
||||||
} catch {
|
} catch {
|
||||||
return false
|
await mainResult(FlutterError(code: "SETUP", message: error.localizedDescription, details: nil))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return true
|
await mainResult(true)
|
||||||
}
|
}
|
||||||
|
case "start":
|
||||||
public func start(args: Any?) async -> Bool {
|
Task {
|
||||||
guard
|
guard
|
||||||
let args = args as? [String:Any?],
|
let args = call.arguments as? [String:Any?],
|
||||||
let path = args["path"] as? String
|
let path = args["path"] as? String
|
||||||
else {
|
else {
|
||||||
return false
|
await mainResult(FlutterError(code: "INVALID_ARGS", message: nil, details: nil))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
VPNConfig.shared.activeConfigPath = path
|
VPNConfig.shared.activeConfigPath = path
|
||||||
var error: NSError?
|
var error: NSError?
|
||||||
let config = MobileBuildConfig(path, VPNConfig.shared.configOptions, &error)
|
let config = MobileBuildConfig(path, VPNConfig.shared.configOptions, &error)
|
||||||
if let error {
|
if let error {
|
||||||
return false
|
await mainResult(FlutterError(code: String(error.code), message: error.description, details: nil))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
try await VPNManager.shared.setup()
|
try await VPNManager.shared.setup()
|
||||||
try await VPNManager.shared.connect(with: config, disableMemoryLimit: VPNConfig.shared.disableMemoryLimit)
|
try await VPNManager.shared.connect(with: config, disableMemoryLimit: VPNConfig.shared.disableMemoryLimit)
|
||||||
} catch {
|
} catch {
|
||||||
return false
|
await mainResult(FlutterError(code: "SETUP_CONNECTION", message: error.localizedDescription, details: nil))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return true
|
await mainResult(true)
|
||||||
}
|
}
|
||||||
|
case "restart":
|
||||||
public func stop() -> Bool {
|
Task { [unowned self] in
|
||||||
|
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
|
||||||
VPNManager.shared.disconnect()
|
VPNManager.shared.disconnect()
|
||||||
return true
|
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)
|
||||||
|
}
|
||||||
|
case "stop":
|
||||||
|
VPNManager.shared.disconnect()
|
||||||
|
result(true)
|
||||||
|
case "url_test":
|
||||||
|
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)
|
||||||
|
case "select_outbound":
|
||||||
|
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)
|
||||||
|
default:
|
||||||
|
result(FlutterMethodNotImplemented)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func waitForStop() -> Future<Void, Never> {
|
private func waitForStop() -> Future<Void, Never> {
|
||||||
@@ -136,61 +172,4 @@ public class MethodHandler: NSObject, FlutterPlugin {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func restart(args: Any?) async -> Bool {
|
|
||||||
guard
|
|
||||||
let args = args as? [String:Any?],
|
|
||||||
let path = args["path"] as? String
|
|
||||||
else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
VPNConfig.shared.activeConfigPath = path
|
|
||||||
VPNManager.shared.disconnect()
|
|
||||||
await waitForStop().value
|
|
||||||
var error: NSError?
|
|
||||||
let config = MobileBuildConfig(path, VPNConfig.shared.configOptions, &error)
|
|
||||||
if let error {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
try await VPNManager.shared.setup()
|
|
||||||
try await VPNManager.shared.connect(with: config, disableMemoryLimit: VPNConfig.shared.disableMemoryLimit)
|
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
public func selectOutbound(args: Any?) -> Bool {
|
|
||||||
guard
|
|
||||||
let args = args as? [String:Any?],
|
|
||||||
let group = args["groupTag"] as? String,
|
|
||||||
let outbound = args["outboundTag"] as? String
|
|
||||||
else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
FileManager.default.changeCurrentDirectoryPath(FilePath.sharedDirectory.path)
|
|
||||||
do {
|
|
||||||
try LibboxNewStandaloneCommandClient()?.selectOutbound(group, outboundTag: outbound)
|
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
public func urlTest(args: Any?) -> Bool {
|
|
||||||
guard
|
|
||||||
let args = args as? [String:Any?]
|
|
||||||
else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
let group = args["groupTag"] as? String
|
|
||||||
FileManager.default.changeCurrentDirectoryPath(FilePath.sharedDirectory.path)
|
|
||||||
do {
|
|
||||||
try LibboxNewStandaloneCommandClient()?.urlTest(group)
|
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user