Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ee6362a254 | |||
| aaa1b3812d | |||
| 20ef0ca985 | |||
| 1512e99d87 | |||
| 95d8102076 | |||
| 035ec9343d |
@ -10,9 +10,14 @@ import NIOCore
|
||||
// 配置项目
|
||||
public class SDLConfiguration {
|
||||
|
||||
struct StunServer {
|
||||
let host: String
|
||||
let ports: [Int]
|
||||
public struct StunServer {
|
||||
public let host: String
|
||||
public let ports: [Int]
|
||||
|
||||
public init(host: String, ports: [Int]) {
|
||||
self.host = host
|
||||
self.ports = ports
|
||||
}
|
||||
}
|
||||
|
||||
// 当前的客户端版本
|
||||
@ -44,7 +49,7 @@ public class SDLConfiguration {
|
||||
let clientId: String
|
||||
let token: String
|
||||
|
||||
init(version: UInt8, installedChannel: String, superHost: String, superPort: Int, stunServers: [StunServer], clientId: String, token: String) {
|
||||
public init(version: UInt8, installedChannel: String, superHost: String, superPort: Int, stunServers: [StunServer], clientId: String, token: String) {
|
||||
self.version = version
|
||||
self.installedChannel = installedChannel
|
||||
self.superHost = superHost
|
||||
|
||||
@ -79,7 +79,7 @@ public class SDLContext: @unchecked Sendable {
|
||||
private var flowTracer = SDLFlowTracerActor()
|
||||
private var flowTracerCancel: AnyCancellable?
|
||||
|
||||
init(provider: NEPacketTunnelProvider, config: SDLConfiguration, rsaCipher: RSACipher, aesCipher: AESCipher) throws {
|
||||
public init(provider: NEPacketTunnelProvider, config: SDLConfiguration, rsaCipher: RSACipher, aesCipher: AESCipher) {
|
||||
self.config = config
|
||||
self.rsaCipher = rsaCipher
|
||||
self.aesCipher = aesCipher
|
||||
@ -96,7 +96,7 @@ public class SDLContext: @unchecked Sendable {
|
||||
self.noticeClient = SDLNoticeClient()
|
||||
}
|
||||
|
||||
func start() async throws {
|
||||
public func start() async throws {
|
||||
try await self.startSuperClient()
|
||||
try await self.startUDPHole()
|
||||
self.noticeClient.start()
|
||||
@ -117,6 +117,16 @@ public class SDLContext: @unchecked Sendable {
|
||||
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 {
|
||||
self.superClient = SDLSuperClient(host: config.superHost, port: config.superPort)
|
||||
// 建立super的绑定关系
|
||||
@ -176,13 +186,13 @@ public class SDLContext: @unchecked Sendable {
|
||||
let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage)
|
||||
self.noticeClient.send(data: alertNotice.binaryData)
|
||||
}
|
||||
SDLLogger.log("Get a SuperNak message exit", level: .error)
|
||||
NSLog("[SDLContext] Get a SuperNak message exit")
|
||||
default:
|
||||
()
|
||||
}
|
||||
|
||||
case .closed:
|
||||
SDLLogger.log("[SDLContext] super client closed", level: .debug)
|
||||
NSLog("[SDLContext] super client closed")
|
||||
await self.arpServer.clear()
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
|
||||
Task {@MainActor in
|
||||
@ -193,10 +203,10 @@ public class SDLContext: @unchecked Sendable {
|
||||
switch evt {
|
||||
case .natChanged(let natChangedEvent):
|
||||
let dstMac = natChangedEvent.mac
|
||||
NSLog("natChangedEvent, dstMac: \(dstMac)")
|
||||
NSLog("[SDLContext] natChangedEvent, dstMac: \(dstMac)")
|
||||
await sessionManager.removeSession(dstMac: dstMac)
|
||||
case .sendRegister(let sendRegisterEvent):
|
||||
NSLog("sendRegisterEvent, ip: \(sendRegisterEvent)")
|
||||
NSLog("[SDLContext] sendRegisterEvent, ip: \(sendRegisterEvent)")
|
||||
let address = SDLUtil.int32ToIp(sendRegisterEvent.natIp)
|
||||
if let remoteAddress = try? SocketAddress.makeAddressResolvingHost(address, port: Int(sendRegisterEvent.natPort)) {
|
||||
// 发送register包
|
||||
@ -350,7 +360,7 @@ public class SDLContext: @unchecked Sendable {
|
||||
}
|
||||
|
||||
// 流量统计
|
||||
func flowReportTask() {
|
||||
public func flowReportTask() {
|
||||
Task {
|
||||
// 每分钟汇报一次
|
||||
self.flowTracerCancel = Timer.publish(every: 60.0, on: .main, in: .common).autoconnect()
|
||||
@ -379,6 +389,8 @@ public class SDLContext: @unchecked Sendable {
|
||||
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])
|
||||
// 设置路由表
|
||||
//NEIPv4Route.default()
|
||||
@ -391,10 +403,11 @@ public class SDLContext: @unchecked Sendable {
|
||||
try await self.provider.setTunnelNetworkSettings(networkSettings)
|
||||
|
||||
await self.holerManager.cleanup()
|
||||
|
||||
self.startReader()
|
||||
|
||||
NSLog("[SDLContext] setTunnelNetworkSettings success, start read packet")
|
||||
} catch let err {
|
||||
SDLLogger.log("[SDLContext] setTunnelNetworkSettings get error: \(err)", level: .error)
|
||||
NSLog("[SDLContext] setTunnelNetworkSettings get error: \(err)")
|
||||
exit(-1)
|
||||
}
|
||||
}
|
||||
@ -506,7 +519,7 @@ public class SDLContext: @unchecked Sendable {
|
||||
|
||||
extension SDLContext {
|
||||
|
||||
static func getUUID() -> String {
|
||||
public static func getUUID() -> String {
|
||||
let userDefaults = UserDefaults.standard
|
||||
if let uuid = userDefaults.value(forKey: "gClientId") as? String {
|
||||
return uuid
|
||||
@ -519,7 +532,7 @@ extension SDLContext {
|
||||
}
|
||||
|
||||
// 获取mac地址
|
||||
static func getMacAddress() -> Data {
|
||||
public static func getMacAddress() -> Data {
|
||||
let key = "gMacAddress2"
|
||||
|
||||
let userDefaults = UserDefaults.standard
|
||||
|
||||
@ -23,7 +23,7 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
|
||||
public typealias InboundIn = AddressedEnvelope<ByteBuffer>
|
||||
public typealias OutboundOut = AddressedEnvelope<ByteBuffer>
|
||||
|
||||
var context: ChannelHandlerContext?
|
||||
var channel: Channel?
|
||||
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
||||
private let remoteAddress: SocketAddress
|
||||
|
||||
@ -40,17 +40,14 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
|
||||
channel.pipeline.addHandler(self)
|
||||
}
|
||||
|
||||
let channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait()
|
||||
SDLLogger.log("[SDLNoticeClient] started and listening on: \(channel.localAddress!)", level: .debug)
|
||||
|
||||
// This will never unblock as we don't close the channel
|
||||
try! channel.closeFuture.wait()
|
||||
self.channel = try! bootstrap.bind(host: "0.0.0.0", port: 0).wait()
|
||||
SDLLogger.log("[SDLNoticeClient] started and listening on: \(self.channel?.localAddress!)", level: .debug)
|
||||
}
|
||||
|
||||
// -- MARK: ChannelInboundHandler Methods
|
||||
|
||||
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
|
||||
// reduce allocations.
|
||||
context.close(promise: nil)
|
||||
self.context = nil
|
||||
self.channel = nil
|
||||
}
|
||||
|
||||
public func channelInactive(context: ChannelHandlerContext) {
|
||||
self.context = nil
|
||||
self.channel = nil
|
||||
context.close(promise: nil)
|
||||
}
|
||||
|
||||
// 处理写入逻辑
|
||||
func send(data: Data) {
|
||||
guard let context = self.context else {
|
||||
guard let channel = self.channel else {
|
||||
return
|
||||
}
|
||||
|
||||
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 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
|
||||
|
||||
// 计数器,用来统计qps
|
||||
class SDLQPSCounter {
|
||||
class SDLQPSCounter: @unchecked Sendable {
|
||||
private var count = 0
|
||||
private let timer: DispatchSourceTimer
|
||||
private let label: String
|
||||
private let queue = DispatchQueue(label: "com.punchnet.qps")
|
||||
|
||||
init(label: String) {
|
||||
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.setEventHandler { [weak self] in
|
||||
guard let self = self else { return }
|
||||
@ -26,7 +27,7 @@ class SDLQPSCounter {
|
||||
}
|
||||
|
||||
func increment(num: Int = 1) {
|
||||
DispatchQueue(label: "com.yourapp.qps").async {
|
||||
queue.async {
|
||||
self.count += num
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user