151 lines
4.1 KiB
Swift
151 lines
4.1 KiB
Swift
//
|
|
// ClashClient.swift
|
|
// Runner
|
|
//
|
|
// Created by LondonX on 2024/3/14.
|
|
//
|
|
|
|
import Foundation
|
|
import ActionCloudLib
|
|
|
|
|
|
public protocol ClashClientProtocol {
|
|
func getAllConnections() async -> String
|
|
func getConfig() async -> String
|
|
func getConfigs() async -> String
|
|
func getProviders() async -> String
|
|
func getProxies() async -> String
|
|
func getTraffic() async -> String
|
|
func getTunMode() async -> String
|
|
func isConfigValid(configPath: String) async -> Int
|
|
func setDelayUpdateXYListener(_ f: @escaping (_ name: String,_ delay: Int) -> Void)
|
|
func setLogListener(_ f: @escaping (_ message: String?) -> Void)
|
|
func asyncTestDelay(proxyName: String, url: String, timeout: Int) async
|
|
func parseOptions() async -> Bool
|
|
func setConfig(configPath: String) async -> Int
|
|
func setHomeDir(home: String) async -> Int
|
|
func setTunMode(s: String) async
|
|
func startLog() async
|
|
func stopLog() async
|
|
func changeProxy(selectorName: String, proxyName: String) async -> Int
|
|
func clashInit(homeDir: String) async -> Int
|
|
func closeAllConnections() async
|
|
func closeConnection(connectionId: String) async -> Bool
|
|
}
|
|
|
|
private class NativeXYClient : NSObject, FclashClientProtocol {
|
|
|
|
func log(_ messageXY: String?) {
|
|
guard messageXY != nil else {
|
|
return
|
|
}
|
|
XingYunVpnKeFuDuan.sharedXY.logListener?(messageXY!)
|
|
}
|
|
|
|
func delayUpdate(_ nameXY: String?, delay: Int64) {
|
|
guard nameXY != nil else {
|
|
return
|
|
}
|
|
XingYunVpnKeFuDuan.sharedXY.delayUpdateXYListener?(nameXY!, Int(delay))
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public class XingYunVpnKeFuDuan: ClashClientProtocol {
|
|
|
|
|
|
private let clientXY = NativeXYClient()
|
|
var delayUpdateXYListener: ((_ name: String,_ delay: Int) -> Void)?
|
|
var logListener: ((_ message: String?) -> Void)?
|
|
|
|
|
|
public static let sharedXY = XingYunVpnKeFuDuan()
|
|
|
|
private init() {}
|
|
|
|
public func setDelayUpdateXYListener(_ f: @escaping (_ name: String,_ delay: Int) -> Void) {
|
|
delayUpdateXYListener = f
|
|
}
|
|
|
|
public func setLogListener(_ f: @escaping (_ message: String?) -> Void) {
|
|
logListener = f
|
|
}
|
|
|
|
public func asyncTestDelay(proxyName: String, url: String, timeout: Int) async {
|
|
FclashAsyncTestDelay(proxyName, url, Int64(timeout))
|
|
}
|
|
|
|
public func changeProxy(selectorName: String, proxyName: String) async -> Int {
|
|
return Int(FclashChangeProxy(selectorName, proxyName))
|
|
}
|
|
|
|
public func clashInit(homeDir: String) async -> Int {
|
|
return Int(FclashClashInit(homeDir, clientXY))
|
|
}
|
|
public func getProxies() async -> String {
|
|
return FclashGetProxies()
|
|
}
|
|
|
|
public func getTraffic() async -> String {
|
|
return FclashGetTraffic()
|
|
}
|
|
|
|
public func getTunMode() async -> String {
|
|
return FclashGetTunMode()
|
|
}
|
|
|
|
public func closeConnection(connectionId: String) async -> Bool {
|
|
return FclashCloseConnection(connectionId)
|
|
}
|
|
|
|
public func getAllConnections() async -> String {
|
|
return FclashGetAllConnections()
|
|
}
|
|
|
|
public func getConfig() async -> String {
|
|
return FclashGetConfig()
|
|
}
|
|
|
|
public func getConfigs() async -> String {
|
|
return FclashGetConfigs()
|
|
}
|
|
|
|
public func getProviders() async -> String {
|
|
return FclashGetProviders()
|
|
}
|
|
|
|
public func isConfigValid(configPath: String) async -> Int {
|
|
return Int(FclashIsConfigValid(configPath))
|
|
}
|
|
|
|
public func parseOptions() async -> Bool {
|
|
return FclashParseOptions()
|
|
}
|
|
|
|
public func closeAllConnections() async {
|
|
FclashCloseAllConnections()
|
|
}
|
|
|
|
public func setConfig(configPath: String) async -> Int {
|
|
return Int(FclashSetConfig(configPath))
|
|
}
|
|
|
|
public func setHomeDir(home: String) async -> Int {
|
|
return Int(FclashSetHomeDir(home))
|
|
}
|
|
|
|
public func setTunMode(s: String) async {
|
|
FclashSetTunMode(s)
|
|
}
|
|
|
|
public func startLog() async {
|
|
FclashStartLog()
|
|
}
|
|
|
|
public func stopLog() async {
|
|
FclashStopLog()
|
|
}
|
|
|
|
}
|