From 739acd393885787afc15b490fa6fa639706894ff Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 15 Apr 2026 11:05:08 +0800 Subject: [PATCH] fix notice --- Tun/Punchnet/SDLNoticeClient.swift | 100 ---------------------- punchnet/Core/NoticeMessage.swift | 52 ----------- punchnet/Core/UDPNoticeCenterServer.swift | 60 ------------- punchnet/Views/AppContext.swift | 44 +++++----- 4 files changed, 24 insertions(+), 232 deletions(-) delete mode 100644 Tun/Punchnet/SDLNoticeClient.swift delete mode 100644 punchnet/Core/NoticeMessage.swift delete mode 100644 punchnet/Core/UDPNoticeCenterServer.swift diff --git a/Tun/Punchnet/SDLNoticeClient.swift b/Tun/Punchnet/SDLNoticeClient.swift deleted file mode 100644 index c19913b..0000000 --- a/Tun/Punchnet/SDLNoticeClient.swift +++ /dev/null @@ -1,100 +0,0 @@ -// -// SDLNoticeClient.swift -// Tun -// -// Created by 安礼成 on 2024/5/20. -// - -import Foundation - -// -// SDLanServer.swift -// Tun -// -// Created by 安礼成 on 2024/1/31. -// - -import Foundation -import NIOCore -import NIOPosix - -// 处理和sn-server服务器之间的通讯 -final class SDLNoticeClient { - private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) - let (writeStream, writeContinuation) = AsyncStream.makeStream(of: Data.self) - private var task: Task? - - private var channel: Channel - private let noticePort: Int - private var isStopped = false - - // 启动函数 - init(noticePort: Int) throws { - self.noticePort = noticePort - - let bootstrap = DatagramBootstrap(group: self.group) - .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) - .channelInitializer { channel in - channel.pipeline.addHandler(SDLNoticeClientInboundHandler()) - } - - self.channel = try bootstrap.bind(host: "0.0.0.0", port: 0).wait() - } - - func start() { - let channel = self.channel - self.task = Task.detached { - guard let remoteAddress = try? await SDLAddressResolver.shared.resolve(host: "127.0.0.1", port: self.noticePort) else { - return - } - - for await data in self.writeStream { - if Task.isCancelled { - break - } - - let buf = channel.allocator.buffer(bytes: data) - let envelope = AddressedEnvelope(remoteAddress: remoteAddress, data: buf) - channel.eventLoop.execute { - channel.writeAndFlush(envelope, promise: nil) - } - } - } - - } - - // 处理写入逻辑 - func send(data: Data) { - self.writeContinuation.yield(data) - } - - func waitClose() async throws { - try await self.channel.closeFuture.get() - } - - func stop() { - guard !self.isStopped else { - return - } - - self.isStopped = true - self.writeContinuation.finish() - self.task?.cancel() - self.task = nil - self.channel.close(promise: nil) - } - - deinit { - self.stop() - try? self.group.syncShutdownGracefully() - } - -} - -extension SDLNoticeClient { - - private class SDLNoticeClientInboundHandler: ChannelInboundHandler { - typealias InboundIn = AddressedEnvelope - } - -} diff --git a/punchnet/Core/NoticeMessage.swift b/punchnet/Core/NoticeMessage.swift deleted file mode 100644 index df8a2ae..0000000 --- a/punchnet/Core/NoticeMessage.swift +++ /dev/null @@ -1,52 +0,0 @@ -// -// NoticeMessage.swift -// sdlan -// -// Created by 安礼成 on 2024/6/3. -// - -import Foundation -import NIOCore - -struct NoticeMessage { - enum InboundMessage { - case none - case alertMessage(alert: String) - } - - static func decodeMessage(buffer: inout ByteBuffer) -> InboundMessage { - guard let type = buffer.readInteger(as: UInt8.self) else { - return .none - } - - switch type { - case 0x01: - if let len0 = buffer.readInteger(as: UInt16.self), - let alert = buffer.readString(length: Int(len0)) { - return .alertMessage(alert: alert) - } - default: - return .none - } - - return .none - } - - static func alert(alert: String) -> Data { - var data = Data() - data.append(contentsOf: [0x01]) - - data.append(contentsOf: lenBytes(UInt16(alert.count))) - data.append(alert.data(using: .utf8)!) - - return data - } - - private static func lenBytes(_ value: UInt16) -> [UInt8] { - let byte1 = UInt8((value >> 8) & 0xFF) - let bytes2 = UInt8(value & 0xFF) - - return [byte1, bytes2] - } - -} diff --git a/punchnet/Core/UDPNoticeCenterServer.swift b/punchnet/Core/UDPNoticeCenterServer.swift deleted file mode 100644 index d93bd8c..0000000 --- a/punchnet/Core/UDPNoticeCenterServer.swift +++ /dev/null @@ -1,60 +0,0 @@ -// -// UDPMessageCenterServer.swift -// sdlan -// -// Created by 安礼成 on 2024/5/20. -// - -import Foundation -import NIOCore -import NIOPosix -import Combine - -final class UDPNoticeCenterServer: ChannelInboundHandler { - public typealias InboundIn = AddressedEnvelope - public typealias OutboundOut = AddressedEnvelope - - private var group: MultiThreadedEventLoopGroup? - private var channel: Channel? - - var messageFlow = PassthroughSubject() - public var port: Int = 0 - - func start() { - self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1) - let bootstrap = DatagramBootstrap(group: self.group!) - .channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) - .channelInitializer { channel in - channel.pipeline.addHandler(self) - } - self.channel = try! bootstrap.bind(host: "127.0.0.1", port: 0).wait() - self.port = self.channel?.localAddress?.port ?? 0 - } - - func stop() { - try? self.group?.syncShutdownGracefully() - } - - // --MARK: ChannelInboundHandler - - public func channelRead(context: ChannelHandlerContext, data: NIOAny) { - let envelope = self.unwrapInboundIn(data) - var buffer = envelope.data - - let notice = NoticeMessage.decodeMessage(buffer: &buffer) - self.messageFlow.send(notice) - } - - public func channelReadComplete(context: ChannelHandlerContext) { - // As we are not really interested getting notified on success or failure we just pass nil as promise to - // reduce allocations. - context.flush() - } - - public func errorCaught(context: ChannelHandlerContext, error: Error) { - // As we are not really interested getting notified on success or failure we just pass nil as promise to - // reduce allocations. - context.close(promise: nil) - } - -} diff --git a/punchnet/Views/AppContext.swift b/punchnet/Views/AppContext.swift index cb61b53..25b02b9 100644 --- a/punchnet/Views/AppContext.swift +++ b/punchnet/Views/AppContext.swift @@ -167,26 +167,9 @@ class AppContext { } return nil } - -} - -// 处理网络出口数据 -extension AppContext { - - func loadExitNodeIp() -> String? { - if let data = try? KeychainStore.shared.load(account: "exitNodeIp") { - return String(data: data, encoding: .utf8) - } - return nil - } - - func saveExitNodeIp(exitNodeIp: String) async throws { - // 将数据缓存到keychain - if let data = exitNodeIp.data(using: .utf8) { - try KeychainStore.shared.save(data, account: "exitNodeIp") - } - } - + + // MARK: TunEvent + func dismissTunnelEvent() { self.tunnelEvent = nil } @@ -211,5 +194,26 @@ extension AppContext { deinit { SDLNotificationCenter.shared.removeObserver(for: .tunnelEventChanged) } + +} + +// 处理网络出口数据 +extension AppContext { + + func loadExitNodeIp() -> String? { + if let data = try? KeychainStore.shared.load(account: "exitNodeIp") { + return String(data: data, encoding: .utf8) + } + return nil + } + + func saveExitNodeIp(exitNodeIp: String) async throws { + // 将数据缓存到keychain + if let data = exitNodeIp.data(using: .utf8) { + try KeychainStore.shared.save(data, account: "exitNodeIp") + } + } + + }