Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

feat(macos): let each proxy resolve DNS (send hostname), like the windows app

AnoF-Cyber7143e7ba30db
diff --git a/MacOS/ProxyBridge/extension/AppProxyProvider.swift b/MacOS/ProxyBridge/extension/AppProxyProvider.swift
index 3ca50b6..4a82b04 100644
--- a/MacOS/ProxyBridge/extension/AppProxyProvider.swift
+++ b/MacOS/ProxyBridge/extension/AppProxyProvider.swift
@@ -482,6 +482,7 @@ class AppProxyProvider: NETransparentProxyProvider {
let port: Int
let username: String?
let password: String?
+ let sendDomain: Bool // hand the proxy the host name instead of the ip when it is known
}
private var storedProxyConfigs: [String: StoredProxyConfig] = [:]
private let proxyLock = NSLock()
@@ -710,7 +711,9 @@ class AppProxyProvider: NETransparentProxyProvider {
storedProxyConfigs[id] = StoredProxyConfig(
type: type, host: host, port: port,
username: configDict["proxyUsername"] as? String,
- password: configDict["proxyPassword"] as? String
+ password: configDict["proxyPassword"] as? String,
+ // an app that doesn't send the key keeps the old behavior (ip)
+ sendDomain: configDict["sendDomainToProxy"] as? Bool ?? false
)
}
proxyLock.unlock()
@@ -1419,7 +1422,19 @@ class AppProxyProvider: NETransparentProxyProvider {
return (Data(payload), destHost, destPort)
}
- private func proxyTCPFlow(_ flow: NEAppProxyTCPFlow, destination: String, port: UInt16, config: StoredProxyConfig) {
+ // how long a DNS answer stays good enough to name a destination for the proxy
+ private static let dnsNameMaxAge: TimeInterval = 300
+
+ private func proxyTCPFlow(_ flow: NEAppProxyTCPFlow, destination originalDestination: String, port: UInt16, config: StoredProxyConfig) {
+ // "let proxy resolve DNS", per proxy like the windows app: when the app
+ // resolved this address through our DNS proxy, ask the proxy for the name
+ // (socks5 domain address / CONNECT host:port) and fall back to the ip
+ var destination = originalDestination
+ if config.sendDomain,
+ IPv4Address(originalDestination) != nil || IPv6Address(originalDestination) != nil,
+ let name = DNSMapStore.shared.latestDomain(forIP: originalDestination, maxAge: AppProxyProvider.dnsNameMaxAge) {
+ destination = name
+ }
let proxyEndpoint = NWHostEndpoint(hostname: config.host, port: String(config.port))
let proxyConnection = createTCPConnection(to: proxyEndpoint, enableTLS: false, tlsParameters: nil, delegate: nil)
armHandshakeTimeout(flow: flow, connection: proxyConnection)
diff --git a/MacOS/ProxyBridge/extension/DNSMapStore.swift b/MacOS/ProxyBridge/extension/DNSMapStore.swift
index 749c2f8..008b70b 100644
--- a/MacOS/ProxyBridge/extension/DNSMapStore.swift
+++ b/MacOS/ProxyBridge/extension/DNSMapStore.swift
@@ -1,4 +1,5 @@
import Foundation
+import Network
// Shared IP -> domain map, filled by the DNS proxy and read by the transparent
// proxy. Both providers are loaded into the same extension process, so a plain
@@ -9,8 +10,13 @@ import Foundation
final class DNSMapStore {
static let shared = DNSMapStore()
+ private struct Entry {
+ let name: String
+ var seen: TimeInterval // system uptime when a DNS answer last mentioned it
+ }
+
private let lock = NSLock()
- private var map: [String: [String]] = [:] // ip -> domains, newest last
+ private var map: [String: [Entry]] = [:] // ip -> domains, newest last
private var order: [String] = [] // ips in insertion order, for eviction
private let maxEntries = 1024
private let maxDomainsPerIP = 8 // a cdn address can serve many names
@@ -28,7 +34,7 @@ final class DNSMapStore {
}
// DNS proxy: remember that these IPs resolved for this domain
- func record(domain: String, ips: [String]) {
+ func record(domain: String, ips: [String], now: TimeInterval = ProcessInfo.processInfo.systemUptime) {
let name = domain.lowercased()
guard !name.isEmpty, !ips.isEmpty else { return }
@@ -45,13 +51,13 @@ final class DNSMapStore {
}
}
var domains = map[ip] ?? []
- if let existing = domains.firstIndex(of: name) {
- // seen again, make it the newest
+ if let existing = domains.firstIndex(where: { $0.name == name }) {
+ // seen again, make it the newest and refresh its age
domains.remove(at: existing)
} else if domains.count >= maxDomainsPerIP {
domains.removeFirst()
}
- domains.append(name)
+ domains.append(Entry(name: name, seen: now))
map[ip] = domains
}
lock.unlock()
@@ -62,7 +68,34 @@ final class DNSMapStore {
lock.lock()
defer { lock.unlock() }
guard let domains = map[ip] else { return [] }
- return domains.reversed()
+ return domains.reversed().map { $0.name }
+ }
+
+ // the name a proxy should get instead of the ip ("let proxy resolve DNS"). the
+ // newest name for this address, only if a DNS answer mentioned it within maxAge
+ // seconds (the windows app uses the same 5 minutes), and only if it is a
+ // plain host name that is safe to put in a CONNECT line
+ func latestDomain(forIP ip: String, maxAge: TimeInterval, now: TimeInterval = ProcessInfo.processInfo.systemUptime) -> String? {
+ lock.lock()
+ defer { lock.unlock() }
+ guard let newest = map[ip]?.last, now - newest.seen <= maxAge,
+ DNSMapStore.isSafeHostname(newest.name) else { return nil }
+ return newest.name
+ }
+
+ // names come straight out of DNS packets, so they can hold any byte. only
+ // letters, digits, '.', '-' and '_' go to a proxy (a space or CRLF would
+ // inject into the http CONNECT request), and an ip literal isn't a name
+ static func isSafeHostname(_ name: String) -> Bool {
+ guard !name.isEmpty, name.utf8.count <= 253, !name.hasPrefix("."), !name.hasSuffix(".") else { return false }
+ for byte in name.utf8 {
+ switch byte {
+ case 0x30...0x39, 0x61...0x7A, 0x41...0x5A, 0x2E, 0x2D, 0x5F: continue
+ default: return false
+ }
+ }
+ if IPv4Address(name) != nil || IPv6Address(name) != nil { return false }
+ return true
}
private func removeLegacyFile() {