增加系统消息通知

This commit is contained in:
anlicheng 2026-04-03 17:29:26 +08:00
parent ab0ee1ccf9
commit c215145123
3 changed files with 121 additions and 0 deletions

View File

@ -17,6 +17,11 @@ 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) {
let shared = UserDefaults(suiteName: "group.com.jihe.punchnetmac")
let msg = shared?.string(forKey: "test_msg")
SDLLogger.shared.log("NE read message: \(msg ?? "failed")")
// host: "192.168.0.101", port: 1265 // host: "192.168.0.101", port: 1265
guard let options, let config = SDLConfiguration.parse(options: options) else { guard let options, let config = SDLConfiguration.parse(options: options) else {
completionHandler(TunnelError.invalidConfiguration) completionHandler(TunnelError.invalidConfiguration)

View File

@ -0,0 +1,112 @@
//
// DarwinNotificationName.swift
// punchnet
//
// Created by on 2026/4/3.
//
import Foundation
// MARK: - Darwin Notification Name
public struct DarwinNotificationName: RawRepresentable, Hashable {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
}
//
extension DarwinNotificationName {
static let vpnStatusChanged = DarwinNotificationName(rawValue: "com.jihe.punchnetmac.vpnStatusChanged")
}
// MARK: - Manager
public final class DarwinNotificationCenter {
public static let shared = DarwinNotificationCenter()
private let center = CFNotificationCenterGetDarwinNotifyCenter()
private var observers: [DarwinNotificationName: (DarwinNotificationName) -> Void] = [:]
private let lock = NSLock()
private init() {}
// MARK: - Add Observer
public func addObserver(for name: DarwinNotificationName, queue: DispatchQueue = .main, using block: @escaping (DarwinNotificationName) -> Void ) {
lock.lock()
defer {
lock.unlock()
}
if observers[name] == nil {
// Darwin Center
CFNotificationCenterAddObserver(
center,
UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()),
{ (_, observer, cfName, _, _) in
guard let observer, let cfName else {
return
}
let instance = Unmanaged<DarwinNotificationCenter>
.fromOpaque(observer)
.takeUnretainedValue()
let name = DarwinNotificationName(rawValue: cfName.rawValue as String)
instance.handle(name: name)
},
name.rawValue as CFString,
nil,
.deliverImmediately
)
observers[name] = { n in
queue.async {
block(n)
}
}
}
}
// MARK: - Remove Observer
public func removeObserver(for name: DarwinNotificationName) {
lock.lock()
defer {
lock.unlock()
}
if observers[name] != nil {
CFNotificationCenterRemoveObserver(
center,
UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()),
CFNotificationName(name.rawValue as CFString),
nil
)
}
observers.removeValue(forKey: name)
}
// MARK: - Post
public func post(_ name: DarwinNotificationName) {
CFNotificationCenterPostNotification(
center,
CFNotificationName(name.rawValue as CFString),
nil,
nil,
true
)
}
// MARK: - Handle
private func handle(name: DarwinNotificationName) {
lock.lock()
let block = observers[name]
lock.unlock()
block?(name)
}
}

View File

@ -90,6 +90,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillFinishLaunching(_ notification: Notification) { func applicationWillFinishLaunching(_ notification: Notification) {
let shared = UserDefaults(suiteName: "group.com.jihe.punchnetmac")
shared?.set("App says hello", forKey: "test_msg")
shared?.synchronize()
} }
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply { func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {