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