Files
bitchat/localPackages/Arti/Sources/TorManager.swift
T
jackandClaude Opus 5 862e44e2e1 Keep working when the network is hostile, and make the app verifiable
Tier 3 of a protest-hardening review. The BLE mesh is already properly
fenced off from network reachability and needs nothing here; every gap is
on the internet side, or in how someone gets a build they can trust.

Say when Tor is blocked. The bootstrap poll loop simply ended at its
75-second deadline, leaving isStarting true with no further state, so a
network that blocks Tor was indistinguishable from a slow one and the UI
said "starting tor…" indefinitely. TorManager now exposes
bootstrapDidStall and posts .TorBootstrapDidStall, and the app reports
that mesh messaging still works while internet delivery is paused. It is
cleared on each new start or restart, so a later attempt can report again.

Let relays be added by hand. The four built-in relays are well-known
clearnet hostnames, which is four names for a censor to block and no
recourse short of a new build. NostrRelaySettings persists up to eight
additional relays, normalized, .onion accepted, with a settings editor.
They join the same target set as the built-ins and are subject to the
same activation policy. Removal reconciles against the previous set:
the teardown path iterates the current targets, so without that a removed
relay's socket and queued sends would linger — covered by a test that
fails without it. The merged list is cached rather than recomputed,
because allowedRelayList consults it once per candidate URL and would
otherwise read UserDefaults inside that loop.

Stop stranding people who denied location. The activation gate required
location permission or a mutual favorite, but teleporting into a geohash
requires neither, so someone with no permission and no favorites could
sit in a channel that never connected while Tor and the relays stayed
suppressed and nothing said why. Being in a location channel is now a
third arm of the gate, in both the activation service and the relay
manager's copy of the policy, and leaving the channel closes it again.

Stop burning the Tor timeout when Tor is off. GeoRelayDirectory awaited
Tor readiness unconditionally, but with the preference off TorManager has
been shut down, so every refresh spent the full bootstrap deadline and the
directory froze on its cached copy. It now keys on the preference, not on
live readiness: Tor wanted but unavailable must still skip the fetch
rather than fall back to clearnet.

Say what turning Tor off costs. The toggle's copy described it as
hiding your IP "for location channels", understating both scope and
consequence. It now names private messages too, and while the toggle is
off the settings screen states that every relay can see the device IP.

Make builds verifiable. There was no release verification of any kind:
no signatures, no checksums, no documented procedure. Post-takedown that
is the acute gap, because mirrors appear and people install whatever they
can find during a shutdown. source-manifest.yml publishes a per-tag
SHA-256 manifest with a provenance attestation, self-checking before it
publishes, and docs/VERIFYING-A-BUILD.md explains how to verify source and
states plainly that compiled builds from anywhere but the App Store cannot
be verified. It also records the gaps honestly: no published signing key,
no reproducible build, no non-GitHub mirror.

docs/TOR-INTEGRATION.md was substantially stale — it documented a
torrc, SOCKSPort and ControlPort that the in-process Arti client does not
use, and claimed there are no user-visible settings — so it is rewritten,
including the deferred gap below.

Deferred: no Tor bridges or pluggable transports. arti-client is built
without pt-client or bridge-client and bootstraps from stock config, so
in a country that blocks Tor outright there is still no circumvention
path — only a clear report that there isn't. Closing it needs the Rust
features, bridge config through the FFI, and an xcframework rebuild under
the pinned toolchain with a provenance update, which is its own change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-26 18:10:50 +02:00

489 lines
18 KiB
Swift

