Добавь ручной и автоматический режимы VPN
This commit is contained in:
@@ -92,10 +92,14 @@ struct VPNMenuView: View {
|
||||
Label("VPN отключён", systemImage: "circle")
|
||||
.disabled(true)
|
||||
Divider()
|
||||
Button("Подключить") {
|
||||
vpnManager.connect()
|
||||
Button("Подключить автоматически") {
|
||||
vpnManager.connect(mode: .auto)
|
||||
}
|
||||
.keyboardShortcut("c")
|
||||
Button("Подключить вручную") {
|
||||
vpnManager.connect(mode: .manual)
|
||||
}
|
||||
.keyboardShortcut("m")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +111,18 @@ enum VPNState: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
enum VPNLaunchMode: String {
|
||||
case auto
|
||||
case manual
|
||||
|
||||
var cliArgument: String {
|
||||
switch self {
|
||||
case .auto: return "--auto"
|
||||
case .manual: return "--manual"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
class VPNManager: ObservableObject {
|
||||
@Published var state: VPNState = .disconnected
|
||||
@@ -131,6 +143,7 @@ class VPNManager: ObservableObject {
|
||||
private var autoReconnectAttempts: Int = 0
|
||||
private var reconnectTimer: Timer?
|
||||
private var consecutiveHealthFailures: Int = 0
|
||||
private var currentLaunchMode: VPNLaunchMode = .auto
|
||||
|
||||
private let healthCheckInterval: TimeInterval = 10
|
||||
private let maxAutoReconnectAttempts: Int = 3
|
||||
@@ -223,12 +236,13 @@ class VPNManager: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func connect() {
|
||||
func connect(mode: VPNLaunchMode = .auto) {
|
||||
guard !isRunning else {
|
||||
log("connect() called but process already running")
|
||||
return
|
||||
}
|
||||
log("-- VPN connect requested --")
|
||||
currentLaunchMode = mode
|
||||
log("-- VPN connect requested (\(mode.rawValue)) --")
|
||||
refreshStatus()
|
||||
state = .connecting
|
||||
lastError = nil
|
||||
@@ -240,7 +254,7 @@ class VPNManager: ObservableObject {
|
||||
|
||||
let proc = Process()
|
||||
proc.executableURL = URL(fileURLWithPath: "/bin/bash")
|
||||
proc.arguments = ["-l", scriptPath, "--json"]
|
||||
proc.arguments = ["-l", scriptPath, "--json", mode.cliArgument]
|
||||
proc.environment = processEnvironment()
|
||||
|
||||
let stdoutPipe = Pipe()
|
||||
@@ -430,6 +444,10 @@ class VPNManager: ObservableObject {
|
||||
state = .disconnected
|
||||
userInitiatedDisconnect = false
|
||||
autoReconnectAttempts = 0
|
||||
} else if currentLaunchMode == .manual {
|
||||
log("Manual connection ended; auto-reconnect is disabled for manual mode")
|
||||
state = .disconnected
|
||||
autoReconnectAttempts = 0
|
||||
} else if [0, 2, 130, 143].contains(exitCode) {
|
||||
scheduleAutoReconnect(reason: "session ended (exit \(exitCode))")
|
||||
} else {
|
||||
@@ -462,7 +480,7 @@ class VPNManager: ObservableObject {
|
||||
self.log("Auto-reconnect cancelled (state changed)")
|
||||
return
|
||||
}
|
||||
self.connect()
|
||||
self.connect(mode: self.currentLaunchMode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user