Skip to content

Commit 39fdec4

Browse files
authored
Merge pull request #289 from InterceptSuite/dev
mac 4.1.3
2 parents cb897b0 + 69222dc commit 39fdec4

17 files changed

Lines changed: 309 additions & 35 deletions

‎MacOS/ProxyBridge/ProxyBridge.xcodeproj/project.pbxproj‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@
414414
"$(inherited)",
415415
"@executable_path/../Frameworks",
416416
);
417-
MARKETING_VERSION = 4.1.2;
417+
MARKETING_VERSION = 4.1.3;
418418
PRODUCT_BUNDLE_IDENTIFIER = com.interceptsuite.ProxyBridge;
419419
PRODUCT_NAME = "$(TARGET_NAME)";
420420
PROVISIONING_PROFILE_SPECIFIER = "";
@@ -460,7 +460,7 @@
460460
"$(inherited)",
461461
"@executable_path/../Frameworks",
462462
);
463-
MARKETING_VERSION = 4.1.2;
463+
MARKETING_VERSION = 4.1.3;
464464
"PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = "ProxyBridge Prod";
465465
REGISTER_APP_GROUPS = YES;
466466
STRING_CATALOG_GENERATE_SYMBOLS = YES;
@@ -478,7 +478,7 @@
478478
CODE_SIGN_ENTITLEMENTS = extension/extension.entitlements;
479479
CODE_SIGN_IDENTITY = "Developer ID Application";
480480
CODE_SIGN_STYLE = Automatic;
481-
CURRENT_PROJECT_VERSION = 8;
481+
CURRENT_PROJECT_VERSION = 10;
482482
DEVELOPMENT_TEAM = "";
483483
ENABLE_APP_SANDBOX = YES;
484484
ENABLE_HARDENED_RUNTIME = YES;
@@ -501,7 +501,7 @@
501501
"@executable_path/../Frameworks",
502502
"@executable_path/../../../../Frameworks",
503503
);
504-
MARKETING_VERSION = 4.1.2;
504+
MARKETING_VERSION = 4.1.3;
505505
PRODUCT_BUNDLE_IDENTIFIER = com.interceptsuite.ProxyBridge.extension;
506506
PRODUCT_NAME = "$(inherited)";
507507
REGISTER_APP_GROUPS = YES;
@@ -518,7 +518,7 @@
518518
isa = XCBuildConfiguration;
519519
baseConfigurationReference = 1B7672932F421ABA00B323D6 /* Signing-Config-ext.xcconfig */;
520520
buildSettings = {
521-
CURRENT_PROJECT_VERSION = 8;
521+
CURRENT_PROJECT_VERSION = 10;
522522
ENABLE_APP_SANDBOX = YES;
523523
ENABLE_HARDENED_RUNTIME = YES;
524524
ENABLE_INCOMING_NETWORK_CONNECTIONS = YES;
@@ -540,7 +540,7 @@
540540
"@executable_path/../Frameworks",
541541
"@executable_path/../../../../Frameworks",
542542
);
543-
MARKETING_VERSION = 4.1.2;
543+
MARKETING_VERSION = 4.1.3;
544544
"PROVISIONING_PROFILE_SPECIFIER[sdk=macosx*]" = "ProxyBridge Extension Prod";
545545
REGISTER_APP_GROUPS = YES;
546546
SKIP_INSTALL = YES;

‎MacOS/ProxyBridge/ProxyBridge/CLIProfile.swift‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct CLIProfile {
3535
var proxies: [CLIProxy]
3636
var rules: [[String: Any]] // as saved by the app: processNames, targetHosts, targetPorts, protocol, action, enabled
3737
var dnsEnabled: Bool = true // the profile's Enable DNS setting, on when the profile has none
38+
var defaultProxyId: String? // the proxy rules set to "DEFAULT" use, the first proxy when missing
3839
}
3940

4041
enum CLIProfileError: Error, Equatable {
@@ -63,7 +64,8 @@ enum CLIProfileLoader {
6364
let proxies = try decodeProxies(obj["proxyConfigs"] ?? [])
6465
let rules = obj["proxyRules"] as? [[String: Any]] ?? []
6566
return CLIProfile(name: (obj["name"] as? String) ?? "profile", proxies: proxies, rules: rules,
66-
dnsEnabled: obj["dnsEnabled"] as? Bool ?? true)
67+
dnsEnabled: obj["dnsEnabled"] as? Bool ?? true,
68+
defaultProxyId: obj["defaultProxyId"] as? String)
6769
}
6870

