Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ee6362a254 | |||
| aaa1b3812d | |||
| 20ef0ca985 |
@ -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) throws {
|
public init(provider: NEPacketTunnelProvider, config: SDLConfiguration, rsaCipher: RSACipher, aesCipher: AESCipher) {
|
||||||
self.config = config
|
self.config = config
|
||||||
self.rsaCipher = rsaCipher
|
self.rsaCipher = rsaCipher
|
||||||
self.aesCipher = aesCipher
|
self.aesCipher = aesCipher
|
||||||
@ -117,6 +117,16 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
self.monitor.start()
|
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 {
|
private func startSuperClient() async throws {
|
||||||
self.superClient = SDLSuperClient(host: config.superHost, port: config.superPort)
|
self.superClient = SDLSuperClient(host: config.superHost, port: config.superPort)
|
||||||
// 建立super的绑定关系
|
// 建立super的绑定关系
|
||||||
@ -176,13 +186,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)
|
||||||
}
|
}
|
||||||
SDLLogger.log("Get a SuperNak message exit", level: .error)
|
NSLog("[SDLContext] Get a SuperNak message exit")
|
||||||
default:
|
default:
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
|
|
||||||
case .closed:
|
case .closed:
|
||||||
SDLLogger.log("[SDLContext] super client closed", level: .debug)
|
NSLog("[SDLContext] super client closed")
|
||||||
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
|
||||||
@ -193,10 +203,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("natChangedEvent, dstMac: \(dstMac)")
|
NSLog("[SDLContext] natChangedEvent, dstMac: \(dstMac)")
|
||||||
await sessionManager.removeSession(dstMac: dstMac)
|
await sessionManager.removeSession(dstMac: dstMac)
|
||||||
case .sendRegister(let sendRegisterEvent):
|
case .sendRegister(let sendRegisterEvent):
|
||||||
NSLog("sendRegisterEvent, ip: \(sendRegisterEvent)")
|
NSLog("[SDLContext] 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包
|
||||||
@ -379,6 +389,8 @@ 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)")
|
||||||
|
|
||||||
let ipv4Settings = NEIPv4Settings(addresses: [netAddress.ipAddress], subnetMasks: [netAddress.maskAddress])
|
let ipv4Settings = NEIPv4Settings(addresses: [netAddress.ipAddress], subnetMasks: [netAddress.maskAddress])
|
||||||
// 设置路由表
|
// 设置路由表
|
||||||
//NEIPv4Route.default()
|
//NEIPv4Route.default()
|
||||||
@ -391,10 +403,11 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
try await self.provider.setTunnelNetworkSettings(networkSettings)
|
try await self.provider.setTunnelNetworkSettings(networkSettings)
|
||||||
|
|
||||||
await self.holerManager.cleanup()
|
await self.holerManager.cleanup()
|
||||||
|
|
||||||
self.startReader()
|
self.startReader()
|
||||||
|
|
||||||
|
NSLog("[SDLContext] setTunnelNetworkSettings success, start read packet")
|
||||||
} catch let err {
|
} catch let err {
|
||||||
SDLLogger.log("[SDLContext] setTunnelNetworkSettings get error: \(err)", level: .error)
|
NSLog("[SDLContext] setTunnelNetworkSettings get error: \(err)")
|
||||||
exit(-1)
|
exit(-1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 context: ChannelHandlerContext?
|
var channel: Channel?
|
||||||
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
||||||
private let remoteAddress: SocketAddress
|
private let remoteAddress: SocketAddress
|
||||||
|
|
||||||
@ -40,17 +40,14 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
|
|||||||
channel.pipeline.addHandler(self)
|
channel.pipeline.addHandler(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
let channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait()
|
self.channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait()
|
||||||
SDLLogger.log("[SDLNoticeClient] started and listening on: \(channel.localAddress!)", level: .debug)
|
SDLLogger.log("[SDLNoticeClient] started and listening on: \(self.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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 接收到的消息, 消息需要根据类型分流
|
// 接收到的消息, 消息需要根据类型分流
|
||||||
@ -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
|
// 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.context = nil
|
self.channel = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
public func channelInactive(context: ChannelHandlerContext) {
|
public func channelInactive(context: ChannelHandlerContext) {
|
||||||
self.context = nil
|
self.channel = nil
|
||||||
context.close(promise: nil)
|
context.close(promise: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理写入逻辑
|
// 处理写入逻辑
|
||||||
func send(data: Data) {
|
func send(data: Data) {
|
||||||
guard let context = self.context else {
|
guard let channel = self.channel else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let remoteAddress = self.remoteAddress
|
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 buffer = allocator.buffer(bytes: data)
|
||||||
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: remoteAddress, data: buffer)
|
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: remoteAddress, data: buffer)
|
||||||
context.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil)
|
channel.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,14 +8,15 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
// 计数器,用来统计qps
|
// 计数器,用来统计qps
|
||||||
class SDLQPSCounter {
|
class SDLQPSCounter: @unchecked Sendable {
|
||||||
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: DispatchQueue(label: "com.yourapp.qps"))
|
timer = DispatchSource.makeTimerSource(queue: queue)
|
||||||
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 }
|
||||||
@ -26,7 +27,7 @@ class SDLQPSCounter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func increment(num: Int = 1) {
|
func increment(num: Int = 1) {
|
||||||
DispatchQueue(label: "com.yourapp.qps").async {
|
queue.async {
|
||||||
self.count += num
|
self.count += num
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user