Resolve issues when toggling/switching profiles (#747)

- Drop logic behind connection button tasks, let the library handle
concurrency
- Drop AppContext observation of saved profiles for reconnection, let
save() actively decide
- NETunnelStrategy and NETunnelManagerRepository are now a single entity
- Avoid flickering when toggling same profile
This commit is contained in:
Davide 2024-10-22 13:03:34 +02:00 committed by GitHub
parent 696f076ac5
commit 39bdf145e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 18 additions and 26 deletions

View File

@ -41,7 +41,7 @@
"kind" : "remoteSourceControl",
"location" : "git@github.com:passepartoutvpn/passepartoutkit-source",
"state" : {
"revision" : "ead8d1652fc6875f8865a1c8610b0cc35828dee6"
"revision" : "288ccbf1e1a984c8dc3a7cd2fd23d3d3fc4464f6"
}
},
{

View File

@ -28,7 +28,7 @@ let package = Package(
],
dependencies: [
// .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", from: "0.9.0"),
.package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", revision: "ead8d1652fc6875f8865a1c8610b0cc35828dee6"),
.package(url: "git@github.com:passepartoutvpn/passepartoutkit-source", revision: "288ccbf1e1a984c8dc3a7cd2fd23d3d3fc4464f6"),
// .package(path: "../../../passepartoutkit-source"),
.package(url: "git@github.com:passepartoutvpn/passepartoutkit-source-openvpn-openssl", from: "0.9.1"),
// .package(url: "git@github.com:passepartoutvpn/passepartoutkit-source-openvpn-openssl", revision: "031863a1cd683962a7dfe68e20b91fa820a1ecce"),

View File

@ -65,13 +65,10 @@ public final class NEProfileRepository: ProfileRepository {
profilesSubject.eraseToAnyPublisher()
}
// unused in app, rely on Tunnel.prepare()
public func loadProfiles(purge: Bool) async throws {
try await repository.load(purge: purge)
}
public func saveProfile(_ profile: Profile) async throws {
try await repository.save(profile, connect: false, title: title)
// FIXME: #379, save + reconnect in some scenarios
try await repository.save(profile, forConnecting: false, title: title)
if let index = profilesSubject.value.firstIndex(where: { $0.id == profile.id }) {
profilesSubject.value[index] = profile
} else {

View File

@ -115,9 +115,6 @@ private extension AppContext {
try await tunnel.disconnect()
return
}
if tunnel.status == .active {
try await tunnel.connect(with: profile, processor: profileProcessor)
}
}
}
}

View File

@ -41,9 +41,6 @@ struct TunnelRestartButton<Label>: View where Label: View {
let label: () -> Label
@State
private var pendingTask: Task<Void, Error>?
var body: some View {
Button {
guard let profile else {
@ -52,10 +49,11 @@ struct TunnelRestartButton<Label>: View where Label: View {
guard tunnel.status == .active else {
return
}
pendingTask?.cancel()
pendingTask = Task {
Task {
do {
try await tunnel.connect(with: profile, processor: profileProcessor)
} catch is CancellationError {
//
} catch {
errorHandler.handle(
error,

View File

@ -62,9 +62,6 @@ struct TunnelToggleButton<Label>: View, TunnelContextProviding, ThemeProviding w
let label: (Bool) -> Label
@State
private var pendingTask: Task<Void, Error>?
var body: some View {
Button(action: tryPerform) {
label(canConnect)
@ -100,12 +97,13 @@ private extension TunnelToggleButton {
private extension TunnelToggleButton {
func tryPerform() {
pendingTask?.cancel()
pendingTask = Task {
Task {
guard let profile else {
return
}
if !isInstalled {
nextProfileId = profile.id
}
defer {
if nextProfileId == profile.id {
nextProfileId = nil
@ -137,6 +135,8 @@ private extension TunnelToggleButton {
} else {
try await tunnel.connect(with: profile, processor: profileProcessor)
}
} catch is CancellationError {
//
} catch {
errorHandler.handle(
error,

View File

@ -182,19 +182,19 @@ private extension ProfileManager {
extension Tunnel {
static let shared = Tunnel(
strategy: NETunnelStrategy(repository: ProfileManager.neRepository)
strategy: ProfileManager.neStrategy
)
}
private extension ProfileManager {
static let localProfileRepository: ProfileRepository = {
NEProfileRepository(repository: neRepository) {
NEProfileRepository(repository: neStrategy) {
ProfileManager.sharedTitle($0)
}
}()
static let neRepository: NETunnelManagerRepository = {
NETunnelManagerRepository(
static let neStrategy: NETunnelStrategy = {
NETunnelStrategy(
bundleIdentifier: BundleConfiguration.mainString(for: .tunnelId),
coder: Registry.sharedProtocolCoder,
environment: .shared