Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic
C ccybersecurityhttp3httpproxylinuxlinux-proxymacosmacos-appmacos-proxyproxifierproxifier-alternativeproxyclienttcpthick-clientudpwindivertwindows
perf(macos): keep the DNS map in memory only
AnoF-Cyber0a38ab22c1bb
diff --git a/MacOS/ProxyBridge/extension/DNSMapStore.swift b/MacOS/ProxyBridge/extension/DNSMapStore.swiftindex e89b006..749c2f8 100644--- a/MacOS/ProxyBridge/extension/DNSMapStore.swift+++ b/MacOS/ProxyBridge/extension/DNSMapStore.swift@@ -1,58 +1,42 @@import Foundation-// Shared IP -> domain map, populated by the DNS proxy and read by the transparent-// proxy. The two providers can run in separate processes, so the map lives in a-// file in the app group container. Each process keeps an in-memory copy; the-// reader reloads only when the file's mtime changes.+// 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+// in-memory map is enough. It used to be mirrored to a json file in the app group+// container, which meant a full serialize and write on every new address, a stat+// on every connection, a re-parse on every change, and a list of the sites that+// were visited sitting on disk.final class DNSMapStore {static let shared = DNSMapStore()- private let appGroup = "group.com.interceptsuite.ProxyBridge"- private let fileURL: URL?private let lock = NSLock()- private var map: [String: Set<String>] = [:] // ip -> domains- private var order: [String] = [] // insertion order for eviction+ private var map: [String: [String]] = [:] // ip -> domains, newest last+ private var order: [String] = [] // ips in insertion order, for evictionprivate let maxEntries = 1024- private var lastMtime: Date?+ private let maxDomainsPerIP = 8 // a cdn address can serve many names- // the file is rewritten at most once per persistInterval no matter how many- // lookups land, and the reader only stats it once per reloadCheckInterval,- // domains(forIP:) runs for every new connection- private let persistInterval: TimeInterval = 1.0- private let reloadCheckInterval: TimeInterval = 1.0- private var persistScheduled = false- private var lastReloadCheck = Date.distantPast- private let ioQueue = DispatchQueue(label: "com.interceptsuite.ProxyBridge.dnsmap", qos: .utility)+ private init() {}- private init() {- fileURL = FileManager.default- .containerURL(forSecurityApplicationGroupIdentifier: appGroup)?- .appendingPathComponent("dnsmap.json")- }-- // wipe the map and its file. called on start so browsing domains don't- // linger on disk across sessions.+ // wipe the map. called on start/stop. it also removes the file older versions+ // wrote, so an upgrade doesn't leave that browsing history behind.func clear() {lock.lock()map.removeAll()order.removeAll()- lastMtime = nillock.unlock()- if let fileURL = fileURL { try? FileManager.default.removeItem(at: fileURL) }+ removeLegacyFile()}- // writer side (DNS proxy): remember that these IPs resolved for this domain+ // DNS proxy: remember that these IPs resolved for this domainfunc record(domain: String, ips: [String]) {let name = domain.lowercased()guard !name.isEmpty, !ips.isEmpty else { return }- var changed = falselock.lock()for ip in ips {if map[ip] == nil {map[ip] = []order.append(ip)- changed = trueif order.count > maxEntries {// drop a batch at once so the O(n) removal isn't paid per insertlet overflow = order.count - maxEntries + 64@@ -60,74 +44,31 @@ final class DNSMapStore {order.removeFirst(min(overflow, order.count))}}- if map[ip]?.insert(name).inserted == true { changed = true }- }- // only touch the file when something actually changed, most resolutions- // are repeats of already-known domains- let shouldSchedule = changed && !persistScheduled- if shouldSchedule { persistScheduled = true }- lock.unlock()-- if shouldSchedule {- ioQueue.asyncAfter(deadline: .now() + persistInterval) { [weak self] in- self?.persist()+ var domains = map[ip] ?? []+ if let existing = domains.firstIndex(of: name) {+ // seen again, make it the newest+ domains.remove(at: existing)+ } else if domains.count >= maxDomainsPerIP {+ domains.removeFirst()}+ domains.append(name)+ map[ip] = domains}+ lock.unlock()}- // reader side (transparent proxy): domains that resolved to this ip, if any+ // transparent proxy: domains that resolved to this ip, newest firstfunc domains(forIP ip: String) -> [String] {- reloadIfChanged()lock.lock()defer { lock.unlock() }- return Array(map[ip] ?? [])+ guard let domains = map[ip] else { return [] }+ return domains.reversed()}- private func persist() {- lock.lock()- persistScheduled = false- let snapshot = map- lock.unlock()-- guard let fileURL = fileURL else { return }- let plain = snapshot.mapValues { Array($0) }- guard let data = try? JSONSerialization.data(withJSONObject: plain) else { return }- try? data.write(to: fileURL, options: .atomic)-- // remember our own write so the reader doesn't parse back what it already has- let mtime = (try? FileManager.default.attributesOfItem(atPath: fileURL.path)[.modificationDate]) as? Date- lock.lock()- lastMtime = mtime- lock.unlock()- }-- private func reloadIfChanged() {- guard let fileURL = fileURL else { return }-- lock.lock()- let now = Date()- if now.timeIntervalSince(lastReloadCheck) < reloadCheckInterval {- lock.unlock()- return- }- lastReloadCheck = now- lock.unlock()-- let mtime = (try? FileManager.default.attributesOfItem(atPath: fileURL.path)[.modificationDate]) as? Date- lock.lock()- let needsReload = mtime != nil && mtime != lastMtime- if needsReload { lastMtime = mtime }- lock.unlock()-- guard needsReload,- let data = try? Data(contentsOf: fileURL),- let plain = try? JSONSerialization.jsonObject(with: data) as? [String: [String]] else {- return- }- lock.lock()- map = plain.mapValues { Set($0) }- // keep eviction order in step with the loaded map- order = Array(plain.keys)- lock.unlock()+ private func removeLegacyFile() {+ guard let url = FileManager.default+ .containerURL(forSecurityApplicationGroupIdentifier: "group.com.interceptsuite.ProxyBridge")?+ .appendingPathComponent("dnsmap.json") else { return }+ try? FileManager.default.removeItem(at: url)}}