Compare commits

..

No commits in common. "main" and "1.1.2" have entirely different histories.
main ... 1.1.2

4 changed files with 31 additions and 47 deletions

View File

@ -10,14 +10,9 @@ import NIOCore
//
public class SDLConfiguration {
public struct StunServer {
public let host: String
public let ports: [Int]
public init(host: String, ports: [Int]) {
self.host = host
self.ports = ports
}
struct StunServer {
let host: String
let ports: [Int]
}
//
@ -49,7 +44,7 @@ public class SDLConfiguration {
let clientId: String
let token: String
public init(version: UInt8, installedChannel: String, superHost: String, superPort: Int, stunServers: [StunServer], clientId: String, token: String) {
init(version: UInt8, installedChannel: String, superHost: String, superPort: Int, stunServers: [StunServer], clientId: String, token: String) {
self.version = version
self.installedChannel = installedChannel
self.superHost = superHost

View File

@ -79,7 +79,7 @@ public class SDLContext: @unchecked Sendable {
private var flowTracer = SDLFlowTracerActor()
private var flowTracerCancel: AnyCancellable?
public init(provider: NEPacketTunnelProvider, config: SDLConfiguration, rsaCipher: RSACipher, aesCipher: AESCipher) {
init(provider: NEPacketTunnelProvider, config: SDLConfiguration, rsaCipher: RSACipher, aesCipher: AESCipher) throws {
self.config = config
self.rsaCipher = rsaCipher
self.aesCipher = aesCipher
@ -96,7 +96,7 @@ public class SDLContext: @unchecked Sendable {
self.noticeClient = SDLNoticeClient()
}
public func start() async throws {
func start() async throws {
try await self.startSuperClient()
try await self.startUDPHole()
self.noticeClient.start()
@ -117,16 +117,6 @@ 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
@ -186,13 +176,13 @@ public class SDLContext: @unchecked Sendable {
let alertNotice = NoticeMessage.AlertMessage(alert: errorMessage)
self.noticeClient.send(data: alertNotice.binaryData)
}
NSLog("[SDLContext] Get a SuperNak message exit")
SDLLogger.log("Get a SuperNak message exit", level: .error)
default:
()
}
case .closed:
NSLog("[SDLContext] super client closed")
SDLLogger.log("[SDLContext] super client closed", level: .debug)
await self.arpServer.clear()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
Task {@MainActor in
@ -203,10 +193,10 @@ public class SDLContext: @unchecked Sendable {
switch evt {
case .natChanged(let natChangedEvent):
let dstMac = natChangedEvent.mac
NSLog("[SDLContext] natChangedEvent, dstMac: \(dstMac)")
NSLog("natChangedEvent, dstMac: \(dstMac)")
await sessionManager.removeSession(dstMac: dstMac)
case .sendRegister(let sendRegisterEvent):
NSLog("[SDLContext] sendRegisterEvent, ip: \(sendRegisterEvent)")
NSLog("sendRegisterEvent, ip: \(sendRegisterEvent)")
let address = SDLUtil.int32ToIp(sendRegisterEvent.natIp)
if let remoteAddress = try? SocketAddress.makeAddressResolvingHost(address, port: Int(sendRegisterEvent.natPort)) {
// register
@ -360,7 +350,7 @@ public class SDLContext: @unchecked Sendable {
}
//
public func flowReportTask() {
func flowReportTask() {
Task {
//
self.flowTracerCancel = Timer.publish(every: 60.0, on: .main, in: .common).autoconnect()
@ -389,8 +379,6 @@ 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()
@ -403,11 +391,10 @@ public class SDLContext: @unchecked Sendable {
try await self.provider.setTunnelNetworkSettings(networkSettings)
await self.holerManager.cleanup()
self.startReader()
NSLog("[SDLContext] setTunnelNetworkSettings success, start read packet")
self.startReader()
} catch let err {
NSLog("[SDLContext] setTunnelNetworkSettings get error: \(err)")
SDLLogger.log("[SDLContext] setTunnelNetworkSettings get error: \(err)", level: .error)
exit(-1)
}
}
@ -519,7 +506,7 @@ public class SDLContext: @unchecked Sendable {
extension SDLContext {
public static func getUUID() -> String {
static func getUUID() -> String {
let userDefaults = UserDefaults.standard
if let uuid = userDefaults.value(forKey: "gClientId") as? String {
return uuid
@ -532,7 +519,7 @@ extension SDLContext {
}
// mac
public static func getMacAddress() -> Data {
static func getMacAddress() -> Data {
let key = "gMacAddress2"
let userDefaults = UserDefaults.standard

View File

@ -23,7 +23,7 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
public typealias InboundIn = AddressedEnvelope<ByteBuffer>
public typealias OutboundOut = AddressedEnvelope<ByteBuffer>
var channel: Channel?
var context: ChannelHandlerContext?
private let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
private let remoteAddress: SocketAddress
@ -40,14 +40,17 @@ class SDLNoticeClient: ChannelInboundHandler, @unchecked Sendable {
channel.pipeline.addHandler(self)
}
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)
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()
}
// -- MARK: ChannelInboundHandler Methods
public func channelActive(context: ChannelHandlerContext) {
self.context = context
}
// ,
@ -59,27 +62,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.channel = nil
self.context = nil
}
public func channelInactive(context: ChannelHandlerContext) {
self.channel = nil
self.context = nil
context.close(promise: nil)
}
//
func send(data: Data) {
guard let channel = self.channel else {
guard let context = self.context else {
return
}
let remoteAddress = self.remoteAddress
let allocator = channel.allocator
let allocator = context.channel.allocator
channel.eventLoop.execute { [allocator] in
context.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)
context.writeAndFlush(self.wrapOutboundOut(envelope), promise: nil)
}
}

View File

@ -8,15 +8,14 @@
import Foundation
// qps
class SDLQPSCounter: @unchecked Sendable {
class SDLQPSCounter {
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: queue)
timer = DispatchSource.makeTimerSource(queue: DispatchQueue(label: "com.yourapp.qps"))
timer.schedule(deadline: .now(), repeating: .seconds(1), leeway: .milliseconds(100))
timer.setEventHandler { [weak self] in
guard let self = self else { return }
@ -27,7 +26,7 @@ class SDLQPSCounter: @unchecked Sendable {
}
func increment(num: Int = 1) {
queue.async {
DispatchQueue(label: "com.yourapp.qps").async {
self.count += num
}
}