Skip to content
Merged

Dev #300

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ Loading eBPF programs needs root, but the window does not. ProxyBridge is split
ProxyBridgeGUI # as yourself; no sudo
```

<p align="center">
<img src="../img/Linux/Connections.png" alt="ProxyBridge Linux main window" width="800"/>
<br/>
<em>Main window: connection log</em>
</p>

<p align="center">
<img src="../img/Linux/Proxy-Server.png" alt="Proxy settings" width="800"/>
<br/>
<em>Proxy settings and the five-step proxy test</em>
</p>

<p align="center">
<img src="../img/Linux/Proxy-Rules.png" alt="Proxy rules" width="800"/>
<br/>
<em>Proxy rules</em>
</p>

<p align="center">
<img src="../img/Linux/Rule-Editor.png" alt="Rule editor" width="500"/>
<br/>
<em>Add or edit a rule: IP networks and domain names in one list</em>
</p>

<p align="center">
<img src="../img/Linux/Live-Connection.png" alt="Live connections" width="800"/>
<br/>
<em>Live Connections: proxied connections and UDP flows right now</em>
</p>

The GTK3-based GUI provides:
- Proxy configuration (SOCKS5/HTTP with authentication)
- Visual rule management with process selection
Expand All @@ -112,6 +142,12 @@ The GTK3-based GUI provides:

### Command Line Interface (CLI)

<p align="center">
<img src="../img/Linux/Proxy-CLI.png" alt="ProxyBridge Linux CLI" width="700"/>
<br/>
<em>The CLI running a profile</em>
</p>

The CLI takes the same options as the Windows CLI. Everything else (proxies, rules, DNS via proxy, connection-log setting) lives in a **profile**, which you build in the GUI (or write by hand) and run with `--profile`.

```
Expand Down
4 changes: 2 additions & 2 deletions MacOS/ProxyBridge/ProxyBridge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@
CODE_SIGN_ENTITLEMENTS = extension/extension.entitlements;
CODE_SIGN_IDENTITY = "Developer ID Application";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 10;
CURRENT_PROJECT_VERSION = 11;
DEVELOPMENT_TEAM = "";
ENABLE_APP_SANDBOX = YES;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -518,7 +518,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 1B7672932F421ABA00B323D6 /* Signing-Config-ext.xcconfig */;
buildSettings = {
CURRENT_PROJECT_VERSION = 10;
CURRENT_PROJECT_VERSION = 11;
ENABLE_APP_SANDBOX = YES;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_INCOMING_NETWORK_CONNECTIONS = YES;
Expand Down
28 changes: 23 additions & 5 deletions MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct ProxyRule: Identifiable, Codable {
let ruleProtocol: String
let action: String
var enabled: Bool
// opt in to rules matching connections to 127.0.0.0/8 and ::1 (off by default)
var includeLocalhost: Bool = false

// localId is the stable gui side identity, ruleId in the extension is not stable
var id: String { localId }
Expand All @@ -25,6 +27,7 @@ struct ProxyRule: Identifiable, Codable {
case ruleProtocol = "protocol"
case action
case enabled
case includeLocalhost
}

init(from decoder: Decoder) throws {
Expand All @@ -37,6 +40,7 @@ struct ProxyRule: Identifiable, Codable {
self.ruleProtocol = try c.decode(String.self, forKey: .ruleProtocol)
self.action = try c.decode(String.self, forKey: .action)
self.enabled = try c.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
self.includeLocalhost = try c.decodeIfPresent(Bool.self, forKey: .includeLocalhost) ?? false
}

func encode(to encoder: Encoder) throws {
Expand All @@ -49,9 +53,10 @@ struct ProxyRule: Identifiable, Codable {
try c.encode(ruleProtocol, forKey: .ruleProtocol)
try c.encode(action, forKey: .action)
try c.encode(enabled, forKey: .enabled)
try c.encode(includeLocalhost, forKey: .includeLocalhost)
}

init(localId: String = UUID().uuidString, name: String = "", processNames: String, targetHosts: String, targetPorts: String, ruleProtocol: String, action: String, enabled: Bool) {
init(localId: String = UUID().uuidString, name: String = "", processNames: String, targetHosts: String, targetPorts: String, ruleProtocol: String, action: String, enabled: Bool, includeLocalhost: Bool = false) {
self.localId = localId
self.name = name
self.processNames = processNames
Expand All @@ -60,6 +65,7 @@ struct ProxyRule: Identifiable, Codable {
self.ruleProtocol = ruleProtocol
self.action = action
self.enabled = enabled
self.includeLocalhost = includeLocalhost
}

// UserDefaults stores rules as plain dictionaries, these bridge to that
Expand All @@ -72,6 +78,7 @@ struct ProxyRule: Identifiable, Codable {
self.ruleProtocol = dict["protocol"] as? String ?? "BOTH"
self.action = dict["action"] as? String ?? "DIRECT"
self.enabled = dict["enabled"] as? Bool ?? true
self.includeLocalhost = dict["includeLocalhost"] as? Bool ?? false
}

func toDict() -> [String: Any] {
Expand All @@ -83,7 +90,8 @@ struct ProxyRule: Identifiable, Codable {
"targetPorts": targetPorts,
"protocol": ruleProtocol,
"action": action,
"enabled": enabled
"enabled": enabled,
"includeLocalhost": includeLocalhost
]
}
}
Expand Down Expand Up @@ -227,7 +235,7 @@ struct ProxyRulesView: View {
.width(min: 100, ideal: 140)

TableColumn("Target Hosts") { rule in
Text(rule.targetHosts.isEmpty ? "Any" : rule.targetHosts)
Text((rule.targetHosts.isEmpty ? "Any" : rule.targetHosts) + (rule.includeLocalhost ? " + localhost" : ""))
}
.width(min: 100, ideal: 150)

Expand Down Expand Up @@ -403,6 +411,7 @@ struct RuleEditorView: View {
@State private var targetPorts: String
@State private var selectedProtocol: String
@State private var selectedAction: String
@State private var includeLocalhost: Bool

private var isEditMode: Bool { existingRule != nil }

Expand All @@ -419,6 +428,7 @@ struct RuleEditorView: View {
// a new rule follows the default proxy when there is a proxy, otherwise it is direct
let defaultAction = existingRule?.action ?? (viewModel.proxyConfigs.isEmpty ? "DIRECT" : DefaultProxy.action)
_selectedAction = State(initialValue: defaultAction)
_includeLocalhost = State(initialValue: existingRule?.includeLocalhost ?? false)
}

var body: some View {
Expand Down Expand Up @@ -465,6 +475,13 @@ struct RuleEditorView: View {
hint: "Example: 80; 8000-9000; 3128"
)

VStack(alignment: .leading, spacing: 4) {
Toggle("Include localhost traffic (127.0.0.1, ::1)", isOn: $includeLocalhost)
.fontWeight(.medium)
Text("Off: connections to this Mac itself are never touched by this rule. On: they can match it too, so a browser can be sent to a local server through the proxy. TCP only. Do not use it with * on a proxy that runs on this Mac, that proxy's own traffic would loop.")
.font(.caption).foregroundColor(.secondary)
}

HStack(spacing: 4) {
Image(systemName: "info.circle").foregroundColor(.secondary)
Text("Target hosts and ports only apply to TCP. UDP rules match on the app (bundle id) only.")
Expand Down Expand Up @@ -545,7 +562,7 @@ struct RuleEditorView: View {
.padding(.horizontal)
}
.padding()
.frame(width: 600, height: 620)
.frame(width: 600, height: 700)
}

@ViewBuilder
Expand All @@ -570,7 +587,8 @@ struct RuleEditorView: View {
targetPorts: targetPorts,
ruleProtocol: selectedProtocol,
action: selectedAction,
enabled: existingRule?.enabled ?? true
enabled: existingRule?.enabled ?? true,
includeLocalhost: includeLocalhost
)
onCommit(rule)
dismiss()
Expand Down
10 changes: 7 additions & 3 deletions MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct RuleManager {
protocol: String,
action: String,
enabled: Bool = true,
includeLocalhost: Bool = false,
completion: @escaping (Bool, String, UInt32?) -> Void
) {
sendMessage(
Expand All @@ -22,7 +23,8 @@ struct RuleManager {
"targetPorts": targetPorts,
"ruleProtocol": `protocol`,
"ruleAction": action,
"enabled": enabled
"enabled": enabled,
"includeLocalhost": includeLocalhost
]
) { success, result in
if success, let result = result, result["status"] as? String == "ok" {
Expand Down Expand Up @@ -124,7 +126,8 @@ struct RuleManager {
"targetPorts": rule["targetPorts"] as? String ?? "",
"ruleProtocol": rule["protocol"] as? String ?? "BOTH",
"ruleAction": rule["action"] as? String ?? "DIRECT",
"enabled": rule["enabled"] as? Bool ?? true
"enabled": rule["enabled"] as? Bool ?? true,
"includeLocalhost": rule["includeLocalhost"] as? Bool ?? false
]
}

Expand Down Expand Up @@ -165,7 +168,8 @@ struct RuleManager {
targetPorts: rule["targetPorts"] as? String ?? "",
protocol: rule["protocol"] as? String ?? "BOTH",
action: rule["action"] as? String ?? "DIRECT",
enabled: rule["enabled"] as? Bool ?? true
enabled: rule["enabled"] as? Bool ?? true,
includeLocalhost: rule["includeLocalhost"] as? Bool ?? false
) { success, _, _ in
addRulesInOrder(
session: session,
Expand Down
54 changes: 50 additions & 4 deletions MacOS/ProxyBridge/extension/AppProxyProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct ProxyRule: Codable {
let ruleProtocol: RuleProtocol
let action: String // "DIRECT", "BLOCK", or a proxy config UUID
var enabled: Bool
// a connection to 127.0.0.0/8 or ::1 only matches rules that opt in. default off, so a `*` rule
// never starts catching a local proxy tool or a local service by surprise
var includeLocalhost: Bool = false

enum CodingKeys: String, CodingKey {
case ruleId
Expand All @@ -24,6 +27,7 @@ struct ProxyRule: Codable {
case ruleProtocol
case action = "ruleAction"
case enabled
case includeLocalhost
}

init(from decoder: Decoder) throws {
Expand All @@ -35,18 +39,31 @@ struct ProxyRule: Codable {
self.ruleProtocol = try container.decode(RuleProtocol.self, forKey: .ruleProtocol)
self.action = try container.decode(String.self, forKey: .action)
self.enabled = try container.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
self.includeLocalhost = try container.decodeIfPresent(Bool.self, forKey: .includeLocalhost) ?? false
}

init(ruleId: UInt32, processNames: String, targetHosts: String, targetPorts: String, ruleProtocol: RuleProtocol, action: String, enabled: Bool) {
init(ruleId: UInt32, processNames: String, targetHosts: String, targetPorts: String, ruleProtocol: RuleProtocol, action: String, enabled: Bool, includeLocalhost: Bool = false) {
self.ruleId = ruleId
self.processNames = processNames
self.targetHosts = targetHosts
self.targetPorts = targetPorts
self.ruleProtocol = ruleProtocol
self.action = action
self.enabled = enabled
self.includeLocalhost = includeLocalhost
}


// 127.0.0.0/8, ::1 and the v4-mapped form of 127.x
static func isLoopback(_ ip: String) -> Bool {
guard let bytes = ipToBytes(ip) else { return false }
if bytes.count == 4 { return bytes[0] == 127 }
if bytes.count == 16 {
if bytes[0..<15].allSatisfy({ $0 == 0 }) && bytes[15] == 1 { return true }
if bytes[0..<10].allSatisfy({ $0 == 0 }) && bytes[10] == 0xff && bytes[11] == 0xff && bytes[12] == 127 { return true }
}
return false
}

func matchesProcess(bundleId: String, processName: String?) -> Bool {
if Self.matchProcessList(processNames, processPath: bundleId) {
return true
Expand Down Expand Up @@ -374,8 +391,10 @@ extension ProxyRule {

func matchesHost(ip: String, domains: [(name: String, chars: [Character])]) -> Bool {
if hostAll { return true }
let loopback = ProxyRule.isLoopback(ip)
for p in hostPatterns {
if ProxyRule.matchIPPattern(p.raw, ipString: ip) { return true }
if loopback && p.glob == Array("localhost") { return true }
for d in domains {
if ProxyRule.globMatch(p.glob, d.chars) { return true }
if let apex = p.apex, d.name == apex { return true }
Expand Down Expand Up @@ -544,7 +563,28 @@ class AppProxyProvider: NETransparentProxyProvider {
direction: .outbound
)

settings.includedNetworkRules = [allTrafficRule]
// a rule with no address matches everything except loopback, so the loopback networks are named
// explicitly. TCP only: a udp flow has no destination when it arrives, so it could not be told
// apart from other udp. A loopback connection only matches a rule that has "include localhost"
// ticked, anything else goes direct and shows up in the connection log.
let loopbackV4 = NENetworkRule(
remoteNetwork: NWHostEndpoint(hostname: "127.0.0.0", port: "0"),
remotePrefix: 8,
localNetwork: nil,
localPrefix: 0,
protocol: .TCP,
direction: .outbound
)
let loopbackV6 = NENetworkRule(
remoteNetwork: NWHostEndpoint(hostname: "::1", port: "0"),
remotePrefix: 128,
localNetwork: nil,
localPrefix: 0,
protocol: .TCP,
direction: .outbound
)

settings.includedNetworkRules = [allTrafficRule, loopbackV4, loopbackV6]

startUDPSweepTimer()
diag("Tunnel starting")
Expand Down Expand Up @@ -766,7 +806,8 @@ class AppProxyProvider: NETransparentProxyProvider {
"targetPorts": rule.targetPorts,
"protocol": rule.ruleProtocol.rawValue,
"action": rule.action,
"enabled": rule.enabled
"enabled": rule.enabled,
"includeLocalhost": rule.includeLocalhost
]
completionHandler?(try? JSONSerialization.data(withJSONObject: response))
} else {
Expand Down Expand Up @@ -1790,10 +1831,15 @@ class AppProxyProvider: NETransparentProxyProvider {
let domainChars = domains.map { (name: $0, chars: Array($0)) }

var wildcardRule: ProxyRule? = nil
let isLoopbackDestination = checkIpPort && ProxyRule.isLoopback(destination)

for (rule, m) in currentRules {
guard rule.enabled else { continue }

// a loopback destination only matches rules that opt in (udp has no destination here and
// loopback udp is never delivered, see startProxy)
if checkIpPort && !rule.includeLocalhost && isLoopbackDestination { continue }

if rule.ruleProtocol != .both && rule.ruleProtocol != connectionProtocol {
continue
}
Expand Down
20 changes: 20 additions & 0 deletions MacOS/tests/logic/matcher_tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,25 @@ t("list of ip and domain", matcher("*", "1.2.3.4;*.github.com", "*").matchesHost
t("domain rule without any known name", matcher("*", "*.github.com", "*").matchesHost(ip: "9.9.9.9", domains: []), false)
t("all hosts", matcher("*", "*", "*").matchesHost(ip: "9.9.9.9", domains: []), true)

// localhost: loopback detection, the "localhost" host name, and the rule's opt-in flag
t("loopback v4", ProxyRule.isLoopback("127.0.0.1"), true)
t("loopback v4 other address", ProxyRule.isLoopback("127.5.6.7"), true)
t("loopback v6", ProxyRule.isLoopback("::1"), true)
t("loopback v6 written long", ProxyRule.isLoopback("0:0:0:0:0:0:0:1"), true)
t("loopback v4-mapped", ProxyRule.isLoopback("::ffff:127.0.0.1"), true)
t("lan is not loopback", ProxyRule.isLoopback("192.168.1.5"), false)
t("128.x is not loopback", ProxyRule.isLoopback("128.0.0.1"), false)
t("public v6 is not loopback", ProxyRule.isLoopback("2001:db8::1"), false)
t("a name is not loopback", ProxyRule.isLoopback("localhost"), false)
t("localhost host name matches loopback", matcher("*", "localhost", "*").matchesHost(ip: "127.0.0.1", domains: []), true)
t("localhost host name matches ::1", matcher("*", "localhost", "*").matchesHost(ip: "::1", domains: []), true)
t("localhost host name does not match a lan ip", matcher("*", "localhost", "*").matchesHost(ip: "192.168.1.5", domains: []), false)
t("loopback ip pattern", matcher("*", "127.0.0.1", "*").matchesHost(ip: "127.0.0.1", domains: []), true)
t("opt in is off by default", ProxyRule(ruleId: 1, processNames: "*", targetHosts: "*", targetPorts: "*", ruleProtocol: .tcp, action: "DIRECT", enabled: true).includeLocalhost, false)
let optIn = try! JSONDecoder().decode(ProxyRule.self, from: Data(#"{"processNames":"*","targetHosts":"*","targetPorts":"*","ruleProtocol":"TCP","ruleAction":"DIRECT","includeLocalhost":true}"#.utf8))
let optOut = try! JSONDecoder().decode(ProxyRule.self, from: Data(#"{"processNames":"*","targetHosts":"*","targetPorts":"*","ruleProtocol":"TCP","ruleAction":"DIRECT"}"#.utf8))
t("opt in is read from the rule", optIn.includeLocalhost, true)
t("a rule without the key keeps it off", optOut.includeLocalhost, false)

print(fails == 0 ? "matcher tests: ALL PASS" : "matcher tests: \(fails) FAILED")
exit(fails == 0 ? 0 : 1)
Loading
Loading