Skip to content

Commit 24dd67a

Browse files
APPLE: fix wrong connection mode on first launch (#3991)
1 parent 3a3dfbb commit 24dd67a

File tree

8 files changed

+81
-357
lines changed

8 files changed

+81
-357
lines changed

nym-vpn-apple/Services/Sources/Services/AppSettings/AppSettings.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ import CountriesManagerTypes
4343
@AppStorage(AppSettingKey.exitRouter.rawValue)
4444
public var exitRouter: String?
4545

46+
@AppStorage(AppSettingKey.connectionConfig.rawValue)
47+
public var connectionConfig: String?
48+
4649
@AppStorage(AppSettingKey.connectionType.rawValue)
4750
public var connectionType: Int?
4851

@@ -133,4 +136,5 @@ public enum AppSettingKey: String {
133136
case quic
134137
case shouldReconnect
135138
case passphraseStored
139+
case connectionConfig
136140
}

nym-vpn-apple/Services/Sources/Services/ConnectionManager/ConnectionManager+macOS.swift

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#if os(macOS)
22
import Foundation
3-
import TunnelMixnet
3+
import ConnectionTypes
44
import NotificationMessages
5+
import TunnelMixnet
56

67
extension ConnectionManager {
78
@MainActor func connect() async throws {
@@ -20,13 +21,8 @@ extension ConnectionManager {
2021
}
2122
}
2223

23-
@MainActor func fetchConnectionConfig() async {
24-
connectionConfig = await grpcManager.config()
25-
}
26-
2724
func updateConnectionConfig() {
2825
Task {
29-
guard let connectionConfig else { return }
3026
try? await grpcManager.updateConfig(newConfig: connectionConfig)
3127
}
3228
}
@@ -63,16 +59,6 @@ extension ConnectionManager {
6359
}
6460
.store(in: &cancellables)
6561

66-
grpcManager.$isServing
67-
.receive(on: DispatchQueue.main)
68-
.sink { [weak self] isServing in
69-
guard isServing else { return }
70-
Task { @MainActor in
71-
await self?.fetchConnectionConfig()
72-
}
73-
}
74-
.store(in: &cancellables)
75-
7662
grpcManager.$connectionInfoData
7763
.removeDuplicates()
7864
.receive(on: DispatchQueue.main)
@@ -82,7 +68,7 @@ extension ConnectionManager {
8268
appSettings.$isQuicEnabledPublisher
8369
.receive(on: DispatchQueue.main)
8470
.sink { [weak self] newValue in
85-
self?.connectionConfig?.enableBridges = newValue
71+
self?.connectionConfig.enableBridges = newValue
8672
guard let self, currentTunnelStatus == .connected, appSettings.shouldReconnect else { return }
8773
updateConnectionConfig()
8874
}

nym-vpn-apple/Services/Sources/Services/ConnectionManager/ConnectionManager.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ import GRPCManager
5252
)
5353
#endif
5454

55-
@Published public var connectionConfig: ConnectionConfig?
55+
@Published public var connectionConfig: ConnectionConfig {
56+
didSet {
57+
connectionStorage.connectionConfig = connectionConfig
58+
}
59+
}
5660
@Published public var connectedDate: Date?
5761
@Published public var connectionRetryAttempt: Int?
5862
@Published public var afterDisconnectAction: AfterDisconnectAction?
@@ -64,9 +68,9 @@ import GRPCManager
6468
didSet {
6569
switch connectionType {
6670
case .mixnet5hop:
67-
connectionConfig?.enableTwoHop = false
71+
connectionConfig.enableTwoHop = false
6872
case .wireguard:
69-
connectionConfig?.enableTwoHop = true
73+
connectionConfig.enableTwoHop = true
7074
}
7175
appSettings.connectionType = connectionType.rawValue
7276
updateConnectionConfig()
@@ -89,7 +93,7 @@ import GRPCManager
8993
@Published public var entryGateway: EntryGateway {
9094
didSet {
9195
Task { @MainActor in
92-
connectionConfig?.entry = entryGateway
96+
connectionConfig.entry = entryGateway
9397
connectionStorage.entryGateway = entryGateway
9498
updateConnectionConfig()
9599
}
@@ -98,7 +102,7 @@ import GRPCManager
98102
@Published public var exitRouter: ExitRouter {
99103
didSet {
100104
Task { @MainActor in
101-
connectionConfig?.exit = exitRouter
105+
connectionConfig.exit = exitRouter
102106
connectionStorage.exitRouter = exitRouter
103107
updateConnectionConfig()
104108
}
@@ -119,6 +123,7 @@ import GRPCManager
119123
self.entryGateway = connectionStorage.entryGateway
120124
self.exitRouter = connectionStorage.exitRouter
121125
self.connectionType = connectionStorage.connectionType
126+
self.connectionConfig = connectionStorage.connectionConfig
122127
setup()
123128
}
124129
#endif
@@ -139,6 +144,7 @@ import GRPCManager
139144
self.entryGateway = connectionStorage.entryGateway
140145
self.exitRouter = connectionStorage.exitRouter
141146
self.connectionType = connectionStorage.connectionType
147+
self.connectionConfig = connectionStorage.connectionConfig
142148
setup()
143149
}
144150
#endif
@@ -169,9 +175,6 @@ private extension ConnectionManager {
169175
setupAppSettingsObservers()
170176
setupConnectionChangeObserver()
171177
setupConnectionErrorObserver()
172-
Task { @MainActor in
173-
await fetchConnectionConfig()
174-
}
175178
}
176179
}
177180

@@ -210,7 +213,7 @@ private extension ConnectionManager {
210213
appSettings.$isQuicEnabledPublisher
211214
.removeDuplicates()
212215
.sink { [weak self] value in
213-
self?.connectionConfig?.enableBridges = value
216+
self?.connectionConfig.enableBridges = value
214217
}
215218
.store(in: &cancellables)
216219

nym-vpn-apple/Services/Sources/Services/ConnectionManager/ConnectionStorage.swift

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import GatewayManager
1414
private let appSettings: AppSettings
1515
private let configurationManager: ConfigurationManager
1616
private let gatewayManager: GatewayManager
17-
private let resolver = GatewaySelectionResolver()
1817

1918
private var entryGatewayType: NodeType { connectionType == .wireguard ? .vpn : .entry }
2019
private var exitGatewayType: NodeType { connectionType == .wireguard ? .vpn : .exit }
@@ -38,49 +37,17 @@ import GatewayManager
3837
set { appSettings.exitRouter = newValue.toJson() }
3938
}
4039

41-
var resolvedEntryGateway: EntryGateway {
42-
get async {
43-
let json = appSettings.entryGateway ?? ""
44-
let snapshotEntry = gatewayManager.entry
45-
let snapshotExit = gatewayManager.exit
46-
let snapshotVPN = gatewayManager.vpn
47-
let entryCountries = gatewayManager.entryCountries
48-
let exitCountries = gatewayManager.exitCountries
49-
let vpnCountries = gatewayManager.vpnCountries
50-
51-
return await resolver.resolveEntryGateway(
52-
jsonString: json,
53-
connectionType: connectionType,
54-
entry: snapshotEntry,
55-
exit: snapshotExit,
56-
vpn: snapshotVPN,
57-
entryCountries: entryCountries,
58-
exitCountries: exitCountries,
59-
vpnCountries: vpnCountries
60-
)
40+
var connectionConfig: ConnectionConfig {
41+
get {
42+
guard let storedConfig = appSettings.connectionConfig,
43+
let decodedConfig = ConnectionConfig.from(jsonString: storedConfig)
44+
else {
45+
return generateInitialConfig()
46+
}
47+
return decodedConfig
6148
}
62-
}
63-
64-
var resolvedExitRouter: ExitRouter {
65-
get async {
66-
let json = appSettings.exitRouter ?? ""
67-
let snapshotEntry = gatewayManager.entry
68-
let snapshotExit = gatewayManager.exit
69-
let snapshotVPN = gatewayManager.vpn
70-
let entryCountries = gatewayManager.entryCountries
71-
let exitCountries = gatewayManager.exitCountries
72-
let vpnCountries = gatewayManager.vpnCountries
73-
74-
return await resolver.resolveExitRouter(
75-
jsonString: json,
76-
connectionType: connectionType,
77-
entry: snapshotEntry,
78-
exit: snapshotExit,
79-
vpn: snapshotVPN,
80-
entryCountries: entryCountries,
81-
exitCountries: exitCountries,
82-
vpnCountries: vpnCountries
83-
)
49+
set {
50+
appSettings.connectionConfig = newValue.toJson()
8451
}
8552
}
8653

@@ -94,3 +61,20 @@ import GatewayManager
9461
self.gatewayManager = gatewayManager
9562
}
9663
}
64+
65+
private extension ConnectionStorage {
66+
func generateInitialConfig() -> ConnectionConfig {
67+
ConnectionConfig(
68+
entry: entryGateway,
69+
exit: exitRouter,
70+
allowLan: false,
71+
disableIpv6: !appSettings.isIPv6TrafficEnabled,
72+
enableTwoHop: connectionType == .wireguard,
73+
enableBridges: appSettings.isQuicEnabled,
74+
netstack: false,
75+
disablePoissonRate: false,
76+
disableBackgroundCoverTraffic: false,
77+
residentialExit: false
78+
)
79+
}
80+
}

0 commit comments

Comments
 (0)