Compare commits

..

3 Commits
1.1.5 ... main

Author SHA1 Message Date
ee6362a254 fix 2025-07-23 12:15:38 +08:00
aaa1b3812d fix udp client 2025-07-17 16:11:01 +08:00
20ef0ca985 add stop 2025-07-17 15:05:00 +08:00
3 changed files with 34 additions and 23 deletions

View File

@ -79,7 +79,7 @@ public class SDLContext: @unchecked Sendable {
private var flowTracer = SDLFlowTracerActor()
private var flowTracerCancel: AnyCancellable?
public init(provider: NEPacketTunnelProvider, config: SDLConfiguration, rsaCipher: RSACipher, aesCipher: AESCipher) throws {
public init(provider: NEPacketTunnelProvider, config: SDLConfiguration, rsaCipher: RSACipher, aesCipher: AESCipher) {
self.config = config
self.rsaCipher = rsaCipher
self.aesCipher = aesCipher
@ -117,6 +117,16 @@ public class SDLContext: @unchecked Sendable {
self.monitor.start()
}
public func stop() async {
self.superCancel?.cancel()
self.superClient = nil
self.udpCancel?.cancel()
self.udpHole = nil
self.readTask?.cancel()
}
private func startSuperClient() async throws {
self.superClient = SDLSuperClient(host: config.superHost, port: config.superPort)
// super
@ -176,13 +186,13 @@ public class SDLContext: @unchecked Sendable {
let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage)
self.noticeClient.send(data: alertNotice.binaryData)
}
SDLLogger.log("Get a SuperNak message exit", level: .error)
NSLog("[SDLContext] Get a SuperNak message exit")
default:
()
}
case .closed:
SDLLogger.log("[SDLContext] super client closed", level: .debug)
NSLog("[SDLContext] super client closed")
await self.arpServer.clear()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
Task {@MainActor in
@ -193,10 +203,10 @@ public class SDLContext: @unchecked Sendable {
switch evt {
case .natChanged(let natChangedEvent):
let dstMac = natChangedEvent.mac
NSLog("natChangedEvent, dstMac: \(dstMac)")
NSLog("[SDLContext] natChangedEvent, dstMac: \(dstMac)")
await sessionManager.removeSession(dstMac: dstMac)
case .sendRegister(let sendRegisterEvent):
NSLog("sendRegisterEvent, ip: \(sendRegisterEvent)")
NSLog("[SDLContext] sendRegisterEvent, ip: \(sendRegisterEvent)")
let address = SDLUtil.int32ToIp(sendRegisterEvent.natIp)
if let remoteAddress = try? SocketAddress.makeAddressResolvingHost(address, port: Int(sendRegisterEvent.natPort)) {
// register
@ -379,6 +389,8 @@ public class SDLContext: @unchecked Sendable {
networkSettings.dnsSettings = NEDNSSettings(servers: ["8.8.8.8", "114.114.114.114"])
}
NSLog("[SDLContext] Tun started at network ip: \(netAddress.ipAddress), mask: \(netAddress.maskAddress)")
let ipv4Settings = NEIPv4Settings(addresses: [netAddress.ipAddress], subnetMasks: [netAddress.maskAddress])
//
//NEIPv4Route.default()
@ -391,10 +403,11 @@ public class SDLContext: @unchecked Sendable {
try await self.provider.setTunnelNetworkSettings(networkSettings)
await self.holerManager.cleanup()
self.startReader()
NSLog("[SDLContext] setTunnelNetworkSettings success, start read packet")
} catch let err {
SDLLogger.log("[SDLContext] setTunnelNetworkSettings get error: \(err)", level: .error)
NSLog("[SDLContext] setTunnelNetworkSettings get error: \(err)")
exit(-1)
}
}

View File

@ -23,7 +23,7 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
public typealias InboundIn = AddressedEnvelope<ByteBuffer>
public typealias OutboundOut = AddressedEnvelope<ByteBuffer>
var context: ChannelHandlerContext?
var channel: Channel?
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
private let remoteAddress: SocketAddress
@ -40,17 +40,14 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
channel.pipeline.addHandler(self)
}
let channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait()
SDLLogger.log("[SDLNoticeClient] started and listening on: \(channel.localAddress!)", level: .debug)
// This will never unblock as we don't close the channel
try! channel.closeFuture.wait()
self.channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait()
SDLLogger.log("[SDLNoticeClient] started and listening on: \(self.channel?.localAddress!)", level: .debug)
}
// -- MARK: ChannelInboundHandler Methods
public func channelActive(context: ChannelHandlerContext) {
self.context = context
}
// ,
@ -62,27 +59,27 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
// 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)
self.context = nil
self.channel = nil
}
public func channelInactive(context: ChannelHandlerContext) {
self.context = nil
self.channel = nil
context.close(promise: nil)
}
//
func send(data: Data) {
guard let context = self.context else {
guard let channel = self.channel else {
return
}
let remoteAddress = self.remoteAddress
let allocator = context.channel.allocator
let allocator = channel.allocator
context.eventLoop.execute { [allocator] in
channel.eventLoop.execute { [allocator] in
let buffer = allocator.buffer(bytes: data)
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: remoteAddress, data: buffer)
context.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil)
channel.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil)
}
}

View File

@ -8,14 +8,15 @@
import Foundation
// qps
class SDLQPSCounter {
class SDLQPSCounter: @unchecked Sendable {
private var count = 0
private let timer: DispatchSourceTimer
private let label: String
private let queue = DispatchQueue(label: "com.punchnet.qps")
init(label: String) {
self.label = label
timer = DispatchSource.makeTimerSource(queue: DispatchQueue(label: "com.yourapp.qps"))
timer = DispatchSource.makeTimerSource(queue: queue)
timer.schedule(deadline: .now(), repeating: .seconds(1), leeway: .milliseconds(100))
timer.setEventHandler { [weak self] in
guard let self = self else { return }
@ -26,7 +27,7 @@ class SDLQPSCounter {
}
func increment(num: Int = 1) {
DispatchQueue(label: "com.yourapp.qps").async {
queue.async {
self.count += num
}
}