@@ -12,6 +12,9 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
1212 // the system DNS proxy lets the extension learn which names belong to which addresses, which
1313 // host name rules and "send hostname" need. per profile, on by default
1414 @ Published var isDNSEnabled = true
15+ // the proxy rules set to "Default" use (issue #274). per profile. nil, or an id that no longer exists,
16+ // means the first proxy. see DefaultProxy
17+ @ Published private( set) var defaultProxyId : String ?
1518 // set once the extension is installed, the DNS entry is only touched after that
1619 private var extensionReady = false
1720 // the tunnel start is watched: macOS can stop it again at once without saying why (#282)
@@ -110,6 +113,7 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
110113 activityLogs . reserveCapacity ( maxLogEntries )
111114 loadTrafficLoggingSetting ( )
112115 loadDNSSetting ( )
116+ loadDefaultProxySetting ( )
113117 loadProfiles ( )
114118 loadProxyConfig ( )
115119 NotificationCenter . default . addObserver (
@@ -198,6 +202,7 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
198202 private func profileCloseKey ( _ name : String ) -> String { " profile. \( name ) .closeToMenuBar " }
199203 private func profileStartupKey ( _ name : String ) -> String { " profile. \( name ) .runAtStartup " }
200204 private func profileDNSKey ( _ name : String ) -> String { " profile. \( name ) .dnsEnabled " }
205+ private func profileDefaultProxyKey ( _ name : String ) -> String { " profile. \( name ) .defaultProxyId " }
201206
202207 private func loadProfiles ( ) {
203208 let d = UserDefaults . standard
@@ -222,6 +227,7 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
222227 d . set ( d . bool ( forKey : " closeToMenuBar " ) , forKey : profileCloseKey ( name ) )
223228 d . set ( d . bool ( forKey : " runAtStartup " ) , forKey : profileStartupKey ( name ) )
224229 d . set ( isDNSEnabled , forKey : profileDNSKey ( name ) )
230+ if let id = defaultProxyId { d . set ( id , forKey : profileDefaultProxyKey ( name ) ) } else { d . removeObject ( forKey : profileDefaultProxyKey ( name ) ) }
225231 }
226232
227233 // load a profile's snapshot into the live working keys
@@ -240,13 +246,15 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
240246 d . set ( d . object ( forKey : profileStartupKey ( name ) ) as? Bool ?? false , forKey : " runAtStartup " )
241247 // DNS is on by default, also for profiles saved before the setting existed
242248 d . set ( d . object ( forKey : profileDNSKey ( name ) ) as? Bool ?? true , forKey : " dnsEnabled " )
249+ if let id = d . string ( forKey : profileDefaultProxyKey ( name ) ) { d . set ( id , forKey : " defaultProxyId " ) } else { d . removeObject ( forKey : " defaultProxyId " ) }
243250 }
244251
245252 // reload in-memory state from the working keys and push it to the extension
246253 private func applyActiveProfile ( ) {
247254 loadProxyConfig ( )
248255 loadTrafficLoggingSetting ( )
249256 loadDNSSetting ( )
257+ loadDefaultProxySetting ( )
250258 // window-close behaviour is read live from the key, only startup needs applying
251259 LoginItem . applyToSystem ( UserDefaults . standard . bool ( forKey : " runAtStartup " ) )
252260 if let session = tunnelSession {
@@ -290,12 +298,14 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
290298 d . set ( d . object ( forKey : profileCloseKey ( old ) ) , forKey : profileCloseKey ( new ) )
291299 d . set ( d . object ( forKey : profileStartupKey ( old ) ) , forKey : profileStartupKey ( new ) )
292300 d . set ( d . object ( forKey : profileDNSKey ( old ) ) , forKey : profileDNSKey ( new ) )
301+ d . set ( d . object ( forKey : profileDefaultProxyKey ( old ) ) , forKey : profileDefaultProxyKey ( new ) )
293302 d . removeObject ( forKey : profileConfigsKey ( old ) )
294303 d . removeObject ( forKey : profileRulesKey ( old ) )
295304 d . removeObject ( forKey : profileLoggingKey ( old ) )
296305 d . removeObject ( forKey : profileCloseKey ( old ) )
297306 d . removeObject ( forKey : profileStartupKey ( old ) )
298307 d . removeObject ( forKey : profileDNSKey ( old ) )
308+ d . removeObject ( forKey : profileDefaultProxyKey ( old ) )
299309 if let i = profiles . firstIndex ( of : old ) { profiles [ i ] = new }
300310 d . set ( profiles , forKey : " profiles " )
301311 if activeProfile == old {
@@ -317,6 +327,7 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
317327 d . removeObject ( forKey : profileCloseKey ( name ) )
318328 d . removeObject ( forKey : profileStartupKey ( name ) )
319329 d . removeObject ( forKey : profileDNSKey ( name ) )
330+ d . removeObject ( forKey : profileDefaultProxyKey ( name ) )
320331 profiles . removeAll { $0 == name }
321332 d . set ( profiles , forKey : " profiles " )
322333 }
@@ -335,6 +346,7 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
335346 " closeToMenuBar " : d . object ( forKey : profileCloseKey ( name ) ) as? Bool ?? false ,
336347 " runAtStartup " : d . object ( forKey : profileStartupKey ( name ) ) as? Bool ?? false ,
337348 " dnsEnabled " : d . object ( forKey : profileDNSKey ( name ) ) as? Bool ?? true ,
349+ " defaultProxyId " : d . string ( forKey : profileDefaultProxyKey ( name ) ) ?? NSNull ( ) ,
338350 " proxyConfigs " : configsJSON ,
339351 " proxyRules " : d . array ( forKey : profileRulesKey ( name ) ) ?? [ ]
340352 ]
@@ -363,6 +375,7 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
363375 d . set ( obj [ " closeToMenuBar " ] as? Bool ?? false , forKey : profileCloseKey ( name ) )
364376 d . set ( obj [ " runAtStartup " ] as? Bool ?? false , forKey : profileStartupKey ( name ) )
365377 d . set ( obj [ " dnsEnabled " ] as? Bool ?? true , forKey : profileDNSKey ( name ) )
378+ if let id = obj [ " defaultProxyId " ] as? String , ! id . isEmpty { d . set ( id , forKey : profileDefaultProxyKey ( name ) ) } else { d . removeObject ( forKey : profileDefaultProxyKey ( name ) ) }
366379
367380 profiles . append ( name )
368381 d . set ( profiles , forKey : " profiles " )
@@ -388,6 +401,32 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
388401 saveProxyConfigs ( )
389402 if let session = tunnelSession {
390403 sendProxyConfigsToExtension ( session : session )
404+ // the first proxy becomes the default, rules set to Default follow it
405+ RuleManager . resyncRules ( session : session ) { _ , _ in }
406+ }
407+ }
408+
409+ // MARK: - Default proxy
410+
411+ private func loadDefaultProxySetting ( ) {
412+ defaultProxyId = UserDefaults . standard . string ( forKey : " defaultProxyId " )
413+ }
414+
415+ // the proxy rules set to "Default" use right now, nil when there is no proxy
416+ var effectiveDefaultProxy : ProxyConfig ? {
417+ guard let id = DefaultProxy . effectiveId ( defaultId : defaultProxyId , proxyIds : proxyConfigs . map { $0 . id } ) else { return nil }
418+ return proxyConfigs . first { $0 . id == id }
419+ }
420+
421+ func setDefaultProxy ( _ id : String ) {
422+ guard proxyConfigs . contains ( where : { $0 . id == id } ) else { return }
423+ // marking the proxy that is already the default (the first one) still saves the choice, so a
424+ // later change in the list order does not move it
425+ let changes = effectiveDefaultProxy ? . id != id
426+ defaultProxyId = id
427+ UserDefaults . standard . set ( id , forKey : " defaultProxyId " )
428+ if changes , let session = tunnelSession {
429+ RuleManager . resyncRules ( session : session ) { _ , _ in }
391430 }
392431 }
393432
@@ -423,8 +462,14 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
423462 }
424463 proxyConfigs . removeAll { $0 . id == config . id }
425464 saveProxyConfigs ( )
465+ if defaultProxyId == config . id {
466+ // the next proxy takes over as the default
467+ defaultProxyId = nil
468+ UserDefaults . standard . removeObject ( forKey : " defaultProxyId " )
469+ }
426470 if let session = tunnelSession {
427471 sendProxyConfigsToExtension ( session : session )
472+ RuleManager . resyncRules ( session : session ) { _ , _ in }
428473 }
429474 }
430475
0 commit comments