import Foundation /// For the gate only: a connection's ICE made to fail, as a path that /// stops carrying anything does (2026-10-21's incident, a peer whose /// connection never came up). /// /// While the block lasts every ICE candidate this app sends or receives — /// trickled, or written into a description — has its port replaced by 9, /// the discard port, which nothing on the gate's Mac listens on. Both ends /// then hold only candidates that lead nowhere, neither receives a check /// from the other (so neither can learn a peer-reflexive address), and ICE /// runs to `failed`. When the block lifts nothing is undone: the candidates /// already exchanged stay wrong, and only an ICE restart (new candidates) /// or a new connection gets the pair talking. That is the incident's shape. /// /// Inert unless the interop harness sets `until`; the apps never do. enum ICETestBlock { /// Until when (system uptime) candidates are mangled; 0 is never. @MainActor static var until: Double = 0 /// Candidates mangled so far, for the gate. @MainActor static var mangled: UInt64 = 0 @MainActor static var active: Bool { until > 0 && ProcessInfo.processInfo.systemUptime < until } /// One candidate line, `candidate:…` or `a=candidate:…`, with its port /// replaced. Anything that is not a candidate line comes back as it was. static func mangle(candidate line: String) -> String { var fields = line.split(separator: " ", omittingEmptySubsequences: false).map(String.init) guard fields.count > 7, fields[0].hasPrefix("candidate:") || fields[0].hasPrefix("a=candidate:"), fields[6] == "typ" else { return line } fields[5] = "9" return fields.joined(separator: " ") } /// A description with every candidate line in it mangled. static func mangle(sdp: String) -> String { let lines = sdp.components(separatedBy: "\r\n") return lines.map { $0.hasPrefix("a=candidate:") ? mangle(candidate: $0) : $0 }.joined(separator: "\r\n") } }