punchnet-macos/Tun/Punchnet/SDLConfiguration.swift

109 lines
3.0 KiB
Swift

//
// SDLConfiguration.swift
// sdlan
//
// Created by on 2025/7/14.
//
import Foundation
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
}
}
public struct NetworkAddress {
public let networkId: UInt32
public let ip: UInt32
public let maskLen: UInt8
public let mac: Data
public let networkDomain: String
}
//
let version: UInt8
//
let installedChannel: String
let superHost: String
let superPort: Int
let stunServers: [StunServer]
let remoteDnsServer: String
let hostname: String
let noticePort: Int
let networkAddress: NetworkAddress = .init(networkId: 8, ip: 0, maskLen: 24, mac: "xyz".data(using: .utf8)!, networkDomain: "punchnet")
lazy var stunSocketAddress: SocketAddress = {
let stunServer = stunServers[0]
return try! SocketAddress.makeAddressResolvingHost(stunServer.host, port: stunServer.ports[0])
}()
//
lazy var stunProbeSocketAddressArray: [[SocketAddress]] = {
return stunServers.map { stunServer in
[
try! SocketAddress.makeAddressResolvingHost(stunServer.host, port: stunServer.ports[0]),
try! SocketAddress.makeAddressResolvingHost(stunServer.host, port: stunServer.ports[1])
]
}
}()
let clientId: String
let token: String
let accessToken: String
public init(version: UInt8, installedChannel: String, superHost: String, superPort: Int, stunServers: [StunServer], clientId: String, noticePort: Int, token: String, accessToken: String, remoteDnsServer: String, hostname: String) {
self.version = version
self.installedChannel = installedChannel
self.superHost = superHost
self.superPort = superPort
self.stunServers = stunServers
self.clientId = clientId
self.noticePort = noticePort
self.token = token
self.accessToken = accessToken
self.remoteDnsServer = remoteDnsServer
self.hostname = hostname
}
// mac
public static func getMacAddress() -> Data {
let key = "gMacAddress2"
let userDefaults = UserDefaults.standard
if let mac = userDefaults.value(forKey: key) as? Data {
return mac
}
else {
let mac = generateMacAddress()
userDefaults.setValue(mac, forKey: key)
return mac
}
}
// mac
private static func generateMacAddress() -> Data {
var macAddress = [UInt8](repeating: 0, count: 6)
for i in 0..<6 {
macAddress[i] = UInt8.random(in: 0...255)
}
return Data(macAddress)
}
}