解决网络状态变化的问题
This commit is contained in:
parent
9a3fb6a8d6
commit
136c73ccd1
@ -13,33 +13,28 @@ enum TunnelError: Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class PacketTunnelProvider: NEPacketTunnelProvider {
|
class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
var contextActor: SDLContextActor?
|
private var runtimeEnv: SDLRuntimeEnvironment?
|
||||||
private var rootTask: Task<Void, Never>?
|
|
||||||
|
|
||||||
override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
|
override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
|
||||||
// 重置通知中心
|
|
||||||
SDLTunnelAppNotifier.shared.clear()
|
|
||||||
|
|
||||||
// 如果当前在运行状态,不允许重复请求
|
// 如果当前在运行状态,不允许重复请求
|
||||||
guard self.contextActor == nil else {
|
guard self.runtimeEnv == nil else {
|
||||||
completionHandler(TunnelError.invalidContext)
|
completionHandler(TunnelError.invalidContext)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加密算法
|
guard let options else {
|
||||||
let rsaCipher = try! CCRSACipher(keySize: 1024)
|
|
||||||
self.rootTask = Task {
|
|
||||||
// host: "192.168.0.101", port: 1265
|
|
||||||
guard let options, let config = await SDLConfiguration.parse(options: options) else {
|
|
||||||
completionHandler(TunnelError.invalidConfiguration)
|
completionHandler(TunnelError.invalidConfiguration)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let contextActor = SDLContextActor(provider: self, config: config, rsaCipher: rsaCipher)
|
self.runtimeEnv = SDLRuntimeEnvironment(options: options)
|
||||||
self.contextActor = contextActor
|
Task {
|
||||||
await contextActor.start()
|
do {
|
||||||
|
try await self.runtimeEnv?.start(provider: self)
|
||||||
completionHandler(nil)
|
completionHandler(nil)
|
||||||
|
} catch let err {
|
||||||
|
completionHandler(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -47,12 +42,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
|
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
|
||||||
// Add code here to start the process of stopping the tunnel.
|
// Add code here to start the process of stopping the tunnel.
|
||||||
Task {
|
Task {
|
||||||
await self.contextActor?.stop()
|
await self.runtimeEnv?.stop()
|
||||||
self.contextActor = nil
|
|
||||||
|
|
||||||
self.rootTask?.cancel()
|
|
||||||
self.rootTask = nil
|
|
||||||
|
|
||||||
completionHandler()
|
completionHandler()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,7 +68,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
override func sleep(completionHandler: @escaping () -> Void) {
|
override func sleep(completionHandler: @escaping () -> Void) {
|
||||||
// Add code here to get ready to sleep.
|
// Add code here to get ready to sleep.
|
||||||
Task {
|
Task {
|
||||||
await self.contextActor?.sleep()
|
await self.runtimeEnv?.stop()
|
||||||
completionHandler()
|
completionHandler()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,12 +76,12 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
override func wake() {
|
override func wake() {
|
||||||
// Add code here to wake up.
|
// Add code here to wake up.
|
||||||
Task {
|
Task {
|
||||||
await self.contextActor?.wake()
|
try await self.runtimeEnv?.start(provider: self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func handleAppRequest(message: AppRequest) async throws -> Data? {
|
private func handleAppRequest(message: AppRequest) async throws -> Data? {
|
||||||
guard let contextActor = self.contextActor else {
|
guard let contextActor = self.runtimeEnv?.getContextActor() else {
|
||||||
throw TunnelError.invalidContext
|
throw TunnelError.invalidContext
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,3 +111,36 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class SDLRuntimeEnvironment {
|
||||||
|
var contextActor: SDLContextActor?
|
||||||
|
private var options: [String: NSObject]
|
||||||
|
|
||||||
|
init(options: [String: NSObject]) {
|
||||||
|
self.options = options
|
||||||
|
}
|
||||||
|
|
||||||
|
func start(provider: PacketTunnelProvider) async throws {
|
||||||
|
// 重置通知中心
|
||||||
|
SDLTunnelAppNotifier.shared.clear()
|
||||||
|
|
||||||
|
guard let config = await SDLConfiguration.parse(options: options) else {
|
||||||
|
throw TunnelError.invalidConfiguration
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加密算法
|
||||||
|
let rsaCipher = try! CCRSACipher(keySize: 1024)
|
||||||
|
let contextActor = SDLContextActor(provider: provider, config: config, rsaCipher: rsaCipher)
|
||||||
|
self.contextActor = contextActor
|
||||||
|
await contextActor.start()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getContextActor() -> SDLContextActor? {
|
||||||
|
return self.contextActor
|
||||||
|
}
|
||||||
|
|
||||||
|
func stop() async {
|
||||||
|
await self.contextActor?.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@ -90,7 +90,7 @@ actor SDLPuncherActor {
|
|||||||
phase: .waitingPeerInfo(deadline: now.addingTimeInterval(self.peerInfoTimeout))
|
phase: .waitingPeerInfo(deadline: now.addingTimeInterval(self.peerInfoTimeout))
|
||||||
)
|
)
|
||||||
|
|
||||||
await quicClient.send(type: .queryInfo, data: queryData)
|
quicClient.send(type: .queryInfo, data: queryData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePeerInfo(using udpHole: SDLUDPHole?, udpHoleV6: SDLUDPHoleV6?, peerInfo: SDLPeerInfo) async {
|
func handlePeerInfo(using udpHole: SDLUDPHole?, udpHoleV6: SDLUDPHoleV6?, peerInfo: SDLPeerInfo) async {
|
||||||
@ -156,10 +156,7 @@ actor SDLPuncherActor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func sendRegister(using udpHole: SDLUDPHole?,
|
private func sendRegister(using udpHole: SDLUDPHole?, udpHoleV6: SDLUDPHoleV6?, registerData: Data, remoteAddress: SocketAddress) {
|
||||||
udpHoleV6: SDLUDPHoleV6?,
|
|
||||||
registerData: Data,
|
|
||||||
remoteAddress: SocketAddress) {
|
|
||||||
switch remoteAddress {
|
switch remoteAddress {
|
||||||
case .v4:
|
case .v4:
|
||||||
guard let udpHole else {
|
guard let udpHole else {
|
||||||
|
|||||||
2
dmg.sh
2
dmg.sh
@ -1,3 +1,3 @@
|
|||||||
#! /bin/sh
|
#! /bin/sh
|
||||||
|
|
||||||
create-dmg --volname "punchnet" --window-pos 200 120 --window-size 800 400 --icon "punchnet.app" 200 190 --hide-extension "punchnet.app" --app-drop-link 600 185 ~/Desktop/punchnet.dmg /Users/anlicheng/Desktop/punchnetv3
|
create-dmg --volname "punchnet" --window-pos 200 120 --window-size 800 400 --icon "punchnet.app" 200 190 --hide-extension "punchnet.app" --app-drop-link 600 185 ~/Desktop/punchnet.dmg /Users/anlicheng/Desktop/punchnetv6
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import SwiftUI
|
|||||||
import AppKit
|
import AppKit
|
||||||
import SwiftData
|
import SwiftData
|
||||||
import Combine
|
import Combine
|
||||||
|
//import SystemConfiguration
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct punchnetApp: App {
|
struct punchnetApp: App {
|
||||||
@ -86,7 +87,11 @@ struct punchnetApp: App {
|
|||||||
class AppDelegate: NSObject, NSApplicationDelegate {
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||||
|
|
||||||
func applicationWillFinishLaunching(_ notification: Notification) {
|
func applicationWillFinishLaunching(_ notification: Notification) {
|
||||||
|
// let settings = SCDynamicStoreCopyProxies(nil)
|
||||||
|
// let proxies = settings as? [String: Any] ?? [:]
|
||||||
|
// for (key, value) in proxies {
|
||||||
|
// print("\(key): \(value)")
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
|
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
|
||||||
|
|||||||
@ -33,6 +33,16 @@ struct SDLAPIClient {
|
|||||||
]
|
]
|
||||||
|
|
||||||
static func doPost<T: Decodable>(path: String, params: [String: Any], as: T.Type) async throws -> T {
|
static func doPost<T: Decodable>(path: String, params: [String: Any], as: T.Type) async throws -> T {
|
||||||
|
let config = URLSessionConfiguration.default
|
||||||
|
config.connectionProxyDictionary = [
|
||||||
|
kCFNetworkProxiesHTTPEnable as String: 0,
|
||||||
|
kCFNetworkProxiesHTTPSEnable as String: 0,
|
||||||
|
kCFNetworkProxiesSOCKSEnable as String: 0,
|
||||||
|
kCFNetworkProxiesProxyAutoConfigEnable as String: 0,
|
||||||
|
kCFNetworkProxiesProxyAutoDiscoveryEnable as String: 0
|
||||||
|
]
|
||||||
|
let session = URLSession(configuration: config)
|
||||||
|
|
||||||
let postData = try! JSONSerialization.data(withJSONObject: params)
|
let postData = try! JSONSerialization.data(withJSONObject: params)
|
||||||
var request = URLRequest(url: URL(string: baseUrl + path)!)
|
var request = URLRequest(url: URL(string: baseUrl + path)!)
|
||||||
request.httpMethod = "POST"
|
request.httpMethod = "POST"
|
||||||
@ -40,7 +50,7 @@ struct SDLAPIClient {
|
|||||||
request.addValue(sign(params: params), forHTTPHeaderField: "X-sign")
|
request.addValue(sign(params: params), forHTTPHeaderField: "X-sign")
|
||||||
request.httpBody = postData
|
request.httpBody = postData
|
||||||
|
|
||||||
let (data, _) = try await URLSession.shared.data(for: request)
|
let (data, _) = try await session.data(for: request)
|
||||||
|
|
||||||
if let response = String(bytes: data, encoding: .utf8) {
|
if let response = String(bytes: data, encoding: .utf8) {
|
||||||
NSLog("url: \(path), response is: \(response)")
|
NSLog("url: \(path), response is: \(response)")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user