import BitLogger
import Foundation
#if canImport(Network)
import Network
#endif
#if !canImport(Network)
private final class NWPathMonitor {
var pathUpdateHandler: ((Any) -> Void)?
func start(queue: DispatchQueue) {
// Path monitoring is unavailable on this platform; nothing to do.
}
}
#endif
// FFI declarations for Arti (Rust)
@_silgen_name("arti_start")
private func arti_start(_ dataDir: UnsafePointer<CChar>, _ socksPort: UInt16) -> Int32
@_silgen_name("arti_stop")
private func arti_stop() -> Int32
@_silgen_name("arti_is_running")
private func arti_is_running() -> Int32
@_silgen_name("arti_bootstrap_progress")
private func arti_bootstrap_progress() -> Int32
@_silgen_name("arti_bootstrap_summary")
private func arti_bootstrap_summary(_ buf: UnsafeMutablePointer<CChar>, _ len: Int32) -> Int32
/// Arti-based Tor integration for BitChat.
/// - Boots a local Arti client and exposes a SOCKS5 proxy
/// on 127.0.0.1:socksPort. All app networking should await readiness and
/// route via this proxy. Fails closed by default when Tor is unavailable.
@MainActor
public final class TorManager: ObservableObject {
public static let shared = TorManager()
// SOCKS endpoint where Arti listens
let socksHost: String = "127.0.0.1"
let socksPort: Int = 39050
// State
@Published private(set) public var isReady: Bool = false
@Published private(set) var isStarting: Bool = false
@Published private(set) var lastError: Error?
@Published private(set) var bootstrapProgress: Int = 0
@Published private(set) var bootstrapSummary: String = ""
/// True once a bootstrap attempt has spent its whole deadline without
/// completing.
///
/// This separates "still starting" from "not getting through", which are
/// indistinguishable from `isStarting` alone. The second is what a network
/// that blocks Tor looks like from inside the app, and without it the UI
/// says "starting tor…" indefinitely while nothing is happening. Cleared on
/// each new start attempt.
@Published private(set) public var bootstrapDidStall: Bool = false
// Internal readiness trackers
private var socksReady: Bool = false { didSet { recomputeReady() } }
private var restarting: Bool = false
// Whether the app must enforce Tor for all connections (fail-closed).
public var torEnforced: Bool {
#if BITCHAT_DEV_ALLOW_CLEARNET
return false
#else
return true
#endif
}
// Returns true only when Tor is actually up (or dev fallback is compiled).
var networkPermitted: Bool {
if torEnforced { return isReady }
return true
}
private var didStart = false
// shutdownCompletely() resets `didStart` asynchronously (after Arti has
// actually stopped). A startIfNeeded() arriving in that window must not be
// dropped — it is recorded here and honored when the shutdown finishes.
private var shutdownsInFlight = 0
private var startPendingAfterShutdown = false
private var bootstrapMonitorStarted = false
private var pathMonitor: NWPathMonitor?
private var isAppForeground: Bool = true
private var lastRestartAt: Date? = nil
private var startedAt: Date? = nil // Tracks initial startup time for grace period
private(set) var allowAutoStart: Bool = false
private init() {}
// MARK: - Public API
public func startIfNeeded() {
guard allowAutoStart else { return }
guard isAppForeground else { return }
if shutdownsInFlight > 0 {
SecureLogger.debug("TorManager: startIfNeeded() deferred - shutdown in flight", category: .session)
startPendingAfterShutdown = true
return
}
guard !didStart else { return }
didStart = true
isStarting = true
bootstrapDidStall = false
startedAt = Date() // Track startup time for grace period
SecureLogger.debug("TorManager: startIfNeeded() - startedAt set", category: .session)
lastError = nil
NotificationCenter.default.post(name: .TorWillStart, object: nil)
ensureFilesystemLayout()
startArti()
startPathMonitorIfNeeded()
}
public func setAppForeground(_ foreground: Bool) {
isAppForeground = foreground
}
public func isForeground() -> Bool { isAppForeground }
nonisolated
// Default matches the bootstrap monitor deadline (75s); a shorter wait here
// reports "not ready" while Arti is still legitimately bootstrapping.
public func awaitReady(timeout: TimeInterval = 75.0) async -> Bool {
await MainActor.run {
if self.isAppForeground { self.startIfNeeded() }
}
let deadline = Date().addingTimeInterval(timeout)
if await MainActor.run(body: { self.networkPermitted }) { return true }
while Date() < deadline {
try? await Task.sleep(nanoseconds: 200_000_000)
if await MainActor.run(body: { self.networkPermitted }) { return true }
}
return await MainActor.run(body: { self.networkPermitted })
}
// MARK: - Filesystem
func dataDirectoryURL() -> URL? {
do {
let base = try FileManager.default.url(
for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
let dir = base.appendingPathComponent("bitchat/arti", isDirectory: true)
return dir
} catch {
return nil
}
}
private func ensureFilesystemLayout() {
guard let dir = dataDirectoryURL() else { return }
do {
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
} catch {
// Non-fatal; Arti will surface errors during start if paths are missing
}
}
// MARK: - Arti Integration
private func startArti() {
guard let dir = dataDirectoryURL()?.path else {
isStarting = false
lastError = NSError(domain: "TorManager", code: -1, userInfo: [NSLocalizedDescriptionKey: "No data directory"])
return
}
// Check if already running
if arti_is_running() != 0 {
SecureLogger.info("TorManager: Arti already running", category: .session)
startBootstrapMonitor()
return
}
let result = dir.withCString { dptr in
arti_start(dptr, UInt16(socksPort))
}
if result != 0 {
SecureLogger.error("TorManager: arti_start failed rc=\(result)", category: .session)
isStarting = false
lastError = NSError(domain: "TorManager", code: Int(result), userInfo: [NSLocalizedDescriptionKey: "Arti start failed"])
return
}
SecureLogger.info("TorManager: arti_start OK (SOCKS \(socksHost):\(socksPort))", category: .session)
startBootstrapMonitor()
// Start SOCKS readiness probe
Task.detached(priority: .userInitiated) { [weak self] in
guard let self else { return }
let ready = await self.waitForSocksReady(timeout: 60.0)
await MainActor.run {
self.socksReady = ready
if ready {
SecureLogger.info("TorManager: SOCKS ready at \(self.socksHost):\(self.socksPort)", category: .session)
} else {
self.lastError = NSError(domain: "TorManager", code: -14, userInfo: [NSLocalizedDescriptionKey: "SOCKS not reachable"])
SecureLogger.error("TorManager: SOCKS not reachable (timeout)", category: .session)
}
}
}
}
private func waitForSocksReady(timeout: TimeInterval) async -> Bool {
let deadline = Date().addingTimeInterval(timeout)
while Date() < deadline {
if await probeSocksOnce() { return true }
try? await Task.sleep(nanoseconds: 250_000_000)
}
return false
}
private func probeSocksOnce() async -> Bool {
#if canImport(Network)
await withCheckedContinuation { cont in
let params = NWParameters.tcp
let host = NWEndpoint.Host.ipv4(.loopback)
guard let port = NWEndpoint.Port(rawValue: UInt16(socksPort)) else {
cont.resume(returning: false)
return
}
let endpoint = NWEndpoint.hostPort(host: host, port: port)
let conn = NWConnection(to: endpoint, using: params)
var resumed = false
let resumeOnce: (Bool) -> Void = { value in
if !resumed {
resumed = true
cont.resume(returning: value)
}
}
conn.stateUpdateHandler = { state in
switch state {
case .ready:
resumeOnce(true)
conn.cancel()
case .failed, .cancelled:
resumeOnce(false)
conn.cancel()
default:
break
}
}
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 1.0) {
resumeOnce(false)
conn.cancel()
}
conn.start(queue: DispatchQueue.global(qos: .utility))
}
#else
return false
#endif
}
// MARK: - Bootstrap Monitoring
private func startBootstrapMonitor() {
guard !bootstrapMonitorStarted else { return }
bootstrapMonitorStarted = true
Task.detached(priority: .utility) { [weak self] in
await self?.bootstrapPollLoop()
}
}
private func bootstrapPollLoop() async {
let deadline = Date().addingTimeInterval(75)
var didComplete = false
while Date() < deadline {
let progress = Int(arti_bootstrap_progress())
let summary = getBootstrapSummary()
await MainActor.run {
self.bootstrapProgress = progress
self.bootstrapSummary = summary
if progress >= 100 { self.isStarting = false }
self.recomputeReady()
}
if progress >= 100 {
didComplete = true
break
}
try? await Task.sleep(nanoseconds: 1_000_000_000)
}
// Running out the deadline is a reportable outcome, not silence. The
// loop previously just ended, leaving `isStarting` true forever, so a
// blocked network was indistinguishable from a slow one.
if !didComplete {
await MainActor.run {
self.isStarting = false
self.bootstrapDidStall = true
SecureLogger.warning(
"TorManager: bootstrap did not complete within its deadline (progress=\(self.bootstrapProgress)); network may be blocking Tor",
category: .session
)
NotificationCenter.default.post(name: .TorBootstrapDidStall, object: nil)
}
}
}
private func getBootstrapSummary() -> String {
var buf = [CChar](repeating: 0, count: 256)
let len = arti_bootstrap_summary(&buf, Int32(buf.count))
if len > 0 {
return String(cString: buf)
}
return ""
}
// MARK: - Foreground/Background
public func ensureRunningOnForeground() {
if !allowAutoStart { return }
SecureLogger.debug("TorManager: ensureRunningOnForeground() started", category: .session)
Task.detached(priority: .userInitiated) { [weak self] in
guard let self = self else { return }
let claimed: Bool = await MainActor.run {
if self.isStarting || self.restarting { return false }
self.restarting = true
return true
}
if !claimed { return }
// Check if already ready
let alreadyReady = await MainActor.run { self.isReady }
if alreadyReady {
await MainActor.run { self.restarting = false }
return
}
// Arti doesn't support dormant/wake (it's a no-op stub), so always do full restart
await self.restartArti()
await MainActor.run { self.restarting = false }
}
}
public func goDormantOnBackground() {
// Arti doesn't support real dormant mode, so just mark as not ready.
// iOS will suspend the runtime anyway. On foreground we do a full restart.
// Clear isStarting so foreground recovery can proceed if bootstrap was interrupted.
SecureLogger.debug("TorManager: goDormantOnBackground() called", category: .session)
Task { @MainActor in
self.isReady = false
self.socksReady = false
self.isStarting = false
}
}
public func shutdownCompletely() {
SecureLogger.debug("TorManager: shutdownCompletely() called", category: .session)
startPendingAfterShutdown = false
shutdownsInFlight += 1
Task.detached { [weak self] in
guard let self = self else { return }
_ = arti_stop()
// Wait for shutdown
var waited = 0
while arti_is_running() != 0 && waited < 50 {
try? await Task.sleep(nanoseconds: 100_000_000)
waited += 1
}
await MainActor.run {
self.isReady = false
self.socksReady = false
self.bootstrapProgress = 0
self.bootstrapSummary = ""
self.isStarting = false
self.didStart = false
self.restarting = false
self.bootstrapMonitorStarted = false
// Note: Don't clear startedAt here - it will be set fresh on next startIfNeeded()
// Clearing it here races with startup and defeats the grace period
self.shutdownsInFlight -= 1
if self.shutdownsInFlight == 0 && self.startPendingAfterShutdown {
self.startPendingAfterShutdown = false
SecureLogger.debug("TorManager: honoring start deferred during shutdown", category: .session)
self.startIfNeeded()
}
}
}
}
private func restartArti() async {
SecureLogger.debug("TorManager: restartArti() starting", category: .session)
await MainActor.run {
NotificationCenter.default.post(name: .TorWillRestart, object: nil)
self.isReady = false
self.socksReady = false
self.bootstrapProgress = 0
self.bootstrapSummary = ""
self.isStarting = true
self.bootstrapDidStall = false
self.lastRestartAt = Date()
}
_ = arti_stop()
// Wait for stop
var waited = 0
while arti_is_running() != 0 && waited < 40 {
try? await Task.sleep(nanoseconds: 100_000_000)
waited += 1
}
await MainActor.run {
self.bootstrapMonitorStarted = false
self.didStart = false
}
await MainActor.run { self.startIfNeeded() }
}
private func recomputeReady() {
let ready = socksReady && bootstrapProgress >= 100
if ready != isReady {
if !ready {
SecureLogger.debug("TorManager: isReady -> false (socksReady=\(socksReady), bootstrap=\(bootstrapProgress))", category: .session)
}
isReady = ready
if ready {
NotificationCenter.default.post(name: .TorDidBecomeReady, object: nil)
}
}
}
private func startPathMonitorIfNeeded() {
#if canImport(Network)
guard pathMonitor == nil else { return }
let monitor = NWPathMonitor()
pathMonitor = monitor
let queue = DispatchQueue(label: "TorPathMonitor")
monitor.pathUpdateHandler = { [weak self] _ in
Task { @MainActor in
guard let self = self else { return }
if self.isAppForeground {
self.pokeTorOnPathChange()
}
}
}
monitor.start(queue: queue)
#endif
}
private func pokeTorOnPathChange() {
// Skip if we recently restarted
if let last = lastRestartAt, Date().timeIntervalSince(last) < 3.0 {
SecureLogger.debug("TorManager: pokeTorOnPathChange() skipped - recent restart", category: .session)
return
}
// Skip during initial startup grace period (15s) to avoid race conditions
if let started = startedAt, Date().timeIntervalSince(started) < 15.0 {
SecureLogger.debug("TorManager: pokeTorOnPathChange() skipped - startup grace period (\(Int(Date().timeIntervalSince(started)))s)", category: .session)
return
}
if isStarting || restarting {
SecureLogger.debug("TorManager: pokeTorOnPathChange() skipped - isStarting=\(isStarting) restarting=\(restarting)", category: .session)
return
}
if isReady { return }
SecureLogger.debug("TorManager: pokeTorOnPathChange() - Arti not ready, initiating recovery", category: .session)
ensureRunningOnForeground()
}
}
// MARK: - Start policy configuration
extension TorManager {
@MainActor
public func setAutoStartAllowed(_ allow: Bool) {
allowAutoStart = allow
}
@MainActor
public func isAutoStartAllowed() -> Bool { allowAutoStart }
}