fix
This commit is contained in:
parent
8a3bfc342f
commit
610a33eced
@ -17,6 +17,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
private var rootTask: Task<Void, Error>?
|
private var rootTask: Task<Void, Error>?
|
||||||
|
|
||||||
override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
|
override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
|
||||||
|
// 重置通知中心
|
||||||
SDLTunnelAppNotifier.shared.clear()
|
SDLTunnelAppNotifier.shared.clear()
|
||||||
|
|
||||||
// 如果当前在运行状态,不允许重复请求
|
// 如果当前在运行状态,不允许重复请求
|
||||||
@ -57,28 +58,18 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
|
|
||||||
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
|
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
|
||||||
// Add code here to handle the message.
|
// Add code here to handle the message.
|
||||||
|
|
||||||
Task {
|
Task {
|
||||||
if let message = try? NEMessage(serializedBytes: messageData) {
|
|
||||||
switch message.message {
|
|
||||||
case .exitNodeIpChanged(let exitNodeIpChanged):
|
|
||||||
let exitNodeIp = exitNodeIpChanged.ip
|
|
||||||
do {
|
do {
|
||||||
try await self.contextActor?.updateExitNode(exitNodeIp: exitNodeIp)
|
let message = try NEMessage(serializedBytes: messageData)
|
||||||
var reply = NEReply()
|
let replyData = try await self.handleAppEvent(message: message)
|
||||||
reply.code = 0
|
completionHandler?(replyData)
|
||||||
reply.message = "操作成功"
|
|
||||||
completionHandler?(try reply.serializedData())
|
|
||||||
|
|
||||||
} catch let err {
|
} catch let err {
|
||||||
var reply = NEReply()
|
var reply = NEReply()
|
||||||
reply.code = 1
|
reply.code = 1
|
||||||
reply.message = err.localizedDescription
|
reply.message = err.localizedDescription
|
||||||
completionHandler?(try reply.serializedData())
|
|
||||||
}
|
let errorReplyData = try? reply.serializedData()
|
||||||
case .none:
|
completionHandler?(errorReplyData)
|
||||||
()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,6 +83,29 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
// Add code here to wake up.
|
// Add code here to wake up.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func handleAppEvent(message: NEMessage) async throws -> Data? {
|
||||||
|
switch message.message {
|
||||||
|
case .exitNodeIpChanged(let exitNodeIpChanged):
|
||||||
|
let exitNodeIp = exitNodeIpChanged.ip
|
||||||
|
do {
|
||||||
|
try await self.contextActor?.updateExitNode(exitNodeIp: exitNodeIp)
|
||||||
|
var reply = NEReply()
|
||||||
|
reply.code = 0
|
||||||
|
reply.message = "操作成功"
|
||||||
|
return try reply.serializedData()
|
||||||
|
|
||||||
|
} catch let err {
|
||||||
|
var reply = NEReply()
|
||||||
|
reply.code = 1
|
||||||
|
reply.message = err.localizedDescription
|
||||||
|
|
||||||
|
return try reply.serializedData()
|
||||||
|
}
|
||||||
|
case .none:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取物理网卡ip地址
|
// 获取物理网卡ip地址
|
||||||
|
|||||||
@ -86,13 +86,7 @@ struct punchnetApp: App {
|
|||||||
class AppDelegate: NSObject, NSApplicationDelegate {
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||||
|
|
||||||
func applicationWillFinishLaunching(_ notification: Notification) {
|
func applicationWillFinishLaunching(_ notification: Notification) {
|
||||||
let shared = UserDefaults(suiteName: SDLNotificationCenter.Configuration.appGroupSuiteName)
|
|
||||||
shared?.set("App says hello", forKey: "test_msg")
|
|
||||||
shared?.synchronize()
|
|
||||||
|
|
||||||
SDLNotificationCenter.shared.addObserver(for: .vpnStatusChanged) { name in
|
|
||||||
NSLog("DarwinNotificationCenter get message: \(name)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
|
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user