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