fix
This commit is contained in:
parent
b55d9913cc
commit
d1452ce0b7
@ -70,7 +70,7 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
private var monitorCancel: AnyCancellable?
|
private var monitorCancel: AnyCancellable?
|
||||||
|
|
||||||
// 内部socket通讯
|
// 内部socket通讯
|
||||||
private var noticeClient: SDLNoticeClient
|
private var noticeClient: SDLNoticeClient?
|
||||||
|
|
||||||
// 流量统计
|
// 流量统计
|
||||||
private var flowTracer = SDLFlowTracerActor()
|
private var flowTracer = SDLFlowTracerActor()
|
||||||
@ -90,13 +90,13 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
self.sessionManager = SessionManager()
|
self.sessionManager = SessionManager()
|
||||||
self.holerManager = HolerManager()
|
self.holerManager = HolerManager()
|
||||||
self.arpServer = ArpServer(known_macs: [:])
|
self.arpServer = ArpServer(known_macs: [:])
|
||||||
self.noticeClient = SDLNoticeClient()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func start() async throws {
|
public func start() async throws {
|
||||||
self.udpHoleActor = try await SDLUDPHoleActor()
|
self.udpHoleActor = try await SDLUDPHoleActor()
|
||||||
self.superClientActor = try await SDLSuperClientActor(host: self.config.superHost, port: self.config.superPort)
|
self.superClientActor = try await SDLSuperClientActor(host: self.config.superHost, port: self.config.superPort)
|
||||||
|
self.noticeClient = try await SDLNoticeClient()
|
||||||
|
|
||||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||||
group.addTask {
|
group.addTask {
|
||||||
try await self.udpHoleActor?.start()
|
try await self.udpHoleActor?.start()
|
||||||
@ -106,6 +106,10 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
try await self.superClientActor?.start()
|
try await self.superClientActor?.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
group.addTask {
|
||||||
|
try await self.noticeClient?.start()
|
||||||
|
}
|
||||||
|
|
||||||
group.addTask {
|
group.addTask {
|
||||||
if let eventFlow = self.superClientActor?.eventFlow {
|
if let eventFlow = self.superClientActor?.eventFlow {
|
||||||
for try await event in eventFlow {
|
for try await event in eventFlow {
|
||||||
@ -123,10 +127,16 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
group.addTask {
|
||||||
|
while !Task.isCancelled {
|
||||||
|
try await Task.sleep(nanoseconds: 5 * 1_000_000_000)
|
||||||
|
self.lastCookie = await self.udpHoleActor?.stunRequest(context: self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try await group.waitForAll()
|
try await group.waitForAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
// self.noticeClient.start()
|
|
||||||
// // 启动网络监控
|
// // 启动网络监控
|
||||||
// self.monitorCancel = self.monitor.eventFlow.sink { event in
|
// self.monitorCancel = self.monitor.eventFlow.sink { event in
|
||||||
// switch event {
|
// switch event {
|
||||||
@ -169,7 +179,7 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
|
|
||||||
if upgradeType == .force {
|
if upgradeType == .force {
|
||||||
let forceUpgrade = NoticeMessage.UpgradeMessage(prompt: registerSuperAck.upgradePrompt, address: registerSuperAck.upgradeAddress)
|
let forceUpgrade = NoticeMessage.UpgradeMessage(prompt: registerSuperAck.upgradePrompt, address: registerSuperAck.upgradeAddress)
|
||||||
self.noticeClient.send(data: forceUpgrade.binaryData)
|
await self.noticeClient?.send(data: forceUpgrade.binaryData)
|
||||||
exit(-1)
|
exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +189,7 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
|
|
||||||
if upgradeType == .normal {
|
if upgradeType == .normal {
|
||||||
let normalUpgrade = NoticeMessage.UpgradeMessage(prompt: registerSuperAck.upgradePrompt, address: registerSuperAck.upgradeAddress)
|
let normalUpgrade = NoticeMessage.UpgradeMessage(prompt: registerSuperAck.upgradePrompt, address: registerSuperAck.upgradeAddress)
|
||||||
self.noticeClient.send(data: normalUpgrade.binaryData)
|
await self.noticeClient?.send(data: normalUpgrade.binaryData)
|
||||||
}
|
}
|
||||||
|
|
||||||
case .registerSuperNak(let nakPacket):
|
case .registerSuperNak(let nakPacket):
|
||||||
@ -191,11 +201,11 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
switch errorCode {
|
switch errorCode {
|
||||||
case .invalidToken, .nodeDisabled:
|
case .invalidToken, .nodeDisabled:
|
||||||
let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage)
|
let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage)
|
||||||
self.noticeClient.send(data: alertNotice.binaryData)
|
await self.noticeClient?.send(data: alertNotice.binaryData)
|
||||||
exit(-1)
|
exit(-1)
|
||||||
case .noIpAddress, .networkFault, .internalFault:
|
case .noIpAddress, .networkFault, .internalFault:
|
||||||
let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage)
|
let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage)
|
||||||
self.noticeClient.send(data: alertNotice.binaryData)
|
await self.noticeClient?.send(data: alertNotice.binaryData)
|
||||||
}
|
}
|
||||||
NSLog("[SDLContext] Get a SuperNak message exit")
|
NSLog("[SDLContext] Get a SuperNak message exit")
|
||||||
default:
|
default:
|
||||||
@ -226,7 +236,7 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
|
|
||||||
case .networkShutdown(let shutdownEvent):
|
case .networkShutdown(let shutdownEvent):
|
||||||
let alertNotice = NoticeMessage.AlertMessage(alert: shutdownEvent.message)
|
let alertNotice = NoticeMessage.AlertMessage(alert: shutdownEvent.message)
|
||||||
self.noticeClient.send(data: alertNotice.binaryData)
|
await self.noticeClient?.send(data: alertNotice.binaryData)
|
||||||
exit(-1)
|
exit(-1)
|
||||||
}
|
}
|
||||||
case .command(let packetId, let command):
|
case .command(let packetId, let command):
|
||||||
@ -267,14 +277,9 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
switch event {
|
switch event {
|
||||||
case .ready:
|
case .ready:
|
||||||
// 获取当前网络的类型
|
// 获取当前网络的类型
|
||||||
self.natType = await SDLNatProber.getNatType(udpHole: self.udpHole, config: self.config)
|
//self.natType = await SDLNatProber.getNatType(udpHole: self.udpHole, config: self.config)
|
||||||
SDLLogger.log("[SDLContext] nat type is: \(self.natType)", level: .debug)
|
SDLLogger.log("[SDLContext] nat type is: \(self.natType)", level: .debug)
|
||||||
|
|
||||||
let timer = Timer.publish(every: 5.0, on: .main, in: .common).autoconnect()
|
|
||||||
// self.stunCancel = Just(Date()).merge(with: timer).sink { _ in
|
|
||||||
// self.lastCookie = await self.udpHoleActor?.stunRequest(context: self)
|
|
||||||
// }
|
|
||||||
|
|
||||||
case .closed:
|
case .closed:
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
|
||||||
Task {
|
Task {
|
||||||
@ -289,7 +294,7 @@ public class SDLContext: @unchecked Sendable {
|
|||||||
// 判断目标地址是否是tun的网卡地址, 并且是在同一个网络下
|
// 判断目标地址是否是tun的网卡地址, 并且是在同一个网络下
|
||||||
if register.dstMac == self.devAddr.mac && register.networkID == self.devAddr.networkID {
|
if register.dstMac == self.devAddr.mac && register.networkID == self.devAddr.networkID {
|
||||||
// 回复ack包
|
// 回复ack包
|
||||||
self.udpHoleActor?.sendRegisterAck(context: self, remoteAddress: remoteAddress, dst_mac: register.srcMac)
|
await self.udpHoleActor?.sendRegisterAck(context: self, remoteAddress: remoteAddress, dst_mac: register.srcMac)
|
||||||
// 这里需要建立到来源的会话, 在复杂网络下,通过super-node查询到的nat地址不一定靠谱,需要通过udp包的来源地址作为nat地址
|
// 这里需要建立到来源的会话, 在复杂网络下,通过super-node查询到的nat地址不一定靠谱,需要通过udp包的来源地址作为nat地址
|
||||||
let session = Session(dstMac: register.srcMac, natAddress: remoteAddress)
|
let session = Session(dstMac: register.srcMac, natAddress: remoteAddress)
|
||||||
await self.sessionManager.addSession(session: session)
|
await self.sessionManager.addSession(session: session)
|
||||||
|
|||||||
@ -54,7 +54,7 @@ enum SDLUpgradeType: UInt32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Id生成器
|
// Id生成器
|
||||||
struct SDLIdGenerator {
|
struct SDLIdGenerator: Sendable {
|
||||||
// 消息体id
|
// 消息体id
|
||||||
private var packetId: UInt32
|
private var packetId: UInt32
|
||||||
|
|
||||||
|
|||||||
@ -22,7 +22,7 @@ struct SDLNatProber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取当前所处的网络的nat类型
|
// 获取当前所处的网络的nat类型
|
||||||
static func getNatType(udpHole: SDLUDPHole?, config: SDLConfiguration) async -> NatType {
|
static func getNatType(udpHole: SDLUDPHoleActor?, config: SDLConfiguration) async -> NatType {
|
||||||
guard let udpHole else {
|
guard let udpHole else {
|
||||||
return .blocked
|
return .blocked
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ struct SDLNatProber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 网络没有在nat下
|
// 网络没有在nat下
|
||||||
if natAddress1 == udpHole.localAddress {
|
if await natAddress1 == udpHole.localAddress {
|
||||||
return .noNat
|
return .noNat
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,8 +67,8 @@ struct SDLNatProber {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func getNatAddress(_ udpHole: SDLUDPHole, remoteAddress: SocketAddress, attr: SDLProbeAttr) async -> SocketAddress? {
|
private static func getNatAddress(_ udpHole: SDLUDPHoleActor, remoteAddress: SocketAddress, attr: SDLProbeAttr) async -> SocketAddress? {
|
||||||
let stunProbeReply = await udpHole.stunProbe(remoteAddress: remoteAddress, attr: attr, timeout: 5)
|
let stunProbeReply = try? await udpHole.stunProbe(remoteAddress: remoteAddress, attr: attr, timeout: 5)
|
||||||
|
|
||||||
return stunProbeReply?.socketAddress()
|
return stunProbeReply?.socketAddress()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,72 +15,59 @@ import Foundation
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
@preconcurrency import NIOCore
|
import NIOCore
|
||||||
import NIOPosix
|
import NIOPosix
|
||||||
|
|
||||||
// 处理和sn-server服务器之间的通讯
|
// 处理和sn-server服务器之间的通讯
|
||||||
class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
|
actor SDLNoticeClient {
|
||||||
public typealias InboundIn = AddressedEnvelope<ByteBuffer>
|
|
||||||
public typealias OutboundOut = AddressedEnvelope<ByteBuffer>
|
|
||||||
|
|
||||||
var channel: Channel?
|
|
||||||
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
||||||
|
private let asyncChannel: NIOAsyncChannel<AddressedEnvelope<ByteBuffer>, AddressedEnvelope<ByteBuffer>>
|
||||||
private let remoteAddress: SocketAddress
|
private let remoteAddress: SocketAddress
|
||||||
|
private let (writeStream, writeContinuation) = AsyncStream.makeStream(of: Data.self, bufferingPolicy: .unbounded)
|
||||||
init() {
|
|
||||||
self.remoteAddress = try! SocketAddress(ipAddress: "127.0.0.1", port: 50195)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 启动函数
|
// 启动函数
|
||||||
func start() {
|
init() async throws {
|
||||||
|
self.remoteAddress = try! SocketAddress(ipAddress: "127.0.0.1", port: 50195)
|
||||||
|
|
||||||
let bootstrap = DatagramBootstrap(group: self.group)
|
let bootstrap = DatagramBootstrap(group: self.group)
|
||||||
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
|
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
|
||||||
.channelInitializer { channel in
|
|
||||||
// 接收缓冲区
|
self.asyncChannel = try await bootstrap.bind(host: "0.0.0.0", port: 0)
|
||||||
channel.pipeline.addHandler(self)
|
.flatMapThrowing {channel in
|
||||||
|
return try NIOAsyncChannel(wrappingChannelSynchronously: channel, configuration: .init(
|
||||||
|
inboundType: AddressedEnvelope<ByteBuffer>.self,
|
||||||
|
outboundType: AddressedEnvelope<ByteBuffer>.self
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
.get()
|
||||||
|
|
||||||
self.channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait()
|
SDLLogger.log("[SDLNoticeClient] started and listening on: \(self.asyncChannel.channel.localAddress!)", level: .debug)
|
||||||
SDLLogger.log("[SDLNoticeClient] started and listening on: \(self.channel?.localAddress!)", level: .debug)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- MARK: ChannelInboundHandler Methods
|
func start() async throws {
|
||||||
|
try await self.asyncChannel.executeThenClose { inbound, outbound in
|
||||||
public func channelActive(context: ChannelHandlerContext) {
|
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||||
|
group.addTask {
|
||||||
}
|
defer {
|
||||||
|
self.writeContinuation.finish()
|
||||||
// 接收到的消息, 消息需要根据类型分流
|
}
|
||||||
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
|
||||||
context.fireChannelRead(data)
|
for try await message in self.writeStream {
|
||||||
}
|
let buf = self.asyncChannel.channel.allocator.buffer(bytes: message)
|
||||||
|
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: self.remoteAddress, data: buf)
|
||||||
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
|
try await outbound.write(envelope)
|
||||||
// reduce allocations.
|
}
|
||||||
context.close(promise: nil)
|
}
|
||||||
self.channel = nil
|
|
||||||
}
|
try await group.waitForAll()
|
||||||
|
}
|
||||||
public func channelInactive(context: ChannelHandlerContext) {
|
}
|
||||||
self.channel = nil
|
|
||||||
context.close(promise: nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理写入逻辑
|
// 处理写入逻辑
|
||||||
func send(data: Data) {
|
func send(data: Data) {
|
||||||
guard let channel = self.channel else {
|
self.writeContinuation.yield(data)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let remoteAddress = self.remoteAddress
|
|
||||||
let allocator = channel.allocator
|
|
||||||
|
|
||||||
channel.eventLoop.execute { [allocator] in
|
|
||||||
let buffer = allocator.buffer(bytes: data)
|
|
||||||
let envelope = AddressedEnvelope<ByteBuffer>(remoteAddress: remoteAddress, data: buffer)
|
|
||||||
channel.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
|
|||||||
@ -100,6 +100,8 @@ actor SDLUDPHoleActor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//eventFlow.send(.ready)
|
//eventFlow.send(.ready)
|
||||||
|
|
||||||
try await group.waitForAll()
|
try await group.waitForAll()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user