6971
// a profile saved in the app. the active profile lives in the working keys
@@ -78,9 +80,11 @@ enum CLIProfileLoader {
7880
let configsKey = name == active ? "proxyConfigs" : "profile.\(name).proxyConfigs"
7981
let rulesKey = name == active ? "proxyRules" : "profile.\(name).proxyRules"
8082
let dnsKey = name == active ? "dnsEnabled" : "profile.\(name).dnsEnabled"
83+
let defaultKey = name == active ? "defaultProxyId" : "profile.\(name).defaultProxyId"
8184
let proxies = try decodeProxies(read(configsKey) ?? [])
8285
let rules = read(rulesKey) as? [[String: Any]] ?? []
83-
return CLIProfile(name: name, proxies: proxies, rules: rules, dnsEnabled: read(dnsKey) as? Bool ?? true)
86+
return CLIProfile(name: name, proxies: proxies, rules: rules, dnsEnabled: read(dnsKey) as? Bool ?? true,
87+
defaultProxyId: read(defaultKey) as? String)
8488
}
8589

8690
static func savedNames(read: Reader) -> (names: [String], active: String?) {
@@ -117,6 +121,7 @@ enum CLIProfileLoader {
117121
var ids = Set<String>()
118122
for p in profile.proxies {
119123
if !ids.insert(p.id).inserted { out.append("two proxies have the same id '\(p.id)'") }
124+
if DefaultProxy.reservedActions.contains(p.id) { out.append("a proxy has the id '\(p.id)', which is a rule action and cannot be a proxy id") }
120125
if p.host.trimmingCharacters(in: .whitespaces).isEmpty { out.append("a \(p.type) proxy has no host") }
121126
if !(1...65535).contains(p.port) { out.append("proxy \(p.host) has the port \(p.port), it must be 1-65535") }
122127
let type = p.type.lowercased()
@@ -138,11 +143,18 @@ enum CLIProfileLoader {
138143
let ids = Set(profile.proxies.map { $0.id })
139144
for (i, rule) in profile.rules.enumerated() {
140145
let action = rule["action"] as? String ?? "DIRECT"
141-
if action != "DIRECT" && action != "BLOCK" && !ids.contains(action) {
146+
if action != "DIRECT" && action != "BLOCK" && action != DefaultProxy.action && !ids.contains(action) {
142147
out.append("rule \(i + 1) sends traffic to a proxy that is not in this profile, it will go direct")
143148
}
144149
}
145150
if profile.rules.isEmpty { out.append("the profile has no rules, nothing will be redirected") }
151+
if DefaultProxy.usesDefault(profile.rules) {
152+
if profile.proxies.isEmpty {
153+
out.append("rules use the default proxy, but the profile has no proxy, so they will go direct")
154+
} else if let id = profile.defaultProxyId, !ids.contains(id) {
155+
out.append("defaultProxyId '\(id)' is not a proxy in this profile, the first proxy is used as the default")
156+
}
157+
}
146158
if !profile.dnsEnabled {
147159
for (i, rule) in profile.rules.enumerated() where hasHostName(rule["targetHosts"] as? String ?? "") {
148160
out.append("rule \(i + 1) uses a host name, but DNS is off in this profile, so it will not match")

‎MacOS/ProxyBridge/ProxyBridge/CLIRunner.swift‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,14 @@ final class CLIRunner: NSObject, OSSystemExtensionRequestDelegate {
315315
// same messages, same order as the GUI: logging, proxies, then the rules in one step
316316
send(["action": "setTrafficLogging", "enabled": verbose >= 2]) { _ in
317317
self.send(["action": "setProxyConfigs", "configs": self.proxyMessages()]) { _ in
318-
RuleManager.applyRules(session: session, rules: self.profile.rules) { _, count in
318+
let proxyIds = self.profile.proxies.map { $0.id }
319+
if DefaultProxy.usesDefault(self.profile.rules) {
320+
let chosen = DefaultProxy.effectiveId(defaultId: self.profile.defaultProxyId, proxyIds: proxyIds)
321+
let label = self.profile.proxies.first { $0.id == chosen }?.label ?? "none, those rules go direct"
322+
CLI.say("Default proxy: \(CLIText.safe(label))")
323+
}
324+
let defaultAction = DefaultProxy.effectiveAction(defaultId: self.profile.defaultProxyId, proxyIds: proxyIds)
325+
RuleManager.applyRules(session: session, rules: self.profile.rules, defaultAction: defaultAction) { _, count in
319326
CLI.say("Profile \"\(CLIText.safe(self.profile.name))\": \(self.profile.proxies.count) proxies, \(count) rules applied")
320327
// the extension skips a rule it can't read, don't let that pass silently
321328
if count < self.profile.rules.count { CLI.warn("\(self.profile.rules.count - count) rule(s) were rejected by the extension (invalid fields)") }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Foundation
2+
3+
// The default proxy (issue #274). A rule whose action is "DEFAULT" follows whichever proxy is the
4+
// profile's default, so a whole rule set can move to another proxy by changing one setting.
5+
//
6+
// The extension never sees "DEFAULT". Just before the rules are sent, the app replaces it with the
7+
// real proxy id (or DIRECT when there is no proxy), and sends the rules again when the default changes.
8+
nonisolated enum DefaultProxy {
9+
// the action a rule has when it follows the default proxy
10+
static let action = "DEFAULT"
11+
12+
// actions that are never a proxy id, so a proxy cannot be given one of these as its id
13+
static let reservedActions: Set<String> = ["DEFAULT", "DIRECT", "BLOCK"]
14+
15+
// what "Default" means right now: the proxy marked as default when it still exists, otherwise the
16+
// first proxy, otherwise DIRECT (no proxy at all)
17+
static func effectiveAction(defaultId: String?, proxyIds: [String]) -> String {
18+
if let id = defaultId, proxyIds.contains(id) { return id }
19+
return proxyIds.first ?? "DIRECT"
20+
}
21+
22+
// the proxy id the default stands for, nil when there is no proxy
23+
static func effectiveId(defaultId: String?, proxyIds: [String]) -> String? {
24+
let a = effectiveAction(defaultId: defaultId, proxyIds: proxyIds)
25+
return proxyIds.contains(a) ? a : nil
26+
}
27+
28+
// the rules with every "DEFAULT" action replaced by `target`
29+
static func resolve(_ rules: [[String: Any]], to target: String) -> [[String: Any]] {
30+
rules.map { rule in
31+
guard (rule["action"] as? String) == action else { return rule }
32+
var copy = rule
33+
copy["action"] = target
34+
return copy
35+
}
36+
}
37+
38+
static func usesDefault(_ rules: [[String: Any]]) -> Bool {
39+
rules.contains { ($0["action"] as? String) == action }
40+
}
41+
}

‎MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,17 @@ struct ProxyRulesView: View {
283283
switch action {
284284
case "BLOCK": return .red
285285
case "DIRECT": return .blue
286+
case DefaultProxy.action: return viewModel.effectiveDefaultProxy == nil ? .blue : .green
286287
default: return .green // any proxy config UUID
287288
}
288289
}
289290

290291
private func actionDisplayName(_ action: String) -> String {
291292
switch action {
292293
case "DIRECT", "BLOCK": return action
294+
case DefaultProxy.action:
295+
if let proxy = viewModel.effectiveDefaultProxy { return "Default (\(proxy.displayName))" }
296+
return "Default (no proxy, goes DIRECT)"
293297
default:
294298
return viewModel.proxyConfigs.first(where: { $0.id == action })?.displayName ?? action
295299
}
@@ -412,8 +416,8 @@ struct RuleEditorView: View {
412416
_targetHosts = State(initialValue: existingRule?.targetHosts ?? "*")
413417
_targetPorts = State(initialValue: existingRule?.targetPorts ?? "*")
414418
_selectedProtocol = State(initialValue: existingRule?.ruleProtocol ?? "TCP")
415-
// default to first proxy config if there is one, otherwise direct
416-
let defaultAction = existingRule?.action ?? viewModel.proxyConfigs.first?.id ?? "DIRECT"
419+
// a new rule follows the default proxy when there is a proxy, otherwise it is direct
420+
let defaultAction = existingRule?.action ?? (viewModel.proxyConfigs.isEmpty ? "DIRECT" : DefaultProxy.action)
417421
_selectedAction = State(initialValue: defaultAction)
418422
}
419423

@@ -492,6 +496,8 @@ struct RuleEditorView: View {
492496
Text("DIRECT").tag("DIRECT")
493497
Text("BLOCK").tag("BLOCK")
494498
if !viewModel.proxyConfigs.isEmpty {
499+
Divider()
500+
Text("Default (\(viewModel.effectiveDefaultProxy?.displayName ?? "no proxy"))").tag(DefaultProxy.action)
495501
Divider()
496502
ForEach(viewModel.proxyConfigs) { config in
497503
Text(config.displayName).tag(config.id)
@@ -500,7 +506,8 @@ struct RuleEditorView: View {
500506
}
501507
.pickerStyle(.menu)
502508
if selectedProtocol == "UDP" || selectedProtocol == "BOTH" {
503-
let isHttp = viewModel.proxyConfigs.first(where: { $0.id == selectedAction })?.type.lowercased() == "http"
509+
let chosenId = selectedAction == DefaultProxy.action ? viewModel.effectiveDefaultProxy?.id : selectedAction
510+
let isHttp = viewModel.proxyConfigs.first(where: { $0.id == chosenId })?.type.lowercased() == "http"
504511
if isHttp {
505512
HStack(spacing: 4) {
506513
Image(systemName: "exclamationmark.triangle.fill").foregroundColor(.yellow)

0 commit comments

Comments
 (0)