Compare commits

..

No commits in common. "main" and "1.1.6" have entirely different histories.
main ... 1.1.6

3 changed files with 26 additions and 22 deletions

View File

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

View File

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

View File

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