fix vpn
This commit is contained in:
parent
d6e6c961a2
commit
c0a60a2f3f
@ -20,13 +20,16 @@ class VPNManager {
|
||||
|
||||
var vpnStatus: VPNStatus = .disconnected
|
||||
|
||||
var vpnStatusStream: AsyncStream<VPNStatus>
|
||||
private var vpnStatusCont: AsyncStream<VPNStatus>.Continuation
|
||||
|
||||
enum VPNStatus {
|
||||
case connected
|
||||
case disconnected
|
||||
}
|
||||
|
||||
private init() {
|
||||
|
||||
(self.vpnStatusStream, self.vpnStatusCont) = AsyncStream.makeStream(of: VPNStatus.self)
|
||||
}
|
||||
|
||||
// 开启vpn
|
||||
@ -63,10 +66,13 @@ class VPNManager {
|
||||
NSLog("status channge: \(manager.connection.status)")
|
||||
switch manager.connection.status {
|
||||
case .invalid, .disconnected, .disconnecting:
|
||||
self?.vpnStatusCont.yield(.disconnected)
|
||||
self?.vpnStatus = .disconnected
|
||||
case .connecting, .connected, .reasserting:
|
||||
self?.vpnStatusCont.yield(.connected)
|
||||
self?.vpnStatus = .connected
|
||||
@unknown default:
|
||||
self?.vpnStatusCont.yield(.disconnected)
|
||||
self?.vpnStatus = .disconnected
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,8 +8,14 @@
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
enum AppContextError: Error {
|
||||
case nullNetworkSession
|
||||
}
|
||||
|
||||
@Observable
|
||||
class AppContext {
|
||||
private var vpnManager = VPNManager.shared
|
||||
|
||||
var noticePort: Int
|
||||
|
||||
// 调用 "/connect" 之后的网络信息
|
||||
@ -81,7 +87,7 @@ class AppContext {
|
||||
// 连接到对应的网络
|
||||
func connectNetwork() async throws {
|
||||
guard let session = self.networkSession else {
|
||||
return
|
||||
throw AppContextError.nullNetworkSession
|
||||
}
|
||||
|
||||
let context = try await SDLAPIClient.connectNetwork(accesToken: session.accessToken)
|
||||
@ -95,12 +101,19 @@ class AppContext {
|
||||
hostname: context.hostname,
|
||||
noticePort: noticePort
|
||||
) {
|
||||
try await VPNManager.shared.enableVpn(options: options)
|
||||
try await self.vpnManager.enableVpn(options: options)
|
||||
self.networkContext = context
|
||||
self.vpnOptions = options
|
||||
}
|
||||
}
|
||||
|
||||
// 退出登陆
|
||||
func logout() async throws {
|
||||
try await self.vpnManager.disableVpn()
|
||||
self.networkContext = .default()
|
||||
self.loginCredit = nil
|
||||
}
|
||||
|
||||
func loadCacheToken() -> String? {
|
||||
if let data = try? KeychainStore.shared.load(account: "token") {
|
||||
return String(data: data, encoding: .utf8)
|
||||
@ -118,12 +131,4 @@ class AppContext {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 退出登陆
|
||||
func logout() async throws {
|
||||
try await VPNManager.shared.disableVpn()
|
||||
self.networkContext = .default()
|
||||
self.loginCredit = nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -115,12 +115,14 @@ extension SettingsAccountView {
|
||||
Task { @MainActor in
|
||||
try await appContext.logout()
|
||||
}
|
||||
self.appContext.appScene = .login(username: username)
|
||||
self.openWindow(id: "main")
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.foregroundColor(.red)
|
||||
}
|
||||
))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -136,7 +138,9 @@ extension SettingsAccountView {
|
||||
Task { @MainActor in
|
||||
try await appContext.logout()
|
||||
}
|
||||
self.appContext.appScene = .login(username: nil)
|
||||
self.openWindow(id: "main")
|
||||
// 执行关闭当前窗口的操作
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.foregroundColor(.red)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user