rename SDLQuicClient -> SDLSuperClient
This commit is contained in:
parent
a56f858910
commit
ff9aaceefe
@ -70,8 +70,8 @@ actor ArpServer {
|
|||||||
self.coolingDown = [:]
|
self.coolingDown = [:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func arpRequest(targetIp: UInt32, use quicClient: SDLQUICClient?) async throws {
|
func arpRequest(targetIp: UInt32, use superClient: SDLSuperClient?) async throws {
|
||||||
guard let quicClient, self.coolingDown[targetIp] == nil else {
|
guard let superClient, self.coolingDown[targetIp] == nil else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ actor ArpServer {
|
|||||||
var arpRequest = SDLArpRequest()
|
var arpRequest = SDLArpRequest()
|
||||||
arpRequest.targetIp = targetIp
|
arpRequest.targetIp = targetIp
|
||||||
|
|
||||||
await quicClient.send(type: .arpRequest, data: try arpRequest.serializedData())
|
await superClient.send(type: .arpRequest, data: try arpRequest.serializedData())
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleArpResponse(arpResponse: SDLArpResponse) {
|
func handleArpResponse(arpResponse: SDLArpResponse) {
|
||||||
|
|||||||
@ -71,7 +71,7 @@ actor SDLContextActor {
|
|||||||
private var dnsLocalClient: DNSLocalClient?
|
private var dnsLocalClient: DNSLocalClient?
|
||||||
private var dnsLocalMonitorTask: Task<Void, Never>?
|
private var dnsLocalMonitorTask: Task<Void, Never>?
|
||||||
|
|
||||||
private var quicClient: SDLQUICClient?
|
private var superClient: SDLSuperClient?
|
||||||
private var superMonitorTask: Task<Void, Never>?
|
private var superMonitorTask: Task<Void, Never>?
|
||||||
|
|
||||||
nonisolated private let puncherActor: SDLPuncherActor
|
nonisolated private let puncherActor: SDLPuncherActor
|
||||||
@ -161,20 +161,20 @@ actor SDLContextActor {
|
|||||||
|
|
||||||
private func startSuperClient() async throws {
|
private func startSuperClient() async throws {
|
||||||
// 启动monitor
|
// 启动monitor
|
||||||
let quicClient = SDLQUICClient(host: self.config.serverHost, port: 1443)
|
let superClient = SDLSuperClient(host: self.config.serverHost, port: 1443)
|
||||||
self.quicClient = quicClient
|
self.superClient = superClient
|
||||||
await quicClient.start()
|
await superClient.start()
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
if self.quicClient === quicClient {
|
if self.superClient === superClient {
|
||||||
self.quicClient = nil
|
self.superClient = nil
|
||||||
}
|
}
|
||||||
SDLLogger.log("[SDLContext] startSuperClient defer")
|
SDLLogger.log("[SDLContext] startSuperClient defer")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 这里必须等待quic的协商完成
|
// 这里必须等待quic的协商完成
|
||||||
try await Task.sleep(for: .seconds(0.5))
|
try await Task.sleep(for: .seconds(0.5))
|
||||||
SDLLogger.log("[SDLContext] start quic client: \(self.config.serverHost)")
|
SDLLogger.log("[SDLContext] start super client: \(self.config.serverHost)")
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try await withTaskCancellationHandler {
|
try await withTaskCancellationHandler {
|
||||||
@ -184,7 +184,7 @@ actor SDLContextActor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group.addTask {
|
group.addTask {
|
||||||
for try await message in await quicClient.messageStream {
|
for try await message in await superClient.messageStream {
|
||||||
try Task.checkCancellation()
|
try Task.checkCancellation()
|
||||||
await self.handleQUICMessage(message: message)
|
await self.handleQUICMessage(message: message)
|
||||||
}
|
}
|
||||||
@ -194,21 +194,21 @@ actor SDLContextActor {
|
|||||||
while true {
|
while true {
|
||||||
try await Task.sleep(for: .seconds(5))
|
try await Task.sleep(for: .seconds(5))
|
||||||
try Task.checkCancellation()
|
try Task.checkCancellation()
|
||||||
await quicClient.send(type: .ping, data: Data())
|
await superClient.send(type: .ping, data: Data())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try await group.next()
|
try await group.next()
|
||||||
}
|
}
|
||||||
} onCancel: {
|
} onCancel: {
|
||||||
SDLLogger.log("[SDLQUICClient] startSuperClient taskGroup cancel", for: .debug)
|
SDLLogger.log("[SDLSuperClient] startSuperClient taskGroup cancel", for: .debug)
|
||||||
Task {
|
Task {
|
||||||
SDLLogger.log("[SDLContext] startSuperClient onCancel")
|
SDLLogger.log("[SDLContext] startSuperClient onCancel")
|
||||||
await quicClient.stop()
|
await superClient.stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch let err {
|
} catch let err {
|
||||||
await quicClient.stop()
|
await superClient.stop()
|
||||||
SDLLogger.log("[SDLContext] startSuperClient catch err: \(err)")
|
SDLLogger.log("[SDLContext] startSuperClient catch err: \(err)")
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
@ -294,23 +294,19 @@ actor SDLContextActor {
|
|||||||
dnsClient.start()
|
dnsClient.start()
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
|
dnsClient.stop()
|
||||||
self.dnsClient = nil
|
self.dnsClient = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
try await withTaskCancellationHandler {
|
||||||
try await withTaskCancellationHandler {
|
for try await packet in dnsClient.packetFlow {
|
||||||
for try await packet in dnsClient.packetFlow {
|
try Task.checkCancellation()
|
||||||
try Task.checkCancellation()
|
|
||||||
|
let nePacket = NEPacket(data: packet, protocolFamily: 2)
|
||||||
let nePacket = NEPacket(data: packet, protocolFamily: 2)
|
self.provider.packetFlow.writePacketObjects([nePacket])
|
||||||
self.provider.packetFlow.writePacketObjects([nePacket])
|
|
||||||
}
|
|
||||||
} onCancel: {
|
|
||||||
dnsClient.stop()
|
|
||||||
}
|
}
|
||||||
} catch let err {
|
} onCancel: {
|
||||||
dnsClient.stop()
|
dnsClient.stop()
|
||||||
throw err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -522,11 +518,11 @@ actor SDLContextActor {
|
|||||||
dnsClient?.stop()
|
dnsClient?.stop()
|
||||||
await dnsLocalClient?.stop()
|
await dnsLocalClient?.stop()
|
||||||
|
|
||||||
let quicClient = self.quicClient
|
let superClient = self.superClient
|
||||||
self.quicClient = nil
|
self.superClient = nil
|
||||||
self.superMonitorTask?.cancel()
|
self.superMonitorTask?.cancel()
|
||||||
self.superMonitorTask = nil
|
self.superMonitorTask = nil
|
||||||
await quicClient?.stop()
|
await superClient?.stop()
|
||||||
|
|
||||||
self.readTask?.cancel()
|
self.readTask?.cancel()
|
||||||
self.readTask = nil
|
self.readTask = nil
|
||||||
@ -565,7 +561,7 @@ actor SDLContextActor {
|
|||||||
while true {
|
while true {
|
||||||
try await Task.sleep(for: .seconds(300))
|
try await Task.sleep(for: .seconds(300))
|
||||||
SDLLogger.log("[SDLContext] updatePolicyTask execute")
|
SDLLogger.log("[SDLContext] updatePolicyTask execute")
|
||||||
await self.identifyStore.batUpdatePolicy(using: self.quicClient, dstIdentityID: self.config.identityId)
|
await self.identifyStore.batUpdatePolicy(using: self.superClient, dstIdentityID: self.config.identityId)
|
||||||
}
|
}
|
||||||
} catch let err {
|
} catch let err {
|
||||||
SDLLogger.log("[SDLContext] updatePolicyTask stop with err: \(err)")
|
SDLLogger.log("[SDLContext] updatePolicyTask stop with err: \(err)")
|
||||||
@ -899,7 +895,7 @@ extension SDLContextActor {
|
|||||||
|
|
||||||
if let registerSuperData = try? registerSuper.serializedData() {
|
if let registerSuperData = try? registerSuper.serializedData() {
|
||||||
SDLLogger.log("[SDLContext] will send register super")
|
SDLLogger.log("[SDLContext] will send register super")
|
||||||
await self.quicClient?.send(type: .registerSuper, data: registerSuperData)
|
await self.superClient?.send(type: .registerSuper, data: registerSuperData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -977,7 +973,7 @@ extension SDLContextActor {
|
|||||||
case .requestPolicy(let srcIdentityID):
|
case .requestPolicy(let srcIdentityID):
|
||||||
SDLLogger.log("[SDLContext] not found identity: \(srcIdentityID) ruleMap", for: .debug)
|
SDLLogger.log("[SDLContext] not found identity: \(srcIdentityID) ruleMap", for: .debug)
|
||||||
// 向服务器请求权限逻辑
|
// 向服务器请求权限逻辑
|
||||||
await self.identifyStore.policyRequest(srcIdentityId: srcIdentityID, dstIdentityId: self.config.identityId, using: self.quicClient)
|
await self.identifyStore.policyRequest(srcIdentityId: srcIdentityID, dstIdentityId: self.config.identityId, using: self.superClient)
|
||||||
case .none:
|
case .none:
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
@ -1049,7 +1045,7 @@ extension SDLContextActor {
|
|||||||
// let arpReqeust = ARPPacket.arpRequest(senderIP: networkAddr.ip, senderMAC: networkAddr.mac, targetIP: dstIp)
|
// let arpReqeust = ARPPacket.arpRequest(senderIP: networkAddr.ip, senderMAC: networkAddr.mac, targetIP: dstIp)
|
||||||
// await self.routeLayerPacket(dstMac: ARPPacket.broadcastMac , type: .arp, data: arpReqeust.marshal())
|
// await self.routeLayerPacket(dstMac: ARPPacket.broadcastMac , type: .arp, data: arpReqeust.marshal())
|
||||||
|
|
||||||
try? await self.arpServer.arpRequest(targetIp: ip, use: self.quicClient)
|
try? await self.arpServer.arpRequest(targetIp: ip, use: self.superClient)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1088,7 +1084,7 @@ extension SDLContextActor {
|
|||||||
self.flowTracer.inc(num: payload.count, type: .forward)
|
self.flowTracer.inc(num: payload.count, type: .forward)
|
||||||
|
|
||||||
// 尝试打洞
|
// 尝试打洞
|
||||||
await self.puncherActor.submitRegisterRequest(quicClient: self.quicClient, request: request)
|
await self.puncherActor.submitRegisterRequest(superClient: self.superClient, request: request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -64,8 +64,8 @@ actor SDLPuncherActor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func submitRegisterRequest(quicClient: SDLQUICClient?, request: RegisterRequest) async {
|
func submitRegisterRequest(superClient: SDLSuperClient?, request: RegisterRequest) async {
|
||||||
guard let quicClient else {
|
guard let superClient else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ actor SDLPuncherActor {
|
|||||||
phase: .waitingPeerInfo(deadline: now.addingTimeInterval(self.peerInfoTimeout))
|
phase: .waitingPeerInfo(deadline: now.addingTimeInterval(self.peerInfoTimeout))
|
||||||
)
|
)
|
||||||
|
|
||||||
await quicClient.send(type: .queryInfo, data: queryData)
|
await superClient.send(type: .queryInfo, data: queryData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePeerInfo(using udpHole: SDLUDPHole?, udpHoleV6: SDLUDPHoleV6?, peerInfo: SDLPeerInfo) async {
|
func handlePeerInfo(using udpHole: SDLUDPHole?, udpHoleV6: SDLUDPHoleV6?, peerInfo: SDLPeerInfo) async {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// SDLQuicClient.swift
|
// SDLSuperClient.swift
|
||||||
// Tun
|
// Tun
|
||||||
//
|
//
|
||||||
// Created by 安礼成 on 2026/2/13.
|
// Created by 安礼成 on 2026/2/13.
|
||||||
@ -25,7 +25,7 @@ enum SDLQUICError: Error {
|
|||||||
case dataStreamClosed
|
case dataStreamClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
actor SDLQUICClient {
|
actor SDLSuperClient {
|
||||||
enum State {
|
enum State {
|
||||||
case idle
|
case idle
|
||||||
case running
|
case running
|
||||||
@ -58,7 +58,7 @@ actor SDLQUICClient {
|
|||||||
|
|
||||||
func start() {
|
func start() {
|
||||||
let host = self.host
|
let host = self.host
|
||||||
let queue = DispatchQueue(label: "com.sdl.QUICClient.queue") // 专用队列保证线程安全
|
let queue = DispatchQueue(label: "com.sdl.SuperClient.queue") // 专用队列保证线程安全
|
||||||
|
|
||||||
let options = NWProtocolTLS.Options()
|
let options = NWProtocolTLS.Options()
|
||||||
sec_protocol_options_add_tls_application_protocol(
|
sec_protocol_options_add_tls_application_protocol(
|
||||||
@ -75,7 +75,7 @@ actor SDLQUICClient {
|
|||||||
},
|
},
|
||||||
queue
|
queue
|
||||||
)
|
)
|
||||||
SDLLogger.log("[SDLQUICClient] start with tls protocol", for: .debug)
|
SDLLogger.log("[SDLSuperClient] start with tls protocol", for: .debug)
|
||||||
|
|
||||||
let params = NWParameters(tls: options)
|
let params = NWParameters(tls: options)
|
||||||
// 关键:让 Network.framework 忽略系统代理
|
// 关键:让 Network.framework 忽略系统代理
|
||||||
@ -84,7 +84,7 @@ actor SDLQUICClient {
|
|||||||
let connection = NWConnection(host: .init(host), port: .init(rawValue: port)!, using: params)
|
let connection = NWConnection(host: .init(host), port: .init(rawValue: port)!, using: params)
|
||||||
|
|
||||||
connection.stateUpdateHandler = { [weak self] state in
|
connection.stateUpdateHandler = { [weak self] state in
|
||||||
SDLLogger.log("[SDLQUICClient] new state: \(state)", for: .debug)
|
SDLLogger.log("[SDLSuperClient] new state: \(state)", for: .debug)
|
||||||
Task {
|
Task {
|
||||||
await self?.handleConnectionState(state: state)
|
await self?.handleConnectionState(state: state)
|
||||||
}
|
}
|
||||||
@ -156,7 +156,7 @@ actor SDLQUICClient {
|
|||||||
connection.send(content: packet, completion: .contentProcessed { [weak self] error in
|
connection.send(content: packet, completion: .contentProcessed { [weak self] error in
|
||||||
if let error {
|
if let error {
|
||||||
Task {
|
Task {
|
||||||
SDLLogger.log("[SDLQUICClient] send data get error: \(error)", for: .debug)
|
SDLLogger.log("[SDLSuperClient] send data get error: \(error)", for: .debug)
|
||||||
await self?.finishMessageContinuationIfNeed(throwing: .writeFailed(error))
|
await self?.finishMessageContinuationIfNeed(throwing: .writeFailed(error))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -201,17 +201,17 @@ actor SDLQUICClient {
|
|||||||
|
|
||||||
self.finishMessageContinuationIfNeed(throwing: nil)
|
self.finishMessageContinuationIfNeed(throwing: nil)
|
||||||
|
|
||||||
SDLLogger.log("[SDLQUICClient] stopped")
|
SDLLogger.log("[SDLSuperClient] stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
SDLLogger.log("[SDLQUICClient] deinit")
|
SDLLogger.log("[SDLSuperClient] deinit")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --MARK: 数据累加器
|
// --MARK: 数据累加器
|
||||||
extension SDLQUICClient {
|
extension SDLSuperClient {
|
||||||
|
|
||||||
class SDLQUICFrameParser {
|
class SDLQUICFrameParser {
|
||||||
private let allocator = ByteBufferAllocator()
|
private let allocator = ByteBufferAllocator()
|
||||||
@ -266,7 +266,7 @@ extension SDLQUICClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --MARK: 编解码器
|
// --MARK: 编解码器
|
||||||
extension SDLQUICClient {
|
extension SDLSuperClient {
|
||||||
|
|
||||||
enum SDLQUICCodec {
|
enum SDLQUICCodec {
|
||||||
public static func decode(frame: ByteBuffer) -> SDLQUICInboundMessage? {
|
public static func decode(frame: ByteBuffer) -> SDLQUICInboundMessage? {
|
||||||
@ -317,14 +317,14 @@ extension SDLQUICClient {
|
|||||||
case .event:
|
case .event:
|
||||||
guard let bytes = buffer.readBytes(length: buffer.readableBytes),
|
guard let bytes = buffer.readBytes(length: buffer.readableBytes),
|
||||||
let event = try? SDLEvent(serializedBytes: bytes) else {
|
let event = try? SDLEvent(serializedBytes: bytes) else {
|
||||||
SDLLogger.log("SDLQUICClient decode Event Error", for: .debug)
|
SDLLogger.log("SDLSuperClient decode Event Error", for: .debug)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return .event(event)
|
return .event(event)
|
||||||
case .pong:
|
case .pong:
|
||||||
return .pong
|
return .pong
|
||||||
default:
|
default:
|
||||||
SDLLogger.log("SDLQUICClient decode miss type: \(type)", for: .debug)
|
SDLLogger.log("SDLSuperClient decode miss type: \(type)", for: .debug)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -333,7 +333,7 @@ extension SDLQUICClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --MARK: tls验证
|
// --MARK: tls验证
|
||||||
extension SDLQUICClient {
|
extension SDLSuperClient {
|
||||||
|
|
||||||
enum TLSVerifier {
|
enum TLSVerifier {
|
||||||
// 你的 Base64 公钥指纹
|
// 你的 Base64 公钥指纹
|
||||||
@ -25,9 +25,9 @@ actor IdentityStore {
|
|||||||
self.publisher = publisher
|
self.publisher = publisher
|
||||||
}
|
}
|
||||||
|
|
||||||
// 批量更新, 有外部任务驱动,因为这里依赖于当前的quicClient
|
// 批量更新, 有外部任务驱动,因为这里依赖于当前的superClient
|
||||||
func batUpdatePolicy(using quicClient: SDLQUICClient?, dstIdentityID: UInt32) async {
|
func batUpdatePolicy(using superClient: SDLSuperClient?, dstIdentityID: UInt32) async {
|
||||||
guard let quicClient else {
|
guard let superClient else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,14 +39,14 @@ actor IdentityStore {
|
|||||||
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
if let queryData = try? policyRequest.serializedData() {
|
if let queryData = try? policyRequest.serializedData() {
|
||||||
await quicClient.send(type: .policyRequest, data: queryData)
|
await superClient.send(type: .policyRequest, data: queryData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提交权限请求
|
// 提交权限请求
|
||||||
func policyRequest(srcIdentityId: UInt32, dstIdentityId: UInt32, using quicClient: SDLQUICClient?) async {
|
func policyRequest(srcIdentityId: UInt32, dstIdentityId: UInt32, using superClient: SDLSuperClient?) async {
|
||||||
guard let quicClient, !coolingDown.contains(srcIdentityId) else {
|
guard let superClient, !coolingDown.contains(srcIdentityId) else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ actor IdentityStore {
|
|||||||
coolingDown.insert(srcIdentityId)
|
coolingDown.insert(srcIdentityId)
|
||||||
// 发送请求
|
// 发送请求
|
||||||
if let queryData = try? policyRequest.serializedData() {
|
if let queryData = try? policyRequest.serializedData() {
|
||||||
await quicClient.send(type: .policyRequest, data: queryData)
|
await superClient.send(type: .policyRequest, data: queryData)
|
||||||
}
|
}
|
||||||
|
|
||||||
Task {
|
Task {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user