55 lines
1.4 KiB
Swift
55 lines
1.4 KiB
Swift
//
|
|
// SDLPuncherActor.swift
|
|
// Tun
|
|
//
|
|
// Created by 安礼成 on 2026/1/7.
|
|
//
|
|
|
|
import Foundation
|
|
import NIOCore
|
|
|
|
actor PolicyRequesterActor {
|
|
nonisolated private let cooldown: Duration = .seconds(5)
|
|
|
|
// identityId
|
|
private var coolingDown: Set<UInt32> = []
|
|
|
|
// 处理各个请求的版本问题, map[identityId] = version
|
|
private var versions: [UInt32: UInt32] = [:]
|
|
|
|
init() {
|
|
|
|
}
|
|
|
|
// 提交权限请求
|
|
func submitPolicyRequest(using quicClient: SDLQUICClient?, request: inout SDLPolicyRequest) {
|
|
let identityId = request.srcIdentityID
|
|
guard let quicClient, !coolingDown.contains(identityId) else {
|
|
return
|
|
}
|
|
|
|
// 触发一次打洞
|
|
coolingDown.insert(identityId)
|
|
|
|
let version = self.versions[identityId, default: 1]
|
|
request.version = version
|
|
// 更新请求的版本问题
|
|
self.versions[identityId] = version + 1
|
|
// 发送请求
|
|
if let queryData = try? request.serializedData() {
|
|
quicClient.send(type: .policyRequest, data: queryData)
|
|
}
|
|
|
|
Task {
|
|
// 启动冷却期
|
|
try? await Task.sleep(for: .seconds(5))
|
|
self.endCooldown(for: identityId)
|
|
}
|
|
}
|
|
|
|
private func endCooldown(for key: UInt32) {
|
|
self.coolingDown.remove(key)
|
|
}
|
|
|
|
}
|