mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 23:45:20 +00:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad2a6f0a20 | ||
|
|
5f7df63238 | ||
|
|
b081c98dba | ||
|
|
76d3b0f1ed | ||
|
|
aa3021c9ca | ||
|
|
ca18843bb0 | ||
|
|
593fd7d737 |
+3
-3
@@ -17,8 +17,8 @@ bitchat is designed for private, account-free communication. This policy describ
|
|||||||
|
|
||||||
1. **Identity and cryptographic keys**
|
1. **Identity and cryptographic keys**
|
||||||
- Noise, signing, group, prekey, and optional Nostr identity material is generated locally.
|
- Noise, signing, group, prekey, and optional Nostr identity material is generated locally.
|
||||||
- Secret keys are stored in the system keychain. Public keys are shared when required for messaging, verification, groups, or Nostr events.
|
- Secret keys are stored in the system keychain as device-only items. Public keys are shared when required for messaging, verification, groups, or Nostr events.
|
||||||
- Keys remain until they are rotated, removed by the relevant feature, erased with panic wipe, or removed with the app.
|
- Keys remain until they are rotated, removed by the relevant feature, or erased with panic wipe. Because operating-system keychains can outlive an uninstall, bitchat records a non-secret install marker and deletes surviving app keys before use after a later reinstall.
|
||||||
|
|
||||||
2. **Nickname, preferences, and relationships**
|
2. **Nickname, preferences, and relationships**
|
||||||
- Your nickname, settings, favorites, petnames, read-receipt identifiers, and bounded operational metadata are stored locally.
|
- Your nickname, settings, favorites, petnames, read-receipt identifiers, and bounded operational metadata are stored locally.
|
||||||
@@ -121,7 +121,7 @@ No cryptographic system can protect content after a recipient reads, copies, scr
|
|||||||
|
|
||||||
## Your Controls
|
## Your Controls
|
||||||
|
|
||||||
- **Panic wipe:** Triple-tap the logo to clear local keys, sessions, preferences, groups, queues, carried mail, public archives, board data, and media managed by the app.
|
- **Panic wipe:** Triple-tap the logo to synchronously cancel in-flight media work and clear local keys, sessions, preferences, groups, queues, carried mail, public archives, board data, and media managed by the app.
|
||||||
- **Feature controls:** Location channels, mesh bridge, internet gateway, and related internet behaviors can be disabled in the app. Some already-published relay data cannot be recalled.
|
- **Feature controls:** Location channels, mesh bridge, internet gateway, and related internet behaviors can be disabled in the app. Some already-published relay data cannot be recalled.
|
||||||
- **System permissions:** Bluetooth, location, microphone, camera, and photo-library access can be revoked in system settings.
|
- **System permissions:** Bluetooth, location, microphone, camera, and photo-library access can be revoked in system settings.
|
||||||
- **No account:** The project operates no account record for you to request or export.
|
- **No account:** The project operates no account record for you to request or export.
|
||||||
|
|||||||
Generated
+1
@@ -337,6 +337,7 @@
|
|||||||
es,
|
es,
|
||||||
ar,
|
ar,
|
||||||
de,
|
de,
|
||||||
|
fa,
|
||||||
fr,
|
fr,
|
||||||
he,
|
he,
|
||||||
id,
|
id,
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ final class AppChromeModel: ObservableObject {
|
|||||||
|
|
||||||
private let chatViewModel: ChatViewModel
|
private let chatViewModel: ChatViewModel
|
||||||
private var cancellables = Set<AnyCancellable>()
|
private var cancellables = Set<AnyCancellable>()
|
||||||
|
/// The composer owns capture state above ChatViewModel. ContentView
|
||||||
|
/// installs this hook so both panic entry points synchronously stop it.
|
||||||
|
private var prepareForPanic: (@MainActor () -> Void)?
|
||||||
|
|
||||||
/// Bulletin-board coordinator, created on first use of the board sheet.
|
/// Bulletin-board coordinator, created on first use of the board sheet.
|
||||||
private(set) lazy var boardManager = BoardManager(transport: chatViewModel.meshService)
|
private(set) lazy var boardManager = BoardManager(transport: chatViewModel.meshService)
|
||||||
@@ -97,7 +100,12 @@ final class AppChromeModel: ObservableObject {
|
|||||||
showScreenshotPrivacyWarning = true
|
showScreenshotPrivacyWarning = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setPanicPreparation(_ preparation: (@MainActor () -> Void)?) {
|
||||||
|
prepareForPanic = preparation
|
||||||
|
}
|
||||||
|
|
||||||
func panicClearAllData() {
|
func panicClearAllData() {
|
||||||
|
prepareForPanic?()
|
||||||
chatViewModel.panicClearAllData()
|
chatViewModel.panicClearAllData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -107,12 +107,15 @@ final class AppRuntime: ObservableObject {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
GeoRelayDirectory.shared.prefetchIfNeeded()
|
if chatViewModel.networkActivationAllowed {
|
||||||
|
GeoRelayDirectory.shared.prefetchIfNeeded()
|
||||||
|
}
|
||||||
bindRuntimeObservers()
|
bindRuntimeObservers()
|
||||||
NotificationDelegate.shared.runtime = self
|
NotificationDelegate.shared.runtime = self
|
||||||
}
|
}
|
||||||
|
|
||||||
func start() {
|
func start() {
|
||||||
|
guard chatViewModel.networkActivationAllowed else { return }
|
||||||
guard !started else {
|
guard !started else {
|
||||||
checkForSharedContent()
|
checkForSharedContent()
|
||||||
return
|
return
|
||||||
@@ -151,12 +154,14 @@ final class AppRuntime: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleDidBecomeActiveNotification() {
|
func handleDidBecomeActiveNotification() {
|
||||||
|
guard chatViewModel.networkActivationAllowed else { return }
|
||||||
chatViewModel.handleDidBecomeActive()
|
chatViewModel.handleDidBecomeActive()
|
||||||
checkForSharedContent()
|
checkForSharedContent()
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
func handleMacDidBecomeActiveNotification() {
|
func handleMacDidBecomeActiveNotification() {
|
||||||
|
guard chatViewModel.networkActivationAllowed else { return }
|
||||||
record(.scenePhaseChanged(.active))
|
record(.scenePhaseChanged(.active))
|
||||||
chatViewModel.handleDidBecomeActive()
|
chatViewModel.handleDidBecomeActive()
|
||||||
checkForSharedContent()
|
checkForSharedContent()
|
||||||
@@ -175,6 +180,7 @@ final class AppRuntime: ObservableObject {
|
|||||||
didEnterBackground = true
|
didEnterBackground = true
|
||||||
|
|
||||||
case .active:
|
case .active:
|
||||||
|
guard chatViewModel.networkActivationAllowed else { return }
|
||||||
record(.scenePhaseChanged(.active))
|
record(.scenePhaseChanged(.active))
|
||||||
chatViewModel.meshService.startServices()
|
chatViewModel.meshService.startServices()
|
||||||
TorManager.shared.setAppForeground(true)
|
TorManager.shared.setAppForeground(true)
|
||||||
@@ -222,6 +228,7 @@ final class AppRuntime: ObservableObject {
|
|||||||
actionIdentifier: String = UNNotificationDefaultActionIdentifier,
|
actionIdentifier: String = UNNotificationDefaultActionIdentifier,
|
||||||
userInfo: [AnyHashable: Any]
|
userInfo: [AnyHashable: Any]
|
||||||
) {
|
) {
|
||||||
|
guard chatViewModel.networkActivationAllowed else { return }
|
||||||
if actionIdentifier == NotificationService.waveActionID {
|
if actionIdentifier == NotificationService.waveActionID {
|
||||||
chatViewModel.sendMeshWave()
|
chatViewModel.sendMeshWave()
|
||||||
return
|
return
|
||||||
@@ -273,6 +280,8 @@ private extension AppRuntime {
|
|||||||
NotificationCenter.default.publisher(for: .TorWillRestart)
|
NotificationCenter.default.publisher(for: .TorWillRestart)
|
||||||
.receive(on: DispatchQueue.main)
|
.receive(on: DispatchQueue.main)
|
||||||
.sink { [weak self] _ in
|
.sink { [weak self] _ in
|
||||||
|
guard self?.chatViewModel.networkActivationAllowed == true
|
||||||
|
else { return }
|
||||||
self?.record(.torLifecycleChanged(.willRestart))
|
self?.record(.torLifecycleChanged(.willRestart))
|
||||||
self?.chatViewModel.handleTorWillRestart()
|
self?.chatViewModel.handleTorWillRestart()
|
||||||
}
|
}
|
||||||
@@ -281,6 +290,8 @@ private extension AppRuntime {
|
|||||||
NotificationCenter.default.publisher(for: .TorDidBecomeReady)
|
NotificationCenter.default.publisher(for: .TorDidBecomeReady)
|
||||||
.receive(on: DispatchQueue.main)
|
.receive(on: DispatchQueue.main)
|
||||||
.sink { [weak self] _ in
|
.sink { [weak self] _ in
|
||||||
|
guard self?.chatViewModel.networkActivationAllowed == true
|
||||||
|
else { return }
|
||||||
self?.record(.torLifecycleChanged(.didBecomeReady))
|
self?.record(.torLifecycleChanged(.didBecomeReady))
|
||||||
self?.chatViewModel.handleTorDidBecomeReady()
|
self?.chatViewModel.handleTorDidBecomeReady()
|
||||||
}
|
}
|
||||||
@@ -289,6 +300,8 @@ private extension AppRuntime {
|
|||||||
NotificationCenter.default.publisher(for: .TorWillStart)
|
NotificationCenter.default.publisher(for: .TorWillStart)
|
||||||
.receive(on: DispatchQueue.main)
|
.receive(on: DispatchQueue.main)
|
||||||
.sink { [weak self] _ in
|
.sink { [weak self] _ in
|
||||||
|
guard self?.chatViewModel.networkActivationAllowed == true
|
||||||
|
else { return }
|
||||||
self?.record(.torLifecycleChanged(.willStart))
|
self?.record(.torLifecycleChanged(.willStart))
|
||||||
self?.chatViewModel.handleTorWillStart()
|
self?.chatViewModel.handleTorWillStart()
|
||||||
}
|
}
|
||||||
@@ -297,6 +310,8 @@ private extension AppRuntime {
|
|||||||
NotificationCenter.default.publisher(for: .TorUserPreferenceChanged)
|
NotificationCenter.default.publisher(for: .TorUserPreferenceChanged)
|
||||||
.receive(on: DispatchQueue.main)
|
.receive(on: DispatchQueue.main)
|
||||||
.sink { [weak self] notification in
|
.sink { [weak self] notification in
|
||||||
|
guard self?.chatViewModel.networkActivationAllowed == true
|
||||||
|
else { return }
|
||||||
self?.record(.torLifecycleChanged(.preferenceChanged))
|
self?.record(.torLifecycleChanged(.preferenceChanged))
|
||||||
self?.chatViewModel.handleTorPreferenceChanged(notification)
|
self?.chatViewModel.handleTorPreferenceChanged(notification)
|
||||||
}
|
}
|
||||||
@@ -313,6 +328,7 @@ private extension AppRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkForSharedContent() {
|
func checkForSharedContent() {
|
||||||
|
guard chatViewModel.networkActivationAllowed else { return }
|
||||||
guard let userDefaults = UserDefaults(suiteName: BitchatApp.groupID) else { return }
|
guard let userDefaults = UserDefaults(suiteName: BitchatApp.groupID) else { return }
|
||||||
let clearSharedContent = {
|
let clearSharedContent = {
|
||||||
userDefaults.removeObject(forKey: "sharedContent")
|
userDefaults.removeObject(forKey: "sharedContent")
|
||||||
@@ -359,7 +375,9 @@ private extension AppRuntime {
|
|||||||
let becameConnected = isConnected && !lastNostrRelayConnectedState
|
let becameConnected = isConnected && !lastNostrRelayConnectedState
|
||||||
lastNostrRelayConnectedState = isConnected
|
lastNostrRelayConnectedState = isConnected
|
||||||
|
|
||||||
guard started, becameConnected else { return }
|
guard chatViewModel.networkActivationAllowed,
|
||||||
|
started,
|
||||||
|
becameConnected else { return }
|
||||||
|
|
||||||
let isInitialConnection = !didHandleInitialNostrConnection
|
let isInitialConnection = !didHandleInitialNostrConnection
|
||||||
didHandleInitialNostrConnection = true
|
didHandleInitialNostrConnection = true
|
||||||
|
|||||||
@@ -7,45 +7,146 @@ final class LocationPresenceStore: ObservableObject {
|
|||||||
@Published private(set) var geoNicknames: [String: String] = [:]
|
@Published private(set) var geoNicknames: [String: String] = [:]
|
||||||
@Published private(set) var teleportedGeo: Set<String> = []
|
@Published private(set) var teleportedGeo: Set<String> = []
|
||||||
|
|
||||||
|
private let teleportedGeoCapacity: Int
|
||||||
|
private var teleportedGeoOrder: [String] = []
|
||||||
|
private let geoNicknameCapacity: Int
|
||||||
|
private var geoNicknameOrder: [String] = []
|
||||||
|
|
||||||
|
init(
|
||||||
|
teleportedGeoCapacity: Int = TransportConfig.geoTeleportedParticipantsCap,
|
||||||
|
geoNicknameCapacity: Int = TransportConfig.geoNicknameParticipantsCap
|
||||||
|
) {
|
||||||
|
self.teleportedGeoCapacity = max(0, teleportedGeoCapacity)
|
||||||
|
self.geoNicknameCapacity = max(0, geoNicknameCapacity)
|
||||||
|
}
|
||||||
|
|
||||||
func setCurrentGeohash(_ geohash: String?) {
|
func setCurrentGeohash(_ geohash: String?) {
|
||||||
currentGeohash = geohash?.lowercased()
|
let normalized = geohash?.lowercased()
|
||||||
|
if currentGeohash != normalized {
|
||||||
|
// Presence markers are scoped to the active geohash channel.
|
||||||
|
clearTeleportedGeo()
|
||||||
|
clearGeoNicknames()
|
||||||
|
}
|
||||||
|
currentGeohash = normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
func setNickname(_ nickname: String, for pubkeyHex: String) {
|
func setNickname(_ nickname: String, for pubkeyHex: String) {
|
||||||
geoNicknames[pubkeyHex.lowercased()] = nickname
|
guard geoNicknameCapacity > 0 else {
|
||||||
|
clearGeoNicknames()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let key = pubkeyHex.lowercased()
|
||||||
|
if geoNicknames[key] != nil {
|
||||||
|
geoNicknames[key] = nickname
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
while geoNicknameOrder.count >= geoNicknameCapacity, let oldest = geoNicknameOrder.first {
|
||||||
|
geoNicknameOrder.removeFirst()
|
||||||
|
geoNicknames.removeValue(forKey: oldest)
|
||||||
|
}
|
||||||
|
|
||||||
|
geoNicknames[key] = nickname
|
||||||
|
geoNicknameOrder.append(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func replaceGeoNicknames(_ nicknames: [String: String]) {
|
func replaceGeoNicknames(_ nicknames: [String: String]) {
|
||||||
geoNicknames = Dictionary(
|
guard geoNicknameCapacity > 0 else {
|
||||||
uniqueKeysWithValues: nicknames.map { key, value in
|
clearGeoNicknames()
|
||||||
(key.lowercased(), value)
|
return
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
var seen: Set<String> = []
|
||||||
|
var ordered: [String] = []
|
||||||
|
var normalized: [String: String] = [:]
|
||||||
|
for (key, value) in nicknames {
|
||||||
|
let lower = key.lowercased()
|
||||||
|
guard seen.insert(lower).inserted else { continue }
|
||||||
|
ordered.append(lower)
|
||||||
|
normalized[lower] = value
|
||||||
|
}
|
||||||
|
if ordered.count > geoNicknameCapacity {
|
||||||
|
let kept = Array(ordered.suffix(geoNicknameCapacity))
|
||||||
|
ordered = kept
|
||||||
|
normalized = Dictionary(uniqueKeysWithValues: kept.compactMap { key in
|
||||||
|
normalized[key].map { (key, $0) }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
geoNicknameOrder = ordered
|
||||||
|
geoNicknames = normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearGeoNicknames() {
|
func clearGeoNicknames() {
|
||||||
geoNicknames.removeAll()
|
geoNicknames.removeAll()
|
||||||
|
geoNicknameOrder.removeAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
func retainGeoNicknames(keeping pubkeys: Set<String>) {
|
||||||
|
let allowed = Set(pubkeys.map { $0.lowercased() })
|
||||||
|
geoNicknameOrder = geoNicknameOrder.filter { allowed.contains($0) }
|
||||||
|
geoNicknames = geoNicknames.filter { allowed.contains($0.key) }
|
||||||
}
|
}
|
||||||
|
|
||||||
func markTeleported(_ pubkeyHex: String) {
|
func markTeleported(_ pubkeyHex: String) {
|
||||||
teleportedGeo.insert(pubkeyHex.lowercased())
|
guard teleportedGeoCapacity > 0 else {
|
||||||
|
clearTeleportedGeo()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let key = pubkeyHex.lowercased()
|
||||||
|
guard !teleportedGeo.contains(key) else { return }
|
||||||
|
|
||||||
|
while teleportedGeoOrder.count >= teleportedGeoCapacity, let oldest = teleportedGeoOrder.first {
|
||||||
|
teleportedGeoOrder.removeFirst()
|
||||||
|
teleportedGeo.remove(oldest)
|
||||||
|
}
|
||||||
|
|
||||||
|
teleportedGeo.insert(key)
|
||||||
|
teleportedGeoOrder.append(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearTeleported(_ pubkeyHex: String) {
|
func clearTeleported(_ pubkeyHex: String) {
|
||||||
teleportedGeo.remove(pubkeyHex.lowercased())
|
let key = pubkeyHex.lowercased()
|
||||||
|
teleportedGeo.remove(key)
|
||||||
|
teleportedGeoOrder.removeAll { $0 == key }
|
||||||
}
|
}
|
||||||
|
|
||||||
func replaceTeleportedGeo(_ pubkeys: Set<String>) {
|
func replaceTeleportedGeo(_ pubkeys: Set<String>) {
|
||||||
teleportedGeo = Set(pubkeys.map { $0.lowercased() })
|
guard teleportedGeoCapacity > 0 else {
|
||||||
|
clearTeleportedGeo()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var seen: Set<String> = []
|
||||||
|
var ordered: [String] = []
|
||||||
|
for key in pubkeys.map({ $0.lowercased() }) where !seen.contains(key) {
|
||||||
|
seen.insert(key)
|
||||||
|
ordered.append(key)
|
||||||
|
}
|
||||||
|
if ordered.count > teleportedGeoCapacity {
|
||||||
|
ordered = Array(ordered.suffix(teleportedGeoCapacity))
|
||||||
|
}
|
||||||
|
teleportedGeoOrder = ordered
|
||||||
|
teleportedGeo = Set(ordered)
|
||||||
|
}
|
||||||
|
|
||||||
|
func retainTeleportedGeo(keeping pubkeys: Set<String>) {
|
||||||
|
let allowed = Set(pubkeys.map { $0.lowercased() })
|
||||||
|
teleportedGeoOrder = teleportedGeoOrder.filter { allowed.contains($0) }
|
||||||
|
teleportedGeo = teleportedGeo.intersection(allowed)
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearTeleportedGeo() {
|
func clearTeleportedGeo() {
|
||||||
teleportedGeo.removeAll()
|
teleportedGeo.removeAll()
|
||||||
|
teleportedGeoOrder.removeAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
func reset() {
|
func reset() {
|
||||||
currentGeohash = nil
|
currentGeohash = nil
|
||||||
geoNicknames.removeAll()
|
geoNicknames.removeAll()
|
||||||
|
geoNicknameOrder.removeAll()
|
||||||
teleportedGeo.removeAll()
|
teleportedGeo.removeAll()
|
||||||
|
teleportedGeoOrder.removeAll()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ protocol VoiceCaptureSession: AnyObject {
|
|||||||
/// nothing valid was captured.
|
/// nothing valid was captured.
|
||||||
func finish() async -> URL?
|
func finish() async -> URL?
|
||||||
func cancel() async
|
func cancel() async
|
||||||
|
/// Stops capture and suppresses every later send before returning.
|
||||||
|
func panicCancelSynchronously()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The classic record-then-send backend, wrapping the shared `VoiceRecorder`.
|
/// The classic record-then-send backend, wrapping the shared `VoiceRecorder`.
|
||||||
@@ -55,6 +57,10 @@ final class VoiceNoteCaptureSession: VoiceCaptureSession {
|
|||||||
func cancel() async {
|
func cancel() async {
|
||||||
await recorder.cancelRecording(owner: owner)
|
await recorder.cancelRecording(owner: owner)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func panicCancelSynchronously() {
|
||||||
|
recorder.panicCancelSynchronously(owner: owner)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Testable surface of the live capture engine. Production uses
|
/// Testable surface of the live capture engine. Production uses
|
||||||
@@ -216,6 +222,13 @@ final class PTTLiveVoiceSession: VoiceCaptureSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func panicCancelSynchronously() {
|
||||||
|
// Do not emit a canceled packet: it would itself be pre-panic
|
||||||
|
// conversation data racing the emergency transport reset.
|
||||||
|
completed = true
|
||||||
|
capture.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
private func sendControlPacket(_ kind: VoiceBurstPacket.Kind) {
|
private func sendControlPacket(_ kind: VoiceBurstPacket.Kind) {
|
||||||
guard let packet = VoiceBurstPacket(burstID: burstID, seq: stream.packetizer.nextSeq, kind: kind) else { return }
|
guard let packet = VoiceBurstPacket(burstID: burstID, seq: stream.packetizer.nextSeq, kind: kind) else { return }
|
||||||
sendPacket(packet.encode())
|
sendPacket(packet.encode())
|
||||||
|
|||||||
@@ -246,6 +246,21 @@ actor VoiceRecorder {
|
|||||||
currentURL = nil
|
currentURL = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Panic is a synchronous security boundary: the caller must know the
|
||||||
|
/// microphone, audio-session lease, and partial file are gone before it
|
||||||
|
/// rotates identities or deletes the media tree. VoiceRecorder is an
|
||||||
|
/// independent actor and this cleanup path never hops to MainActor, so a
|
||||||
|
/// short semaphore join is safe even when invoked by the UI actor.
|
||||||
|
nonisolated
|
||||||
|
func panicCancelSynchronously(owner: RecordingOwner) {
|
||||||
|
let finished = DispatchSemaphore(value: 0)
|
||||||
|
Task {
|
||||||
|
await cancelRecording(owner: owner)
|
||||||
|
finished.signal()
|
||||||
|
}
|
||||||
|
finished.wait()
|
||||||
|
}
|
||||||
|
|
||||||
/// The audio session was interrupted (call, Siri) or reconfigured: stop
|
/// The audio session was interrupted (call, Siri) or reconfigured: stop
|
||||||
/// the recorder but keep `recorder`/`currentURL` so the caller's pending
|
/// the recorder but keep `recorder`/`currentURL` so the caller's pending
|
||||||
/// `stopRecording()` still returns the partial note.
|
/// `stopRecording()` still returns the partial note.
|
||||||
|
|||||||
+3099
-1
File diff suppressed because it is too large
Load Diff
@@ -66,7 +66,10 @@ class NoiseSession {
|
|||||||
|
|
||||||
// Only initiator writes the first message
|
// Only initiator writes the first message
|
||||||
if role == .initiator {
|
if role == .initiator {
|
||||||
let message = try handshakeState!.writeMessage()
|
guard let handshake = handshakeState else {
|
||||||
|
throw NoiseSessionError.invalidState
|
||||||
|
}
|
||||||
|
let message = try handshake.writeMessage()
|
||||||
sentHandshakeMessages.append(message)
|
sentHandshakeMessages.append(message)
|
||||||
return message
|
return message
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -700,6 +700,10 @@ struct NostrEvent: Codable {
|
|||||||
let content = dict["content"] as? String else {
|
let content = dict["content"] as? String else {
|
||||||
throw NostrError.invalidEvent
|
throw NostrError.invalidEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
guard Self.isWithinInboundTagLimits(tags) else {
|
||||||
|
throw NostrError.invalidEvent
|
||||||
|
}
|
||||||
|
|
||||||
self.id = dict["id"] as? String ?? ""
|
self.id = dict["id"] as? String ?? ""
|
||||||
self.pubkey = pubkey
|
self.pubkey = pubkey
|
||||||
@@ -709,6 +713,21 @@ struct NostrEvent: Codable {
|
|||||||
self.content = content
|
self.content = content
|
||||||
self.sig = dict["sig"] as? String
|
self.sig = dict["sig"] as? String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Bounds untrusted relay tag arrays so attackers cannot force large
|
||||||
|
/// allocations or expensive joins on the inbound hot path.
|
||||||
|
static func isWithinInboundTagLimits(_ tags: [[String]]) -> Bool {
|
||||||
|
guard tags.count <= TransportConfig.nostrMaxEventTags else { return false }
|
||||||
|
|
||||||
|
for tag in tags {
|
||||||
|
guard tag.count <= TransportConfig.nostrMaxEventTagValues else { return false }
|
||||||
|
guard tag.allSatisfy({ $0.utf8.count <= TransportConfig.nostrMaxEventTagValueBytes }) else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func sign(with key: P256K.Schnorr.PrivateKey) throws -> NostrEvent {
|
func sign(with key: P256K.Schnorr.PrivateKey) throws -> NostrEvent {
|
||||||
let (eventId, eventIdHash) = try calculateEventId()
|
let (eventId, eventIdHash) = try calculateEventId()
|
||||||
|
|||||||
@@ -1480,7 +1480,7 @@ private enum ParsedInbound {
|
|||||||
case notice(String)
|
case notice(String)
|
||||||
|
|
||||||
init?(_ message: URLSessionWebSocketTask.Message) {
|
init?(_ message: URLSessionWebSocketTask.Message) {
|
||||||
guard let data = message.data,
|
guard let data = message.dataWithinInboundLimit,
|
||||||
let array = try? JSONSerialization.jsonObject(with: data) as? [Any],
|
let array = try? JSONSerialization.jsonObject(with: data) as? [Any],
|
||||||
array.count >= 2,
|
array.count >= 2,
|
||||||
let type = array[0] as? String else {
|
let type = array[0] as? String else {
|
||||||
@@ -1525,11 +1525,19 @@ private enum ParsedInbound {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private extension URLSessionWebSocketTask.Message {
|
private extension URLSessionWebSocketTask.Message {
|
||||||
var data: Data? {
|
/// Prefer rejecting oversized frames before UTF-8/Data materialization
|
||||||
|
/// where we can (string length), and always before JSON parse.
|
||||||
|
var dataWithinInboundLimit: Data? {
|
||||||
|
let maxBytes = TransportConfig.nostrMaxInboundMessageBytes
|
||||||
switch self {
|
switch self {
|
||||||
case .string(let text): text.data(using: .utf8)
|
case .string(let text):
|
||||||
case .data(let data): data
|
guard text.utf8.count <= maxBytes else { return nil }
|
||||||
@unknown default: nil
|
return text.data(using: .utf8)
|
||||||
|
case .data(let data):
|
||||||
|
guard data.count <= maxBytes else { return nil }
|
||||||
|
return data
|
||||||
|
@unknown default:
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
/// Thread-safe announce admission state.
|
struct BLEAnnounceThrottle {
|
||||||
///
|
|
||||||
/// Announce requests originate from the Bluetooth delegate queue, the
|
|
||||||
/// concurrent message queue, and the maintenance timer. Keeping the timestamp
|
|
||||||
/// behind a lock makes admission and maintenance snapshots atomic when those
|
|
||||||
/// request sources race.
|
|
||||||
final class BLEAnnounceThrottle: @unchecked Sendable {
|
|
||||||
private let lock = NSLock()
|
|
||||||
private var lastSent: Date
|
private var lastSent: Date
|
||||||
private let normalMinimumInterval: TimeInterval
|
private let normalMinimumInterval: TimeInterval
|
||||||
private let forcedMinimumInterval: TimeInterval
|
private let forcedMinimumInterval: TimeInterval
|
||||||
@@ -23,18 +16,16 @@ final class BLEAnnounceThrottle: @unchecked Sendable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func elapsed(since now: Date) -> TimeInterval {
|
func elapsed(since now: Date) -> TimeInterval {
|
||||||
lock.withLock { now.timeIntervalSince(lastSent) }
|
now.timeIntervalSince(lastSent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func shouldSend(force: Bool, now: Date) -> Bool {
|
mutating func shouldSend(force: Bool, now: Date) -> Bool {
|
||||||
lock.withLock {
|
let minimumInterval = force ? forcedMinimumInterval : normalMinimumInterval
|
||||||
let minimumInterval = force ? forcedMinimumInterval : normalMinimumInterval
|
guard elapsed(since: now) >= minimumInterval else {
|
||||||
guard now.timeIntervalSince(lastSent) >= minimumInterval else {
|
return false
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
lastSent = now
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastSent = now
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,124 @@ import BitLogger
|
|||||||
import BitFoundation
|
import BitFoundation
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
struct PanicRecoveryIntent {
|
||||||
|
let fileMarkerEstablished: Bool
|
||||||
|
let externalMarkerEstablished: Bool
|
||||||
|
|
||||||
|
var hasDurableMarker: Bool {
|
||||||
|
fileMarkerEstablished || externalMarkerEstablished
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Small, dependency-injectable transaction surface used by ChatViewModel.
|
||||||
|
/// Production persists the same intent in two independent locations before
|
||||||
|
/// any application state is erased. Tests can inject an ephemeral operation
|
||||||
|
/// set without touching the developer's Application Support directory.
|
||||||
|
struct PanicRecoveryOperations {
|
||||||
|
let isPending: () throws -> Bool
|
||||||
|
let begin: () -> PanicRecoveryIntent
|
||||||
|
let wipeMedia: (PanicRecoveryIntent) throws -> Void
|
||||||
|
let complete: () throws -> Void
|
||||||
|
|
||||||
|
static func ephemeral(
|
||||||
|
wipeMedia: @escaping () throws -> Void = {}
|
||||||
|
) -> PanicRecoveryOperations {
|
||||||
|
PanicRecoveryOperations(
|
||||||
|
isPending: { false },
|
||||||
|
begin: {
|
||||||
|
PanicRecoveryIntent(
|
||||||
|
fileMarkerEstablished: false,
|
||||||
|
externalMarkerEstablished: false
|
||||||
|
)
|
||||||
|
},
|
||||||
|
wipeMedia: { _ in try wipeMedia() },
|
||||||
|
complete: {}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func live(
|
||||||
|
fileStore: BLEIncomingFileStore = BLEIncomingFileStore(),
|
||||||
|
defaults: UserDefaults = .standard
|
||||||
|
) -> PanicRecoveryOperations {
|
||||||
|
let defaultsKey = "bitchat.panicResetPending"
|
||||||
|
return PanicRecoveryOperations(
|
||||||
|
isPending: {
|
||||||
|
if defaults.bool(forKey: defaultsKey) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return try fileStore.isPanicRecoveryPending()
|
||||||
|
},
|
||||||
|
begin: {
|
||||||
|
defaults.set(true, forKey: defaultsKey)
|
||||||
|
let externalMarkerEstablished =
|
||||||
|
defaults.synchronize()
|
||||||
|
&& defaults.bool(forKey: defaultsKey)
|
||||||
|
|
||||||
|
let fileMarkerEstablished: Bool
|
||||||
|
do {
|
||||||
|
try fileStore.markPanicRecoveryPending()
|
||||||
|
fileMarkerEstablished = true
|
||||||
|
} catch {
|
||||||
|
fileMarkerEstablished = false
|
||||||
|
SecureLogger.error(
|
||||||
|
"Failed to persist file panic-recovery marker: \(error)",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return PanicRecoveryIntent(
|
||||||
|
fileMarkerEstablished: fileMarkerEstablished,
|
||||||
|
externalMarkerEstablished: externalMarkerEstablished
|
||||||
|
)
|
||||||
|
},
|
||||||
|
wipeMedia: { intent in
|
||||||
|
try fileStore.panicWipe(
|
||||||
|
hasDurablePendingMarker: intent.hasDurableMarker
|
||||||
|
)
|
||||||
|
},
|
||||||
|
complete: {
|
||||||
|
// Keep the independent defaults latch until the file marker
|
||||||
|
// has definitely cleared. Any failure therefore remains
|
||||||
|
// visible to the next launch.
|
||||||
|
try fileStore.completePanicRecovery()
|
||||||
|
defaults.removeObject(forKey: defaultsKey)
|
||||||
|
guard defaults.synchronize(),
|
||||||
|
!defaults.bool(forKey: defaultsKey) else {
|
||||||
|
throw BLEIncomingFileStore.PanicRecoveryError
|
||||||
|
.externalMarkerCommitFailed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct BLEIncomingFileStore {
|
struct BLEIncomingFileStore {
|
||||||
|
enum PanicRecoveryError: Error {
|
||||||
|
case externalMarkerCommitFailed
|
||||||
|
case markerWriteFailed(Error)
|
||||||
|
case markerWriteAndMediaWipeFailed(
|
||||||
|
markerError: Error,
|
||||||
|
mediaError: Error
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
private static let quotaBytes: Int64 = 100 * 1024 * 1024
|
private static let quotaBytes: Int64 = 100 * 1024 * 1024
|
||||||
|
/// Kept outside `files/` so deleting the media tree cannot erase the
|
||||||
|
/// fail-closed startup decision before the full panic has committed.
|
||||||
|
private static let panicRecoveryPendingMarkerFileName =
|
||||||
|
".panic-recovery-pending"
|
||||||
|
/// Compatibility with a short-lived development build that used the
|
||||||
|
/// media-specific name for the same full-transaction latch.
|
||||||
|
private static let legacyPanicRecoveryPendingMarkerFileName =
|
||||||
|
".panic-media-wipe-pending"
|
||||||
|
private static let mediaSubdirectories = [
|
||||||
|
"voicenotes/incoming",
|
||||||
|
"voicenotes/outgoing",
|
||||||
|
"images/incoming",
|
||||||
|
"images/outgoing",
|
||||||
|
"files/incoming",
|
||||||
|
"files/outgoing"
|
||||||
|
]
|
||||||
|
|
||||||
/// Name prefix of in-flight live voice captures (progressively written by
|
/// Name prefix of in-flight live voice captures (progressively written by
|
||||||
/// `ChatLiveVoiceCoordinator`). Quota eviction skips them by pattern —
|
/// `ChatLiveVoiceCoordinator`). Quota eviction skips them by pattern —
|
||||||
@@ -17,11 +133,96 @@ struct BLEIncomingFileStore {
|
|||||||
let fileManager: FileManager
|
let fileManager: FileManager
|
||||||
private let baseDirectory: URL?
|
private let baseDirectory: URL?
|
||||||
private let dateProvider: () -> Date
|
private let dateProvider: () -> Date
|
||||||
|
private let panicMarkerWriter: (Data, URL) throws -> Void
|
||||||
|
|
||||||
init(fileManager: FileManager = .default, baseDirectory: URL? = nil, dateProvider: @escaping () -> Date = Date.init) {
|
init(
|
||||||
|
fileManager: FileManager = .default,
|
||||||
|
baseDirectory: URL? = nil,
|
||||||
|
dateProvider: @escaping () -> Date = Date.init,
|
||||||
|
panicMarkerWriter: @escaping (Data, URL) throws -> Void = {
|
||||||
|
try $0.write(to: $1, options: .atomic)
|
||||||
|
}
|
||||||
|
) {
|
||||||
self.fileManager = fileManager
|
self.fileManager = fileManager
|
||||||
self.baseDirectory = baseDirectory
|
self.baseDirectory = baseDirectory
|
||||||
self.dateProvider = dateProvider
|
self.dateProvider = dateProvider
|
||||||
|
self.panicMarkerWriter = panicMarkerWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Panic-wipe every managed incoming and outgoing media artifact before
|
||||||
|
/// returning. Recreating the directory tree keeps later capture/receive
|
||||||
|
/// paths usable without allowing a detached cleanup task to race them.
|
||||||
|
///
|
||||||
|
/// Marker persistence and deletion are deliberately separate error
|
||||||
|
/// domains: even when both durable marker channels fail, deletion is
|
||||||
|
/// still attempted before this method reports the marker failure.
|
||||||
|
func panicWipe(
|
||||||
|
hasDurablePendingMarker: Bool = false
|
||||||
|
) throws {
|
||||||
|
let markerError: Error?
|
||||||
|
do {
|
||||||
|
try markPanicRecoveryPending()
|
||||||
|
markerError = nil
|
||||||
|
} catch {
|
||||||
|
markerError = error
|
||||||
|
SecureLogger.error(
|
||||||
|
"Could not persist file panic-recovery marker; attempting media deletion anyway: \(error)",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
let filesDirectory = try rootDirectory()
|
||||||
|
.appendingPathComponent("files", isDirectory: true)
|
||||||
|
if fileManager.fileExists(atPath: filesDirectory.path) {
|
||||||
|
try fileManager.removeItem(at: filesDirectory)
|
||||||
|
}
|
||||||
|
for subdirectory in Self.mediaSubdirectories {
|
||||||
|
try fileManager.createDirectory(
|
||||||
|
at: filesDirectory.appendingPathComponent(
|
||||||
|
subdirectory,
|
||||||
|
isDirectory: true
|
||||||
|
),
|
||||||
|
withIntermediateDirectories: true,
|
||||||
|
attributes: nil
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
if let markerError {
|
||||||
|
throw PanicRecoveryError.markerWriteAndMediaWipeFailed(
|
||||||
|
markerError: markerError,
|
||||||
|
mediaError: error
|
||||||
|
)
|
||||||
|
}
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
if let markerError, !hasDurablePendingMarker {
|
||||||
|
throw PanicRecoveryError.markerWriteFailed(markerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func markPanicRecoveryPending() throws {
|
||||||
|
let markerURL = try panicRecoveryPendingMarkerURL()
|
||||||
|
try fileManager.createDirectory(
|
||||||
|
at: markerURL.deletingLastPathComponent(),
|
||||||
|
withIntermediateDirectories: true,
|
||||||
|
attributes: nil
|
||||||
|
)
|
||||||
|
try panicMarkerWriter(Data([1]), markerURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPanicRecoveryPending() throws -> Bool {
|
||||||
|
try panicRecoveryMarkerURLs().contains {
|
||||||
|
fileManager.fileExists(atPath: $0.path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func completePanicRecovery() throws {
|
||||||
|
for markerURL in try panicRecoveryMarkerURLs()
|
||||||
|
where fileManager.fileExists(atPath: markerURL.path) {
|
||||||
|
try fileManager.removeItem(at: markerURL)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves (and creates) an incoming-media directory for callers that
|
/// Resolves (and creates) an incoming-media directory for callers that
|
||||||
@@ -113,15 +314,39 @@ struct BLEIncomingFileStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func filesDirectory() throws -> URL {
|
private func filesDirectory() throws -> URL {
|
||||||
let root = try baseDirectory ?? fileManager.url(
|
let filesDir = try rootDirectory().appendingPathComponent("files", isDirectory: true)
|
||||||
|
try fileManager.createDirectory(at: filesDir, withIntermediateDirectories: true, attributes: nil)
|
||||||
|
return filesDir
|
||||||
|
}
|
||||||
|
|
||||||
|
private func rootDirectory() throws -> URL {
|
||||||
|
try baseDirectory ?? fileManager.url(
|
||||||
for: .applicationSupportDirectory,
|
for: .applicationSupportDirectory,
|
||||||
in: .userDomainMask,
|
in: .userDomainMask,
|
||||||
appropriateFor: nil,
|
appropriateFor: nil,
|
||||||
create: true
|
create: true
|
||||||
)
|
)
|
||||||
let filesDir = root.appendingPathComponent("files", isDirectory: true)
|
}
|
||||||
try fileManager.createDirectory(at: filesDir, withIntermediateDirectories: true, attributes: nil)
|
|
||||||
return filesDir
|
private func panicRecoveryPendingMarkerURL() throws -> URL {
|
||||||
|
try rootDirectory().appendingPathComponent(
|
||||||
|
Self.panicRecoveryPendingMarkerFileName,
|
||||||
|
isDirectory: false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func panicRecoveryMarkerURLs() throws -> [URL] {
|
||||||
|
let root = try rootDirectory()
|
||||||
|
return [
|
||||||
|
root.appendingPathComponent(
|
||||||
|
Self.panicRecoveryPendingMarkerFileName,
|
||||||
|
isDirectory: false
|
||||||
|
),
|
||||||
|
root.appendingPathComponent(
|
||||||
|
Self.legacyPanicRecoveryPendingMarkerFileName,
|
||||||
|
isDirectory: false
|
||||||
|
)
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
private func sanitizedFileName(_ name: String?, defaultName: String, fallbackExtension: String?) -> String {
|
private func sanitizedFileName(_ name: String?, defaultName: String, fallbackExtension: String?) -> String {
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
import BitFoundation
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct BLELocalIdentitySnapshot: Equatable, Sendable {
|
|
||||||
let peerID: PeerID
|
|
||||||
let peerIDData: Data
|
|
||||||
let nickname: String
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Lock-backed local identity state shared by the transport's message,
|
|
||||||
/// Bluetooth, maintenance, and main-actor entry points.
|
|
||||||
///
|
|
||||||
/// `peerID` and its binary wire representation must change as one unit during
|
|
||||||
/// panic rotation. A snapshot also gives announce construction one consistent
|
|
||||||
/// view of the nickname and identity instead of reading three independently
|
|
||||||
/// mutable properties across queues.
|
|
||||||
final class BLELocalIdentityStateStore: @unchecked Sendable {
|
|
||||||
private let lock = NSLock()
|
|
||||||
private var state: BLELocalIdentitySnapshot
|
|
||||||
|
|
||||||
init(
|
|
||||||
peerID: PeerID = PeerID(str: ""),
|
|
||||||
nickname: String = "anon"
|
|
||||||
) {
|
|
||||||
state = BLELocalIdentitySnapshot(
|
|
||||||
peerID: peerID,
|
|
||||||
peerIDData: Data(hexString: peerID.id) ?? Data(),
|
|
||||||
nickname: nickname
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func snapshot() -> BLELocalIdentitySnapshot {
|
|
||||||
lock.withLock { state }
|
|
||||||
}
|
|
||||||
|
|
||||||
func setNickname(_ nickname: String) {
|
|
||||||
lock.withLock {
|
|
||||||
state = BLELocalIdentitySnapshot(
|
|
||||||
peerID: state.peerID,
|
|
||||||
peerIDData: state.peerIDData,
|
|
||||||
nickname: nickname
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func replacePeerIdentity(with peerID: PeerID) {
|
|
||||||
lock.withLock {
|
|
||||||
state = BLELocalIdentitySnapshot(
|
|
||||||
peerID: peerID,
|
|
||||||
peerIDData: Data(hexString: peerID.id) ?? Data(),
|
|
||||||
nickname: state.nickname
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -104,6 +104,10 @@ final class BLEService: NSObject {
|
|||||||
// Test-only tap on the outbound pipeline so multi-node tests can ferry
|
// Test-only tap on the outbound pipeline so multi-node tests can ferry
|
||||||
// packets between in-process service instances.
|
// packets between in-process service instances.
|
||||||
var _test_onOutboundPacket: ((BitchatPacket) -> Void)?
|
var _test_onOutboundPacket: ((BitchatPacket) -> Void)?
|
||||||
|
/// May block a synthetic CoreBluetooth receive callback immediately
|
||||||
|
/// before it hands a packet to `messageQueue`.
|
||||||
|
var _test_beforeReceivePacketHandoff: (() -> Void)?
|
||||||
|
var _test_onReceivePacketHandoff: (() -> Void)?
|
||||||
#endif
|
#endif
|
||||||
private var selfBroadcastTracker = BLESelfBroadcastTracker()
|
private var selfBroadcastTracker = BLESelfBroadcastTracker()
|
||||||
private let meshTopology = MeshTopologyTracker()
|
private let meshTopology = MeshTopologyTracker()
|
||||||
@@ -119,6 +123,7 @@ final class BLEService: NSObject {
|
|||||||
private struct PendingMeshPing {
|
private struct PendingMeshPing {
|
||||||
let peerID: PeerID
|
let peerID: PeerID
|
||||||
let sentAt: Date
|
let sentAt: Date
|
||||||
|
let lifecycleGeneration: UInt64
|
||||||
let completion: @MainActor (MeshPingResult?) -> Void
|
let completion: @MainActor (MeshPingResult?) -> Void
|
||||||
let timeout: DispatchWorkItem
|
let timeout: DispatchWorkItem
|
||||||
}
|
}
|
||||||
@@ -134,7 +139,7 @@ final class BLEService: NSObject {
|
|||||||
private let incomingFileStore = BLEIncomingFileStore()
|
private let incomingFileStore = BLEIncomingFileStore()
|
||||||
|
|
||||||
// Simple announce throttling
|
// Simple announce throttling
|
||||||
private let announceThrottle = BLEAnnounceThrottle()
|
private var announceThrottle = BLEAnnounceThrottle()
|
||||||
|
|
||||||
// Application state tracking (thread-safe)
|
// Application state tracking (thread-safe)
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
@@ -155,6 +160,10 @@ final class BLEService: NSObject {
|
|||||||
private var centralManager: CBCentralManager?
|
private var centralManager: CBCentralManager?
|
||||||
private var peripheralManager: CBPeripheralManager?
|
private var peripheralManager: CBPeripheralManager?
|
||||||
private var characteristic: CBMutableCharacteristic?
|
private var characteristic: CBMutableCharacteristic?
|
||||||
|
private let shouldInitializeBluetoothManagers: Bool
|
||||||
|
private let panicLifecycleLock = NSLock()
|
||||||
|
private var _isPanicSuspended: Bool
|
||||||
|
private var panicLifecycleGeneration: UInt64 = 0
|
||||||
|
|
||||||
// MARK: - Identity
|
// MARK: - Identity
|
||||||
|
|
||||||
@@ -162,7 +171,9 @@ final class BLEService: NSObject {
|
|||||||
private let identityManager: SecureIdentityStateManagerProtocol
|
private let identityManager: SecureIdentityStateManagerProtocol
|
||||||
private let keychain: KeychainManagerProtocol
|
private let keychain: KeychainManagerProtocol
|
||||||
private let idBridge: NostrIdentityBridge
|
private let idBridge: NostrIdentityBridge
|
||||||
private let localIdentityState = BLELocalIdentityStateStore()
|
/// Binary form of `myPeerID`; same contract — mutated only inside a
|
||||||
|
/// `messageQueue` barrier via `refreshPeerIdentity()`.
|
||||||
|
private var myPeerIDData: Data = Data()
|
||||||
|
|
||||||
// MARK: - Advertising Privacy
|
// MARK: - Advertising Privacy
|
||||||
// No Local Name by default for maximum privacy. No rotating alias.
|
// No Local Name by default for maximum privacy. No rotating alias.
|
||||||
@@ -273,10 +284,13 @@ final class BLEService: NSObject {
|
|||||||
keychain: KeychainManagerProtocol,
|
keychain: KeychainManagerProtocol,
|
||||||
idBridge: NostrIdentityBridge,
|
idBridge: NostrIdentityBridge,
|
||||||
identityManager: SecureIdentityStateManagerProtocol,
|
identityManager: SecureIdentityStateManagerProtocol,
|
||||||
initializeBluetoothManagers: Bool = true
|
initializeBluetoothManagers: Bool = true,
|
||||||
|
startSuspendedForPanicRecovery: Bool = false
|
||||||
) {
|
) {
|
||||||
self.keychain = keychain
|
self.keychain = keychain
|
||||||
self.idBridge = idBridge
|
self.idBridge = idBridge
|
||||||
|
self.shouldInitializeBluetoothManagers = initializeBluetoothManagers
|
||||||
|
self._isPanicSuspended = startSuspendedForPanicRecovery
|
||||||
noiseService = NoiseEncryptionService(keychain: keychain)
|
noiseService = NoiseEncryptionService(keychain: keychain)
|
||||||
self.identityManager = identityManager
|
self.identityManager = identityManager
|
||||||
super.init()
|
super.init()
|
||||||
@@ -325,37 +339,90 @@ final class BLEService: NSObject {
|
|||||||
// any access from another queue (cross-queue reads use readLinkState).
|
// any access from another queue (cross-queue reads use readLinkState).
|
||||||
linkStateStore.assumeOwnership(of: bleQueue)
|
linkStateStore.assumeOwnership(of: bleQueue)
|
||||||
|
|
||||||
if initializeBluetoothManagers {
|
if !startSuspendedForPanicRecovery {
|
||||||
// Initialize BLE on background queue to prevent main thread blocking.
|
initializeBluetoothManagersIfNeeded()
|
||||||
#if os(iOS)
|
|
||||||
let centralOptions: [String: Any] = [
|
|
||||||
CBCentralManagerOptionRestoreIdentifierKey: BLEService.centralRestorationID
|
|
||||||
]
|
|
||||||
centralManager = CBCentralManager(delegate: self, queue: bleQueue, options: centralOptions)
|
|
||||||
|
|
||||||
let peripheralOptions: [String: Any] = [
|
|
||||||
CBPeripheralManagerOptionRestoreIdentifierKey: BLEService.peripheralRestorationID
|
|
||||||
]
|
|
||||||
peripheralManager = CBPeripheralManager(delegate: self, queue: bleQueue, options: peripheralOptions)
|
|
||||||
#else
|
|
||||||
centralManager = CBCentralManager(delegate: self, queue: bleQueue)
|
|
||||||
peripheralManager = CBPeripheralManager(delegate: self, queue: bleQueue)
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Single maintenance timer for all periodic tasks (dispatch-based for
|
// Single maintenance timer for all periodic tasks (dispatch-based for
|
||||||
// determinism). Only run it when real Bluetooth managers exist.
|
// determinism). Only run it when real Bluetooth managers exist.
|
||||||
meshBackgroundEnabled = initializeBluetoothManagers
|
meshBackgroundEnabled = initializeBluetoothManagers
|
||||||
startMaintenanceTimer()
|
if !startSuspendedForPanicRecovery {
|
||||||
|
startMaintenanceTimer()
|
||||||
|
}
|
||||||
|
|
||||||
// Publish initial empty state
|
// Publish initial empty state
|
||||||
requestPeerDataPublish()
|
requestPeerDataPublish()
|
||||||
|
|
||||||
// Initialize gossip sync manager
|
// Initialize gossip sync manager
|
||||||
restartGossipManager()
|
if !startSuspendedForPanicRecovery {
|
||||||
|
restartGossipManager()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var isPanicSuspended: Bool {
|
||||||
|
panicLifecycleLock.lock()
|
||||||
|
defer { panicLifecycleLock.unlock() }
|
||||||
|
return _isPanicSuspended
|
||||||
|
}
|
||||||
|
|
||||||
|
private func setPanicSuspended(_ suspended: Bool) {
|
||||||
|
panicLifecycleLock.lock()
|
||||||
|
if suspended {
|
||||||
|
panicLifecycleGeneration &+= 1
|
||||||
|
}
|
||||||
|
_isPanicSuspended = suspended
|
||||||
|
panicLifecycleLock.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func capturePanicLifecycleGeneration() -> UInt64? {
|
||||||
|
panicLifecycleLock.lock()
|
||||||
|
defer { panicLifecycleLock.unlock() }
|
||||||
|
return _isPanicSuspended ? nil : panicLifecycleGeneration
|
||||||
|
}
|
||||||
|
|
||||||
|
private func isCurrentPanicLifecycleGeneration(_ generation: UInt64) -> Bool {
|
||||||
|
panicLifecycleLock.lock()
|
||||||
|
defer { panicLifecycleLock.unlock() }
|
||||||
|
return !_isPanicSuspended && panicLifecycleGeneration == generation
|
||||||
|
}
|
||||||
|
|
||||||
|
private func initializeBluetoothManagersIfNeeded() {
|
||||||
|
guard shouldInitializeBluetoothManagers,
|
||||||
|
centralManager == nil,
|
||||||
|
peripheralManager == nil,
|
||||||
|
!isPanicSuspended else { return }
|
||||||
|
|
||||||
|
// Initialize BLE on its dedicated delegate queue. On iOS, retain the
|
||||||
|
// restoration identifiers even when construction was deferred by a
|
||||||
|
// pending panic-recovery latch.
|
||||||
|
#if os(iOS)
|
||||||
|
let centralOptions: [String: Any] = [
|
||||||
|
CBCentralManagerOptionRestoreIdentifierKey:
|
||||||
|
BLEService.centralRestorationID
|
||||||
|
]
|
||||||
|
centralManager = CBCentralManager(
|
||||||
|
delegate: self,
|
||||||
|
queue: bleQueue,
|
||||||
|
options: centralOptions
|
||||||
|
)
|
||||||
|
|
||||||
|
let peripheralOptions: [String: Any] = [
|
||||||
|
CBPeripheralManagerOptionRestoreIdentifierKey:
|
||||||
|
BLEService.peripheralRestorationID
|
||||||
|
]
|
||||||
|
peripheralManager = CBPeripheralManager(
|
||||||
|
delegate: self,
|
||||||
|
queue: bleQueue,
|
||||||
|
options: peripheralOptions
|
||||||
|
)
|
||||||
|
#else
|
||||||
|
centralManager = CBCentralManager(delegate: self, queue: bleQueue)
|
||||||
|
peripheralManager = CBPeripheralManager(delegate: self, queue: bleQueue)
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private func restartGossipManager() {
|
private func restartGossipManager() {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
// Stop existing
|
// Stop existing
|
||||||
gossipSyncManager?.stop()
|
gossipSyncManager?.stop()
|
||||||
|
|
||||||
@@ -414,8 +481,40 @@ final class BLEService: NSObject {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func resetIdentityForPanic(currentNickname: String) {
|
/// Close radio admission before application state starts disappearing.
|
||||||
collectionsQueue.sync(flags: .barrier) {
|
/// CoreBluetooth callbacks consult the same gate and cannot restart scan
|
||||||
|
/// or advertising while the full panic transaction is incomplete.
|
||||||
|
func suspendForPanicReset() {
|
||||||
|
setPanicSuspended(true)
|
||||||
|
gossipSyncManager?.stop()
|
||||||
|
gossipSyncManager = nil
|
||||||
|
// Stop the radio and drain CoreBluetooth's delegate queue first. A
|
||||||
|
// callback may already have passed its initial suspension check; the
|
||||||
|
// bleQueue drain forces its final messageQueue handoff to happen
|
||||||
|
// before the receive barrier below.
|
||||||
|
stopServicesImmediatelyForPanic()
|
||||||
|
// Drain every receive/send submitted by callbacks that finished ahead
|
||||||
|
// of the radio stop. Later callbacks observe the closed lifecycle, and
|
||||||
|
// generation-bound handoffs that raced this barrier reject themselves.
|
||||||
|
messageQueue.sync(flags: .barrier) {}
|
||||||
|
clearEmergencySessionState()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reopen the radio only after media deletion and recovery-marker commit.
|
||||||
|
func completePanicReset(restartServices: Bool) {
|
||||||
|
setPanicSuspended(false)
|
||||||
|
guard restartServices else { return }
|
||||||
|
startServices()
|
||||||
|
sendAnnounce(forceSend: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resetIdentityForPanic(
|
||||||
|
currentNickname: String,
|
||||||
|
restartServices: Bool = true
|
||||||
|
) {
|
||||||
|
gossipSyncManager?.stop()
|
||||||
|
gossipSyncManager = nil
|
||||||
|
messageQueue.sync(flags: .barrier) {
|
||||||
pendingNoiseSessionQueues.removeAll()
|
pendingNoiseSessionQueues.removeAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -458,16 +557,19 @@ final class BLEService: NSObject {
|
|||||||
configureNoiseServiceCallbacks(for: newNoise)
|
configureNoiseServiceCallbacks(for: newNoise)
|
||||||
refreshPeerIdentity()
|
refreshPeerIdentity()
|
||||||
}
|
}
|
||||||
restartGossipManager()
|
// Keep the transport silent until the application-level transaction
|
||||||
|
// has also removed its media and committed both recovery markers.
|
||||||
setNickname(currentNickname)
|
myNickname = currentNickname
|
||||||
|
|
||||||
messageDeduplicator.reset()
|
messageDeduplicator.reset()
|
||||||
messageQueue.async(flags: .barrier) { [weak self] in
|
messageQueue.async(flags: .barrier) { [weak self] in
|
||||||
self?.selfBroadcastTracker.removeAll()
|
self?.selfBroadcastTracker.removeAll()
|
||||||
}
|
}
|
||||||
requestPeerDataPublish()
|
requestPeerDataPublish()
|
||||||
startServices()
|
if restartServices {
|
||||||
|
restartGossipManager()
|
||||||
|
startServices()
|
||||||
|
sendAnnounce(forceSend: true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure this runs on message queue to avoid main thread blocking
|
// Ensure this runs on message queue to avoid main thread blocking
|
||||||
@@ -479,6 +581,7 @@ final class BLEService: NSObject {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
|
|
||||||
guard content.count <= maxMessageLength else {
|
guard content.count <= maxMessageLength else {
|
||||||
SecureLogger.error("Message too long: \(content.count) chars", category: .session)
|
SecureLogger.error("Message too long: \(content.count) chars", category: .session)
|
||||||
@@ -535,17 +638,20 @@ final class BLEService: NSObject {
|
|||||||
|
|
||||||
// MARK: Identity
|
// MARK: Identity
|
||||||
|
|
||||||
/// Derived from the Noise identity fingerprint. Reads can originate from
|
/// Derived from the Noise identity fingerprint; rotated only via
|
||||||
/// the main actor, message queue, Bluetooth queue, and maintenance timer,
|
/// `refreshPeerIdentity()` (e.g. panic reset), which performs the swap
|
||||||
/// so all three local identity fields live in one lock-backed snapshot.
|
/// inside a `messageQueue` barrier so concurrent queue work never sees a
|
||||||
var myPeerID: PeerID { localIdentityState.snapshot().peerID }
|
/// half-updated identity. Externally read-only — no out-of-band mutation
|
||||||
var myNickname: String { localIdentityState.snapshot().nickname }
|
/// may bypass that derivation.
|
||||||
private var myPeerIDData: Data { localIdentityState.snapshot().peerIDData }
|
private(set) var myPeerID = PeerID(str: "")
|
||||||
|
/// Externally read-only; mutate via `setNickname(_:)`, which also
|
||||||
|
/// broadcasts the change to peers.
|
||||||
|
private(set) var myNickname: String = "anon"
|
||||||
|
|
||||||
/// Sole mutator for `myNickname`: updates the stored value and force-sends
|
/// Sole mutator for `myNickname`: updates the stored value and force-sends
|
||||||
/// an announce so peers learn the new name.
|
/// an announce so peers learn the new name.
|
||||||
func setNickname(_ nickname: String) {
|
func setNickname(_ nickname: String) {
|
||||||
localIdentityState.setNickname(nickname)
|
self.myNickname = nickname
|
||||||
// Send announce to notify peers of nickname change (force send)
|
// Send announce to notify peers of nickname change (force send)
|
||||||
sendAnnounce(forceSend: true)
|
sendAnnounce(forceSend: true)
|
||||||
}
|
}
|
||||||
@@ -557,7 +663,9 @@ final class BLEService: NSObject {
|
|||||||
/// `startServices()` — the latter matters after a panic reset, where
|
/// `startServices()` — the latter matters after a panic reset, where
|
||||||
/// `stopServices()` cancels and nils the timer.
|
/// `stopServices()` cancels and nils the timer.
|
||||||
private func startMaintenanceTimer() {
|
private func startMaintenanceTimer() {
|
||||||
guard meshBackgroundEnabled, maintenanceTimer == nil else { return }
|
guard !isPanicSuspended,
|
||||||
|
meshBackgroundEnabled,
|
||||||
|
maintenanceTimer == nil else { return }
|
||||||
let timer = DispatchSource.makeTimerSource(queue: bleQueue)
|
let timer = DispatchSource.makeTimerSource(queue: bleQueue)
|
||||||
timer.schedule(deadline: .now() + TransportConfig.bleMaintenanceInterval,
|
timer.schedule(deadline: .now() + TransportConfig.bleMaintenanceInterval,
|
||||||
repeating: TransportConfig.bleMaintenanceInterval,
|
repeating: TransportConfig.bleMaintenanceInterval,
|
||||||
@@ -570,6 +678,12 @@ final class BLEService: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func startServices() {
|
func startServices() {
|
||||||
|
guard let lifecycleGeneration =
|
||||||
|
capturePanicLifecycleGeneration() else { return }
|
||||||
|
initializeBluetoothManagersIfNeeded()
|
||||||
|
if gossipSyncManager == nil {
|
||||||
|
restartGossipManager()
|
||||||
|
}
|
||||||
// Restart the maintenance timer if a prior stopServices() cancelled it
|
// Restart the maintenance timer if a prior stopServices() cancelled it
|
||||||
// (e.g. the panic flow), otherwise periodic announces, peer reconciliation
|
// (e.g. the panic flow), otherwise periodic announces, peer reconciliation
|
||||||
// and cache cleanup would never resume until app restart.
|
// and cache cleanup would never resume until app restart.
|
||||||
@@ -586,16 +700,19 @@ final class BLEService: NSObject {
|
|||||||
// Send initial announce after services are ready
|
// Send initial announce after services are ready
|
||||||
// Use longer delay to avoid conflicts with other announces
|
// Use longer delay to avoid conflicts with other announces
|
||||||
messageQueue.asyncAfter(deadline: .now() + TransportConfig.bleInitialAnnounceDelaySeconds) { [weak self] in
|
messageQueue.asyncAfter(deadline: .now() + TransportConfig.bleInitialAnnounceDelaySeconds) { [weak self] in
|
||||||
self?.sendAnnounce(forceSend: true)
|
guard let self,
|
||||||
|
self.isCurrentPanicLifecycleGeneration(
|
||||||
|
lifecycleGeneration
|
||||||
|
) else { return }
|
||||||
|
self.sendAnnounce(forceSend: true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopServices() {
|
func stopServices() {
|
||||||
let localIdentity = localIdentityState.snapshot()
|
|
||||||
// Send leave message synchronously to ensure delivery
|
// Send leave message synchronously to ensure delivery
|
||||||
var leavePacket = BitchatPacket(
|
var leavePacket = BitchatPacket(
|
||||||
type: MessageType.leave.rawValue,
|
type: MessageType.leave.rawValue,
|
||||||
senderID: localIdentity.peerIDData,
|
senderID: myPeerIDData,
|
||||||
recipientID: nil,
|
recipientID: nil,
|
||||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||||
payload: Data(),
|
payload: Data(),
|
||||||
@@ -655,26 +772,62 @@ final class BLEService: NSObject {
|
|||||||
centralManager?.cancelPeripheralConnection(state.peripheral)
|
centralManager?.cancelPeripheralConnection(state.peripheral)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Panic cannot spend its security boundary sending a signed LEAVE or
|
||||||
|
/// pumping the main run loop. Close the radio and timers immediately;
|
||||||
|
/// the identity/session cleanup follows synchronously.
|
||||||
|
private func stopServicesImmediatelyForPanic() {
|
||||||
|
collectionsQueue.sync(flags: .barrier) {
|
||||||
|
pendingNotifications.removeAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
maintenanceTimer?.cancel()
|
||||||
|
maintenanceTimer = nil
|
||||||
|
scanDutyTimer?.cancel()
|
||||||
|
scanDutyTimer = nil
|
||||||
|
|
||||||
|
centralManager?.stopScan()
|
||||||
|
peripheralManager?.stopAdvertising()
|
||||||
|
|
||||||
|
let peripheralsToDisconnect = bleQueue.sync {
|
||||||
|
linkStateStore.peripheralStates
|
||||||
|
}
|
||||||
|
for state in peripheralsToDisconnect {
|
||||||
|
centralManager?.cancelPeripheralConnection(state.peripheral)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func emergencyDisconnectAll() {
|
func emergencyDisconnectAll() {
|
||||||
stopServices()
|
stopServices()
|
||||||
|
clearEmergencySessionState()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func clearEmergencySessionState() {
|
||||||
// Clear all sessions and peers
|
// Clear all sessions and peers
|
||||||
let cancelledTransfers: [(id: String, items: [DispatchWorkItem])] = collectionsQueue.sync(flags: .barrier) {
|
let cancelled = collectionsQueue.sync(flags: .barrier) {
|
||||||
let entries = outboundFragmentTransfers.removeAll().map { ($0.id, $0.workItems) }
|
let entries = outboundFragmentTransfers.removeAll().map {
|
||||||
|
(id: $0.id, items: $0.workItems)
|
||||||
|
}
|
||||||
|
let pingTimeouts = pendingMeshPings.values.map(\.timeout)
|
||||||
|
pendingMeshPings.removeAll()
|
||||||
|
meshPingResponseLimiter = SyncResponseRateLimiter(
|
||||||
|
maxResponses: TransportConfig.meshPingInboundMaxPerLink,
|
||||||
|
window: TransportConfig.meshPingInboundWindowSeconds
|
||||||
|
)
|
||||||
peerRegistry.removeAll()
|
peerRegistry.removeAll()
|
||||||
fragmentAssemblyBuffer.removeAll()
|
fragmentAssemblyBuffer.removeAll()
|
||||||
sourceRouteFailures = BLESourceRouteFailureCache()
|
sourceRouteFailures = BLESourceRouteFailureCache()
|
||||||
// Also clear pending message queues to avoid stale state across sessions
|
// Also clear pending message queues to avoid stale state across sessions
|
||||||
pendingNoiseSessionQueues.removeAll()
|
pendingNoiseSessionQueues.removeAll()
|
||||||
pendingDirectedRelays.removeAll()
|
pendingDirectedRelays.removeAll()
|
||||||
return entries
|
return (transfers: entries, pingTimeouts: pingTimeouts)
|
||||||
}
|
}
|
||||||
|
|
||||||
for entry in cancelledTransfers {
|
for entry in cancelled.transfers {
|
||||||
entry.items.forEach { $0.cancel() }
|
entry.items.forEach { $0.cancel() }
|
||||||
TransferProgressManager.shared.cancel(id: entry.id)
|
TransferProgressManager.shared.cancel(id: entry.id)
|
||||||
}
|
}
|
||||||
|
cancelled.pingTimeouts.forEach { $0.cancel() }
|
||||||
|
|
||||||
// Clear processed messages
|
// Clear processed messages
|
||||||
messageDeduplicator.reset()
|
messageDeduplicator.reset()
|
||||||
@@ -895,6 +1048,7 @@ final class BLEService: NSObject {
|
|||||||
func sendFileBroadcast(_ filePacket: BitchatFilePacket, transferId: String) {
|
func sendFileBroadcast(_ filePacket: BitchatFilePacket, transferId: String) {
|
||||||
messageQueue.async { [weak self] in
|
messageQueue.async { [weak self] in
|
||||||
guard let self = self else { return }
|
guard let self = self else { return }
|
||||||
|
guard !self.isPanicSuspended else { return }
|
||||||
guard let payload = filePacket.encode() else {
|
guard let payload = filePacket.encode() else {
|
||||||
SecureLogger.error("❌ Failed to encode file packet for broadcast", category: .session)
|
SecureLogger.error("❌ Failed to encode file packet for broadcast", category: .session)
|
||||||
return
|
return
|
||||||
@@ -931,6 +1085,7 @@ final class BLEService: NSObject {
|
|||||||
func sendFilePrivate(_ filePacket: BitchatFilePacket, to peerID: PeerID, transferId: String) {
|
func sendFilePrivate(_ filePacket: BitchatFilePacket, to peerID: PeerID, transferId: String) {
|
||||||
messageQueue.async { [weak self] in
|
messageQueue.async { [weak self] in
|
||||||
guard let self = self else { return }
|
guard let self = self else { return }
|
||||||
|
guard !self.isPanicSuspended else { return }
|
||||||
guard let payload = filePacket.encode() else {
|
guard let payload = filePacket.encode() else {
|
||||||
SecureLogger.error("❌ Failed to encode file packet for private send", category: .session)
|
SecureLogger.error("❌ Failed to encode file packet for private send", category: .session)
|
||||||
return
|
return
|
||||||
@@ -1084,6 +1239,7 @@ final class BLEService: NSObject {
|
|||||||
// MARK: - Packet Broadcasting
|
// MARK: - Packet Broadcasting
|
||||||
|
|
||||||
private func broadcastPacket(_ packet: BitchatPacket, transferId: String? = nil) {
|
private func broadcastPacket(_ packet: BitchatPacket, transferId: String? = nil) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
// Apply route if recipient exists (centralized route application)
|
// Apply route if recipient exists (centralized route application)
|
||||||
let packetToSend: BitchatPacket
|
let packetToSend: BitchatPacket
|
||||||
if let recipientPeerID = PeerID(hexData: packet.recipientID) {
|
if let recipientPeerID = PeerID(hexData: packet.recipientID) {
|
||||||
@@ -1165,8 +1321,10 @@ final class BLEService: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func enqueuePendingNotification(data: Data, centrals: [CBCentral]?, context: String, attempt: Int = 0) {
|
private func enqueuePendingNotification(data: Data, centrals: [CBCentral]?, context: String, attempt: Int = 0) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
collectionsQueue.async(flags: .barrier) { [weak self] in
|
collectionsQueue.async(flags: .barrier) { [weak self] in
|
||||||
guard let self = self else { return }
|
guard let self = self else { return }
|
||||||
|
guard !self.isPanicSuspended else { return }
|
||||||
let result = self.pendingNotifications.enqueue(
|
let result = self.pendingNotifications.enqueue(
|
||||||
data: data,
|
data: data,
|
||||||
targets: centrals,
|
targets: centrals,
|
||||||
@@ -1267,6 +1425,7 @@ final class BLEService: NSObject {
|
|||||||
requireDirectPeerLink: Bool = false,
|
requireDirectPeerLink: Bool = false,
|
||||||
requireNoiseAuthenticatedPeerLink: Bool = false
|
requireNoiseAuthenticatedPeerLink: Bool = false
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
|
guard !isPanicSuspended else { return false }
|
||||||
let ingressRecord = collectionsQueue.sync { ingressLinks.record(for: packet) }
|
let ingressRecord = collectionsQueue.sync { ingressLinks.record(for: packet) }
|
||||||
var excludedPeerLinks = links(to: ingressRecord?.peerID)
|
var excludedPeerLinks = links(to: ingressRecord?.peerID)
|
||||||
if requireNoiseAuthenticatedPeerLink {
|
if requireNoiseAuthenticatedPeerLink {
|
||||||
@@ -1417,6 +1576,7 @@ final class BLEService: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func flushDirectedSpool() {
|
private func flushDirectedSpool() {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
// Move items out and attempt broadcast; if still no links, they'll be re-spooled
|
// Move items out and attempt broadcast; if still no links, they'll be re-spooled
|
||||||
let toSend = collectionsQueue.sync(flags: .barrier) {
|
let toSend = collectionsQueue.sync(flags: .barrier) {
|
||||||
pendingDirectedRelays.drainUnexpired(
|
pendingDirectedRelays.drainUnexpired(
|
||||||
@@ -1460,22 +1620,40 @@ final class BLEService: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func collectArchivedPublicMessages(completion: @escaping @MainActor ([ArchivedPublicMessage]) -> Void) {
|
func collectArchivedPublicMessages(completion: @escaping @MainActor ([ArchivedPublicMessage]) -> Void) {
|
||||||
|
guard let generation = capturePanicLifecycleGeneration() else {
|
||||||
|
return
|
||||||
|
}
|
||||||
guard let sync = gossipSyncManager else {
|
guard let sync = gossipSyncManager else {
|
||||||
Task { @MainActor in completion([]) }
|
notifyUI { [weak self] in
|
||||||
|
guard let self,
|
||||||
|
self.isCurrentPanicLifecycleGeneration(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
completion([])
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sync.collectPublicMessagePackets { [weak self] packets in
|
sync.collectPublicMessagePackets { [weak self] packets in
|
||||||
guard let self = self else {
|
guard let self,
|
||||||
Task { @MainActor in completion([]) }
|
self.isCurrentPanicLifecycleGeneration(generation) else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Signature verification and registry lookups run on messageQueue
|
// Signature verification and registry lookups run on messageQueue
|
||||||
// like the live receive path.
|
// like the live receive path.
|
||||||
self.messageQueue.async {
|
self.messageQueue.async {
|
||||||
|
guard self.isCurrentPanicLifecycleGeneration(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
let decoded = packets
|
let decoded = packets
|
||||||
.compactMap { self.decodeArchivedPublicMessage($0) }
|
.compactMap { self.decodeArchivedPublicMessage($0) }
|
||||||
.sorted { $0.timestamp < $1.timestamp }
|
.sorted { $0.timestamp < $1.timestamp }
|
||||||
Task { @MainActor in completion(decoded) }
|
self.notifyUI { [weak self] in
|
||||||
|
guard let self,
|
||||||
|
self.isCurrentPanicLifecycleGeneration(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
completion(decoded)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1633,16 +1811,7 @@ final class BLEService: NSObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
private func sendAnnounce(forceSend: Bool = false) {
|
private func sendAnnounce(forceSend: Bool = false) {
|
||||||
// Announce construction reads the replaceable Noise service and several
|
guard !isPanicSuspended else { return }
|
||||||
// related state snapshots. Serialize the whole operation with identity
|
|
||||||
// rotation instead of letting CoreBluetooth and maintenance callbacks
|
|
||||||
// execute it directly on their own queues.
|
|
||||||
messageQueue.async(flags: .barrier) { [weak self] in
|
|
||||||
self?.sendAnnounceNow(forceSend: forceSend)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func sendAnnounceNow(forceSend: Bool) {
|
|
||||||
// Throttle announces to prevent flooding
|
// Throttle announces to prevent flooding
|
||||||
if !announceThrottle.shouldSend(force: forceSend, now: Date()) {
|
if !announceThrottle.shouldSend(force: forceSend, now: Date()) {
|
||||||
return
|
return
|
||||||
@@ -1662,9 +1831,8 @@ final class BLEService: NSObject {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
let localIdentity = localIdentityState.snapshot()
|
|
||||||
let announcement = AnnouncementPacket(
|
let announcement = AnnouncementPacket(
|
||||||
nickname: localIdentity.nickname,
|
nickname: myNickname,
|
||||||
noisePublicKey: noisePub,
|
noisePublicKey: noisePub,
|
||||||
signingPublicKey: signingPub,
|
signingPublicKey: signingPub,
|
||||||
directNeighbors: connectedPeerIDs,
|
directNeighbors: connectedPeerIDs,
|
||||||
@@ -1680,7 +1848,7 @@ final class BLEService: NSObject {
|
|||||||
// Create packet with signature using the noise private key
|
// Create packet with signature using the noise private key
|
||||||
let packet = BitchatPacket(
|
let packet = BitchatPacket(
|
||||||
type: MessageType.announce.rawValue,
|
type: MessageType.announce.rawValue,
|
||||||
senderID: localIdentity.peerIDData,
|
senderID: myPeerIDData,
|
||||||
recipientID: nil,
|
recipientID: nil,
|
||||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||||
payload: payload,
|
payload: payload,
|
||||||
@@ -1694,7 +1862,14 @@ final class BLEService: NSObject {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcastPacket(signedPacket)
|
// Call directly if on messageQueue, otherwise dispatch
|
||||||
|
if DispatchQueue.getSpecific(key: messageQueueKey) != nil {
|
||||||
|
broadcastPacket(signedPacket)
|
||||||
|
} else {
|
||||||
|
messageQueue.async { [weak self] in
|
||||||
|
self?.broadcastPacket(signedPacket)
|
||||||
|
}
|
||||||
|
}
|
||||||
// Ensure our own announce is included in sync state
|
// Ensure our own announce is included in sync state
|
||||||
gossipSyncManager?.onPublicPacketSeen(signedPacket)
|
gossipSyncManager?.onPublicPacketSeen(signedPacket)
|
||||||
|
|
||||||
@@ -1846,6 +2021,13 @@ extension BLEService: CBCentralManagerDelegate {
|
|||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String: Any]) {
|
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String: Any]) {
|
||||||
let restoredPeripherals = (dict[CBCentralManagerRestoredStatePeripheralsKey] as? [CBPeripheral]) ?? []
|
let restoredPeripherals = (dict[CBCentralManagerRestoredStatePeripheralsKey] as? [CBPeripheral]) ?? []
|
||||||
|
guard !isPanicSuspended else {
|
||||||
|
central.stopScan()
|
||||||
|
restoredPeripherals.forEach {
|
||||||
|
central.cancelPeripheralConnection($0)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
let restoredServices = (dict[CBCentralManagerRestoredStateScanServicesKey] as? [CBUUID]) ?? []
|
let restoredServices = (dict[CBCentralManagerRestoredStateScanServicesKey] as? [CBUUID]) ?? []
|
||||||
let restoredOptions = (dict[CBCentralManagerRestoredStateScanOptionsKey] as? [String: Any]) ?? [:]
|
let restoredOptions = (dict[CBCentralManagerRestoredStateScanOptionsKey] as? [String: Any]) ?? [:]
|
||||||
let allowDuplicates = restoredOptions[CBCentralManagerScanOptionAllowDuplicatesKey] as? Bool
|
let allowDuplicates = restoredOptions[CBCentralManagerScanOptionAllowDuplicatesKey] as? Bool
|
||||||
@@ -1901,6 +2083,10 @@ extension BLEService: CBCentralManagerDelegate {
|
|||||||
|
|
||||||
switch central.state {
|
switch central.state {
|
||||||
case .poweredOn:
|
case .poweredOn:
|
||||||
|
guard !isPanicSuspended else {
|
||||||
|
central.stopScan()
|
||||||
|
return
|
||||||
|
}
|
||||||
// Links restored as connected have no characteristic in the new
|
// Links restored as connected have no characteristic in the new
|
||||||
// process; without rediscovery they sit connected-but-unusable
|
// process; without rediscovery they sit connected-but-unusable
|
||||||
// until the peer disconnects. Runs here (not willRestoreState)
|
// until the peer disconnects. Runs here (not willRestoreState)
|
||||||
@@ -1957,7 +2143,8 @@ extension BLEService: CBCentralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func startScanning() {
|
private func startScanning() {
|
||||||
guard let central = centralManager,
|
guard !isPanicSuspended,
|
||||||
|
let central = centralManager,
|
||||||
central.state == .poweredOn,
|
central.state == .poweredOn,
|
||||||
!central.isScanning else { return }
|
!central.isScanning else { return }
|
||||||
|
|
||||||
@@ -1978,6 +2165,7 @@ extension BLEService: CBCentralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
|
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
let peripheralID = peripheral.identifier.uuidString
|
let peripheralID = peripheral.identifier.uuidString
|
||||||
let advertisedName = advertisementData[CBAdvertisementDataLocalNameKey] as? String ?? (peripheralID.prefix(6) + "…")
|
let advertisedName = advertisementData[CBAdvertisementDataLocalNameKey] as? String ?? (peripheralID.prefix(6) + "…")
|
||||||
let isConnectable = (advertisementData[CBAdvertisementDataIsConnectable] as? NSNumber)?.boolValue ?? true
|
let isConnectable = (advertisementData[CBAdvertisementDataIsConnectable] as? NSNumber)?.boolValue ?? true
|
||||||
@@ -2019,6 +2207,10 @@ extension BLEService: CBCentralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
||||||
|
guard !isPanicSuspended else {
|
||||||
|
central.cancelPeripheralConnection(peripheral)
|
||||||
|
return
|
||||||
|
}
|
||||||
let peripheralID = peripheral.identifier.uuidString
|
let peripheralID = peripheral.identifier.uuidString
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
@@ -2169,7 +2361,9 @@ private extension CBPeripheralState {
|
|||||||
|
|
||||||
extension BLEService {
|
extension BLEService {
|
||||||
private func tryConnectFromQueue() {
|
private func tryConnectFromQueue() {
|
||||||
guard let central = centralManager, central.state == .poweredOn else { return }
|
guard !isPanicSuspended,
|
||||||
|
let central = centralManager,
|
||||||
|
central.state == .poweredOn else { return }
|
||||||
|
|
||||||
let decision = connectionScheduler.nextCandidate(
|
let decision = connectionScheduler.nextCandidate(
|
||||||
connectedOrConnectingCount: linkStateStore.connectedOrConnectingPeripheralCount,
|
connectedOrConnectingCount: linkStateStore.connectedOrConnectingPeripheralCount,
|
||||||
@@ -2195,6 +2389,7 @@ extension BLEService {
|
|||||||
using central: CBCentralManager,
|
using central: CBCentralManager,
|
||||||
logPrefix: String
|
logPrefix: String
|
||||||
) {
|
) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
let peripheral = candidate.peripheral
|
let peripheral = candidate.peripheral
|
||||||
let peripheralID = candidate.peripheralID
|
let peripheralID = candidate.peripheralID
|
||||||
linkStateStore.beginConnecting(to: peripheral, at: Date())
|
linkStateStore.beginConnecting(to: peripheral, at: Date())
|
||||||
@@ -2255,6 +2450,28 @@ private extension BLEService {
|
|||||||
#if DEBUG
|
#if DEBUG
|
||||||
// Test-only helper to inject packets into the receive pipeline
|
// Test-only helper to inject packets into the receive pipeline
|
||||||
extension BLEService {
|
extension BLEService {
|
||||||
|
/// Queues an event through the same MainActor hop as production receive
|
||||||
|
/// handlers so panic-boundary tests can deterministically invalidate it.
|
||||||
|
func _test_emitTransportEvent(_ event: TransportEvent) {
|
||||||
|
emitTransportEvent(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _test_isPanicIngressOpen: Bool {
|
||||||
|
capturePanicLifecycleGeneration() != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Models a CoreBluetooth delegate callback without requiring a physical
|
||||||
|
/// peripheral. The callback itself runs on `bleQueue`, exactly where the
|
||||||
|
/// panic radio-stop barrier must linearize it.
|
||||||
|
func _test_handlePacketFromBLEQueue(
|
||||||
|
_ packet: BitchatPacket,
|
||||||
|
fromPeerID: PeerID
|
||||||
|
) {
|
||||||
|
bleQueue.async { [weak self] in
|
||||||
|
self?.handleReceivedPacket(packet, from: fromPeerID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func _test_handlePacket(_ packet: BitchatPacket, fromPeerID: PeerID, preseedPeer: Bool = true, signingPublicKey: Data? = nil) {
|
func _test_handlePacket(_ packet: BitchatPacket, fromPeerID: PeerID, preseedPeer: Bool = true, signingPublicKey: Data? = nil) {
|
||||||
if preseedPeer {
|
if preseedPeer {
|
||||||
// Ensure the synthetic peer is known and marked verified for public-message tests
|
// Ensure the synthetic peer is known and marked verified for public-message tests
|
||||||
@@ -2376,6 +2593,7 @@ extension BLEService {
|
|||||||
|
|
||||||
extension BLEService: CBPeripheralDelegate {
|
extension BLEService: CBPeripheralDelegate {
|
||||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
if let error = error {
|
if let error = error {
|
||||||
SecureLogger.error("❌ Error discovering services for \(peripheral.name ?? "Unknown"): \(error.localizedDescription)", category: .session)
|
SecureLogger.error("❌ Error discovering services for \(peripheral.name ?? "Unknown"): \(error.localizedDescription)", category: .session)
|
||||||
// Retry service discovery after a delay
|
// Retry service discovery after a delay
|
||||||
@@ -2402,6 +2620,7 @@ extension BLEService: CBPeripheralDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
|
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
if let error = error {
|
if let error = error {
|
||||||
SecureLogger.error("❌ Error discovering characteristics for \(peripheral.name ?? "Unknown"): \(error.localizedDescription)", category: .session)
|
SecureLogger.error("❌ Error discovering characteristics for \(peripheral.name ?? "Unknown"): \(error.localizedDescription)", category: .session)
|
||||||
return
|
return
|
||||||
@@ -2449,6 +2668,7 @@ extension BLEService: CBPeripheralDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
|
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
if let error = error {
|
if let error = error {
|
||||||
SecureLogger.error("❌ Error receiving notification: \(error.localizedDescription)", category: .session)
|
SecureLogger.error("❌ Error receiving notification: \(error.localizedDescription)", category: .session)
|
||||||
return
|
return
|
||||||
@@ -2566,6 +2786,7 @@ extension BLEService: CBPeripheralDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func peripheralIsReady(toSendWriteWithoutResponse peripheral: CBPeripheral) {
|
func peripheralIsReady(toSendWriteWithoutResponse peripheral: CBPeripheral) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
// Resume queued writes for this peripheral - called when canSendWriteWithoutResponse becomes true again
|
// Resume queued writes for this peripheral - called when canSendWriteWithoutResponse becomes true again
|
||||||
if logRateLimiter.shouldLog(key: "peripheral-ready:\(peripheral.identifier.uuidString)") {
|
if logRateLimiter.shouldLog(key: "peripheral-ready:\(peripheral.identifier.uuidString)") {
|
||||||
SecureLogger.debug("📤 Peripheral \(peripheral.name ?? peripheral.identifier.uuidString.prefix(8).description) ready for more writes", category: .session)
|
SecureLogger.debug("📤 Peripheral \(peripheral.name ?? peripheral.identifier.uuidString.prefix(8).description) ready for more writes", category: .session)
|
||||||
@@ -2574,6 +2795,7 @@ extension BLEService: CBPeripheralDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService]) {
|
func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService]) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
SecureLogger.warning("⚠️ Services modified for \(peripheral.name ?? peripheral.identifier.uuidString)", category: .session)
|
SecureLogger.warning("⚠️ Services modified for \(peripheral.name ?? peripheral.identifier.uuidString)", category: .session)
|
||||||
|
|
||||||
let shouldRediscover = BLEService.shouldRediscoverBitChatService(
|
let shouldRediscover = BLEService.shouldRediscoverBitChatService(
|
||||||
@@ -2594,6 +2816,7 @@ extension BLEService: CBPeripheralDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
|
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
if let error = error {
|
if let error = error {
|
||||||
SecureLogger.error("❌ Error updating notification state: \(error.localizedDescription)", category: .session)
|
SecureLogger.error("❌ Error updating notification state: \(error.localizedDescription)", category: .session)
|
||||||
} else {
|
} else {
|
||||||
@@ -2617,6 +2840,12 @@ extension BLEService: CBPeripheralManagerDelegate {
|
|||||||
|
|
||||||
switch peripheral.state {
|
switch peripheral.state {
|
||||||
case .poweredOn:
|
case .poweredOn:
|
||||||
|
guard !isPanicSuspended else {
|
||||||
|
peripheral.stopAdvertising()
|
||||||
|
peripheral.removeAllServices()
|
||||||
|
characteristic = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
// Remove all services first to ensure clean state
|
// Remove all services first to ensure clean state
|
||||||
peripheral.removeAllServices()
|
peripheral.removeAllServices()
|
||||||
|
|
||||||
@@ -2677,6 +2906,12 @@ extension BLEService: CBPeripheralManagerDelegate {
|
|||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
func peripheralManager(_ peripheral: CBPeripheralManager, willRestoreState dict: [String: Any]) {
|
func peripheralManager(_ peripheral: CBPeripheralManager, willRestoreState dict: [String: Any]) {
|
||||||
|
guard !isPanicSuspended else {
|
||||||
|
peripheral.stopAdvertising()
|
||||||
|
peripheral.removeAllServices()
|
||||||
|
characteristic = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
let restoredServices = (dict[CBPeripheralManagerRestoredStateServicesKey] as? [CBMutableService]) ?? []
|
let restoredServices = (dict[CBPeripheralManagerRestoredStateServicesKey] as? [CBMutableService]) ?? []
|
||||||
let restoredAdvertisement = (dict[CBPeripheralManagerRestoredStateAdvertisementDataKey] as? [String: Any]) ?? [:]
|
let restoredAdvertisement = (dict[CBPeripheralManagerRestoredStateAdvertisementDataKey] as? [String: Any]) ?? [:]
|
||||||
|
|
||||||
@@ -2703,6 +2938,10 @@ extension BLEService: CBPeripheralManagerDelegate {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
|
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
|
||||||
|
guard !isPanicSuspended else {
|
||||||
|
peripheral.stopAdvertising()
|
||||||
|
return
|
||||||
|
}
|
||||||
if let error = error {
|
if let error = error {
|
||||||
SecureLogger.error("❌ Failed to add service: \(error.localizedDescription)", category: .session)
|
SecureLogger.error("❌ Failed to add service: \(error.localizedDescription)", category: .session)
|
||||||
return
|
return
|
||||||
@@ -2718,6 +2957,7 @@ extension BLEService: CBPeripheralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
|
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
let centralUUID = central.identifier.uuidString
|
let centralUUID = central.identifier.uuidString
|
||||||
SecureLogger.debug("📥 Central subscribed: \(centralUUID.prefix(8))…", category: .session)
|
SecureLogger.debug("📥 Central subscribed: \(centralUUID.prefix(8))…", category: .session)
|
||||||
linkStateStore.addSubscribedCentral(central)
|
linkStateStore.addSubscribedCentral(central)
|
||||||
@@ -2759,7 +2999,7 @@ extension BLEService: CBPeripheralManagerDelegate {
|
|||||||
let removedPeerID = linkStateStore.removeSubscribedCentral(central)
|
let removedPeerID = linkStateStore.removeSubscribedCentral(central)
|
||||||
|
|
||||||
// Ensure we're still advertising for other devices to find us
|
// Ensure we're still advertising for other devices to find us
|
||||||
if peripheral.isAdvertising == false {
|
if !isPanicSuspended, peripheral.isAdvertising == false {
|
||||||
SecureLogger.debug("📡 Restarting advertising after central unsubscribed", category: .session)
|
SecureLogger.debug("📡 Restarting advertising after central unsubscribed", category: .session)
|
||||||
peripheral.startAdvertising(buildAdvertisementData())
|
peripheral.startAdvertising(buildAdvertisementData())
|
||||||
}
|
}
|
||||||
@@ -2796,6 +3036,7 @@ extension BLEService: CBPeripheralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func peripheralManagerIsReady(toUpdateSubscribers peripheral: CBPeripheralManager) {
|
func peripheralManagerIsReady(toUpdateSubscribers peripheral: CBPeripheralManager) {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
drainPendingNotifications(logPrefix: "✅ Sent")
|
drainPendingNotifications(logPrefix: "✅ Sent")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2856,6 +3097,7 @@ extension BLEService: CBPeripheralManagerDelegate {
|
|||||||
for request in requests {
|
for request in requests {
|
||||||
peripheral.respond(to: request, withResult: .success)
|
peripheral.respond(to: request, withResult: .success)
|
||||||
}
|
}
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
|
|
||||||
// Process writes. For long writes, CoreBluetooth may deliver multiple CBATTRequest values with offsets.
|
// Process writes. For long writes, CoreBluetooth may deliver multiple CBATTRequest values with offsets.
|
||||||
// Combine per-central request values by offset before decoding.
|
// Combine per-central request values by offset before decoding.
|
||||||
@@ -2965,8 +3207,18 @@ extension BLEService {
|
|||||||
|
|
||||||
/// Notify UI on the MainActor to satisfy Swift concurrency isolation
|
/// Notify UI on the MainActor to satisfy Swift concurrency isolation
|
||||||
private func notifyUI(_ block: @escaping @MainActor () -> Void) {
|
private func notifyUI(_ block: @escaping @MainActor () -> Void) {
|
||||||
// Always hop onto the MainActor so calls to @MainActor delegates are safe
|
// Capture the panic lifecycle before queueing the MainActor hop. A
|
||||||
Task { @MainActor in
|
// receive callback can enqueue UI delivery immediately before panic
|
||||||
|
// clears application state; rechecking here prevents that stale work
|
||||||
|
// from repopulating the wiped conversation store afterward.
|
||||||
|
guard let generation = capturePanicLifecycleGeneration() else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Task { @MainActor [weak self] in
|
||||||
|
guard let self,
|
||||||
|
self.isCurrentPanicLifecycleGeneration(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
block()
|
block()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3131,14 +3383,24 @@ extension BLEService {
|
|||||||
/// The completion fires exactly once on the main actor: with RTT/hops
|
/// The completion fires exactly once on the main actor: with RTT/hops
|
||||||
/// when the matching pong returns, or nil after the timeout window.
|
/// when the matching pong returns, or nil after the timeout window.
|
||||||
func sendMeshPing(to peerID: PeerID, completion: @escaping @MainActor (MeshPingResult?) -> Void) {
|
func sendMeshPing(to peerID: PeerID, completion: @escaping @MainActor (MeshPingResult?) -> Void) {
|
||||||
|
guard let generation = capturePanicLifecycleGeneration() else {
|
||||||
|
return
|
||||||
|
}
|
||||||
messageQueue.async { [weak self] in
|
messageQueue.async { [weak self] in
|
||||||
guard let self,
|
guard let self,
|
||||||
|
self.isCurrentPanicLifecycleGeneration(generation),
|
||||||
let recipientData = peerID.toShort().routingData,
|
let recipientData = peerID.toShort().routingData,
|
||||||
let payload = MeshPingPayload(
|
let payload = MeshPingPayload(
|
||||||
nonce: Data((0..<MeshPingPayload.nonceLength).map { _ in UInt8.random(in: .min ... .max) }),
|
nonce: Data((0..<MeshPingPayload.nonceLength).map { _ in UInt8.random(in: .min ... .max) }),
|
||||||
originTTL: self.messageTTL
|
originTTL: self.messageTTL
|
||||||
) else {
|
) else {
|
||||||
Task { @MainActor in completion(nil) }
|
self?.notifyUI { [weak self] in
|
||||||
|
guard let self,
|
||||||
|
self.isCurrentPanicLifecycleGeneration(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
completion(nil)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let nonce = payload.nonce
|
let nonce = payload.nonce
|
||||||
@@ -3157,12 +3419,21 @@ extension BLEService {
|
|||||||
self.pendingMeshPings.removeValue(forKey: nonce)
|
self.pendingMeshPings.removeValue(forKey: nonce)
|
||||||
}
|
}
|
||||||
guard let expired else { return }
|
guard let expired else { return }
|
||||||
Task { @MainActor in expired.completion(nil) }
|
self.notifyUI { [weak self] in
|
||||||
|
guard let self,
|
||||||
|
self.isCurrentPanicLifecycleGeneration(
|
||||||
|
expired.lifecycleGeneration
|
||||||
|
) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expired.completion(nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.collectionsQueue.sync(flags: .barrier) {
|
self.collectionsQueue.sync(flags: .barrier) {
|
||||||
self.pendingMeshPings[nonce] = PendingMeshPing(
|
self.pendingMeshPings[nonce] = PendingMeshPing(
|
||||||
peerID: PeerID(hexData: recipientData),
|
peerID: PeerID(hexData: recipientData),
|
||||||
sentAt: Date(),
|
sentAt: Date(),
|
||||||
|
lifecycleGeneration: generation,
|
||||||
completion: completion,
|
completion: completion,
|
||||||
timeout: timeout
|
timeout: timeout
|
||||||
)
|
)
|
||||||
@@ -3229,7 +3500,15 @@ extension BLEService {
|
|||||||
rttMs: max(0, rttMs),
|
rttMs: max(0, rttMs),
|
||||||
hops: MeshPingPayload.hopCount(originTTL: pong.originTTL, receivedTTL: packet.ttl)
|
hops: MeshPingPayload.hopCount(originTTL: pong.originTTL, receivedTTL: packet.ttl)
|
||||||
)
|
)
|
||||||
Task { @MainActor in pending.completion(result) }
|
notifyUI { [weak self] in
|
||||||
|
guard let self,
|
||||||
|
self.isCurrentPanicLifecycleGeneration(
|
||||||
|
pending.lifecycleGeneration
|
||||||
|
) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pending.completion(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Estimated intermediate hops toward `peerID`, BFS over gossiped
|
/// Estimated intermediate hops toward `peerID`, BFS over gossiped
|
||||||
@@ -3358,9 +3637,8 @@ extension BLEService {
|
|||||||
private func refreshPeerIdentity() {
|
private func refreshPeerIdentity() {
|
||||||
let swap = {
|
let swap = {
|
||||||
let fingerprint = self.noiseService.getIdentityFingerprint()
|
let fingerprint = self.noiseService.getIdentityFingerprint()
|
||||||
self.localIdentityState.replacePeerIdentity(
|
self.myPeerID = PeerID(str: fingerprint.prefix(16))
|
||||||
with: PeerID(str: fingerprint.prefix(16))
|
self.myPeerIDData = Data(hexString: self.myPeerID.id) ?? Data()
|
||||||
)
|
|
||||||
self.meshTopology.reset()
|
self.meshTopology.reset()
|
||||||
}
|
}
|
||||||
if DispatchQueue.getSpecific(key: messageQueueKey) != nil {
|
if DispatchQueue.getSpecific(key: messageQueueKey) != nil {
|
||||||
@@ -3714,7 +3992,7 @@ extension BLEService {
|
|||||||
let store = courierStore
|
let store = courierStore
|
||||||
let policy = courierDepositPolicy
|
let policy = courierDepositPolicy
|
||||||
let metrics = sfMetrics
|
let metrics = sfMetrics
|
||||||
Task { @MainActor in
|
notifyUI {
|
||||||
guard let tier = policy(depositorKey, isVerifiedPeer) else {
|
guard let tier = policy(depositorKey, isVerifiedPeer) else {
|
||||||
SecureLogger.debug("📦 Courier deposit from \(peerID.id.prefix(8))… rejected (neither favorite nor verified)", category: .session)
|
SecureLogger.debug("📦 Courier deposit from \(peerID.id.prefix(8))… rejected (neither favorite nor verified)", category: .session)
|
||||||
return
|
return
|
||||||
@@ -3794,7 +4072,7 @@ extension BLEService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
let policy = courierDepositPolicy
|
let policy = courierDepositPolicy
|
||||||
Task { @MainActor in
|
notifyUI {
|
||||||
// Same trust gate as deposits: don't hand mail to a peer who
|
// Same trust gate as deposits: don't hand mail to a peer who
|
||||||
// would reject it from us.
|
// would reject it from us.
|
||||||
guard policy(noiseKey, isVerifiedPeer) != nil else { return }
|
guard policy(noiseKey, isVerifiedPeer) != nil else { return }
|
||||||
@@ -4139,6 +4417,7 @@ extension BLEService {
|
|||||||
let uuid = peripheral.identifier.uuidString
|
let uuid = peripheral.identifier.uuidString
|
||||||
bleQueue.async { [weak self] in
|
bleQueue.async { [weak self] in
|
||||||
guard let self = self else { return }
|
guard let self = self else { return }
|
||||||
|
guard !self.isPanicSuspended else { return }
|
||||||
guard let state = self.linkStateStore.state(forPeripheralID: uuid), let ch = state.characteristic else { return }
|
guard let state = self.linkStateStore.state(forPeripheralID: uuid), let ch = state.characteristic else { return }
|
||||||
|
|
||||||
// Atomically take all pending items from the queue to avoid race conditions
|
// Atomically take all pending items from the queue to avoid race conditions
|
||||||
@@ -4229,7 +4508,10 @@ extension BLEService {
|
|||||||
slotReserve: Int = TransportConfig.bleBackgroundPendingConnectSlotReserve
|
slotReserve: Int = TransportConfig.bleBackgroundPendingConnectSlotReserve
|
||||||
) {
|
) {
|
||||||
bleQueue.async { [weak self] in
|
bleQueue.async { [weak self] in
|
||||||
guard let self, let central = self.centralManager, central.state == .poweredOn else { return }
|
guard let self,
|
||||||
|
!self.isPanicSuspended,
|
||||||
|
let central = self.centralManager,
|
||||||
|
central.state == .poweredOn else { return }
|
||||||
let budget = TransportConfig.bleMaxCentralLinks
|
let budget = TransportConfig.bleMaxCentralLinks
|
||||||
- slotReserve
|
- slotReserve
|
||||||
- self.linkStateStore.connectedOrConnectingPeripheralCount
|
- self.linkStateStore.connectedOrConnectingPeripheralCount
|
||||||
@@ -4682,8 +4964,24 @@ extension BLEService {
|
|||||||
private func handleReceivedPacket(_ packet: BitchatPacket, from peerID: PeerID) {
|
private func handleReceivedPacket(_ packet: BitchatPacket, from peerID: PeerID) {
|
||||||
// Call directly if already on messageQueue, otherwise dispatch
|
// Call directly if already on messageQueue, otherwise dispatch
|
||||||
if DispatchQueue.getSpecific(key: messageQueueKey) == nil {
|
if DispatchQueue.getSpecific(key: messageQueueKey) == nil {
|
||||||
|
guard let lifecycleGeneration =
|
||||||
|
capturePanicLifecycleGeneration() else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
#if DEBUG
|
||||||
|
_test_beforeReceivePacketHandoff?()
|
||||||
|
#endif
|
||||||
messageQueue.async { [weak self] in
|
messageQueue.async { [weak self] in
|
||||||
self?.handleReceivedPacket(packet, from: peerID)
|
guard let self,
|
||||||
|
self.isCurrentPanicLifecycleGeneration(
|
||||||
|
lifecycleGeneration
|
||||||
|
) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
#if DEBUG
|
||||||
|
self._test_onReceivePacketHandoff?()
|
||||||
|
#endif
|
||||||
|
self.handleReceivedPacket(packet, from: peerID)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -5527,8 +5825,7 @@ extension BLEService {
|
|||||||
let transportPeers: [TransportPeerSnapshot] = collectionsQueue.sync {
|
let transportPeers: [TransportPeerSnapshot] = collectionsQueue.sync {
|
||||||
peerRegistry.transportSnapshots(selfNickname: myNickname)
|
peerRegistry.transportSnapshots(selfNickname: myNickname)
|
||||||
}
|
}
|
||||||
// Notify UI on MainActor via delegate
|
notifyUI { [weak self] in
|
||||||
Task { @MainActor [weak self] in
|
|
||||||
self?.peerEventsDelegate?.didUpdatePeerSnapshots(transportPeers)
|
self?.peerEventsDelegate?.didUpdatePeerSnapshots(transportPeers)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5536,6 +5833,7 @@ extension BLEService {
|
|||||||
// MARK: Consolidated Maintenance
|
// MARK: Consolidated Maintenance
|
||||||
|
|
||||||
private func performMaintenance() {
|
private func performMaintenance() {
|
||||||
|
guard !isPanicSuspended else { return }
|
||||||
maintenanceCounter += 1
|
maintenanceCounter += 1
|
||||||
lastMaintenanceAt = Date()
|
lastMaintenanceAt = Date()
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,9 @@ final class GeohashPresenceService: ObservableObject {
|
|||||||
|
|
||||||
private var subscriptions = Set<AnyCancellable>()
|
private var subscriptions = Set<AnyCancellable>()
|
||||||
private var heartbeatTimer: GeohashPresenceTimerProtocol?
|
private var heartbeatTimer: GeohashPresenceTimerProtocol?
|
||||||
|
private var pendingBroadcastTasks: [UUID: Task<Void, Never>] = [:]
|
||||||
|
private var heartbeatGeneration: UInt64 = 0
|
||||||
|
private var started = false
|
||||||
private let availableChannelsProvider: () -> [GeohashChannel]
|
private let availableChannelsProvider: () -> [GeohashChannel]
|
||||||
private let locationChanges: AnyPublisher<[GeohashChannel], Never>
|
private let locationChanges: AnyPublisher<[GeohashChannel], Never>
|
||||||
private let torReadyPublisher: AnyPublisher<Void, Never>
|
private let torReadyPublisher: AnyPublisher<Void, Never>
|
||||||
@@ -147,10 +150,25 @@ final class GeohashPresenceService: ObservableObject {
|
|||||||
|
|
||||||
/// Start the service (safe to call multiple times)
|
/// Start the service (safe to call multiple times)
|
||||||
func start() {
|
func start() {
|
||||||
|
guard !started else { return }
|
||||||
|
started = true
|
||||||
|
heartbeatGeneration &+= 1
|
||||||
SecureLogger.info("Presence: service starting...", category: .session)
|
SecureLogger.info("Presence: service starting...", category: .session)
|
||||||
scheduleNextHeartbeat()
|
scheduleNextHeartbeat()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Stops the timer and every decorrelation task synchronously at the panic
|
||||||
|
/// boundary. Generation checks also protect against custom sleepers that
|
||||||
|
/// ignore task cancellation and return later.
|
||||||
|
func stopForPanic() {
|
||||||
|
started = false
|
||||||
|
heartbeatGeneration &+= 1
|
||||||
|
heartbeatTimer?.invalidate()
|
||||||
|
heartbeatTimer = nil
|
||||||
|
pendingBroadcastTasks.values.forEach { $0.cancel() }
|
||||||
|
pendingBroadcastTasks.removeAll(keepingCapacity: false)
|
||||||
|
}
|
||||||
|
|
||||||
private func setupObservers() {
|
private func setupObservers() {
|
||||||
// Monitor location channel changes
|
// Monitor location channel changes
|
||||||
locationChanges
|
locationChanges
|
||||||
@@ -169,20 +187,26 @@ final class GeohashPresenceService: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleLocationChange() {
|
func handleLocationChange() {
|
||||||
|
guard started else { return }
|
||||||
// When location changes, we trigger an immediate (but slightly delayed) heartbeat
|
// When location changes, we trigger an immediate (but slightly delayed) heartbeat
|
||||||
// to announce presence in the new zone, then reset the loop.
|
// to announce presence in the new zone, then reset the loop.
|
||||||
SecureLogger.debug("Presence: location changed, scheduling update", category: .session)
|
SecureLogger.debug("Presence: location changed, scheduling update", category: .session)
|
||||||
heartbeatTimer?.invalidate()
|
heartbeatTimer?.invalidate()
|
||||||
|
|
||||||
// Small delay to allow location state to settle
|
// Small delay to allow location state to settle
|
||||||
|
let generation = heartbeatGeneration
|
||||||
heartbeatTimer = scheduleTimer(5.0) { [weak self] in
|
heartbeatTimer = scheduleTimer(5.0) { [weak self] in
|
||||||
Task { @MainActor [weak self] in
|
Task { @MainActor [weak self] in
|
||||||
self?.performHeartbeat()
|
guard let self,
|
||||||
|
self.started,
|
||||||
|
self.heartbeatGeneration == generation else { return }
|
||||||
|
self.performHeartbeat()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleConnectivityChange() {
|
func handleConnectivityChange() {
|
||||||
|
guard started else { return }
|
||||||
SecureLogger.debug("Presence: connectivity restored, triggering heartbeat", category: .session)
|
SecureLogger.debug("Presence: connectivity restored, triggering heartbeat", category: .session)
|
||||||
// If we were waiting for network, do it now
|
// If we were waiting for network, do it now
|
||||||
if heartbeatTimer == nil || !heartbeatTimer!.isValid {
|
if heartbeatTimer == nil || !heartbeatTimer!.isValid {
|
||||||
@@ -191,18 +215,29 @@ final class GeohashPresenceService: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func scheduleNextHeartbeat() {
|
func scheduleNextHeartbeat() {
|
||||||
|
guard started else { return }
|
||||||
heartbeatTimer?.invalidate()
|
heartbeatTimer?.invalidate()
|
||||||
let interval = TimeInterval.random(in: loopMinInterval...loopMaxInterval)
|
let interval = TimeInterval.random(in: loopMinInterval...loopMaxInterval)
|
||||||
|
let generation = heartbeatGeneration
|
||||||
heartbeatTimer = scheduleTimer(interval) { [weak self] in
|
heartbeatTimer = scheduleTimer(interval) { [weak self] in
|
||||||
Task { @MainActor [weak self] in
|
Task { @MainActor [weak self] in
|
||||||
self?.performHeartbeat()
|
guard let self,
|
||||||
|
self.started,
|
||||||
|
self.heartbeatGeneration == generation else { return }
|
||||||
|
self.performHeartbeat()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func performHeartbeat() {
|
func performHeartbeat() {
|
||||||
|
guard started else { return }
|
||||||
|
let generation = heartbeatGeneration
|
||||||
// Always schedule next loop first ensures continuity even if this one fails/skips
|
// Always schedule next loop first ensures continuity even if this one fails/skips
|
||||||
defer { scheduleNextHeartbeat() }
|
defer {
|
||||||
|
if started, heartbeatGeneration == generation {
|
||||||
|
scheduleNextHeartbeat()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 1. Check preconditions
|
// 1. Check preconditions
|
||||||
guard torIsReady() else {
|
guard torIsReady() else {
|
||||||
@@ -228,14 +263,27 @@ final class GeohashPresenceService: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Launch independent task for each channel's delay
|
// Launch independent task for each channel's delay
|
||||||
Task { @MainActor in
|
let taskID = UUID()
|
||||||
|
let sleeper = self.sleeper
|
||||||
|
let delay = TimeInterval.random(
|
||||||
|
in: burstMinDelay...burstMaxDelay
|
||||||
|
)
|
||||||
|
let nanoseconds = UInt64(delay * 1_000_000_000)
|
||||||
|
let task = Task { @MainActor [weak self] in
|
||||||
// Random delay for decorrelation
|
// Random delay for decorrelation
|
||||||
let delay = TimeInterval.random(in: self.burstMinDelay...self.burstMaxDelay)
|
await sleeper(nanoseconds)
|
||||||
let nanoseconds = UInt64(delay * 1_000_000_000)
|
|
||||||
await self.sleeper(nanoseconds)
|
guard let self else { return }
|
||||||
|
guard !Task.isCancelled,
|
||||||
|
self.started,
|
||||||
|
self.heartbeatGeneration == generation else {
|
||||||
|
self.pendingBroadcastTasks.removeValue(forKey: taskID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.pendingBroadcastTasks.removeValue(forKey: taskID)
|
||||||
self.broadcastPresence(for: channel.geohash)
|
self.broadcastPresence(for: channel.geohash)
|
||||||
}
|
}
|
||||||
|
pendingBroadcastTasks[taskID] = task
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,54 @@ import BitFoundation
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Security
|
import Security
|
||||||
|
|
||||||
|
enum KeychainInstallLifecycleAction: Equatable {
|
||||||
|
case markerPresent
|
||||||
|
case bootstrapMarker
|
||||||
|
case clearStaleKeys
|
||||||
|
case retryLater
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Process-local fail-closed gate for an unresolved install lifecycle.
|
||||||
|
///
|
||||||
|
/// A blocked caller may perform one synchronous reconciliation attempt.
|
||||||
|
/// Concurrent callers fail closed instead of reading while that cleanup is
|
||||||
|
/// in flight. Once reconciliation succeeds, access remains open.
|
||||||
|
final class KeychainInstallAccessGate: @unchecked Sendable {
|
||||||
|
private let lock = NSLock()
|
||||||
|
private var blocked = false
|
||||||
|
private var reconciliationInProgress = false
|
||||||
|
|
||||||
|
func block() {
|
||||||
|
lock.lock()
|
||||||
|
blocked = true
|
||||||
|
lock.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func allowsAccess(reconcile: () -> Bool) -> Bool {
|
||||||
|
lock.lock()
|
||||||
|
if !blocked {
|
||||||
|
lock.unlock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
guard !reconciliationInProgress else {
|
||||||
|
lock.unlock()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
reconciliationInProgress = true
|
||||||
|
lock.unlock()
|
||||||
|
|
||||||
|
let completed = reconcile()
|
||||||
|
|
||||||
|
lock.lock()
|
||||||
|
if completed {
|
||||||
|
blocked = false
|
||||||
|
}
|
||||||
|
reconciliationInProgress = false
|
||||||
|
lock.unlock()
|
||||||
|
return completed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final class KeychainManager: KeychainManagerProtocol {
|
final class KeychainManager: KeychainManagerProtocol {
|
||||||
/// Default keychain for components that construct their own rather than
|
/// Default keychain for components that construct their own rather than
|
||||||
/// having one injected. Under test this is an in-memory keychain: the
|
/// having one injected. Under test this is an in-memory keychain: the
|
||||||
@@ -41,53 +89,281 @@ final class KeychainManager: KeychainManagerProtocol {
|
|||||||
// Use consistent service name for all keychain items
|
// Use consistent service name for all keychain items
|
||||||
private let service = BitchatApp.bundleID
|
private let service = BitchatApp.bundleID
|
||||||
private let appGroup = "group.\(BitchatApp.bundleID)"
|
private let appGroup = "group.\(BitchatApp.bundleID)"
|
||||||
|
#if os(iOS)
|
||||||
|
private let installAccessGate = KeychainInstallAccessGate()
|
||||||
|
#endif
|
||||||
|
/// Every generic-password service owned by this app, including names used
|
||||||
|
/// by older releases. Keep custom services here so one-time security
|
||||||
|
/// migrations and panic deletion cannot silently miss them.
|
||||||
|
private static let additionalApplicationOwnedServices = [
|
||||||
|
"chat.bitchat.nostr",
|
||||||
|
"chat.bitchat.favorites",
|
||||||
|
"chat.bitchat.outbox",
|
||||||
|
"com.bitchat.passwords",
|
||||||
|
"com.bitchat.deviceidentity",
|
||||||
|
"com.bitchat.noise.identity",
|
||||||
|
"chat.bitchat.passwords",
|
||||||
|
"bitchat.keychain",
|
||||||
|
"bitchat",
|
||||||
|
"com.bitchat"
|
||||||
|
]
|
||||||
// AfterFirstUnlock, not WhenUnlocked: the mesh keeps running with the
|
// AfterFirstUnlock, not WhenUnlocked: the mesh keeps running with the
|
||||||
// device locked (identity-cache saves failed with -25308 throughout
|
// device locked (identity-cache saves failed with -25308 throughout
|
||||||
// locked-phone testing), and a wake-on-proximity relaunch via BLE state
|
// locked-phone testing), and a wake-on-proximity relaunch via BLE state
|
||||||
// restoration must be able to read the noise keys before the user
|
// restoration must be able to read the noise keys before the user
|
||||||
// unlocks. Backup/sync semantics are unchanged (not ThisDeviceOnly).
|
// unlocks. ThisDeviceOnly prevents private identities and group keys from
|
||||||
private static let itemAccessibility = kSecAttrAccessibleAfterFirstUnlock
|
// migrating through device backups onto a second device.
|
||||||
|
private static let itemAccessibility = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
migrateAccessibilityIfNeeded()
|
if reconcileInstallLifecycle() {
|
||||||
|
migrateAccessibilityIfNeeded()
|
||||||
|
} else {
|
||||||
|
installAccessGate.block()
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func installLifecycleAction(
|
||||||
|
containerKnowsMarker: Bool,
|
||||||
|
cleanupPending: Bool = false,
|
||||||
|
markerRead: KeychainReadResult
|
||||||
|
) -> KeychainInstallLifecycleAction {
|
||||||
|
// Once a reinstall cleanup has started, its container-local latch
|
||||||
|
// must win even if the keychain marker was deleted before a later
|
||||||
|
// keychain operation failed. Otherwise the next launch could mistake
|
||||||
|
// a partial cleanup for a fresh bootstrap and preserve stale secrets.
|
||||||
|
if cleanupPending {
|
||||||
|
return .clearStaleKeys
|
||||||
|
}
|
||||||
|
|
||||||
|
switch markerRead {
|
||||||
|
case .success:
|
||||||
|
return containerKnowsMarker ? .markerPresent : .clearStaleKeys
|
||||||
|
case .itemNotFound:
|
||||||
|
return .bootstrapMarker
|
||||||
|
case .accessDenied, .deviceLocked, .authenticationFailed, .otherError:
|
||||||
|
return .retryLater
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static func applicationOwnedKeychainServices(primaryService: String) -> [String] {
|
||||||
|
var seen = Set<String>()
|
||||||
|
return ([primaryService] + additionalApplicationOwnedServices).filter {
|
||||||
|
seen.insert($0).inserted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs every service update even after one failure. Successful updates
|
||||||
|
/// are idempotent, while returning false keeps the one-time flag unset so
|
||||||
|
/// a later unlocked launch retries the incomplete migration.
|
||||||
|
static func migrateAccessibilityForApplicationOwnedServices(
|
||||||
|
primaryService: String,
|
||||||
|
updateService: (String) -> OSStatus
|
||||||
|
) -> Bool {
|
||||||
|
var completed = true
|
||||||
|
for serviceName in applicationOwnedKeychainServices(
|
||||||
|
primaryService: primaryService
|
||||||
|
) {
|
||||||
|
let status = updateService(serviceName)
|
||||||
|
if status != errSecSuccess && status != errSecItemNotFound {
|
||||||
|
completed = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return completed
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deletes every declared service even after one failure. An empty scope
|
||||||
|
/// is already clean, while any other status leaves the cleanup
|
||||||
|
/// incomplete so its durable retry marker remains set.
|
||||||
|
static func deleteApplicationOwnedKeychainServices(
|
||||||
|
primaryService: String,
|
||||||
|
deleteService: (String) -> OSStatus
|
||||||
|
) -> Bool {
|
||||||
|
var completed = true
|
||||||
|
for serviceName in applicationOwnedKeychainServices(
|
||||||
|
primaryService: primaryService
|
||||||
|
) {
|
||||||
|
let status = deleteService(serviceName)
|
||||||
|
if status != errSecSuccess && status != errSecItemNotFound {
|
||||||
|
completed = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return completed
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The app currently has an application-group entitlement, not a
|
||||||
|
/// keychain-access-group entitlement. Keep the historical group cleanup
|
||||||
|
/// probe as best effort without making its expected -34018 response block
|
||||||
|
/// panic recovery forever.
|
||||||
|
static func completedApplicationGroupDelete(status: OSStatus) -> Bool {
|
||||||
|
status == errSecSuccess
|
||||||
|
|| status == errSecItemNotFound
|
||||||
|
|| status == -34018
|
||||||
|
}
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
|
|
||||||
|
private static let installMarkerAccount = "install_lifecycle_marker"
|
||||||
|
private static let installMarkerDefaultsKey = "keychain.installLifecycleMarker.present"
|
||||||
|
private static let installCleanupPendingDefaultsKey =
|
||||||
|
"keychain.installLifecycleCleanup.pending"
|
||||||
|
|
||||||
|
/// Keychain items can survive app removal while the app container and its
|
||||||
|
/// UserDefaults do not. The first version carrying this marker bootstraps
|
||||||
|
/// without deleting existing users' identities. On a later reinstall, a
|
||||||
|
/// surviving keychain marker plus a missing defaults marker proves the app
|
||||||
|
/// container was replaced, so stale secrets are removed before use.
|
||||||
|
@discardableResult
|
||||||
|
private func reconcileInstallLifecycle() -> Bool {
|
||||||
|
let defaults = UserDefaults.standard
|
||||||
|
let containerKnowsMarker = defaults.bool(forKey: Self.installMarkerDefaultsKey)
|
||||||
|
let cleanupPending = defaults.bool(
|
||||||
|
forKey: Self.installCleanupPendingDefaultsKey
|
||||||
|
)
|
||||||
|
|
||||||
|
let markerRead = retrieveDataWithResult(forKey: Self.installMarkerAccount)
|
||||||
|
switch Self.installLifecycleAction(
|
||||||
|
containerKnowsMarker: containerKnowsMarker,
|
||||||
|
cleanupPending: cleanupPending,
|
||||||
|
markerRead: markerRead
|
||||||
|
) {
|
||||||
|
case .markerPresent:
|
||||||
|
defaults.set(true, forKey: Self.installMarkerDefaultsKey)
|
||||||
|
return true
|
||||||
|
|
||||||
|
case .bootstrapMarker:
|
||||||
|
if case .success = saveDataWithResult(Data([1]), forKey: Self.installMarkerAccount) {
|
||||||
|
defaults.set(true, forKey: Self.installMarkerDefaultsKey)
|
||||||
|
}
|
||||||
|
// A missing marker is the intentional bootstrap path for both a
|
||||||
|
// fresh install and the first marker-carrying upgrade. Preserve
|
||||||
|
// existing users' identities even if marker creation must retry
|
||||||
|
// on a later construction.
|
||||||
|
return true
|
||||||
|
|
||||||
|
case .clearStaleKeys:
|
||||||
|
// Establish a container-local retry latch before deleting the
|
||||||
|
// surviving keychain marker. If the process exits or any keychain
|
||||||
|
// operation fails, the next launch retries even when that marker
|
||||||
|
// can no longer be read.
|
||||||
|
defaults.set(true, forKey: Self.installCleanupPendingDefaultsKey)
|
||||||
|
guard defaults.synchronize(),
|
||||||
|
defaults.bool(forKey: Self.installCleanupPendingDefaultsKey)
|
||||||
|
else {
|
||||||
|
SecureLogger.error(
|
||||||
|
"Could not persist reinstall keychain-cleanup intent",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
guard deleteAllKeychainData() else {
|
||||||
|
SecureLogger.error(
|
||||||
|
"Reinstall keychain cleanup incomplete; retry remains pending",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
defaults.set(true, forKey: Self.installMarkerDefaultsKey)
|
||||||
|
defaults.removeObject(
|
||||||
|
forKey: Self.installCleanupPendingDefaultsKey
|
||||||
|
)
|
||||||
|
guard defaults.synchronize(),
|
||||||
|
defaults.bool(forKey: Self.installMarkerDefaultsKey),
|
||||||
|
!defaults.bool(
|
||||||
|
forKey: Self.installCleanupPendingDefaultsKey
|
||||||
|
)
|
||||||
|
else {
|
||||||
|
// Preserve the fail-closed state in memory and make one more
|
||||||
|
// best-effort persistence attempt before startup continues.
|
||||||
|
defaults.set(
|
||||||
|
true,
|
||||||
|
forKey: Self.installCleanupPendingDefaultsKey
|
||||||
|
)
|
||||||
|
_ = defaults.synchronize()
|
||||||
|
SecureLogger.error(
|
||||||
|
"Could not commit reinstall keychain-cleanup state; retry remains pending",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
|
||||||
|
case .retryLater:
|
||||||
|
// Do not guess that a temporarily unreadable marker is absent.
|
||||||
|
// An established container may keep using ordinary protected-data
|
||||||
|
// semantics: reads fail while locked and recover after unlock. A
|
||||||
|
// container that has not committed the marker must stay blocked
|
||||||
|
// until the marker becomes readable and this state machine can
|
||||||
|
// distinguish bootstrap from reinstall.
|
||||||
|
return containerKnowsMarker
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// One-time upgrade of items created under WhenUnlocked. New saves get
|
/// One-time upgrade of items created under WhenUnlocked. New saves get
|
||||||
/// the right class on their own (saves are delete-then-add), but the
|
/// the right class on their own (saves are delete-then-add), but the
|
||||||
/// long-lived identity keys are written once and would otherwise stay
|
/// long-lived identity keys are written once and would otherwise stay
|
||||||
/// unreadable while the device is locked.
|
/// unreadable while the device is locked.
|
||||||
private func migrateAccessibilityIfNeeded() {
|
private func migrateAccessibilityIfNeeded() {
|
||||||
let flag = "keychain.accessibility.afterFirstUnlock.migrated"
|
let flag = "keychain.accessibility.afterFirstUnlockThisDeviceOnly.migrated"
|
||||||
guard !UserDefaults.standard.bool(forKey: flag) else { return }
|
guard !UserDefaults.standard.bool(forKey: flag) else { return }
|
||||||
|
|
||||||
let query: [String: Any] = [
|
|
||||||
kSecClass as String: kSecClassGenericPassword,
|
|
||||||
kSecAttrService as String: service
|
|
||||||
]
|
|
||||||
let update: [String: Any] = [
|
let update: [String: Any] = [
|
||||||
kSecAttrAccessible as String: Self.itemAccessibility
|
kSecAttrAccessible as String: Self.itemAccessibility
|
||||||
]
|
]
|
||||||
let status = SecItemUpdate(query as CFDictionary, update as CFDictionary)
|
let completed = Self.migrateAccessibilityForApplicationOwnedServices(
|
||||||
switch status {
|
primaryService: service
|
||||||
case errSecSuccess, errSecItemNotFound:
|
) { serviceName in
|
||||||
// Nothing to migrate on a fresh install; both are terminal.
|
let query: [String: Any] = [
|
||||||
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
|
kSecAttrService as String: serviceName
|
||||||
|
]
|
||||||
|
return SecItemUpdate(
|
||||||
|
query as CFDictionary,
|
||||||
|
update as CFDictionary
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if completed {
|
||||||
|
// Missing services on a fresh install are terminal, but the flag is
|
||||||
|
// set only after every application-owned service was considered.
|
||||||
UserDefaults.standard.set(true, forKey: flag)
|
UserDefaults.standard.set(true, forKey: flag)
|
||||||
SecureLogger.info("Keychain accessibility migrated to AfterFirstUnlock (status \(status))", category: .keychain)
|
SecureLogger.info(
|
||||||
default:
|
"Keychain accessibility migrated to AfterFirstUnlockThisDeviceOnly",
|
||||||
|
category: .keychain
|
||||||
|
)
|
||||||
|
} else {
|
||||||
// Likely errSecInteractionNotAllowed (relaunched while locked) —
|
// Likely errSecInteractionNotAllowed (relaunched while locked) —
|
||||||
// leave the flag unset so the next launch retries.
|
// leave the flag unset so the next launch retries.
|
||||||
SecureLogger.warning("Keychain accessibility migration deferred (status \(status))", category: .keychain)
|
SecureLogger.warning(
|
||||||
|
"Keychain accessibility migration deferred for at least one application-owned service",
|
||||||
|
category: .keychain
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
private func installAccessAllowed() -> Bool {
|
||||||
|
#if os(iOS)
|
||||||
|
return installAccessGate.allowsAccess { [self] in
|
||||||
|
guard reconcileInstallLifecycle() else { return false }
|
||||||
|
migrateAccessibilityIfNeeded()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
return true
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Identity Keys
|
// MARK: - Identity Keys
|
||||||
|
|
||||||
func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool {
|
func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool {
|
||||||
|
guard installAccessAllowed() else {
|
||||||
|
SecureLogger.logKeyOperation(.save, keyType: key, success: false)
|
||||||
|
return false
|
||||||
|
}
|
||||||
let fullKey = "identity_\(key)"
|
let fullKey = "identity_\(key)"
|
||||||
let result = saveData(keyData, forKey: fullKey)
|
let result = saveData(keyData, forKey: fullKey)
|
||||||
SecureLogger.logKeyOperation(.save, keyType: key, success: result)
|
SecureLogger.logKeyOperation(.save, keyType: key, success: result)
|
||||||
@@ -95,11 +371,16 @@ final class KeychainManager: KeychainManagerProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getIdentityKey(forKey key: String) -> Data? {
|
func getIdentityKey(forKey key: String) -> Data? {
|
||||||
|
guard installAccessAllowed() else { return nil }
|
||||||
let fullKey = "identity_\(key)"
|
let fullKey = "identity_\(key)"
|
||||||
return retrieveData(forKey: fullKey)
|
return retrieveData(forKey: fullKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteIdentityKey(forKey key: String) -> Bool {
|
func deleteIdentityKey(forKey key: String) -> Bool {
|
||||||
|
guard installAccessAllowed() else {
|
||||||
|
SecureLogger.logKeyOperation(.delete, keyType: key, success: false)
|
||||||
|
return false
|
||||||
|
}
|
||||||
let result = delete(forKey: "identity_\(key)")
|
let result = delete(forKey: "identity_\(key)")
|
||||||
SecureLogger.logKeyOperation(.delete, keyType: key, success: result)
|
SecureLogger.logKeyOperation(.delete, keyType: key, success: result)
|
||||||
return result
|
return result
|
||||||
@@ -110,12 +391,14 @@ final class KeychainManager: KeychainManagerProtocol {
|
|||||||
/// Get identity key with detailed result for proper error handling
|
/// Get identity key with detailed result for proper error handling
|
||||||
/// Distinguishes between missing keys (expected) and critical failures
|
/// Distinguishes between missing keys (expected) and critical failures
|
||||||
func getIdentityKeyWithResult(forKey key: String) -> KeychainReadResult {
|
func getIdentityKeyWithResult(forKey key: String) -> KeychainReadResult {
|
||||||
|
guard installAccessAllowed() else { return .accessDenied }
|
||||||
let fullKey = "identity_\(key)"
|
let fullKey = "identity_\(key)"
|
||||||
return retrieveDataWithResult(forKey: fullKey)
|
return retrieveDataWithResult(forKey: fullKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Save identity key with detailed result and retry logic for transient errors
|
/// Save identity key with detailed result and retry logic for transient errors
|
||||||
func saveIdentityKeyWithResult(_ keyData: Data, forKey key: String) -> KeychainSaveResult {
|
func saveIdentityKeyWithResult(_ keyData: Data, forKey key: String) -> KeychainSaveResult {
|
||||||
|
guard installAccessAllowed() else { return .accessDenied }
|
||||||
let fullKey = "identity_\(key)"
|
let fullKey = "identity_\(key)"
|
||||||
return saveDataWithResult(keyData, forKey: fullKey)
|
return saveDataWithResult(keyData, forKey: fullKey)
|
||||||
}
|
}
|
||||||
@@ -385,114 +668,165 @@ final class KeychainManager: KeychainManagerProtocol {
|
|||||||
// Delete ALL keychain data for panic mode
|
// Delete ALL keychain data for panic mode
|
||||||
func deleteAllKeychainData() -> Bool {
|
func deleteAllKeychainData() -> Bool {
|
||||||
SecureLogger.warning("Panic mode - deleting all keychain data", category: .security)
|
SecureLogger.warning("Panic mode - deleting all keychain data", category: .security)
|
||||||
|
|
||||||
var totalDeleted = 0
|
let ownedServices = Set(
|
||||||
|
Self.applicationOwnedKeychainServices(
|
||||||
// Search without service restriction to catch all items
|
primaryService: service
|
||||||
|
)
|
||||||
|
)
|
||||||
|
var enumerationCompleted = true
|
||||||
let searchQuery: [String: Any] = [
|
let searchQuery: [String: Any] = [
|
||||||
kSecClass as String: kSecClassGenericPassword,
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
kSecMatchLimit as String: kSecMatchLimitAll,
|
kSecMatchLimit as String: kSecMatchLimitAll,
|
||||||
kSecReturnAttributes as String: true
|
kSecReturnAttributes as String: true
|
||||||
]
|
]
|
||||||
|
|
||||||
var result: AnyObject?
|
var result: AnyObject?
|
||||||
let searchStatus = SecItemCopyMatching(searchQuery as CFDictionary, &result)
|
let searchStatus = SecItemCopyMatching(
|
||||||
|
searchQuery as CFDictionary,
|
||||||
if searchStatus == errSecSuccess, let items = result as? [[String: Any]] {
|
&result
|
||||||
|
)
|
||||||
|
switch searchStatus {
|
||||||
|
case errSecSuccess:
|
||||||
|
guard let items = result as? [[String: Any]] else {
|
||||||
|
enumerationCompleted = false
|
||||||
|
SecureLogger.error(
|
||||||
|
"Unable to decode application-owned keychain inventory",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve the access-group sweep for custom services that are
|
||||||
|
// not yet in the declared legacy-service list.
|
||||||
for item in items {
|
for item in items {
|
||||||
var shouldDelete = false
|
let account =
|
||||||
let account = item[kSecAttrAccount as String] as? String ?? ""
|
item[kSecAttrAccount as String] as? String ?? ""
|
||||||
let service = item[kSecAttrService as String] as? String ?? ""
|
let itemService =
|
||||||
let accessGroup = item[kSecAttrAccessGroup as String] as? String
|
item[kSecAttrService as String] as? String ?? ""
|
||||||
|
let accessGroup =
|
||||||
// More precise deletion criteria:
|
item[kSecAttrAccessGroup as String] as? String
|
||||||
// 1. Check for our specific app group
|
guard accessGroup == appGroup
|
||||||
// 2. OR check for our exact service name
|
|| ownedServices.contains(itemService)
|
||||||
// 3. OR check for known legacy service names
|
else {
|
||||||
if accessGroup == appGroup {
|
continue
|
||||||
shouldDelete = true
|
|
||||||
} else if service == self.service {
|
|
||||||
shouldDelete = true
|
|
||||||
} else if [
|
|
||||||
"com.bitchat.passwords",
|
|
||||||
"com.bitchat.deviceidentity",
|
|
||||||
"com.bitchat.noise.identity",
|
|
||||||
"chat.bitchat.passwords",
|
|
||||||
"bitchat.keychain",
|
|
||||||
"bitchat",
|
|
||||||
"com.bitchat"
|
|
||||||
].contains(service) {
|
|
||||||
shouldDelete = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if shouldDelete {
|
var deleteQuery: [String: Any] = [
|
||||||
// Build delete query with all available attributes for precise deletion
|
kSecClass as String: kSecClassGenericPassword
|
||||||
var deleteQuery: [String: Any] = [
|
]
|
||||||
kSecClass as String: kSecClassGenericPassword
|
if !account.isEmpty {
|
||||||
]
|
deleteQuery[kSecAttrAccount as String] = account
|
||||||
|
}
|
||||||
if !account.isEmpty {
|
if !itemService.isEmpty {
|
||||||
deleteQuery[kSecAttrAccount as String] = account
|
deleteQuery[kSecAttrService as String] = itemService
|
||||||
}
|
}
|
||||||
if !service.isEmpty {
|
if let accessGroup,
|
||||||
deleteQuery[kSecAttrService as String] = service
|
!accessGroup.isEmpty,
|
||||||
}
|
accessGroup != "test" {
|
||||||
|
deleteQuery[kSecAttrAccessGroup as String] = accessGroup
|
||||||
// Add access group if present
|
}
|
||||||
if let accessGroup = item[kSecAttrAccessGroup as String] as? String,
|
|
||||||
!accessGroup.isEmpty && accessGroup != "test" {
|
let status = SecItemDelete(deleteQuery as CFDictionary)
|
||||||
deleteQuery[kSecAttrAccessGroup as String] = accessGroup
|
if status != errSecSuccess && status != errSecItemNotFound {
|
||||||
}
|
enumerationCompleted = false
|
||||||
|
SecureLogger.error(
|
||||||
let deleteStatus = SecItemDelete(deleteQuery as CFDictionary)
|
NSError(domain: "Keychain", code: Int(status)),
|
||||||
if deleteStatus == errSecSuccess {
|
context: "Unable to delete enumerated application-owned keychain item",
|
||||||
totalDeleted += 1
|
category: .keychain
|
||||||
SecureLogger.info("Deleted keychain item: \(account) from \(service)", category: .keychain)
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case errSecItemNotFound:
|
||||||
|
break
|
||||||
|
|
||||||
|
default:
|
||||||
|
enumerationCompleted = false
|
||||||
|
SecureLogger.error(
|
||||||
|
NSError(domain: "Keychain", code: Int(searchStatus)),
|
||||||
|
context: "Unable to enumerate application-owned keychain items",
|
||||||
|
category: .keychain
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also try to delete by known service names and app group
|
// Bulk deletion by every application-owned service is authoritative
|
||||||
// This catches any items that might have been missed above
|
// and idempotent. It also verifies that every known service scope is
|
||||||
let knownServices = [
|
// empty even when the inventory pass found no items.
|
||||||
self.service, // Current service name
|
let servicesCompleted =
|
||||||
"com.bitchat.passwords",
|
Self.deleteApplicationOwnedKeychainServices(
|
||||||
"com.bitchat.deviceidentity",
|
primaryService: service
|
||||||
"com.bitchat.noise.identity",
|
) { serviceName in
|
||||||
"chat.bitchat.passwords",
|
let query: [String: Any] = [
|
||||||
"chat.bitchat.nostr",
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
"bitchat.keychain",
|
kSecAttrService as String: serviceName
|
||||||
"bitchat",
|
]
|
||||||
"com.bitchat"
|
let status = SecItemDelete(query as CFDictionary)
|
||||||
]
|
if status != errSecSuccess && status != errSecItemNotFound {
|
||||||
|
SecureLogger.error(
|
||||||
for serviceName in knownServices {
|
NSError(domain: "Keychain", code: Int(status)),
|
||||||
let query: [String: Any] = [
|
context: "Unable to delete application-owned keychain service \(serviceName)",
|
||||||
kSecClass as String: kSecClassGenericPassword,
|
category: .keychain
|
||||||
kSecAttrService as String: serviceName
|
)
|
||||||
]
|
}
|
||||||
|
return status
|
||||||
let status = SecItemDelete(query as CFDictionary)
|
|
||||||
if status == errSecSuccess {
|
|
||||||
totalDeleted += 1
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// Historical builds attempted this application-group identifier as a
|
||||||
// Also delete by app group to ensure complete cleanup
|
// keychain access group. It is not currently entitled, so -34018
|
||||||
|
// means the scope is inapplicable rather than partially deleted.
|
||||||
let groupQuery: [String: Any] = [
|
let groupQuery: [String: Any] = [
|
||||||
kSecClass as String: kSecClassGenericPassword,
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
kSecAttrAccessGroup as String: appGroup
|
kSecAttrAccessGroup as String: appGroup
|
||||||
]
|
]
|
||||||
|
|
||||||
let groupStatus = SecItemDelete(groupQuery as CFDictionary)
|
let groupStatus = SecItemDelete(groupQuery as CFDictionary)
|
||||||
if groupStatus == errSecSuccess {
|
let groupCompleted = Self.completedApplicationGroupDelete(
|
||||||
totalDeleted += 1
|
status: groupStatus
|
||||||
|
)
|
||||||
|
if !groupCompleted {
|
||||||
|
SecureLogger.error(
|
||||||
|
NSError(domain: "Keychain", code: Int(groupStatus)),
|
||||||
|
context: "Unable to delete historical application-group keychain items",
|
||||||
|
category: .keychain
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
SecureLogger.warning("Panic mode cleanup completed. Total items deleted: \(totalDeleted)", category: .keychain)
|
var markerCompleted = true
|
||||||
|
#if os(iOS)
|
||||||
return totalDeleted > 0
|
// The non-secret marker is intentionally recreated after a panic so a
|
||||||
|
// later uninstall/reinstall can still be distinguished from an in-place
|
||||||
|
// upgrade. Do not commit the container-side marker here: reinstall
|
||||||
|
// reconciliation may still need to retry an incomplete cleanup.
|
||||||
|
if case .success = saveDataWithResult(
|
||||||
|
Data([1]),
|
||||||
|
forKey: Self.installMarkerAccount
|
||||||
|
) {
|
||||||
|
markerCompleted = true
|
||||||
|
} else {
|
||||||
|
markerCompleted = false
|
||||||
|
SecureLogger.error(
|
||||||
|
"Unable to restore install-lifecycle keychain marker",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
let completed =
|
||||||
|
enumerationCompleted
|
||||||
|
&& servicesCompleted
|
||||||
|
&& groupCompleted
|
||||||
|
&& markerCompleted
|
||||||
|
if completed {
|
||||||
|
SecureLogger.warning(
|
||||||
|
"Panic mode keychain cleanup completed",
|
||||||
|
category: .keychain
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
SecureLogger.error(
|
||||||
|
"Panic mode keychain cleanup incomplete",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return completed
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Security Utilities
|
// MARK: - Security Utilities
|
||||||
@@ -518,6 +852,7 @@ final class KeychainManager: KeychainManagerProtocol {
|
|||||||
// MARK: - Debug
|
// MARK: - Debug
|
||||||
|
|
||||||
func verifyIdentityKeyExists() -> Bool {
|
func verifyIdentityKeyExists() -> Bool {
|
||||||
|
guard installAccessAllowed() else { return false }
|
||||||
let key = "identity_noiseStaticKey"
|
let key = "identity_noiseStaticKey"
|
||||||
return retrieveData(forKey: key) != nil
|
return retrieveData(forKey: key) != nil
|
||||||
}
|
}
|
||||||
@@ -526,18 +861,40 @@ final class KeychainManager: KeychainManagerProtocol {
|
|||||||
|
|
||||||
/// Save data with a custom service name
|
/// Save data with a custom service name
|
||||||
func save(key: String, data: Data, service customService: String, accessible: CFString?) {
|
func save(key: String, data: Data, service customService: String, accessible: CFString?) {
|
||||||
var query: [String: Any] = [
|
guard installAccessAllowed() else { return }
|
||||||
|
let primaryKeyQuery: [String: Any] = [
|
||||||
kSecClass as String: kSecClassGenericPassword,
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
kSecAttrService as String: customService,
|
kSecAttrService as String: customService,
|
||||||
kSecAttrAccount as String: key,
|
kSecAttrAccount as String: key
|
||||||
kSecValueData as String: data
|
|
||||||
]
|
]
|
||||||
if let accessible = accessible {
|
var addQuery = primaryKeyQuery
|
||||||
query[kSecAttrAccessible as String] = accessible
|
addQuery.merge([
|
||||||
}
|
kSecValueData as String: data,
|
||||||
|
kSecAttrAccessible as String: accessible ?? Self.itemAccessibility,
|
||||||
|
kSecAttrSynchronizable as String: false
|
||||||
|
]) { _, new in new }
|
||||||
|
|
||||||
SecItemDelete(query as CFDictionary)
|
// Delete by the item's primary key only. Value/accessibility fields
|
||||||
SecItemAdd(query as CFDictionary, nil)
|
// are add attributes, not valid selectors for replacing an existing
|
||||||
|
// item; including them can leave the old item in place and make the
|
||||||
|
// subsequent add fail as a duplicate.
|
||||||
|
let deleteStatus = SecItemDelete(primaryKeyQuery as CFDictionary)
|
||||||
|
guard deleteStatus == errSecSuccess || deleteStatus == errSecItemNotFound else {
|
||||||
|
SecureLogger.error(
|
||||||
|
NSError(domain: "Keychain", code: Int(deleteStatus)),
|
||||||
|
context: "Unable to replace custom-service keychain item",
|
||||||
|
category: .keychain
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let addStatus = SecItemAdd(addQuery as CFDictionary, nil)
|
||||||
|
if addStatus != errSecSuccess {
|
||||||
|
SecureLogger.error(
|
||||||
|
NSError(domain: "Keychain", code: Int(addStatus)),
|
||||||
|
context: "Unable to save custom-service keychain item",
|
||||||
|
category: .keychain
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load data from a custom service
|
/// Load data from a custom service
|
||||||
@@ -551,6 +908,7 @@ final class KeychainManager: KeychainManagerProtocol {
|
|||||||
/// Load custom-service data without collapsing `itemNotFound` and
|
/// Load custom-service data without collapsing `itemNotFound` and
|
||||||
/// protected-data/keychain failures into the same nil result.
|
/// protected-data/keychain failures into the same nil result.
|
||||||
func loadWithResult(key: String, service customService: String) -> KeychainReadResult {
|
func loadWithResult(key: String, service customService: String) -> KeychainReadResult {
|
||||||
|
guard installAccessAllowed() else { return .accessDenied }
|
||||||
let query: [String: Any] = [
|
let query: [String: Any] = [
|
||||||
kSecClass as String: kSecClassGenericPassword,
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
kSecAttrService as String: customService,
|
kSecAttrService as String: customService,
|
||||||
@@ -565,6 +923,7 @@ final class KeychainManager: KeychainManagerProtocol {
|
|||||||
|
|
||||||
/// Delete data from a custom service
|
/// Delete data from a custom service
|
||||||
func delete(key: String, service customService: String) {
|
func delete(key: String, service customService: String) {
|
||||||
|
guard installAccessAllowed() else { return }
|
||||||
let query: [String: Any] = [
|
let query: [String: Any] = [
|
||||||
kSecClass as String: kSecClassGenericPassword,
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
kSecAttrService as String: customService,
|
kSecAttrService as String: customService,
|
||||||
@@ -576,6 +935,7 @@ final class KeychainManager: KeychainManagerProtocol {
|
|||||||
|
|
||||||
/// Delete every item stored under a custom service
|
/// Delete every item stored under a custom service
|
||||||
func deleteAll(service customService: String) {
|
func deleteAll(service customService: String) {
|
||||||
|
guard installAccessAllowed() else { return }
|
||||||
let query: [String: Any] = [
|
let query: [String: Any] = [
|
||||||
kSecClass as String: kSecClassGenericPassword,
|
kSecClass as String: kSecClassGenericPassword,
|
||||||
kSecAttrService as String: customService,
|
kSecAttrService as String: customService,
|
||||||
|
|||||||
@@ -251,9 +251,9 @@ final class MessageFormattingEngine {
|
|||||||
isSelf: Bool,
|
isSelf: Bool,
|
||||||
isMentioned: Bool
|
isMentioned: Bool
|
||||||
) -> AttributedString {
|
) -> AttributedString {
|
||||||
// For very long content without special tokens, use plain formatting
|
// For very long content, use plain formatting to avoid expensive
|
||||||
let containsCashu = containsCashuToken(content)
|
// regex/detector work. Cashu presence must not disable this guard.
|
||||||
if (content.count > 4000 || content.hasVeryLongToken(threshold: 1024)) && !containsCashu {
|
if content.isOversizedForRichFormatting() {
|
||||||
return formatPlainContent(content, baseColor: baseColor, isSelf: isSelf)
|
return formatPlainContent(content, baseColor: baseColor, isSelf: isSelf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -154,6 +154,19 @@ final class NetworkActivationService: ObservableObject {
|
|||||||
.store(in: &cancellables)
|
.store(in: &cancellables)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Stops all internet-facing work at the synchronous panic boundary.
|
||||||
|
/// `start()` may be called again only after the full wipe commits.
|
||||||
|
func stopForPanic() {
|
||||||
|
cancellables.removeAll()
|
||||||
|
started = false
|
||||||
|
reachabilityMonitor.stop()
|
||||||
|
activationAllowed = false
|
||||||
|
torAutoStartDesired = false
|
||||||
|
relayController.disconnect()
|
||||||
|
torController.setAutoStartAllowed(false)
|
||||||
|
applyTorState(torDesired: false)
|
||||||
|
}
|
||||||
|
|
||||||
func setUserTorEnabled(_ enabled: Bool) {
|
func setUserTorEnabled(_ enabled: Bool) {
|
||||||
guard enabled != userTorEnabled else { return }
|
guard enabled != userTorEnabled else { return }
|
||||||
userTorEnabled = enabled
|
userTorEnabled = enabled
|
||||||
@@ -167,6 +180,7 @@ final class NetworkActivationService: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func reevaluate() {
|
private func reevaluate() {
|
||||||
|
guard started else { return }
|
||||||
let allowed = effectiveAllowed()
|
let allowed = effectiveAllowed()
|
||||||
let torDesired = allowed && userTorEnabled
|
let torDesired = allowed && userTorEnabled
|
||||||
let statusChanged = allowed != activationAllowed
|
let statusChanged = allowed != activationAllowed
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ protocol NetworkReachabilityMonitoring: AnyObject {
|
|||||||
var reachabilityPublisher: AnyPublisher<Bool, Never> { get }
|
var reachabilityPublisher: AnyPublisher<Bool, Never> { get }
|
||||||
/// Begin monitoring. Idempotent.
|
/// Begin monitoring. Idempotent.
|
||||||
func start()
|
func start()
|
||||||
|
/// Stop monitoring and discard pending debounce work. Idempotent.
|
||||||
|
func stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pure debounce/decision logic for reachability, split out so it can be
|
/// Pure debounce/decision logic for reachability, split out so it can be
|
||||||
@@ -88,18 +90,6 @@ struct ReachabilityDebounce {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Always-reachable stub. Used as the default in tests and as the fallback on
|
|
||||||
/// platforms without the Network framework, so reachability never suppresses
|
|
||||||
/// startup by itself.
|
|
||||||
@MainActor
|
|
||||||
final class AlwaysReachableMonitor: NetworkReachabilityMonitoring {
|
|
||||||
var isReachable: Bool { true }
|
|
||||||
var reachabilityPublisher: AnyPublisher<Bool, Never> {
|
|
||||||
Empty(completeImmediately: false).eraseToAnyPublisher()
|
|
||||||
}
|
|
||||||
func start() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `NWPathMonitor`-backed reachability. All state lives on the main actor; the
|
/// `NWPathMonitor`-backed reachability. All state lives on the main actor; the
|
||||||
/// background path callback hops here before touching the debounce.
|
/// background path callback hops here before touching the debounce.
|
||||||
@MainActor
|
@MainActor
|
||||||
@@ -146,6 +136,18 @@ final class NWPathReachabilityMonitor: NetworkReachabilityMonitoring {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func stop() {
|
||||||
|
guard started else { return }
|
||||||
|
started = false
|
||||||
|
flushWorkItem?.cancel()
|
||||||
|
flushWorkItem = nil
|
||||||
|
#if canImport(Network)
|
||||||
|
monitor?.pathUpdateHandler = nil
|
||||||
|
monitor?.cancel()
|
||||||
|
monitor = nil
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/// Feed an observation into the debounce and publish committed changes.
|
/// Feed an observation into the debounce and publish committed changes.
|
||||||
/// Exposed internally so higher layers/tests could drive it if needed.
|
/// Exposed internally so higher layers/tests could drive it if needed.
|
||||||
func ingest(reachable: Bool) {
|
func ingest(reachable: Bool) {
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ enum TransportConfig {
|
|||||||
static let privateChatCap: Int = 1337
|
static let privateChatCap: Int = 1337
|
||||||
static let meshTimelineCap: Int = 1337
|
static let meshTimelineCap: Int = 1337
|
||||||
static let geoTimelineCap: Int = 1337
|
static let geoTimelineCap: Int = 1337
|
||||||
|
static let geoNicknameParticipantsCap: Int = 1337
|
||||||
static let contentLRUCap: Int = 2000
|
static let contentLRUCap: Int = 2000
|
||||||
static let geoSamplingEventLRUCap: Int = 2000
|
static let geoSamplingEventLRUCap: Int = 2000
|
||||||
|
|
||||||
@@ -81,6 +82,11 @@ enum TransportConfig {
|
|||||||
static let nostrDuplicateEventLogInterval: Int = 50
|
static let nostrDuplicateEventLogInterval: Int = 50
|
||||||
// Sample interval for per-event debug logs on the inbound hot path.
|
// Sample interval for per-event debug logs on the inbound hot path.
|
||||||
static let nostrInboundEventLogInterval: Int = 100
|
static let nostrInboundEventLogInterval: Int = 100
|
||||||
|
// Reject oversized/untrusted relay frames before JSON parse / store.
|
||||||
|
static let nostrMaxInboundMessageBytes: Int = 256 * 1024
|
||||||
|
static let nostrMaxEventTags: Int = 64
|
||||||
|
static let nostrMaxEventTagValues: Int = 16
|
||||||
|
static let nostrMaxEventTagValueBytes: Int = 1024
|
||||||
|
|
||||||
// Conversation store diagnostics (field observability)
|
// Conversation store diagnostics (field observability)
|
||||||
// Sample interval for the periodic store-audit "OK" heartbeat line
|
// Sample interval for the periodic store-audit "OK" heartbeat line
|
||||||
@@ -98,11 +104,16 @@ enum TransportConfig {
|
|||||||
static let uiSenderRateBucketRefillPerSec: Double = 1.0
|
static let uiSenderRateBucketRefillPerSec: Double = 1.0
|
||||||
static let uiContentRateBucketCapacity: Double = 3
|
static let uiContentRateBucketCapacity: Double = 3
|
||||||
static let uiContentRateBucketRefillPerSec: Double = 0.5
|
static let uiContentRateBucketRefillPerSec: Double = 0.5
|
||||||
|
// Bound attacker-keyed bucket maps (sender IDs / content digests).
|
||||||
|
static let uiSenderRateBucketMaxEntries: Int = 2000
|
||||||
|
static let uiContentRateBucketMaxEntries: Int = 2000
|
||||||
|
static let uiRateBucketIdleTTL: TimeInterval = 10 * 60
|
||||||
|
// Cap teleported-participant markers so remote events cannot grow the set.
|
||||||
|
static let geoTeleportedParticipantsCap: Int = 1337
|
||||||
|
|
||||||
// UI sleeps/delays
|
// UI sleeps/delays
|
||||||
static let uiStartupInitialDelaySeconds: TimeInterval = 1.0
|
static let uiStartupInitialDelaySeconds: TimeInterval = 1.0
|
||||||
static let uiStartupPhaseDurationSeconds: TimeInterval = 2.0
|
static let uiStartupPhaseDurationSeconds: TimeInterval = 2.0
|
||||||
static let uiAsyncShortSleepNs: UInt64 = 100_000_000
|
|
||||||
static let uiReadReceiptRetryShortSeconds: TimeInterval = 0.1
|
static let uiReadReceiptRetryShortSeconds: TimeInterval = 0.1
|
||||||
static let uiReadReceiptRetryLongSeconds: TimeInterval = 0.5
|
static let uiReadReceiptRetryLongSeconds: TimeInterval = 0.5
|
||||||
static let uiBatchDispatchStaggerSeconds: TimeInterval = 0.15
|
static let uiBatchDispatchStaggerSeconds: TimeInterval = 0.15
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
/// In-app override for the UI language, on top of the system per-app
|
||||||
|
/// language. Apple resolves localization from the AppleLanguages default at
|
||||||
|
/// process start, so a new choice takes effect on the next launch — callers
|
||||||
|
/// surface a "restart to apply" note after changing it.
|
||||||
|
enum AppLanguageSettings {
|
||||||
|
/// "" means no override: follow the device (or per-app system) language.
|
||||||
|
static let overrideKey = "app.languageOverride"
|
||||||
|
private static let appleLanguagesKey = "AppleLanguages"
|
||||||
|
|
||||||
|
/// Language codes the app ships translations for, straight from the
|
||||||
|
/// built bundle so this never drifts from the string catalog.
|
||||||
|
static var availableLanguages: [String] {
|
||||||
|
Bundle.main.localizations
|
||||||
|
.filter { $0 != "Base" }
|
||||||
|
.sorted { endonym(for: $0).localizedCaseInsensitiveCompare(endonym(for: $1)) == .orderedAscending }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The language's name in that language ("فارسی", "한국어") so every user
|
||||||
|
/// can find their own entry regardless of the current UI language.
|
||||||
|
static func endonym(for code: String) -> String {
|
||||||
|
let locale = Locale(identifier: code)
|
||||||
|
let name = locale.localizedString(forIdentifier: code) ?? code
|
||||||
|
return name.lowercased(with: locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Persists the override (nil clears it). AppleLanguages drives the
|
||||||
|
/// actual localization lookup on next launch.
|
||||||
|
static func setOverride(_ code: String?) {
|
||||||
|
let defaults = UserDefaults.standard
|
||||||
|
if let code, !code.isEmpty {
|
||||||
|
defaults.set(code, forKey: overrideKey)
|
||||||
|
defaults.set([code], forKey: appleLanguagesKey)
|
||||||
|
} else {
|
||||||
|
defaults.removeObject(forKey: overrideKey)
|
||||||
|
defaults.removeObject(forKey: appleLanguagesKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -226,6 +226,21 @@ final class ChatLiveVoiceCoordinator {
|
|||||||
assemblies.values.contains { $0.messageID == message.id }
|
assemblies.values.contains { $0.messageID == message.id }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Stop every live file handle/player before the panic media directory is
|
||||||
|
/// removed. This prevents an in-flight assembly from continuing to write
|
||||||
|
/// through an unlinked file after the wipe returns.
|
||||||
|
func resetForPanic() {
|
||||||
|
for assembly in Array(assemblies.values) {
|
||||||
|
cancelAssembly(assembly)
|
||||||
|
}
|
||||||
|
for player in drainingPlayers.values {
|
||||||
|
player.stop()
|
||||||
|
}
|
||||||
|
drainingPlayers.removeAll(keepingCapacity: false)
|
||||||
|
finishedBursts.removeAll(keepingCapacity: false)
|
||||||
|
updatePublicTalkerIndicator()
|
||||||
|
}
|
||||||
|
|
||||||
/// Called for every inbound private message: when it is the finalized
|
/// Called for every inbound private message: when it is the finalized
|
||||||
/// voice note of a burst we assembled (matched by burst ID in the file
|
/// voice note of a burst we assembled (matched by burst ID in the file
|
||||||
/// name), swap it into the existing live bubble and report `true` so the
|
/// name), swap it into the existing live bubble and report `true` so the
|
||||||
|
|||||||
@@ -72,15 +72,79 @@ extension ChatViewModel: ChatMediaTransferContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Synchronous boundary between detached image writers and panic deletion.
|
||||||
|
///
|
||||||
|
/// Invalidation closes admission before waiting for writers that already
|
||||||
|
/// entered. Those writers never need the main actor while inside the boundary,
|
||||||
|
/// so a synchronous panic transaction can safely join them and then delete
|
||||||
|
/// every output before reporting completion.
|
||||||
|
private final class ImagePreparationBarrier: @unchecked Sendable {
|
||||||
|
private let condition = NSCondition()
|
||||||
|
private var generation: UInt64 = 0
|
||||||
|
private var activeOperations = 0
|
||||||
|
|
||||||
|
var currentGeneration: UInt64 {
|
||||||
|
condition.lock()
|
||||||
|
defer { condition.unlock() }
|
||||||
|
return generation
|
||||||
|
}
|
||||||
|
|
||||||
|
func isCurrent(_ candidate: UInt64) -> Bool {
|
||||||
|
condition.lock()
|
||||||
|
defer { condition.unlock() }
|
||||||
|
return generation == candidate
|
||||||
|
}
|
||||||
|
|
||||||
|
func performIfCurrent<T>(
|
||||||
|
generation candidate: UInt64,
|
||||||
|
operation: () throws -> T
|
||||||
|
) rethrows -> T? {
|
||||||
|
condition.lock()
|
||||||
|
guard generation == candidate else {
|
||||||
|
condition.unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
activeOperations += 1
|
||||||
|
condition.unlock()
|
||||||
|
|
||||||
|
defer {
|
||||||
|
condition.lock()
|
||||||
|
activeOperations -= 1
|
||||||
|
if activeOperations == 0 {
|
||||||
|
condition.broadcast()
|
||||||
|
}
|
||||||
|
condition.unlock()
|
||||||
|
}
|
||||||
|
return try operation()
|
||||||
|
}
|
||||||
|
|
||||||
|
func invalidateAndWait() {
|
||||||
|
condition.lock()
|
||||||
|
generation &+= 1
|
||||||
|
while activeOperations > 0 {
|
||||||
|
condition.wait()
|
||||||
|
}
|
||||||
|
condition.unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
final class ChatMediaTransferCoordinator {
|
final class ChatMediaTransferCoordinator {
|
||||||
private unowned let context: any ChatMediaTransferContext
|
private unowned let context: any ChatMediaTransferContext
|
||||||
|
private let prepareImagePacket: @Sendable (URL) throws -> ChatPreparedImage
|
||||||
|
private let imagePreparationBarrier = ImagePreparationBarrier()
|
||||||
|
|
||||||
private(set) var transferIdToMessageIDs: [String: [String]] = [:]
|
private(set) var transferIdToMessageIDs: [String: [String]] = [:]
|
||||||
private(set) var messageIDToTransferId: [String: String] = [:]
|
private(set) var messageIDToTransferId: [String: String] = [:]
|
||||||
|
|
||||||
init(context: any ChatMediaTransferContext) {
|
init(
|
||||||
|
context: any ChatMediaTransferContext,
|
||||||
|
prepareImagePacket: @escaping @Sendable (URL) throws -> ChatPreparedImage = {
|
||||||
|
try ChatMediaPreparation.prepareImagePacket(from: $0)
|
||||||
|
}
|
||||||
|
) {
|
||||||
self.context = context
|
self.context = context
|
||||||
|
self.prepareImagePacket = prepareImagePacket
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendVoiceNote(at url: URL) {
|
func sendVoiceNote(at url: URL) {
|
||||||
@@ -98,13 +162,17 @@ final class ChatMediaTransferCoordinator {
|
|||||||
)
|
)
|
||||||
let messageID = message.id
|
let messageID = message.id
|
||||||
let transferId = makeTransferID(messageID: messageID)
|
let transferId = makeTransferID(messageID: messageID)
|
||||||
|
let generation = imagePreparationBarrier.currentGeneration
|
||||||
|
|
||||||
Task.detached(priority: .userInitiated) { [weak self] in
|
Task.detached(priority: .userInitiated) { [weak self] in
|
||||||
do {
|
do {
|
||||||
let packet = try ChatMediaPreparation.prepareVoiceNotePacket(at: url)
|
let packet = try ChatMediaPreparation.prepareVoiceNotePacket(at: url)
|
||||||
|
|
||||||
await MainActor.run { [weak self] in
|
await MainActor.run { [weak self] in
|
||||||
guard let self else { return }
|
guard let self,
|
||||||
|
self.imagePreparationBarrier.isCurrent(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
self.registerTransfer(transferId: transferId, messageID: messageID)
|
self.registerTransfer(transferId: transferId, messageID: messageID)
|
||||||
if let peerID = targetPeer {
|
if let peerID = targetPeer {
|
||||||
self.context.sendFilePrivate(packet, to: peerID, transferId: transferId)
|
self.context.sendFilePrivate(packet, to: peerID, transferId: transferId)
|
||||||
@@ -116,13 +184,19 @@ final class ChatMediaTransferCoordinator {
|
|||||||
SecureLogger.warning("Voice note exceeds size limit (\(size) bytes)", category: .session)
|
SecureLogger.warning("Voice note exceeds size limit (\(size) bytes)", category: .session)
|
||||||
try? FileManager.default.removeItem(at: url)
|
try? FileManager.default.removeItem(at: url)
|
||||||
await MainActor.run { [weak self] in
|
await MainActor.run { [weak self] in
|
||||||
guard let self else { return }
|
guard let self,
|
||||||
|
self.imagePreparationBarrier.isCurrent(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
self.handleMediaSendFailure(messageID: messageID, reason: String(localized: "content.delivery.reason.voice_too_large", comment: "Failure reason shown when a voice note exceeds the size limit"))
|
self.handleMediaSendFailure(messageID: messageID, reason: String(localized: "content.delivery.reason.voice_too_large", comment: "Failure reason shown when a voice note exceeds the size limit"))
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
SecureLogger.error("Voice note send failed: \(error)", category: .session)
|
SecureLogger.error("Voice note send failed: \(error)", category: .session)
|
||||||
await MainActor.run { [weak self] in
|
await MainActor.run { [weak self] in
|
||||||
guard let self else { return }
|
guard let self,
|
||||||
|
self.imagePreparationBarrier.isCurrent(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
self.handleMediaSendFailure(messageID: messageID, reason: String(localized: "content.delivery.reason.voice_send_failed", comment: "Failure reason shown when a voice note could not be sent"))
|
self.handleMediaSendFailure(messageID: messageID, reason: String(localized: "content.delivery.reason.voice_send_failed", comment: "Failure reason shown when a voice note could not be sent"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,11 +206,24 @@ final class ChatMediaTransferCoordinator {
|
|||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
func processThenSendImage(_ image: UIImage?) {
|
func processThenSendImage(_ image: UIImage?) {
|
||||||
guard let image else { return }
|
guard let image else { return }
|
||||||
Task.detached { [weak self] in
|
let generation = imagePreparationBarrier.currentGeneration
|
||||||
|
let barrier = imagePreparationBarrier
|
||||||
|
Task.detached(priority: .userInitiated) { [weak self, barrier] in
|
||||||
do {
|
do {
|
||||||
let processedURL = try ImageUtils.processImage(image)
|
guard let processedURL = try barrier.performIfCurrent(
|
||||||
await MainActor.run { [weak self] in
|
generation: generation,
|
||||||
guard let self else { return }
|
operation: {
|
||||||
|
try ImageUtils.processImage(image)
|
||||||
|
}
|
||||||
|
) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await MainActor.run { [weak self, barrier] in
|
||||||
|
guard let self,
|
||||||
|
barrier.isCurrent(generation) else {
|
||||||
|
try? FileManager.default.removeItem(at: processedURL)
|
||||||
|
return
|
||||||
|
}
|
||||||
self.sendImage(from: processedURL)
|
self.sendImage(from: processedURL)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -147,11 +234,24 @@ final class ChatMediaTransferCoordinator {
|
|||||||
#elseif os(macOS)
|
#elseif os(macOS)
|
||||||
func processThenSendImage(from url: URL?) {
|
func processThenSendImage(from url: URL?) {
|
||||||
guard let url else { return }
|
guard let url else { return }
|
||||||
Task.detached { [weak self] in
|
let generation = imagePreparationBarrier.currentGeneration
|
||||||
|
let barrier = imagePreparationBarrier
|
||||||
|
Task.detached(priority: .userInitiated) { [weak self, barrier] in
|
||||||
do {
|
do {
|
||||||
let processedURL = try ImageUtils.processImage(at: url)
|
guard let processedURL = try barrier.performIfCurrent(
|
||||||
await MainActor.run { [weak self] in
|
generation: generation,
|
||||||
guard let self else { return }
|
operation: {
|
||||||
|
try ImageUtils.processImage(at: url)
|
||||||
|
}
|
||||||
|
) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await MainActor.run { [weak self, barrier] in
|
||||||
|
guard let self,
|
||||||
|
barrier.isCurrent(generation) else {
|
||||||
|
try? FileManager.default.removeItem(at: processedURL)
|
||||||
|
return
|
||||||
|
}
|
||||||
self.sendImage(from: processedURL)
|
self.sendImage(from: processedURL)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -170,6 +270,7 @@ final class ChatMediaTransferCoordinator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let targetPeer = context.selectedPrivateChatPeer
|
let targetPeer = context.selectedPrivateChatPeer
|
||||||
|
let generation = imagePreparationBarrier.currentGeneration
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try ImageUtils.validateImageSource(at: sourceURL)
|
try ImageUtils.validateImageSource(at: sourceURL)
|
||||||
@@ -179,12 +280,25 @@ final class ChatMediaTransferCoordinator {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Task.detached(priority: .userInitiated) { [weak self] in
|
let prepareImagePacket = self.prepareImagePacket
|
||||||
|
let barrier = imagePreparationBarrier
|
||||||
|
Task.detached(priority: .userInitiated) { [weak self, barrier] in
|
||||||
do {
|
do {
|
||||||
let prepared = try ChatMediaPreparation.prepareImagePacket(from: sourceURL)
|
guard let prepared = try barrier.performIfCurrent(
|
||||||
|
generation: generation,
|
||||||
|
operation: {
|
||||||
|
try prepareImagePacket(sourceURL)
|
||||||
|
}
|
||||||
|
) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
await MainActor.run { [weak self] in
|
await MainActor.run { [weak self, barrier] in
|
||||||
guard let self else { return }
|
guard let self,
|
||||||
|
barrier.isCurrent(generation) else {
|
||||||
|
try? FileManager.default.removeItem(at: prepared.outputURL)
|
||||||
|
return
|
||||||
|
}
|
||||||
let message = self.enqueueMediaMessage(
|
let message = self.enqueueMediaMessage(
|
||||||
content: "\(MimeType.Category.image.messagePrefix)\(prepared.outputURL.lastPathComponent)",
|
content: "\(MimeType.Category.image.messagePrefix)\(prepared.outputURL.lastPathComponent)",
|
||||||
targetPeer: targetPeer
|
targetPeer: targetPeer
|
||||||
@@ -200,14 +314,20 @@ final class ChatMediaTransferCoordinator {
|
|||||||
}
|
}
|
||||||
} catch ChatMediaPreparationError.imageTooLarge(let size) {
|
} catch ChatMediaPreparationError.imageTooLarge(let size) {
|
||||||
SecureLogger.warning("Processed image exceeds size limit (\(size) bytes)", category: .session)
|
SecureLogger.warning("Processed image exceeds size limit (\(size) bytes)", category: .session)
|
||||||
await MainActor.run { [weak self] in
|
await MainActor.run { [weak self, barrier] in
|
||||||
guard let self else { return }
|
guard let self,
|
||||||
|
barrier.isCurrent(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
self.context.addSystemMessage("Image is too large to send.")
|
self.context.addSystemMessage("Image is too large to send.")
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
SecureLogger.error("Image send preparation failed: \(error)", category: .session)
|
SecureLogger.error("Image send preparation failed: \(error)", category: .session)
|
||||||
await MainActor.run { [weak self] in
|
await MainActor.run { [weak self, barrier] in
|
||||||
guard let self else { return }
|
guard let self,
|
||||||
|
barrier.isCurrent(generation) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
self.context.addSystemMessage("Failed to prepare image for sending.")
|
self.context.addSystemMessage("Failed to prepare image for sending.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -341,6 +461,20 @@ final class ChatMediaTransferCoordinator {
|
|||||||
clearTransferMapping(for: messageID)
|
clearTransferMapping(for: messageID)
|
||||||
context.removeMessage(withID: messageID, cleanupFile: true)
|
context.removeMessage(withID: messageID, cleanupFile: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Invalidates detached preparation work and cancels every transfer that
|
||||||
|
/// reached the transport. Closing image-preparation admission and joining
|
||||||
|
/// active synchronous writers ensures the following panic media deletion
|
||||||
|
/// is the last filesystem mutation before the transaction can complete.
|
||||||
|
func resetForPanic() {
|
||||||
|
imagePreparationBarrier.invalidateAndWait()
|
||||||
|
let transferIDs = Set(transferIdToMessageIDs.keys)
|
||||||
|
transferIdToMessageIDs.removeAll(keepingCapacity: false)
|
||||||
|
messageIDToTransferId.removeAll(keepingCapacity: false)
|
||||||
|
for transferID in transferIDs {
|
||||||
|
context.cancelTransfer(transferID)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension ChatMediaTransferCoordinator {
|
private extension ChatMediaTransferCoordinator {
|
||||||
|
|||||||
@@ -71,12 +71,8 @@ final class ChatMessageFormatter {
|
|||||||
let content = message.content
|
let content = message.content
|
||||||
let nsContent = content as NSString
|
let nsContent = content as NSString
|
||||||
let nsLen = nsContent.length
|
let nsLen = nsContent.length
|
||||||
let containsCashuEarly: Bool = {
|
|
||||||
let regex = Patterns.quickCashuPresence
|
|
||||||
return regex.numberOfMatches(in: content, options: [], range: NSRange(location: 0, length: nsLen)) > 0
|
|
||||||
}()
|
|
||||||
|
|
||||||
if (content.count > 4000 || content.hasVeryLongToken(threshold: 1024)) && !containsCashuEarly {
|
if content.isOversizedForRichFormatting() {
|
||||||
var plainStyle = AttributeContainer()
|
var plainStyle = AttributeContainer()
|
||||||
plainStyle.foregroundColor = baseColor
|
plainStyle.foregroundColor = baseColor
|
||||||
plainStyle.font = isSelf
|
plainStyle.font = isSelf
|
||||||
|
|||||||
@@ -89,6 +89,26 @@ import UIKit
|
|||||||
#endif
|
#endif
|
||||||
import UniformTypeIdentifiers
|
import UniformTypeIdentifiers
|
||||||
|
|
||||||
|
struct PanicNetworkLifecycle {
|
||||||
|
let stop: @MainActor () -> Void
|
||||||
|
let restart: @MainActor () -> Void
|
||||||
|
|
||||||
|
static let noop = PanicNetworkLifecycle(stop: {}, restart: {})
|
||||||
|
|
||||||
|
static var live: PanicNetworkLifecycle {
|
||||||
|
PanicNetworkLifecycle(
|
||||||
|
stop: {
|
||||||
|
GeohashPresenceService.shared.stopForPanic()
|
||||||
|
NetworkActivationService.shared.stopForPanic()
|
||||||
|
},
|
||||||
|
restart: {
|
||||||
|
NetworkActivationService.shared.start()
|
||||||
|
GeohashPresenceService.shared.start()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Manages the application state and business logic for BitChat.
|
/// Manages the application state and business logic for BitChat.
|
||||||
/// Acts as the primary coordinator between UI components and backend services,
|
/// Acts as the primary coordinator between UI components and backend services,
|
||||||
/// implementing the BitchatDelegate protocol to handle network events.
|
/// implementing the BitchatDelegate protocol to handle network events.
|
||||||
@@ -142,6 +162,8 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
@Published var currentColorScheme: ColorScheme = .light
|
@Published var currentColorScheme: ColorScheme = .light
|
||||||
@Published var currentTheme: AppTheme = .matrix
|
@Published var currentTheme: AppTheme = .matrix
|
||||||
@Published var isConnected = false
|
@Published var isConnected = false
|
||||||
|
@Published private(set) var panicRecoveryBlocked = false
|
||||||
|
var networkActivationAllowed: Bool { !panicRecoveryBlocked }
|
||||||
@Published var nickname: String = "" {
|
@Published var nickname: String = "" {
|
||||||
didSet {
|
didSet {
|
||||||
// Trim whitespace whenever nickname is set; whitespace-only becomes ""
|
// Trim whitespace whenever nickname is set; whitespace-only becomes ""
|
||||||
@@ -151,7 +173,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Update mesh service nickname if it's initialized
|
// Update mesh service nickname if it's initialized
|
||||||
if !meshService.myPeerID.isEmpty {
|
if !isPanicResetting, !meshService.myPeerID.isEmpty {
|
||||||
meshService.setNickname(nickname)
|
meshService.setNickname(nickname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -177,7 +199,10 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
lazy var privateConversationCoordinator = ChatPrivateConversationCoordinator(context: self)
|
lazy var privateConversationCoordinator = ChatPrivateConversationCoordinator(context: self)
|
||||||
lazy var nostrCoordinator = ChatNostrCoordinator(context: self)
|
lazy var nostrCoordinator = ChatNostrCoordinator(context: self)
|
||||||
lazy var mediaTransferCoordinator = ChatMediaTransferCoordinator(context: self)
|
lazy var mediaTransferCoordinator = ChatMediaTransferCoordinator(context: self)
|
||||||
lazy var liveVoiceCoordinator = ChatLiveVoiceCoordinator(context: self)
|
lazy var liveVoiceCoordinator = ChatLiveVoiceCoordinator(
|
||||||
|
context: self,
|
||||||
|
sweepsOnInit: !TestEnvironment.isRunningTests
|
||||||
|
)
|
||||||
lazy var verificationCoordinator = ChatVerificationCoordinator(context: self)
|
lazy var verificationCoordinator = ChatVerificationCoordinator(context: self)
|
||||||
lazy var groupCoordinator = ChatGroupCoordinator(context: self)
|
lazy var groupCoordinator = ChatGroupCoordinator(context: self)
|
||||||
lazy var vouchCoordinator = ChatVouchCoordinator(context: self)
|
lazy var vouchCoordinator = ChatVouchCoordinator(context: self)
|
||||||
@@ -292,6 +317,9 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
var nostrRelayManager: NostrRelayManager?
|
var nostrRelayManager: NostrRelayManager?
|
||||||
private let userDefaults = UserDefaults.standard
|
private let userDefaults = UserDefaults.standard
|
||||||
let keychain: KeychainManagerProtocol
|
let keychain: KeychainManagerProtocol
|
||||||
|
private let panicRecoveryOperations: PanicRecoveryOperations
|
||||||
|
private let panicNetworkLifecycle: PanicNetworkLifecycle
|
||||||
|
private var isPanicResetting = false
|
||||||
/// Private group membership: keys in the keychain, metadata on disk.
|
/// Private group membership: keys in the keychain, metadata on disk.
|
||||||
let groupStore: GroupStore
|
let groupStore: GroupStore
|
||||||
private let nicknameKey = "bitchat.nickname"
|
private let nicknameKey = "bitchat.nickname"
|
||||||
@@ -769,7 +797,34 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
locationPresenceStore: LocationPresenceStore? = nil,
|
locationPresenceStore: LocationPresenceStore? = nil,
|
||||||
locationManager: LocationChannelManager = .shared
|
locationManager: LocationChannelManager = .shared
|
||||||
) {
|
) {
|
||||||
let meshService = BLEService(keychain: keychain, idBridge: idBridge, identityManager: identityManager)
|
let livePanicRecoveryOperations = PanicRecoveryOperations.live()
|
||||||
|
let startSuspendedForRecovery: Bool
|
||||||
|
do {
|
||||||
|
startSuspendedForRecovery =
|
||||||
|
try livePanicRecoveryOperations.isPending()
|
||||||
|
} catch {
|
||||||
|
startSuspendedForRecovery = true
|
||||||
|
}
|
||||||
|
// Preserve the preflight decision used to defer CoreBluetooth. A
|
||||||
|
// transiently successful second read must not skip recovery and leave
|
||||||
|
// the service permanently suspended without running the wipe.
|
||||||
|
let panicRecoveryOperations = PanicRecoveryOperations(
|
||||||
|
isPending: {
|
||||||
|
if startSuspendedForRecovery {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return try livePanicRecoveryOperations.isPending()
|
||||||
|
},
|
||||||
|
begin: livePanicRecoveryOperations.begin,
|
||||||
|
wipeMedia: livePanicRecoveryOperations.wipeMedia,
|
||||||
|
complete: livePanicRecoveryOperations.complete
|
||||||
|
)
|
||||||
|
let meshService = BLEService(
|
||||||
|
keychain: keychain,
|
||||||
|
idBridge: idBridge,
|
||||||
|
identityManager: identityManager,
|
||||||
|
startSuspendedForPanicRecovery: startSuspendedForRecovery
|
||||||
|
)
|
||||||
meshService.sfMetrics = .shared
|
meshService.sfMetrics = .shared
|
||||||
self.init(
|
self.init(
|
||||||
keychain: keychain,
|
keychain: keychain,
|
||||||
@@ -781,7 +836,9 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
locationPresenceStore: locationPresenceStore ?? LocationPresenceStore(),
|
locationPresenceStore: locationPresenceStore ?? LocationPresenceStore(),
|
||||||
locationManager: locationManager,
|
locationManager: locationManager,
|
||||||
outboxStore: MessageOutboxStore(keychain: keychain),
|
outboxStore: MessageOutboxStore(keychain: keychain),
|
||||||
sfMetrics: .shared
|
sfMetrics: .shared,
|
||||||
|
panicRecoveryOperations: panicRecoveryOperations,
|
||||||
|
panicNetworkLifecycle: .live
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -799,7 +856,10 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
locationManager: LocationChannelManager = .shared,
|
locationManager: LocationChannelManager = .shared,
|
||||||
readReceiptsDefaults: UserDefaults? = nil,
|
readReceiptsDefaults: UserDefaults? = nil,
|
||||||
outboxStore: MessageOutboxStore? = nil,
|
outboxStore: MessageOutboxStore? = nil,
|
||||||
sfMetrics: StoreAndForwardMetrics? = nil
|
sfMetrics: StoreAndForwardMetrics? = nil,
|
||||||
|
panicMediaWipe: (() throws -> Void)? = nil,
|
||||||
|
panicRecoveryOperations: PanicRecoveryOperations? = nil,
|
||||||
|
panicNetworkLifecycle: PanicNetworkLifecycle = .noop
|
||||||
) {
|
) {
|
||||||
let conversations = conversations ?? ConversationStore()
|
let conversations = conversations ?? ConversationStore()
|
||||||
let peerIdentityStore = peerIdentityStore ?? PeerIdentityStore()
|
let peerIdentityStore = peerIdentityStore ?? PeerIdentityStore()
|
||||||
@@ -814,6 +874,9 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.keychain = keychain
|
self.keychain = keychain
|
||||||
|
self.panicRecoveryOperations = panicRecoveryOperations
|
||||||
|
?? .ephemeral(wipeMedia: panicMediaWipe ?? {})
|
||||||
|
self.panicNetworkLifecycle = panicNetworkLifecycle
|
||||||
self.groupStore = GroupStore(keychain: keychain)
|
self.groupStore = GroupStore(keychain: keychain)
|
||||||
self.idBridge = idBridge
|
self.idBridge = idBridge
|
||||||
self.identityManager = identityManager
|
self.identityManager = identityManager
|
||||||
@@ -849,7 +912,31 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
}
|
}
|
||||||
.store(in: &cancellables)
|
.store(in: &cancellables)
|
||||||
|
|
||||||
ChatViewModelBootstrapper(viewModel: self).configure()
|
let recoveryRequired: Bool
|
||||||
|
do {
|
||||||
|
recoveryRequired = try self.panicRecoveryOperations.isPending()
|
||||||
|
} catch {
|
||||||
|
// Failure to read the latch cannot fail open. Re-run the complete
|
||||||
|
// transaction; a persistent storage failure leaves services
|
||||||
|
// blocked below.
|
||||||
|
recoveryRequired = true
|
||||||
|
SecureLogger.error(
|
||||||
|
"Could not read panic-recovery state; retrying the full wipe before startup: \(error)",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if recoveryRequired {
|
||||||
|
SecureLogger.warning(
|
||||||
|
"Pending panic recovery detected; wiping before runtime services start",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
_ = panicClearAllData(restartServices: false)
|
||||||
|
}
|
||||||
|
|
||||||
|
if networkActivationAllowed {
|
||||||
|
ChatViewModelBootstrapper(viewModel: self).configure()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Deinitialization
|
// MARK: - Deinitialization
|
||||||
@@ -1153,8 +1240,33 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
|
|
||||||
// PANIC: Emergency data clearing for activist safety
|
// PANIC: Emergency data clearing for activist safety
|
||||||
@MainActor
|
@MainActor
|
||||||
func panicClearAllData() {
|
@discardableResult
|
||||||
// Messages are processed immediately - nothing to flush
|
func panicClearAllData(restartServices: Bool = true) -> Bool {
|
||||||
|
panicRecoveryBlocked = true
|
||||||
|
isPanicResetting = true
|
||||||
|
defer { isPanicResetting = false }
|
||||||
|
|
||||||
|
// Stop internet and location-presence work before clearing identity or
|
||||||
|
// state. These services cancel their subscriptions and delayed tasks,
|
||||||
|
// so old callbacks cannot reconnect during the transaction.
|
||||||
|
panicNetworkLifecycle.stop()
|
||||||
|
|
||||||
|
// Establish both independent durable intents before erasing anything.
|
||||||
|
// `wipeMedia` will still attempt deletion if neither write succeeds.
|
||||||
|
let recoveryIntent = panicRecoveryOperations.begin()
|
||||||
|
|
||||||
|
// Quiesce the mesh before clearing stores. Identity replacement below
|
||||||
|
// deliberately stays stopped until media deletion and marker commit.
|
||||||
|
if let bleService = meshService as? BLEService {
|
||||||
|
bleService.suspendForPanicReset()
|
||||||
|
} else {
|
||||||
|
meshService.emergencyDisconnectAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalidate detached media preparation and close live capture file
|
||||||
|
// handles before clearing state or removing the media directory.
|
||||||
|
mediaTransferCoordinator.resetForPanic()
|
||||||
|
liveVoiceCoordinator.resetForPanic()
|
||||||
|
|
||||||
// Clear all messages (public timelines and private chats live in the
|
// Clear all messages (public timelines and private chats live in the
|
||||||
// single-writer ConversationStore; the derived `messages` view and
|
// single-writer ConversationStore; the derived `messages` view and
|
||||||
@@ -1163,7 +1275,13 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
pendingGeohashSystemMessages.removeAll()
|
pendingGeohashSystemMessages.removeAll()
|
||||||
|
|
||||||
// Delete all keychain data (including Noise and Nostr keys)
|
// Delete all keychain data (including Noise and Nostr keys)
|
||||||
_ = keychain.deleteAllKeychainData()
|
let keychainWipeCompleted = keychain.deleteAllKeychainData()
|
||||||
|
if !keychainWipeCompleted {
|
||||||
|
SecureLogger.error(
|
||||||
|
"Panic keychain cleanup incomplete; recovery remains pending",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Clear UserDefaults identity data
|
// Clear UserDefaults identity data
|
||||||
userDefaults.removeObject(forKey: "bitchat.noiseIdentityKey")
|
userDefaults.removeObject(forKey: "bitchat.noiseIdentityKey")
|
||||||
@@ -1176,13 +1294,14 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
|
|
||||||
// Reset nickname to anonymous
|
// Reset nickname to anonymous
|
||||||
nickname = "anon\(Int.random(in: 1000...9999))"
|
nickname = "anon\(Int.random(in: 1000...9999))"
|
||||||
saveNickname()
|
userDefaults.set(nickname, forKey: nicknameKey)
|
||||||
|
|
||||||
// Clear favorites and peer mappings
|
// Clear favorites and peer mappings
|
||||||
// Clear through SecureIdentityStateManager instead of directly
|
// Clear through SecureIdentityStateManager instead of directly
|
||||||
identityManager.clearAllIdentityData()
|
identityManager.clearAllIdentityData()
|
||||||
peerIdentityStore.clearAll()
|
peerIdentityStore.clearAll()
|
||||||
locationPresenceStore.reset()
|
locationPresenceStore.reset()
|
||||||
|
publicRateLimiter.reset()
|
||||||
|
|
||||||
// Clear persistent favorites from keychain
|
// Clear persistent favorites from keychain
|
||||||
FavoritesPersistenceService.shared.clearAllFavorites()
|
FavoritesPersistenceService.shared.clearAllFavorites()
|
||||||
@@ -1247,78 +1366,77 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
|||||||
// Clear Nostr identity associations
|
// Clear Nostr identity associations
|
||||||
idBridge.clearAllAssociations()
|
idBridge.clearAllAssociations()
|
||||||
|
|
||||||
// Disconnect from all peers and clear persistent identity
|
// Replace the BLE identity while keeping the radio stopped. It may
|
||||||
// This will force creation of a new identity (new fingerprint) on next launch
|
// reopen only after the durable panic transaction commits.
|
||||||
meshService.emergencyDisconnectAll()
|
|
||||||
if let bleService = meshService as? BLEService {
|
if let bleService = meshService as? BLEService {
|
||||||
bleService.resetIdentityForPanic(currentNickname: nickname)
|
bleService.resetIdentityForPanic(
|
||||||
|
currentNickname: nickname,
|
||||||
|
restartServices: false
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
meshService.setNickname(nickname)
|
||||||
}
|
}
|
||||||
|
|
||||||
// No need to force UserDefaults synchronization
|
// The wipe must finish before this security action returns. A detached
|
||||||
|
// task could otherwise lose a race with a new capture or app exit and
|
||||||
|
// leave pre-panic media behind.
|
||||||
|
let panicCompleted: Bool
|
||||||
|
do {
|
||||||
|
try panicRecoveryOperations.wipeMedia(recoveryIntent)
|
||||||
|
if keychainWipeCompleted {
|
||||||
|
try panicRecoveryOperations.complete()
|
||||||
|
panicCompleted = true
|
||||||
|
SecureLogger.info(
|
||||||
|
"🗑️ Deleted all media files during panic clear",
|
||||||
|
category: .session
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
// Do not clear either durable recovery marker. Startup must
|
||||||
|
// retry the entire transaction before any transport restarts.
|
||||||
|
panicCompleted = false
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
panicCompleted = false
|
||||||
|
SecureLogger.error(
|
||||||
|
"Panic transaction did not commit; services remain stopped: \(error)",
|
||||||
|
category: .security
|
||||||
|
)
|
||||||
|
}
|
||||||
|
panicRecoveryBlocked = !panicCompleted
|
||||||
|
|
||||||
// Reinitialize Nostr with new identity
|
// BCH-01-013: Clear iOS app switcher snapshots. Keep tests away from
|
||||||
// This will generate new Nostr keys derived from new Noise keys.
|
// the host user's real cache tree just as the default media wipe does.
|
||||||
// Skipped under tests: connecting the shared relay singleton starts
|
#if os(iOS)
|
||||||
// real network/reconnect work that never completes and would keep the
|
|
||||||
// test process alive (the singleton, unlike a discardable instance, is
|
|
||||||
// never deallocated to cancel it).
|
|
||||||
if !TestEnvironment.isRunningTests {
|
if !TestEnvironment.isRunningTests {
|
||||||
Task { @MainActor in
|
Self.clearAppSwitcherSnapshots()
|
||||||
// Small delay to ensure cleanup completes
|
}
|
||||||
try? await Task.sleep(nanoseconds: TransportConfig.uiAsyncShortSleepNs) // 0.1 seconds
|
#endif
|
||||||
|
|
||||||
// Reinitialize Nostr relay manager with new identity. Reuse the
|
guard panicCompleted else { return false }
|
||||||
// shared singleton — every other component (NostrTransport, geohash
|
|
||||||
// subscriptions, AppRuntime observers) is bound to `.shared`, so
|
if let bleService = meshService as? BLEService {
|
||||||
// creating a fresh instance here would split relay state and leave
|
// Startup recovery reopens admission but leaves actual service
|
||||||
// sends running against a disconnected manager.
|
// start to the bootstrapper immediately after this method.
|
||||||
|
bleService.completePanicReset(
|
||||||
|
restartServices: restartServices
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if restartServices {
|
||||||
|
// All persistent state and media are gone. Bring each service back
|
||||||
|
// only now, under the new identity.
|
||||||
|
if !(meshService is BLEService) {
|
||||||
|
meshService.startServices()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !TestEnvironment.isRunningTests {
|
||||||
nostrRelayManager = NostrRelayManager.shared
|
nostrRelayManager = NostrRelayManager.shared
|
||||||
setupNostrMessageHandling()
|
setupNostrMessageHandling()
|
||||||
nostrRelayManager?.connect()
|
|
||||||
}
|
}
|
||||||
|
panicNetworkLifecycle.restart()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete ALL media files (incoming and outgoing) in background
|
return true
|
||||||
Task.detached(priority: .utility) {
|
|
||||||
// Skipped under tests: the test process shares the user's real
|
|
||||||
// ~/Library/Application Support/files tree, and this detached
|
|
||||||
// utility-priority wipe fires at a nondeterministic time —
|
|
||||||
// deleting media that concurrently running tests (e.g. the
|
|
||||||
// sendImage flow) just wrote there, and the developer's real
|
|
||||||
// app data with it.
|
|
||||||
guard !TestEnvironment.isRunningTests else { return }
|
|
||||||
do {
|
|
||||||
let base = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
||||||
let filesDir = base.appendingPathComponent("files", isDirectory: true)
|
|
||||||
|
|
||||||
// Delete the entire files directory and recreate it
|
|
||||||
if FileManager.default.fileExists(atPath: filesDir.path) {
|
|
||||||
try FileManager.default.removeItem(at: filesDir)
|
|
||||||
SecureLogger.info("🗑️ Deleted all media files during panic clear", category: .session)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recreate empty directory structure
|
|
||||||
try FileManager.default.createDirectory(at: filesDir, withIntermediateDirectories: true, attributes: nil)
|
|
||||||
try FileManager.default.createDirectory(at: filesDir.appendingPathComponent("voicenotes/incoming", isDirectory: true), withIntermediateDirectories: true, attributes: nil)
|
|
||||||
try FileManager.default.createDirectory(at: filesDir.appendingPathComponent("voicenotes/outgoing", isDirectory: true), withIntermediateDirectories: true, attributes: nil)
|
|
||||||
try FileManager.default.createDirectory(at: filesDir.appendingPathComponent("images/incoming", isDirectory: true), withIntermediateDirectories: true, attributes: nil)
|
|
||||||
try FileManager.default.createDirectory(at: filesDir.appendingPathComponent("images/outgoing", isDirectory: true), withIntermediateDirectories: true, attributes: nil)
|
|
||||||
try FileManager.default.createDirectory(at: filesDir.appendingPathComponent("files/incoming", isDirectory: true), withIntermediateDirectories: true, attributes: nil)
|
|
||||||
try FileManager.default.createDirectory(at: filesDir.appendingPathComponent("files/outgoing", isDirectory: true), withIntermediateDirectories: true, attributes: nil)
|
|
||||||
} catch {
|
|
||||||
SecureLogger.error("Failed to clear media files during panic: \(error)", category: .session)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BCH-01-013: Clear iOS app switcher snapshots
|
|
||||||
// These are stored in Library/Caches/Snapshots/<bundle_id>/
|
|
||||||
#if os(iOS)
|
|
||||||
Self.clearAppSwitcherSnapshots()
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// Force immediate UI update for panic mode
|
|
||||||
// UI updates immediately - no flushing needed
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// BCH-01-013: Clear iOS app switcher snapshots during panic mode
|
/// BCH-01-013: Clear iOS app switcher snapshots during panic mode
|
||||||
|
|||||||
@@ -156,6 +156,17 @@ private extension ChatViewModelBootstrapper {
|
|||||||
viewModel?.objectWillChange.send()
|
viewModel?.objectWillChange.send()
|
||||||
}
|
}
|
||||||
.store(in: &viewModel.cancellables)
|
.store(in: &viewModel.cancellables)
|
||||||
|
|
||||||
|
viewModel.participantTracker.$visiblePeople
|
||||||
|
.receive(on: DispatchQueue.main)
|
||||||
|
.sink { [weak viewModel] people in
|
||||||
|
Task { @MainActor [weak viewModel] in
|
||||||
|
let visible = Set(people.map { $0.id })
|
||||||
|
viewModel?.locationPresenceStore.retainTeleportedGeo(keeping: visible)
|
||||||
|
viewModel?.locationPresenceStore.retainGeoNicknames(keeping: visible)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.store(in: &viewModel.cancellables)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadPersistedViewState() {
|
func loadPersistedViewState() {
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ struct MessageRateLimiter {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isIdle(since now: Date, idleTTL: TimeInterval) -> Bool {
|
||||||
|
now.timeIntervalSince(lastRefill) >= idleTTL
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var senderBuckets: [String: TokenBucket] = [:]
|
private var senderBuckets: [String: TokenBucket] = [:]
|
||||||
@@ -35,17 +39,26 @@ struct MessageRateLimiter {
|
|||||||
private let senderRefill: Double
|
private let senderRefill: Double
|
||||||
private let contentCapacity: Double
|
private let contentCapacity: Double
|
||||||
private let contentRefill: Double
|
private let contentRefill: Double
|
||||||
|
private let maxSenderBuckets: Int
|
||||||
|
private let maxContentBuckets: Int
|
||||||
|
private let bucketIdleTTL: TimeInterval
|
||||||
|
|
||||||
init(
|
init(
|
||||||
senderCapacity: Double,
|
senderCapacity: Double,
|
||||||
senderRefillPerSec: Double,
|
senderRefillPerSec: Double,
|
||||||
contentCapacity: Double,
|
contentCapacity: Double,
|
||||||
contentRefillPerSec: Double
|
contentRefillPerSec: Double,
|
||||||
|
maxSenderBuckets: Int = TransportConfig.uiSenderRateBucketMaxEntries,
|
||||||
|
maxContentBuckets: Int = TransportConfig.uiContentRateBucketMaxEntries,
|
||||||
|
bucketIdleTTL: TimeInterval = TransportConfig.uiRateBucketIdleTTL
|
||||||
) {
|
) {
|
||||||
self.senderCapacity = senderCapacity
|
self.senderCapacity = senderCapacity
|
||||||
self.senderRefill = senderRefillPerSec
|
self.senderRefill = senderRefillPerSec
|
||||||
self.contentCapacity = contentCapacity
|
self.contentCapacity = contentCapacity
|
||||||
self.contentRefill = contentRefillPerSec
|
self.contentRefill = contentRefillPerSec
|
||||||
|
self.maxSenderBuckets = max(1, maxSenderBuckets)
|
||||||
|
self.maxContentBuckets = max(1, maxContentBuckets)
|
||||||
|
self.bucketIdleTTL = bucketIdleTTL
|
||||||
}
|
}
|
||||||
|
|
||||||
/// - Parameter powBits: validated NIP-13 difficulty of the event
|
/// - Parameter powBits: validated NIP-13 difficulty of the event
|
||||||
@@ -58,25 +71,83 @@ struct MessageRateLimiter {
|
|||||||
if powBits >= NostrPoW.rateLimitBypassBits {
|
if powBits >= NostrPoW.rateLimitBypassBits {
|
||||||
senderAllowed = true
|
senderAllowed = true
|
||||||
} else {
|
} else {
|
||||||
var senderBucket = senderBuckets[senderKey] ?? TokenBucket(
|
var senderBucket = Self.bucket(
|
||||||
|
for: senderKey,
|
||||||
|
in: &senderBuckets,
|
||||||
capacity: senderCapacity,
|
capacity: senderCapacity,
|
||||||
tokens: senderCapacity,
|
|
||||||
refillPerSec: senderRefill,
|
refillPerSec: senderRefill,
|
||||||
lastRefill: now
|
maxBuckets: maxSenderBuckets,
|
||||||
|
idleTTL: bucketIdleTTL,
|
||||||
|
now: now
|
||||||
)
|
)
|
||||||
senderAllowed = senderBucket.allow(now: now)
|
senderAllowed = senderBucket.allow(now: now)
|
||||||
senderBuckets[senderKey] = senderBucket
|
senderBuckets[senderKey] = senderBucket
|
||||||
}
|
}
|
||||||
|
|
||||||
var contentBucket = contentBuckets[contentKey] ?? TokenBucket(
|
// Rejected senders must not mint attacker-keyed content entries.
|
||||||
|
guard senderAllowed else { return false }
|
||||||
|
|
||||||
|
var contentBucket = Self.bucket(
|
||||||
|
for: contentKey,
|
||||||
|
in: &contentBuckets,
|
||||||
capacity: contentCapacity,
|
capacity: contentCapacity,
|
||||||
tokens: contentCapacity,
|
|
||||||
refillPerSec: contentRefill,
|
refillPerSec: contentRefill,
|
||||||
lastRefill: now
|
maxBuckets: maxContentBuckets,
|
||||||
|
idleTTL: bucketIdleTTL,
|
||||||
|
now: now
|
||||||
)
|
)
|
||||||
let contentAllowed = contentBucket.allow(now: now)
|
let contentAllowed = contentBucket.allow(now: now)
|
||||||
contentBuckets[contentKey] = contentBucket
|
contentBuckets[contentKey] = contentBucket
|
||||||
|
|
||||||
return senderAllowed && contentAllowed
|
return contentAllowed
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func reset() {
|
||||||
|
senderBuckets.removeAll()
|
||||||
|
contentBuckets.removeAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
var bucketCountsForTesting: (sender: Int, content: Int) {
|
||||||
|
(senderBuckets.count, contentBuckets.count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Static so we can take `inout` on a stored dictionary without overlapping
|
||||||
|
// exclusive access through a mutating method on `self`.
|
||||||
|
private static func bucket(
|
||||||
|
for key: String,
|
||||||
|
in buckets: inout [String: TokenBucket],
|
||||||
|
capacity: Double,
|
||||||
|
refillPerSec: Double,
|
||||||
|
maxBuckets: Int,
|
||||||
|
idleTTL: TimeInterval,
|
||||||
|
now: Date
|
||||||
|
) -> TokenBucket {
|
||||||
|
if let existing = buckets[key] {
|
||||||
|
return existing
|
||||||
|
}
|
||||||
|
|
||||||
|
evictIfNeeded(from: &buckets, maxBuckets: maxBuckets, idleTTL: idleTTL, now: now)
|
||||||
|
return TokenBucket(
|
||||||
|
capacity: capacity,
|
||||||
|
tokens: capacity,
|
||||||
|
refillPerSec: refillPerSec,
|
||||||
|
lastRefill: now
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func evictIfNeeded(
|
||||||
|
from buckets: inout [String: TokenBucket],
|
||||||
|
maxBuckets: Int,
|
||||||
|
idleTTL: TimeInterval,
|
||||||
|
now: Date
|
||||||
|
) {
|
||||||
|
guard buckets.count >= maxBuckets else { return }
|
||||||
|
|
||||||
|
buckets = buckets.filter { !$0.value.isIdle(since: now, idleTTL: idleTTL) }
|
||||||
|
guard buckets.count >= maxBuckets else { return }
|
||||||
|
|
||||||
|
if let oldestKey = buckets.min(by: { $0.value.lastRefill < $1.value.lastRefill })?.key {
|
||||||
|
buckets.removeValue(forKey: oldestKey)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -196,7 +196,10 @@ final class NostrInboundPipeline {
|
|||||||
// Sampled: fires for every geo event and floods dev logs in busy geohashes.
|
// Sampled: fires for every geo event and floods dev logs in busy geohashes.
|
||||||
geoEventLogCount += 1
|
geoEventLogCount += 1
|
||||||
if geoEventLogCount == 1 || geoEventLogCount.isMultiple(of: TransportConfig.nostrInboundEventLogInterval) {
|
if geoEventLogCount == 1 || geoEventLogCount.isMultiple(of: TransportConfig.nostrInboundEventLogInterval) {
|
||||||
SecureLogger.debug("GeoTeleport: recv #\(geoEventLogCount) pub=\(event.pubkey.prefix(8))… pow=\(powBits) tags=\(event.tags.map { "[" + $0.joined(separator: ",") + "]" }.joined(separator: ","))", category: .session)
|
SecureLogger.debug(
|
||||||
|
"GeoTeleport: recv #\(geoEventLogCount) pub=\(event.pubkey.prefix(8))… pow=\(powBits) tagCount=\(event.tags.count)",
|
||||||
|
category: .session
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if context.isNostrBlocked(pubkeyHexLowercased: event.pubkey) {
|
if context.isNostrBlocked(pubkeyHexLowercased: event.pubkey) {
|
||||||
|
|||||||
@@ -188,8 +188,25 @@ final class VoiceRecordingViewModel: ObservableObject {
|
|||||||
|
|
||||||
Task {
|
Task {
|
||||||
let finalDuration = Date().timeIntervalSince(startDate)
|
let finalDuration = Date().timeIntervalSince(startDate)
|
||||||
if let url = await session.finish(),
|
if let url = await session.finish() {
|
||||||
isValidRecording(at: url, duration: finalDuration) {
|
// Panic and a newer hold both invalidate this completion.
|
||||||
|
// Never route an old recording using a post-panic target.
|
||||||
|
guard generation == holdGeneration else {
|
||||||
|
try? FileManager.default.removeItem(at: url)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard isValidRecording(
|
||||||
|
at: url,
|
||||||
|
duration: finalDuration
|
||||||
|
) else {
|
||||||
|
guard state == .idle else { return }
|
||||||
|
state = .error(
|
||||||
|
message: finalDuration < VoiceRecorder.minRecordingDuration
|
||||||
|
? "Recording is too short."
|
||||||
|
: "Recording failed to save."
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
completion(url)
|
completion(url)
|
||||||
} else {
|
} else {
|
||||||
guard generation == holdGeneration, state == .idle else { return }
|
guard generation == holdGeneration, state == .idle else { return }
|
||||||
@@ -206,6 +223,17 @@ final class VoiceRecordingViewModel: ObservableObject {
|
|||||||
finish(completion: nil)
|
finish(completion: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Invalidates in-flight permission/start/finalize callbacks and tears
|
||||||
|
/// down an active microphone before the panic transaction continues.
|
||||||
|
func panicWipe() {
|
||||||
|
holdGeneration &+= 1
|
||||||
|
let session = activeSession
|
||||||
|
activeSession = nil
|
||||||
|
state = .idle
|
||||||
|
isLiveStreaming = false
|
||||||
|
session?.panicCancelSynchronously()
|
||||||
|
}
|
||||||
|
|
||||||
private func isValidRecording(at url: URL, duration: TimeInterval) -> Bool {
|
private func isValidRecording(at url: URL, duration: TimeInterval) -> Bool {
|
||||||
if let attributes = try? FileManager.default.attributesOfItem(atPath: url.path),
|
if let attributes = try? FileManager.default.attributesOfItem(atPath: url.path),
|
||||||
let fileSize = attributes[.size] as? NSNumber,
|
let fileSize = attributes[.size] as? NSNumber,
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ struct AppInfoView: View {
|
|||||||
/// introduction), and afterwards the sheet reopens wherever it was left.
|
/// introduction), and afterwards the sheet reopens wherever it was left.
|
||||||
@AppStorage("appInfo.selectedPane") private var selectedPane: Pane = .info
|
@AppStorage("appInfo.selectedPane") private var selectedPane: Pane = .info
|
||||||
@State private var showPanicConfirmation = false
|
@State private var showPanicConfirmation = false
|
||||||
|
@AppStorage(AppLanguageSettings.overrideKey) private var languageOverride = ""
|
||||||
|
/// The override changed this session; localization resolves at process
|
||||||
|
/// start, so surface the restart hint.
|
||||||
|
@State private var showLanguageRestartNote = false
|
||||||
|
|
||||||
private enum Pane: String {
|
private enum Pane: String {
|
||||||
case settings
|
case settings
|
||||||
@@ -55,6 +59,11 @@ struct AppInfoView: View {
|
|||||||
|
|
||||||
static let connectivityTitle = String(localized: "app_info.settings.connectivity.title", defaultValue: "CONNECTIVITY", comment: "Section header (uppercase) for the connectivity toggles: mesh bridge, internet gateway, tor routing")
|
static let connectivityTitle = String(localized: "app_info.settings.connectivity.title", defaultValue: "CONNECTIVITY", comment: "Section header (uppercase) for the connectivity toggles: mesh bridge, internet gateway, tor routing")
|
||||||
|
|
||||||
|
static let languageTitle = String(localized: "app_info.settings.language.title", defaultValue: "LANGUAGE", comment: "Section header (uppercase) for the app language picker in settings")
|
||||||
|
static let languagePickerLabel = String(localized: "app_info.settings.language.picker_label", defaultValue: "app language", comment: "Label of the app language picker row in settings")
|
||||||
|
static let languageSystem = String(localized: "app_info.settings.language.system", defaultValue: "system default", comment: "Menu option that clears the in-app language override so the app follows the device language")
|
||||||
|
static let languageRestartNote = String(localized: "app_info.settings.language.restart_note", defaultValue: "restart bitchat to apply the new language", comment: "Caption shown after the user picks a different app language; the change takes effect on next launch")
|
||||||
|
|
||||||
static let bridgeTitle = String(localized: "app_info.settings.bridge.title", defaultValue: "mesh bridge", comment: "Title of the mesh bridge toggle in settings")
|
static let bridgeTitle = String(localized: "app_info.settings.bridge.title", defaultValue: "mesh bridge", comment: "Title of the mesh bridge toggle in settings")
|
||||||
static let bridgeSubtitle = String(localized: "app_info.settings.bridge.subtitle", defaultValue: "joins nearby mesh islands over the internet: what you say in the mesh channel also reaches people in your area beyond radio range, and their messages appear here marked with the network glyph. while you have internet, your device also carries bridge and location-channel traffic for phones around you that have none.", comment: "Subtitle explaining what the mesh bridge toggle does")
|
static let bridgeSubtitle = String(localized: "app_info.settings.bridge.subtitle", defaultValue: "joins nearby mesh islands over the internet: what you say in the mesh channel also reaches people in your area beyond radio range, and their messages appear here marked with the network glyph. while you have internet, your device also carries bridge and location-channel traffic for phones around you that have none.", comment: "Subtitle explaining what the mesh bridge toggle does")
|
||||||
static func bridgeCell(_ cell: String) -> String {
|
static func bridgeCell(_ cell: String) -> String {
|
||||||
@@ -313,6 +322,52 @@ struct AppInfoView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Language — an in-app override so the UI language can differ
|
||||||
|
// from the device language (takes effect on next launch).
|
||||||
|
VStack(alignment: .leading, spacing: 12) {
|
||||||
|
SectionHeader(verbatim: Strings.Settings.languageTitle)
|
||||||
|
|
||||||
|
settingsCard {
|
||||||
|
Menu {
|
||||||
|
Button {
|
||||||
|
selectLanguage(nil)
|
||||||
|
} label: {
|
||||||
|
menuItemLabel(Strings.Settings.languageSystem, isSelected: languageOverride.isEmpty)
|
||||||
|
}
|
||||||
|
Divider()
|
||||||
|
ForEach(AppLanguageSettings.availableLanguages, id: \.self) { code in
|
||||||
|
Button {
|
||||||
|
selectLanguage(code)
|
||||||
|
} label: {
|
||||||
|
menuItemLabel(AppLanguageSettings.endonym(for: code), isSelected: languageOverride == code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} label: {
|
||||||
|
HStack {
|
||||||
|
Text(Strings.Settings.languagePickerLabel)
|
||||||
|
.bitchatFont(size: 12, weight: .semibold)
|
||||||
|
.foregroundColor(textColor)
|
||||||
|
Spacer()
|
||||||
|
Text(languageOverride.isEmpty ? Strings.Settings.languageSystem : AppLanguageSettings.endonym(for: languageOverride))
|
||||||
|
.bitchatFont(size: 12)
|
||||||
|
.foregroundColor(palette.accent)
|
||||||
|
Image(systemName: "chevron.up.chevron.down")
|
||||||
|
.font(.system(size: 10))
|
||||||
|
.foregroundColor(secondaryTextColor)
|
||||||
|
}
|
||||||
|
.contentShape(Rectangle())
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
|
||||||
|
if showLanguageRestartNote {
|
||||||
|
Text(Strings.Settings.languageRestartNote)
|
||||||
|
.bitchatFont(size: 11)
|
||||||
|
.foregroundColor(secondaryTextColor)
|
||||||
|
.fixedSize(horizontal: false, vertical: true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Voice — same card + IRC pill as every other toggle setting.
|
// Voice — same card + IRC pill as every other toggle setting.
|
||||||
VStack(alignment: .leading, spacing: 12) {
|
VStack(alignment: .leading, spacing: 12) {
|
||||||
SectionHeader(Strings.Voice.title)
|
SectionHeader(Strings.Voice.title)
|
||||||
@@ -458,6 +513,24 @@ struct AppInfoView: View {
|
|||||||
.padding()
|
.padding()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func selectLanguage(_ code: String?) {
|
||||||
|
let previous = languageOverride
|
||||||
|
AppLanguageSettings.setOverride(code)
|
||||||
|
languageOverride = code ?? ""
|
||||||
|
if languageOverride != previous {
|
||||||
|
showLanguageRestartNote = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func menuItemLabel(_ title: String, isSelected: Bool) -> some View {
|
||||||
|
HStack {
|
||||||
|
Text(title)
|
||||||
|
if isSelected {
|
||||||
|
Image(systemName: "checkmark")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private var bridgeToggleBinding: Binding<Bool> {
|
private var bridgeToggleBinding: Binding<Bool> {
|
||||||
Binding(
|
Binding(
|
||||||
get: { bridgeService.isEnabled },
|
get: { bridgeService.isEnabled },
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ struct TextMessageView: View {
|
|||||||
// first text line; a fixed top padding left the lock's solid body
|
// first text line; a fixed top padding left the lock's solid body
|
||||||
// hanging below the line's visual center.
|
// hanging below the line's visual center.
|
||||||
HStack(alignment: .firstTextBaseline, spacing: 0) {
|
HStack(alignment: .firstTextBaseline, spacing: 0) {
|
||||||
let isLong = (message.content.count > TransportConfig.uiLongMessageLengthThreshold || message.content.hasVeryLongToken(threshold: TransportConfig.uiVeryLongTokenThreshold)) && cashuLinks.isEmpty
|
let isLong = message.content.isLongForDisplay()
|
||||||
let isExpanded = expandedMessageIDs.contains(message.id)
|
let isExpanded = expandedMessageIDs.contains(message.id)
|
||||||
if message.isPrivate {
|
if message.isPrivate {
|
||||||
Image(systemName: "lock.fill")
|
Image(systemName: "lock.fill")
|
||||||
@@ -103,7 +103,7 @@ struct TextMessageView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Expand/Collapse for very long messages
|
// Expand/Collapse for very long messages
|
||||||
if (message.content.count > TransportConfig.uiLongMessageLengthThreshold || message.content.hasVeryLongToken(threshold: TransportConfig.uiVeryLongTokenThreshold)) && cashuLinks.isEmpty {
|
if message.content.isLongForDisplay() {
|
||||||
let isExpanded = expandedMessageIDs.contains(message.id)
|
let isExpanded = expandedMessageIDs.contains(message.id)
|
||||||
let labelKey = isExpanded ? LocalizedStringKey("content.message.show_less") : LocalizedStringKey("content.message.show_more")
|
let labelKey = isExpanded ? LocalizedStringKey("content.message.show_less") : LocalizedStringKey("content.message.show_more")
|
||||||
Button(labelKey) {
|
Button(labelKey) {
|
||||||
|
|||||||
@@ -79,6 +79,9 @@ struct ContentView: View {
|
|||||||
voiceRecordingVM.sessionProvider = { [weak conversationUIModel] in
|
voiceRecordingVM.sessionProvider = { [weak conversationUIModel] in
|
||||||
conversationUIModel?.makeVoiceCaptureSession() ?? VoiceNoteCaptureSession()
|
conversationUIModel?.makeVoiceCaptureSession() ?? VoiceNoteCaptureSession()
|
||||||
}
|
}
|
||||||
|
appChromeModel.setPanicPreparation { [weak voiceRecordingVM] in
|
||||||
|
voiceRecordingVM?.panicWipe()
|
||||||
|
}
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
isNicknameFieldFocused = false
|
isNicknameFieldFocused = false
|
||||||
@@ -229,6 +232,7 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
.onDisappear {
|
.onDisappear {
|
||||||
autocompleteDebounceTimer?.invalidate()
|
autocompleteDebounceTimer?.invalidate()
|
||||||
|
appChromeModel.setPanicPreparation(nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,26 @@ extension String {
|
|||||||
return current >= threshold
|
return current >= threshold
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// True when the message should collapse behind Show more in the UI.
|
||||||
|
/// Length alone decides this — embedding a Cashu-looking token must not
|
||||||
|
/// disable the guard (remote DoS via unbounded layout).
|
||||||
|
func isLongForDisplay(
|
||||||
|
lengthThreshold: Int = TransportConfig.uiLongMessageLengthThreshold,
|
||||||
|
tokenThreshold: Int = TransportConfig.uiVeryLongTokenThreshold
|
||||||
|
) -> Bool {
|
||||||
|
count > lengthThreshold || hasVeryLongToken(threshold: tokenThreshold)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// True when rich formatting (regex / link detectors) should be skipped.
|
||||||
|
/// Cashu presence used to exempt oversized content from the plain path;
|
||||||
|
/// that let untrusted input force expensive formatting work.
|
||||||
|
func isOversizedForRichFormatting(
|
||||||
|
lengthThreshold: Int = 4000,
|
||||||
|
tokenThreshold: Int = 1024
|
||||||
|
) -> Bool {
|
||||||
|
count > lengthThreshold || hasVeryLongToken(threshold: tokenThreshold)
|
||||||
|
}
|
||||||
|
|
||||||
// Extract up to `max` distinct Cashu tokens (cashuA/cashuB), as the bare
|
// Extract up to `max` distinct Cashu tokens (cashuA/cashuB), as the bare
|
||||||
// bearer strings. Allow dot '.' and shorter lengths. The `cashu:` URI
|
// bearer strings. Allow dot '.' and shorter lengths. The `cashu:` URI
|
||||||
// form matches too — the token embedded after the scheme is the match.
|
// form matches too — the token embedded after the scheme is the match.
|
||||||
|
|||||||
@@ -14,11 +14,25 @@ final class PreviewKeychainManager: KeychainManagerProtocol {
|
|||||||
// every default-constructed component under test, which access it from
|
// every default-constructed component under test, which access it from
|
||||||
// arbitrary threads.
|
// arbitrary threads.
|
||||||
private let lock = NSLock()
|
private let lock = NSLock()
|
||||||
|
private let installAccessGate: KeychainInstallAccessGate
|
||||||
|
private let reconcileInstallAccess: () -> Bool
|
||||||
private var storage: [String: Data] = [:]
|
private var storage: [String: Data] = [:]
|
||||||
private var serviceStorage: [String: [String: Data]] = [:]
|
private var serviceStorage: [String: [String: Data]] = [:]
|
||||||
init() {}
|
|
||||||
|
init(
|
||||||
|
installAccessGate: KeychainInstallAccessGate = KeychainInstallAccessGate(),
|
||||||
|
reconcileInstallAccess: @escaping () -> Bool = { true }
|
||||||
|
) {
|
||||||
|
self.installAccessGate = installAccessGate
|
||||||
|
self.reconcileInstallAccess = reconcileInstallAccess
|
||||||
|
}
|
||||||
|
|
||||||
|
private func installAccessAllowed() -> Bool {
|
||||||
|
installAccessGate.allowsAccess(reconcile: reconcileInstallAccess)
|
||||||
|
}
|
||||||
|
|
||||||
func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool {
|
func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool {
|
||||||
|
guard installAccessAllowed() else { return false }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
storage[key] = keyData
|
storage[key] = keyData
|
||||||
@@ -26,12 +40,14 @@ final class PreviewKeychainManager: KeychainManagerProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getIdentityKey(forKey key: String) -> Data? {
|
func getIdentityKey(forKey key: String) -> Data? {
|
||||||
|
guard installAccessAllowed() else { return nil }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
return storage[key]
|
return storage[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteIdentityKey(forKey key: String) -> Bool {
|
func deleteIdentityKey(forKey key: String) -> Bool {
|
||||||
|
guard installAccessAllowed() else { return false }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
storage.removeValue(forKey: key)
|
storage.removeValue(forKey: key)
|
||||||
@@ -51,6 +67,7 @@ final class PreviewKeychainManager: KeychainManagerProtocol {
|
|||||||
func secureClear(_ string: inout String) {}
|
func secureClear(_ string: inout String) {}
|
||||||
|
|
||||||
func verifyIdentityKeyExists() -> Bool {
|
func verifyIdentityKeyExists() -> Bool {
|
||||||
|
guard installAccessAllowed() else { return false }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
return storage["identity_noiseStaticKey"] != nil
|
return storage["identity_noiseStaticKey"] != nil
|
||||||
@@ -58,6 +75,7 @@ final class PreviewKeychainManager: KeychainManagerProtocol {
|
|||||||
|
|
||||||
// BCH-01-009: New methods with proper error classification
|
// BCH-01-009: New methods with proper error classification
|
||||||
func getIdentityKeyWithResult(forKey key: String) -> KeychainReadResult {
|
func getIdentityKeyWithResult(forKey key: String) -> KeychainReadResult {
|
||||||
|
guard installAccessAllowed() else { return .accessDenied }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
if let data = storage[key] {
|
if let data = storage[key] {
|
||||||
@@ -67,6 +85,7 @@ final class PreviewKeychainManager: KeychainManagerProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func saveIdentityKeyWithResult(_ keyData: Data, forKey key: String) -> KeychainSaveResult {
|
func saveIdentityKeyWithResult(_ keyData: Data, forKey key: String) -> KeychainSaveResult {
|
||||||
|
guard installAccessAllowed() else { return .accessDenied }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
storage[key] = keyData
|
storage[key] = keyData
|
||||||
@@ -76,24 +95,38 @@ final class PreviewKeychainManager: KeychainManagerProtocol {
|
|||||||
// MARK: - Generic Data Storage (consolidated from KeychainHelper)
|
// MARK: - Generic Data Storage (consolidated from KeychainHelper)
|
||||||
|
|
||||||
func save(key: String, data: Data, service: String, accessible: CFString?) {
|
func save(key: String, data: Data, service: String, accessible: CFString?) {
|
||||||
|
guard installAccessAllowed() else { return }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
serviceStorage[service, default: [:]][key] = data
|
serviceStorage[service, default: [:]][key] = data
|
||||||
}
|
}
|
||||||
|
|
||||||
func load(key: String, service: String) -> Data? {
|
func load(key: String, service: String) -> Data? {
|
||||||
|
guard case .success(let data) = loadWithResult(key: key, service: service) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadWithResult(key: String, service: String) -> KeychainReadResult {
|
||||||
|
guard installAccessAllowed() else { return .accessDenied }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
return serviceStorage[service]?[key]
|
guard let data = serviceStorage[service]?[key] else {
|
||||||
|
return .itemNotFound
|
||||||
|
}
|
||||||
|
return .success(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func delete(key: String, service: String) {
|
func delete(key: String, service: String) {
|
||||||
|
guard installAccessAllowed() else { return }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
serviceStorage[service]?.removeValue(forKey: key)
|
serviceStorage[service]?.removeValue(forKey: key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteAll(service: String) {
|
func deleteAll(service: String) {
|
||||||
|
guard installAccessAllowed() else { return }
|
||||||
lock.lock()
|
lock.lock()
|
||||||
defer { lock.unlock() }
|
defer { lock.unlock() }
|
||||||
serviceStorage.removeValue(forKey: service)
|
serviceStorage.removeValue(forKey: service)
|
||||||
|
|||||||
@@ -38,6 +38,12 @@
|
|||||||
"comment" : "Fallback title when saving a shared link"
|
"comment" : "Fallback title when saving a shared link"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"fa" : {
|
||||||
|
"stringUnit" : {
|
||||||
|
"state" : "translated",
|
||||||
|
"value" : "پیوند اشتراکگذاریشده"
|
||||||
|
}
|
||||||
|
},
|
||||||
"fil" : {
|
"fil" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
"state" : "needs_review",
|
"state" : "needs_review",
|
||||||
@@ -233,6 +239,12 @@
|
|||||||
"comment" : "Shown when the share payload cannot be encoded"
|
"comment" : "Shown when the share payload cannot be encoded"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"fa" : {
|
||||||
|
"stringUnit" : {
|
||||||
|
"state" : "translated",
|
||||||
|
"value" : "کدگذاری پیوند ناموفق بود"
|
||||||
|
}
|
||||||
|
},
|
||||||
"fil" : {
|
"fil" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
"state" : "needs_review",
|
"state" : "needs_review",
|
||||||
@@ -428,6 +440,12 @@
|
|||||||
"comment" : "Shown when provided content cannot be shared"
|
"comment" : "Shown when provided content cannot be shared"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"fa" : {
|
||||||
|
"stringUnit" : {
|
||||||
|
"state" : "translated",
|
||||||
|
"value" : "محتوای قابل اشتراکگذاری وجود ندارد"
|
||||||
|
}
|
||||||
|
},
|
||||||
"fil" : {
|
"fil" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
"state" : "needs_review",
|
"state" : "needs_review",
|
||||||
@@ -623,6 +641,12 @@
|
|||||||
"comment" : "Shown when the share extension receives no content"
|
"comment" : "Shown when the share extension receives no content"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"fa" : {
|
||||||
|
"stringUnit" : {
|
||||||
|
"state" : "translated",
|
||||||
|
"value" : "چیزی برای اشتراکگذاری نیست"
|
||||||
|
}
|
||||||
|
},
|
||||||
"fil" : {
|
"fil" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
"state" : "needs_review",
|
"state" : "needs_review",
|
||||||
@@ -818,6 +842,12 @@
|
|||||||
"comment" : "Confirmation after successfully sharing a link"
|
"comment" : "Confirmation after successfully sharing a link"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"fa" : {
|
||||||
|
"stringUnit" : {
|
||||||
|
"state" : "translated",
|
||||||
|
"value" : "✓ پیوند در bitchat به اشتراک گذاشته شد"
|
||||||
|
}
|
||||||
|
},
|
||||||
"fil" : {
|
"fil" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
"state" : "needs_review",
|
"state" : "needs_review",
|
||||||
@@ -1013,6 +1043,12 @@
|
|||||||
"comment" : "Confirmation after successfully sharing text"
|
"comment" : "Confirmation after successfully sharing text"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"fa" : {
|
||||||
|
"stringUnit" : {
|
||||||
|
"state" : "translated",
|
||||||
|
"value" : "✓ متن در bitchat به اشتراک گذاشته شد"
|
||||||
|
}
|
||||||
|
},
|
||||||
"fil" : {
|
"fil" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
"state" : "needs_review",
|
"state" : "needs_review",
|
||||||
|
|||||||
@@ -147,6 +147,44 @@ struct AppArchitectureTests {
|
|||||||
#expect(store.teleportedGeo.isEmpty)
|
#expect(store.teleportedGeo.isEmpty)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test("LocationPresenceStore bounds and prunes teleported geohash participants")
|
||||||
|
@MainActor
|
||||||
|
func locationPresenceStoreBoundsTeleportedParticipants() {
|
||||||
|
let store = LocationPresenceStore(teleportedGeoCapacity: 2)
|
||||||
|
|
||||||
|
store.setCurrentGeohash("u4pruy")
|
||||||
|
store.markTeleported("AAAAAA")
|
||||||
|
store.markTeleported("BBBBBB")
|
||||||
|
store.markTeleported("CCCCCC")
|
||||||
|
|
||||||
|
#expect(store.teleportedGeo == Set(["bbbbbb", "cccccc"]))
|
||||||
|
|
||||||
|
store.retainTeleportedGeo(keeping: Set(["CCCCCC"]))
|
||||||
|
#expect(store.teleportedGeo == Set(["cccccc"]))
|
||||||
|
|
||||||
|
store.setCurrentGeohash("u4pruz")
|
||||||
|
#expect(store.teleportedGeo.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test("LocationPresenceStore bounds geohash nicknames and clears on channel switch")
|
||||||
|
@MainActor
|
||||||
|
func locationPresenceStoreBoundsGeoNicknames() {
|
||||||
|
let store = LocationPresenceStore(geoNicknameCapacity: 2)
|
||||||
|
|
||||||
|
store.setCurrentGeohash("u4pruy")
|
||||||
|
store.setNickname("alice", for: "AAAAAA")
|
||||||
|
store.setNickname("bob", for: "BBBBBB")
|
||||||
|
store.setNickname("carol", for: "CCCCCC")
|
||||||
|
|
||||||
|
#expect(store.geoNicknames == ["bbbbbb": "bob", "cccccc": "carol"])
|
||||||
|
|
||||||
|
store.retainGeoNicknames(keeping: Set(["CCCCCC"]))
|
||||||
|
#expect(store.geoNicknames == ["cccccc": "carol"])
|
||||||
|
|
||||||
|
store.setCurrentGeohash("u4pruz")
|
||||||
|
#expect(store.geoNicknames.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
@Test("PeerHandle equality and hashing use the canonical identity only")
|
@Test("PeerHandle equality and hashing use the canonical identity only")
|
||||||
func peerHandleEqualityUsesCanonicalIdentity() {
|
func peerHandleEqualityUsesCanonicalIdentity() {
|
||||||
let first = PeerHandle(id: "noise:abc123", routingPeerID: PeerID(str: "peer-a"))
|
let first = PeerHandle(id: "noise:abc123", routingPeerID: PeerID(str: "peer-a"))
|
||||||
|
|||||||
@@ -572,6 +572,108 @@ struct BLEServiceCoreTests {
|
|||||||
#expect(ble.myPeerID == PeerID(str: newFingerprint.prefix(16)))
|
#expect(ble.myPeerID == PeerID(str: newFingerprint.prefix(16)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func panicSuspension_dropsLateOutboundWorkUntilCommit() async {
|
||||||
|
let ble = makeService()
|
||||||
|
let outbound = OutboundPacketTap()
|
||||||
|
ble._test_onOutboundPacket = outbound.record
|
||||||
|
let packet = makePublicPacket(
|
||||||
|
content: "late callback",
|
||||||
|
sender: ble.myPeerID,
|
||||||
|
timestamp: UInt64(Date().timeIntervalSince1970 * 1000)
|
||||||
|
)
|
||||||
|
|
||||||
|
ble.suspendForPanicReset()
|
||||||
|
ble.sendPacket(packet)
|
||||||
|
#expect(outbound.count(ofType: .message) == 0)
|
||||||
|
|
||||||
|
ble.completePanicReset(restartServices: false)
|
||||||
|
ble.sendPacket(packet)
|
||||||
|
#expect(outbound.count(ofType: .message) == 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func panicSuspension_invalidatesQueuedMainActorIngress() async {
|
||||||
|
let ble = makeService()
|
||||||
|
let delegate = TransportEventCaptureDelegate()
|
||||||
|
ble.eventDelegate = delegate
|
||||||
|
let message = BitchatMessage(
|
||||||
|
id: "pre-panic-ingress",
|
||||||
|
sender: "Peer",
|
||||||
|
content: "must not survive panic",
|
||||||
|
timestamp: Date(),
|
||||||
|
isRelay: false,
|
||||||
|
isPrivate: true,
|
||||||
|
recipientNickname: "Me",
|
||||||
|
senderPeerID: PeerID(str: "1122334455667788")
|
||||||
|
)
|
||||||
|
|
||||||
|
// The test already owns MainActor, so this task cannot run until the
|
||||||
|
// synchronous panic boundary below has invalidated its generation.
|
||||||
|
ble._test_emitTransportEvent(.messageReceived(message))
|
||||||
|
ble.suspendForPanicReset()
|
||||||
|
await Task.yield()
|
||||||
|
#expect(delegate.messageIDs.isEmpty)
|
||||||
|
|
||||||
|
ble.completePanicReset(restartServices: false)
|
||||||
|
ble._test_emitTransportEvent(.messageReceived(message))
|
||||||
|
await Task.yield()
|
||||||
|
#expect(delegate.messageIDs == [message.id])
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func panicSuspension_rejectsPausedBLEReceiveBeforeMessageQueueHandoff() async {
|
||||||
|
let ble = makeService()
|
||||||
|
let gate = ReceivePacketHandoffGate()
|
||||||
|
ble._test_beforeReceivePacketHandoff = gate.pause
|
||||||
|
ble._test_onReceivePacketHandoff = gate.recordHandoff
|
||||||
|
defer {
|
||||||
|
gate.release()
|
||||||
|
ble._test_beforeReceivePacketHandoff = nil
|
||||||
|
ble._test_onReceivePacketHandoff = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
let sender = PeerID(str: "1122334455667788")
|
||||||
|
let packet = makePublicPacket(
|
||||||
|
content: "must not cross panic",
|
||||||
|
sender: sender,
|
||||||
|
timestamp: UInt64(Date().timeIntervalSince1970 * 1000)
|
||||||
|
)
|
||||||
|
ble._test_handlePacketFromBLEQueue(packet, fromPeerID: sender)
|
||||||
|
#expect(await TestHelpers.waitUntil(
|
||||||
|
{ gate.hasPaused },
|
||||||
|
timeout: TestConstants.longTimeout
|
||||||
|
))
|
||||||
|
|
||||||
|
// Panic closes the lifecycle before waiting for the paused bleQueue
|
||||||
|
// callback. Releasing it afterward lets the callback enqueue its
|
||||||
|
// messageQueue handoff, where the captured generation must be rejected
|
||||||
|
// before packet processing starts.
|
||||||
|
let panicIngressObserver = PanicIngressObserver(service: ble)
|
||||||
|
let didObservePanicClosure = await withCheckedContinuation { continuation in
|
||||||
|
DispatchQueue.global(qos: .userInitiated).async {
|
||||||
|
let didObserveClosure = panicIngressObserver.waitUntilClosed(
|
||||||
|
timeout: TestConstants.defaultTimeout
|
||||||
|
)
|
||||||
|
gate.release()
|
||||||
|
continuation.resume(returning: didObserveClosure)
|
||||||
|
}
|
||||||
|
ble.suspendForPanicReset()
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(didObservePanicClosure)
|
||||||
|
#expect(gate.handoffCount == 0)
|
||||||
|
|
||||||
|
// A packet captured under the reopened lifecycle still crosses the
|
||||||
|
// same handoff, proving the test did not merely disable the hook.
|
||||||
|
ble.completePanicReset(restartServices: false)
|
||||||
|
ble._test_handlePacketFromBLEQueue(packet, fromPeerID: sender)
|
||||||
|
#expect(await TestHelpers.waitUntil(
|
||||||
|
{ gate.handoffCount == 1 },
|
||||||
|
timeout: TestConstants.longTimeout
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
func modifiedServices_rediscoverWhenBitChatServiceIsInvalidated() async throws {
|
func modifiedServices_rediscoverWhenBitChatServiceIsInvalidated() async throws {
|
||||||
let otherService = CBUUID(string: "0000180F-0000-1000-8000-00805F9B34FB")
|
let otherService = CBUUID(string: "0000180F-0000-1000-8000-00805F9B34FB")
|
||||||
@@ -666,6 +768,68 @@ private final class OutboundPacketTap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final class ReceivePacketHandoffGate: @unchecked Sendable {
|
||||||
|
private let condition = NSCondition()
|
||||||
|
private var paused = false
|
||||||
|
private var released = false
|
||||||
|
private var recordedHandoffCount = 0
|
||||||
|
|
||||||
|
var hasPaused: Bool {
|
||||||
|
condition.lock()
|
||||||
|
defer { condition.unlock() }
|
||||||
|
return paused
|
||||||
|
}
|
||||||
|
|
||||||
|
var handoffCount: Int {
|
||||||
|
condition.lock()
|
||||||
|
defer { condition.unlock() }
|
||||||
|
return recordedHandoffCount
|
||||||
|
}
|
||||||
|
|
||||||
|
func pause() {
|
||||||
|
condition.lock()
|
||||||
|
paused = true
|
||||||
|
condition.broadcast()
|
||||||
|
while !released {
|
||||||
|
condition.wait()
|
||||||
|
}
|
||||||
|
condition.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func release() {
|
||||||
|
condition.lock()
|
||||||
|
released = true
|
||||||
|
condition.broadcast()
|
||||||
|
condition.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func recordHandoff() {
|
||||||
|
condition.lock()
|
||||||
|
recordedHandoffCount += 1
|
||||||
|
condition.unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Lets a dedicated dispatch worker observe the lock-protected panic gate
|
||||||
|
/// without treating the full BLE service as generally Sendable.
|
||||||
|
private final class PanicIngressObserver: @unchecked Sendable {
|
||||||
|
private let service: BLEService
|
||||||
|
|
||||||
|
init(service: BLEService) {
|
||||||
|
self.service = service
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitUntilClosed(timeout: TimeInterval) -> Bool {
|
||||||
|
let deadline = DispatchTime.now().uptimeNanoseconds
|
||||||
|
+ UInt64(timeout * 1_000_000_000)
|
||||||
|
while service._test_isPanicIngressOpen,
|
||||||
|
DispatchTime.now().uptimeNanoseconds < deadline {
|
||||||
|
Thread.sleep(forTimeInterval: 0.001)
|
||||||
|
}
|
||||||
|
return !service._test_isPanicIngressOpen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private func makeService() -> BLEService {
|
private func makeService() -> BLEService {
|
||||||
let keychain = MockKeychain()
|
let keychain = MockKeychain()
|
||||||
let identityManager = MockIdentityManager(keychain)
|
let identityManager = MockIdentityManager(keychain)
|
||||||
@@ -724,3 +888,13 @@ private final class PublicCaptureDelegate: BitchatDelegate {
|
|||||||
return publicMessages
|
return publicMessages
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
private final class TransportEventCaptureDelegate: TransportEventDelegate {
|
||||||
|
private(set) var messageIDs: [String] = []
|
||||||
|
|
||||||
|
func didReceiveTransportEvent(_ event: TransportEvent) {
|
||||||
|
guard case .messageReceived(let message) = event else { return }
|
||||||
|
messageIDs.append(message.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,6 +16,11 @@
|
|||||||
import Testing
|
import Testing
|
||||||
import Foundation
|
import Foundation
|
||||||
import BitFoundation
|
import BitFoundation
|
||||||
|
#if os(iOS)
|
||||||
|
import UIKit
|
||||||
|
#else
|
||||||
|
import AppKit
|
||||||
|
#endif
|
||||||
@testable import bitchat
|
@testable import bitchat
|
||||||
|
|
||||||
// MARK: - Mock Context
|
// MARK: - Mock Context
|
||||||
@@ -188,6 +193,114 @@ struct ChatMediaTransferCoordinatorContextTests {
|
|||||||
#expect(coordinator.messageIDToTransferId.isEmpty)
|
#expect(coordinator.messageIDToTransferId.isEmpty)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func resetForPanic_cancelsEveryTransportTransferAndClearsMappings() {
|
||||||
|
let context = MockChatMediaTransferContext()
|
||||||
|
let coordinator = ChatMediaTransferCoordinator(context: context)
|
||||||
|
coordinator.registerTransfer(transferId: "t1", messageID: "m1")
|
||||||
|
coordinator.registerTransfer(transferId: "t1", messageID: "m2")
|
||||||
|
coordinator.registerTransfer(transferId: "t2", messageID: "m3")
|
||||||
|
|
||||||
|
coordinator.resetForPanic()
|
||||||
|
|
||||||
|
#expect(Set(context.cancelledTransfers) == Set(["t1", "t2"]))
|
||||||
|
#expect(coordinator.transferIdToMessageIDs.isEmpty)
|
||||||
|
#expect(coordinator.messageIDToTransferId.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func resetForPanic_waitsForActiveImageWriterBeforeReturning() async throws {
|
||||||
|
let context = MockChatMediaTransferContext()
|
||||||
|
let sourceURL = try makeCoordinatorTestImageURL()
|
||||||
|
let outputURL = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent("panic-prepared-\(UUID().uuidString).jpg")
|
||||||
|
let preparer = PausedImagePreparer(outputURL: outputURL)
|
||||||
|
let coordinator = ChatMediaTransferCoordinator(
|
||||||
|
context: context,
|
||||||
|
prepareImagePacket: { sourceURL in
|
||||||
|
try preparer.prepare(sourceURL)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
defer {
|
||||||
|
preparer.release()
|
||||||
|
try? FileManager.default.removeItem(at: sourceURL)
|
||||||
|
try? FileManager.default.removeItem(at: outputURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
coordinator.sendImage(from: sourceURL)
|
||||||
|
#expect(await TestHelpers.waitUntil(
|
||||||
|
{ preparer.hasStarted },
|
||||||
|
timeout: TestConstants.longTimeout
|
||||||
|
))
|
||||||
|
|
||||||
|
DispatchQueue.global(qos: .userInitiated).asyncAfter(
|
||||||
|
deadline: .now() + .milliseconds(100)
|
||||||
|
) {
|
||||||
|
preparer.release()
|
||||||
|
}
|
||||||
|
coordinator.resetForPanic()
|
||||||
|
|
||||||
|
// The synchronous reset boundary cannot return while a pre-panic
|
||||||
|
// writer can still create output. The real panic path deletes media
|
||||||
|
// immediately after this method returns.
|
||||||
|
#expect(preparer.hasFinished)
|
||||||
|
|
||||||
|
try? FileManager.default.removeItem(at: outputURL)
|
||||||
|
#expect(await TestHelpers.waitUntil(
|
||||||
|
{ !FileManager.default.fileExists(atPath: outputURL.path) },
|
||||||
|
timeout: TestConstants.longTimeout
|
||||||
|
))
|
||||||
|
await Task.yield()
|
||||||
|
#expect(context.privateFileSends.isEmpty)
|
||||||
|
#expect(context.broadcastFileSends.isEmpty)
|
||||||
|
#expect(context.systemMessages.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func imagePreparation_doesNotRetainCoordinatorOrDeallocatedContext() async throws {
|
||||||
|
let sourceURL = try makeCoordinatorTestImageURL()
|
||||||
|
let outputURL = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent("released-context-\(UUID().uuidString).jpg")
|
||||||
|
let preparer = PausedImagePreparer(outputURL: outputURL)
|
||||||
|
var context: MockChatMediaTransferContext? = MockChatMediaTransferContext()
|
||||||
|
var coordinator: ChatMediaTransferCoordinator? = ChatMediaTransferCoordinator(
|
||||||
|
context: context!,
|
||||||
|
prepareImagePacket: { sourceURL in
|
||||||
|
try preparer.prepare(sourceURL)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
weak var weakContext: MockChatMediaTransferContext?
|
||||||
|
weak var weakCoordinator: ChatMediaTransferCoordinator?
|
||||||
|
weakContext = context
|
||||||
|
weakCoordinator = coordinator
|
||||||
|
defer {
|
||||||
|
preparer.release()
|
||||||
|
try? FileManager.default.removeItem(at: sourceURL)
|
||||||
|
try? FileManager.default.removeItem(at: outputURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
coordinator?.sendImage(from: sourceURL)
|
||||||
|
#expect(await TestHelpers.waitUntil(
|
||||||
|
{ preparer.hasStarted },
|
||||||
|
timeout: TestConstants.longTimeout
|
||||||
|
))
|
||||||
|
|
||||||
|
coordinator = nil
|
||||||
|
context = nil
|
||||||
|
#expect(weakCoordinator == nil)
|
||||||
|
#expect(weakContext == nil)
|
||||||
|
|
||||||
|
preparer.release()
|
||||||
|
#expect(await TestHelpers.waitUntil(
|
||||||
|
{ preparer.hasFinished },
|
||||||
|
timeout: TestConstants.longTimeout
|
||||||
|
))
|
||||||
|
#expect(await TestHelpers.waitUntil(
|
||||||
|
{ !FileManager.default.fileExists(atPath: outputURL.path) },
|
||||||
|
timeout: TestConstants.longTimeout
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
@Test @MainActor
|
@Test @MainActor
|
||||||
func sendVoiceNote_blockedContextRemovesFileAndExplains() async throws {
|
func sendVoiceNote_blockedContextRemovesFileAndExplains() async throws {
|
||||||
let context = MockChatMediaTransferContext()
|
let context = MockChatMediaTransferContext()
|
||||||
@@ -207,3 +320,91 @@ struct ChatMediaTransferCoordinatorContextTests {
|
|||||||
#expect(coordinator.transferIdToMessageIDs.isEmpty)
|
#expect(coordinator.transferIdToMessageIDs.isEmpty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final class PausedImagePreparer: @unchecked Sendable {
|
||||||
|
private let condition = NSCondition()
|
||||||
|
private let outputURL: URL
|
||||||
|
private var started = false
|
||||||
|
private var released = false
|
||||||
|
private var finished = false
|
||||||
|
|
||||||
|
init(outputURL: URL) {
|
||||||
|
self.outputURL = outputURL
|
||||||
|
}
|
||||||
|
|
||||||
|
var hasStarted: Bool {
|
||||||
|
condition.lock()
|
||||||
|
defer { condition.unlock() }
|
||||||
|
return started
|
||||||
|
}
|
||||||
|
|
||||||
|
var hasFinished: Bool {
|
||||||
|
condition.lock()
|
||||||
|
defer { condition.unlock() }
|
||||||
|
return finished
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepare(_ _: URL) throws -> ChatPreparedImage {
|
||||||
|
condition.lock()
|
||||||
|
started = true
|
||||||
|
condition.broadcast()
|
||||||
|
while !released {
|
||||||
|
condition.wait()
|
||||||
|
}
|
||||||
|
condition.unlock()
|
||||||
|
|
||||||
|
let data = Data("prepared image".utf8)
|
||||||
|
try data.write(to: outputURL, options: .atomic)
|
||||||
|
let packet = BitchatFilePacket(
|
||||||
|
fileName: outputURL.lastPathComponent,
|
||||||
|
fileSize: UInt64(data.count),
|
||||||
|
mimeType: "image/jpeg",
|
||||||
|
content: data
|
||||||
|
)
|
||||||
|
|
||||||
|
condition.lock()
|
||||||
|
finished = true
|
||||||
|
condition.broadcast()
|
||||||
|
condition.unlock()
|
||||||
|
return ChatPreparedImage(outputURL: outputURL, packet: packet)
|
||||||
|
}
|
||||||
|
|
||||||
|
func release() {
|
||||||
|
condition.lock()
|
||||||
|
released = true
|
||||||
|
condition.broadcast()
|
||||||
|
condition.unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func makeCoordinatorTestImageURL() throws -> URL {
|
||||||
|
let url = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent("coordinator-image-\(UUID().uuidString).png")
|
||||||
|
#if os(iOS)
|
||||||
|
let image = UIGraphicsImageRenderer(size: CGSize(width: 16, height: 16))
|
||||||
|
.image { context in
|
||||||
|
UIColor.systemBlue.setFill()
|
||||||
|
context.fill(CGRect(x: 0, y: 0, width: 16, height: 16))
|
||||||
|
}
|
||||||
|
guard let data = image.pngData() else {
|
||||||
|
throw CoordinatorImageTestError.encodingFailed
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
let image = NSImage(size: NSSize(width: 16, height: 16))
|
||||||
|
image.lockFocus()
|
||||||
|
NSColor.systemBlue.setFill()
|
||||||
|
NSRect(x: 0, y: 0, width: 16, height: 16).fill()
|
||||||
|
image.unlockFocus()
|
||||||
|
guard let tiff = image.tiffRepresentation,
|
||||||
|
let bitmap = NSBitmapImageRep(data: tiff),
|
||||||
|
let data = bitmap.representation(using: .png, properties: [:]) else {
|
||||||
|
throw CoordinatorImageTestError.encodingFailed
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
try data.write(to: url, options: .atomic)
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum CoordinatorImageTestError: Error {
|
||||||
|
case encodingFailed
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,8 +15,13 @@ import BitFoundation
|
|||||||
|
|
||||||
/// Creates a ChatViewModel with mock dependencies for testing
|
/// Creates a ChatViewModel with mock dependencies for testing
|
||||||
@MainActor
|
@MainActor
|
||||||
private func makeTestableViewModel() -> (viewModel: ChatViewModel, transport: MockTransport) {
|
private func makeTestableViewModel(
|
||||||
let keychain = MockKeychain()
|
keychain injectedKeychain: MockKeychain? = nil,
|
||||||
|
panicMediaWipe: (() throws -> Void)? = nil,
|
||||||
|
panicRecoveryOperations: PanicRecoveryOperations? = nil,
|
||||||
|
panicNetworkLifecycle: PanicNetworkLifecycle = .noop
|
||||||
|
) -> (viewModel: ChatViewModel, transport: MockTransport) {
|
||||||
|
let keychain = injectedKeychain ?? MockKeychain()
|
||||||
let keychainHelper = MockKeychainHelper()
|
let keychainHelper = MockKeychainHelper()
|
||||||
let idBridge = NostrIdentityBridge(keychain: keychainHelper)
|
let idBridge = NostrIdentityBridge(keychain: keychainHelper)
|
||||||
let identityManager = MockIdentityManager(keychain)
|
let identityManager = MockIdentityManager(keychain)
|
||||||
@@ -26,7 +31,10 @@ private func makeTestableViewModel() -> (viewModel: ChatViewModel, transport: Mo
|
|||||||
keychain: keychain,
|
keychain: keychain,
|
||||||
idBridge: idBridge,
|
idBridge: idBridge,
|
||||||
identityManager: identityManager,
|
identityManager: identityManager,
|
||||||
transport: transport
|
transport: transport,
|
||||||
|
panicMediaWipe: panicMediaWipe,
|
||||||
|
panicRecoveryOperations: panicRecoveryOperations,
|
||||||
|
panicNetworkLifecycle: panicNetworkLifecycle
|
||||||
)
|
)
|
||||||
|
|
||||||
return (viewModel, transport)
|
return (viewModel, transport)
|
||||||
@@ -646,6 +654,25 @@ struct ChatViewModelFormattingTests {
|
|||||||
#expect(String(formatted.characters) == "<@Alice#a1b2> hello #mesh [\(message.formattedTimestamp)]")
|
#expect(String(formatted.characters) == "<@Alice#a1b2> hello #mesh [\(message.formattedTimestamp)]")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func formatMessageAsText_longCashuFallsBackToPlain() async {
|
||||||
|
let (viewModel, _) = makeTestableViewModel()
|
||||||
|
let cashu = "cashuA" + String(repeating: "a", count: 40)
|
||||||
|
let longContent = "hi @bob " + cashu + " " + String(repeating: "x", count: 4_100)
|
||||||
|
let message = BitchatMessage(
|
||||||
|
id: "fmt-long-cashu",
|
||||||
|
sender: "Alice#a1b2",
|
||||||
|
content: longContent,
|
||||||
|
timestamp: Date(timeIntervalSince1970: 1_700_010_123),
|
||||||
|
isRelay: false,
|
||||||
|
senderPeerID: PeerID(str: "00000000000000b3")
|
||||||
|
)
|
||||||
|
|
||||||
|
let formatted = viewModel.formatMessageAsText(message, colorScheme: .light)
|
||||||
|
|
||||||
|
#expect(String(formatted.characters) == "<@Alice#a1b2> \(longContent) [\(message.formattedTimestamp)]")
|
||||||
|
}
|
||||||
|
|
||||||
@Test @MainActor
|
@Test @MainActor
|
||||||
func formatMessageHeader_formatsSenderHeader() async {
|
func formatMessageHeader_formatsSenderHeader() async {
|
||||||
let (viewModel, _) = makeTestableViewModel()
|
let (viewModel, _) = makeTestableViewModel()
|
||||||
@@ -1097,6 +1124,159 @@ struct ChatViewModelBluetoothTests {
|
|||||||
|
|
||||||
struct ChatViewModelPanicTests {
|
struct ChatViewModelPanicTests {
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func panicClearAllData_finishesMediaWipeBeforeReturning() {
|
||||||
|
var wipeFinished = false
|
||||||
|
let (viewModel, _) = makeTestableViewModel(panicMediaWipe: {
|
||||||
|
wipeFinished = true
|
||||||
|
})
|
||||||
|
|
||||||
|
viewModel.panicClearAllData()
|
||||||
|
|
||||||
|
#expect(wipeFinished)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func panicClearAllData_stopsNetworkBeforeWipeAndRestartsAfterCommit() {
|
||||||
|
var events: [String] = []
|
||||||
|
let lifecycle = PanicNetworkLifecycle(
|
||||||
|
stop: { events.append("stop") },
|
||||||
|
restart: { events.append("restart") }
|
||||||
|
)
|
||||||
|
let (viewModel, _) = makeTestableViewModel(
|
||||||
|
panicMediaWipe: { events.append("wipe") },
|
||||||
|
panicNetworkLifecycle: lifecycle
|
||||||
|
)
|
||||||
|
|
||||||
|
let completed = viewModel.panicClearAllData()
|
||||||
|
|
||||||
|
#expect(completed)
|
||||||
|
#expect(events == ["stop", "wipe", "restart"])
|
||||||
|
#expect(viewModel.networkActivationAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func panicKeychainFailureKeepsRecoveryPendingAndServicesStopped() {
|
||||||
|
let keychain = MockKeychain()
|
||||||
|
keychain.simulatedDeleteAllResult = false
|
||||||
|
var events: [String] = []
|
||||||
|
let operations = PanicRecoveryOperations(
|
||||||
|
isPending: { false },
|
||||||
|
begin: {
|
||||||
|
events.append("begin")
|
||||||
|
return PanicRecoveryIntent(
|
||||||
|
fileMarkerEstablished: true,
|
||||||
|
externalMarkerEstablished: false
|
||||||
|
)
|
||||||
|
},
|
||||||
|
wipeMedia: { _ in events.append("wipe") },
|
||||||
|
complete: { events.append("complete") }
|
||||||
|
)
|
||||||
|
let lifecycle = PanicNetworkLifecycle(
|
||||||
|
stop: { events.append("stop") },
|
||||||
|
restart: { events.append("restart") }
|
||||||
|
)
|
||||||
|
let (viewModel, transport) = makeTestableViewModel(
|
||||||
|
keychain: keychain,
|
||||||
|
panicRecoveryOperations: operations,
|
||||||
|
panicNetworkLifecycle: lifecycle
|
||||||
|
)
|
||||||
|
let startsBeforePanic = transport.startServicesCallCount
|
||||||
|
|
||||||
|
let completed = viewModel.panicClearAllData()
|
||||||
|
|
||||||
|
#expect(!completed)
|
||||||
|
#expect(events == ["stop", "begin", "wipe"])
|
||||||
|
#expect(keychain.deleteAllCallCount == 1)
|
||||||
|
#expect(transport.startServicesCallCount == startsBeforePanic)
|
||||||
|
#expect(!viewModel.networkActivationAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func pendingPanicRecoveryCompletesBeforeTransportBootstrap() {
|
||||||
|
var events: [String] = []
|
||||||
|
let operations = PanicRecoveryOperations(
|
||||||
|
isPending: {
|
||||||
|
events.append("read")
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
begin: {
|
||||||
|
events.append("begin")
|
||||||
|
return PanicRecoveryIntent(
|
||||||
|
fileMarkerEstablished: true,
|
||||||
|
externalMarkerEstablished: false
|
||||||
|
)
|
||||||
|
},
|
||||||
|
wipeMedia: { _ in events.append("wipe") },
|
||||||
|
complete: { events.append("complete") }
|
||||||
|
)
|
||||||
|
|
||||||
|
let (viewModel, transport) = makeTestableViewModel(
|
||||||
|
panicRecoveryOperations: operations
|
||||||
|
)
|
||||||
|
|
||||||
|
#expect(events == ["read", "begin", "wipe", "complete"])
|
||||||
|
#expect(transport.emergencyDisconnectCallCount == 1)
|
||||||
|
#expect(transport.startServicesCallCount == 1)
|
||||||
|
#expect(viewModel.networkActivationAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func failedStartupRecoveryLeavesTransportAndNetworkBlocked() {
|
||||||
|
enum WipeFailure: Error { case failed }
|
||||||
|
var completedMarker = false
|
||||||
|
let operations = PanicRecoveryOperations(
|
||||||
|
isPending: { true },
|
||||||
|
begin: {
|
||||||
|
PanicRecoveryIntent(
|
||||||
|
fileMarkerEstablished: true,
|
||||||
|
externalMarkerEstablished: false
|
||||||
|
)
|
||||||
|
},
|
||||||
|
wipeMedia: { _ in throw WipeFailure.failed },
|
||||||
|
complete: { completedMarker = true }
|
||||||
|
)
|
||||||
|
|
||||||
|
let (viewModel, transport) = makeTestableViewModel(
|
||||||
|
panicRecoveryOperations: operations
|
||||||
|
)
|
||||||
|
|
||||||
|
#expect(!completedMarker)
|
||||||
|
#expect(transport.emergencyDisconnectCallCount == 1)
|
||||||
|
#expect(transport.startServicesCallCount == 0)
|
||||||
|
#expect(!viewModel.networkActivationAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test @MainActor
|
||||||
|
func failedStartupKeychainRecoveryLeavesIntentAndTransportBlocked() {
|
||||||
|
let keychain = MockKeychain()
|
||||||
|
keychain.simulatedDeleteAllResult = false
|
||||||
|
var events: [String] = []
|
||||||
|
let operations = PanicRecoveryOperations(
|
||||||
|
isPending: { true },
|
||||||
|
begin: {
|
||||||
|
events.append("begin")
|
||||||
|
return PanicRecoveryIntent(
|
||||||
|
fileMarkerEstablished: true,
|
||||||
|
externalMarkerEstablished: true
|
||||||
|
)
|
||||||
|
},
|
||||||
|
wipeMedia: { _ in events.append("wipe") },
|
||||||
|
complete: { events.append("complete") }
|
||||||
|
)
|
||||||
|
|
||||||
|
let (viewModel, transport) = makeTestableViewModel(
|
||||||
|
keychain: keychain,
|
||||||
|
panicRecoveryOperations: operations
|
||||||
|
)
|
||||||
|
|
||||||
|
#expect(events == ["begin", "wipe"])
|
||||||
|
#expect(keychain.deleteAllCallCount == 1)
|
||||||
|
#expect(transport.emergencyDisconnectCallCount == 1)
|
||||||
|
#expect(transport.startServicesCallCount == 0)
|
||||||
|
#expect(!viewModel.networkActivationAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
@Test @MainActor
|
@Test @MainActor
|
||||||
func panicClearAllData_delegatesToTransport() async {
|
func panicClearAllData_delegatesToTransport() async {
|
||||||
let (viewModel, transport) = makeTestableViewModel()
|
let (viewModel, transport) = makeTestableViewModel()
|
||||||
|
|||||||
@@ -323,6 +323,32 @@ struct MessageFormattingEngineTests {
|
|||||||
// Exactly at threshold DOES trigger (uses >= comparison)
|
// Exactly at threshold DOES trigger (uses >= comparison)
|
||||||
#expect(content.hasVeryLongToken(threshold: 50))
|
#expect(content.hasVeryLongToken(threshold: 50))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test func isLongForDisplay_doesNotIgnoreCashuLinks() {
|
||||||
|
let cashu = "cashuA" + String(repeating: "a", count: 40)
|
||||||
|
let content = String(repeating: "a", count: TransportConfig.uiLongMessageLengthThreshold + 1) + " " + cashu
|
||||||
|
|
||||||
|
#expect(content.extractCashuLinks().count == 1)
|
||||||
|
#expect(content.isLongForDisplay())
|
||||||
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
@Test func formatMessage_longCashuMessageFallsBackToPlainContentPath() {
|
||||||
|
let context = MockMessageFormattingContext(nickname: "carol")
|
||||||
|
let cashu = "cashuA" + String(repeating: "a", count: 40)
|
||||||
|
let longContent = "hi @bob " + cashu + " " + String(repeating: "x", count: 4_100)
|
||||||
|
let message = BitchatMessage(
|
||||||
|
id: "long-cashu",
|
||||||
|
sender: "alice",
|
||||||
|
content: longContent,
|
||||||
|
timestamp: Date(timeIntervalSince1970: 1_700_000_999),
|
||||||
|
isRelay: false
|
||||||
|
)
|
||||||
|
|
||||||
|
let formatted = MessageFormattingEngine.formatMessage(message, context: context, colorScheme: .light)
|
||||||
|
|
||||||
|
#expect(String(formatted.characters) == "<@alice> \(longContent) [\(message.formattedTimestamp)]")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
|
|||||||
@@ -116,4 +116,87 @@ struct MessageRateLimiterTests {
|
|||||||
#expect(plain)
|
#expect(plain)
|
||||||
#expect(!plainExhausted)
|
#expect(!plainExhausted)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test("Content buckets do not grow when sender is rate limited")
|
||||||
|
func contentBucketsDoNotGrowAfterSenderLimit() {
|
||||||
|
var limiter = MessageRateLimiter(
|
||||||
|
senderCapacity: 1,
|
||||||
|
senderRefillPerSec: 0,
|
||||||
|
contentCapacity: 1,
|
||||||
|
contentRefillPerSec: 0,
|
||||||
|
maxSenderBuckets: 10,
|
||||||
|
maxContentBuckets: 10,
|
||||||
|
bucketIdleTTL: 60
|
||||||
|
)
|
||||||
|
let now = Date()
|
||||||
|
|
||||||
|
let first = limiter.allow(senderKey: "sender", contentKey: "content-0", now: now)
|
||||||
|
var rejected = true
|
||||||
|
for index in 1...100 {
|
||||||
|
if limiter.allow(senderKey: "sender", contentKey: "content-\(index)", now: now) {
|
||||||
|
rejected = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(first)
|
||||||
|
#expect(rejected)
|
||||||
|
#expect(limiter.bucketCountsForTesting.sender == 1)
|
||||||
|
#expect(limiter.bucketCountsForTesting.content == 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test("Bucket maps evict entries at configured caps")
|
||||||
|
func bucketMapsEvictAtConfiguredCaps() {
|
||||||
|
let maxEntries = 3
|
||||||
|
var limiter = MessageRateLimiter(
|
||||||
|
senderCapacity: 1,
|
||||||
|
senderRefillPerSec: 0,
|
||||||
|
contentCapacity: 1,
|
||||||
|
contentRefillPerSec: 0,
|
||||||
|
maxSenderBuckets: maxEntries,
|
||||||
|
maxContentBuckets: maxEntries,
|
||||||
|
bucketIdleTTL: 60
|
||||||
|
)
|
||||||
|
let now = Date()
|
||||||
|
|
||||||
|
for index in 0..<25 {
|
||||||
|
_ = limiter.allow(
|
||||||
|
senderKey: "sender-\(index)",
|
||||||
|
contentKey: "content-\(index)",
|
||||||
|
now: now.addingTimeInterval(TimeInterval(index))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(limiter.bucketCountsForTesting.sender == maxEntries)
|
||||||
|
#expect(limiter.bucketCountsForTesting.content == maxEntries)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test("PoW bypass still creates content buckets under the cap")
|
||||||
|
func powBypassCreatesBoundedContentBuckets() {
|
||||||
|
let maxEntries = 3
|
||||||
|
var limiter = MessageRateLimiter(
|
||||||
|
senderCapacity: 1,
|
||||||
|
senderRefillPerSec: 0,
|
||||||
|
contentCapacity: 100,
|
||||||
|
contentRefillPerSec: 0,
|
||||||
|
maxSenderBuckets: maxEntries,
|
||||||
|
maxContentBuckets: maxEntries,
|
||||||
|
bucketIdleTTL: 60
|
||||||
|
)
|
||||||
|
let now = Date()
|
||||||
|
|
||||||
|
var allAllowed = true
|
||||||
|
for index in 0..<10 {
|
||||||
|
let allowed = limiter.allow(
|
||||||
|
senderKey: "sender",
|
||||||
|
contentKey: "content-\(index)",
|
||||||
|
powBits: NostrPoW.rateLimitBypassBits,
|
||||||
|
now: now.addingTimeInterval(TimeInterval(index))
|
||||||
|
)
|
||||||
|
if !allowed { allAllowed = false }
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(allAllowed)
|
||||||
|
#expect(limiter.bucketCountsForTesting.sender == 0)
|
||||||
|
#expect(limiter.bucketCountsForTesting.content == maxEntries)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ final class MockKeychain: KeychainManagerProtocol {
|
|||||||
var simulatedReadError: KeychainReadResult?
|
var simulatedReadError: KeychainReadResult?
|
||||||
var simulatedSaveError: KeychainSaveResult?
|
var simulatedSaveError: KeychainSaveResult?
|
||||||
var simulatedGenericReadError: KeychainReadResult?
|
var simulatedGenericReadError: KeychainReadResult?
|
||||||
|
var simulatedDeleteAllResult = true
|
||||||
|
private(set) var deleteAllCallCount = 0
|
||||||
|
|
||||||
func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool {
|
func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool {
|
||||||
storage[key] = keyData
|
storage[key] = keyData
|
||||||
@@ -34,6 +36,8 @@ final class MockKeychain: KeychainManagerProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func deleteAllKeychainData() -> Bool {
|
func deleteAllKeychainData() -> Bool {
|
||||||
|
deleteAllCallCount += 1
|
||||||
|
guard simulatedDeleteAllResult else { return false }
|
||||||
storage.removeAll()
|
storage.removeAll()
|
||||||
serviceStorage.removeAll()
|
serviceStorage.removeAll()
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -290,7 +290,65 @@ struct NostrProtocolTests {
|
|||||||
#expect(object["limit"] as? Int == 42)
|
#expect(object["limit"] as? Int == 42)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test func inboundNostrEventRejectsTooManyTags() throws {
|
||||||
|
var eventDict = Self.validInboundEventDict()
|
||||||
|
eventDict["tags"] = Array(
|
||||||
|
repeating: ["g", "u4pruyd"],
|
||||||
|
count: TransportConfig.nostrMaxEventTags + 1
|
||||||
|
)
|
||||||
|
|
||||||
|
#expect(throws: NostrError.invalidEvent) {
|
||||||
|
_ = try NostrEvent(from: eventDict)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func inboundNostrEventRejectsTooManyTagValues() throws {
|
||||||
|
var eventDict = Self.validInboundEventDict()
|
||||||
|
eventDict["tags"] = [Array(
|
||||||
|
repeating: "value",
|
||||||
|
count: TransportConfig.nostrMaxEventTagValues + 1
|
||||||
|
)]
|
||||||
|
|
||||||
|
#expect(throws: NostrError.invalidEvent) {
|
||||||
|
_ = try NostrEvent(from: eventDict)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func inboundNostrEventRejectsOversizedTagValues() throws {
|
||||||
|
var eventDict = Self.validInboundEventDict()
|
||||||
|
eventDict["tags"] = [[
|
||||||
|
"g",
|
||||||
|
String(repeating: "a", count: TransportConfig.nostrMaxEventTagValueBytes + 1)
|
||||||
|
]]
|
||||||
|
|
||||||
|
#expect(throws: NostrError.invalidEvent) {
|
||||||
|
_ = try NostrEvent(from: eventDict)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func inboundNostrEventAcceptsTagsWithinLimits() throws {
|
||||||
|
var eventDict = Self.validInboundEventDict()
|
||||||
|
eventDict["tags"] = [["g", "u4pruyd"], ["t", "teleport"]]
|
||||||
|
|
||||||
|
let event = try NostrEvent(from: eventDict)
|
||||||
|
|
||||||
|
#expect(event.tags.count == 2)
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Helpers
|
// MARK: - Helpers
|
||||||
|
private static func validInboundEventDict() -> [String: Any] {
|
||||||
|
[
|
||||||
|
"id": String(repeating: "0", count: 64),
|
||||||
|
"pubkey": String(repeating: "1", count: 64),
|
||||||
|
"created_at": 1_234_567,
|
||||||
|
"kind": NostrProtocol.EventKind.ephemeralEvent.rawValue,
|
||||||
|
"tags": [["g", "u4pruyd"]],
|
||||||
|
"content": "hello",
|
||||||
|
"sig": String(repeating: "2", count: 128)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
private static func base64URLDecode(_ s: String) -> Data? {
|
private static func base64URLDecode(_ s: String) -> Data? {
|
||||||
var str = s.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
|
var str = s.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
|
||||||
let rem = str.count % 4
|
let rem = str.count % 4
|
||||||
|
|||||||
@@ -1,10 +1,103 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
import Security
|
||||||
import Testing
|
import Testing
|
||||||
|
import BitFoundation
|
||||||
@testable import bitchat
|
@testable import bitchat
|
||||||
|
|
||||||
@Suite("PreviewKeychainManager Tests")
|
@Suite("PreviewKeychainManager Tests")
|
||||||
struct PreviewKeychainManagerTests {
|
struct PreviewKeychainManagerTests {
|
||||||
|
|
||||||
|
@Test("Install lifecycle distinguishes upgrade, reinstall, bootstrap, and unreadable keychain")
|
||||||
|
func installLifecycleDecision() {
|
||||||
|
#expect(KeychainManager.installLifecycleAction(
|
||||||
|
containerKnowsMarker: true,
|
||||||
|
markerRead: .success(Data([1]))
|
||||||
|
) == .markerPresent)
|
||||||
|
#expect(KeychainManager.installLifecycleAction(
|
||||||
|
containerKnowsMarker: false,
|
||||||
|
markerRead: .success(Data([1]))
|
||||||
|
) == .clearStaleKeys)
|
||||||
|
#expect(KeychainManager.installLifecycleAction(
|
||||||
|
containerKnowsMarker: false,
|
||||||
|
markerRead: .itemNotFound
|
||||||
|
) == .bootstrapMarker)
|
||||||
|
#expect(KeychainManager.installLifecycleAction(
|
||||||
|
containerKnowsMarker: false,
|
||||||
|
markerRead: .deviceLocked
|
||||||
|
) == .retryLater)
|
||||||
|
#expect(KeychainManager.installLifecycleAction(
|
||||||
|
containerKnowsMarker: false,
|
||||||
|
cleanupPending: true,
|
||||||
|
markerRead: .itemNotFound
|
||||||
|
) == .clearStaleKeys)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test("Accessibility migration covers custom services and retries after any incomplete update")
|
||||||
|
func accessibilityMigrationCoversEveryApplicationOwnedService() {
|
||||||
|
let primaryService = "chat.bitchat.test-primary"
|
||||||
|
var visitedServices: [String] = []
|
||||||
|
|
||||||
|
let completed = KeychainManager
|
||||||
|
.migrateAccessibilityForApplicationOwnedServices(
|
||||||
|
primaryService: primaryService
|
||||||
|
) { service in
|
||||||
|
visitedServices.append(service)
|
||||||
|
return service == "chat.bitchat.favorites"
|
||||||
|
? errSecInteractionNotAllowed
|
||||||
|
: errSecItemNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(!completed)
|
||||||
|
#expect(visitedServices.first == primaryService)
|
||||||
|
#expect(Set(visitedServices).isSuperset(of: [
|
||||||
|
"chat.bitchat.nostr",
|
||||||
|
"chat.bitchat.favorites",
|
||||||
|
"chat.bitchat.outbox"
|
||||||
|
]))
|
||||||
|
#expect(Set(visitedServices).count == visitedServices.count)
|
||||||
|
|
||||||
|
let retryCompleted = KeychainManager
|
||||||
|
.migrateAccessibilityForApplicationOwnedServices(
|
||||||
|
primaryService: primaryService
|
||||||
|
) { _ in errSecSuccess }
|
||||||
|
#expect(retryCompleted)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test("Keychain cleanup is complete only when every owned scope is clean")
|
||||||
|
func keychainCleanupRequiresEveryApplicationOwnedService() {
|
||||||
|
let primaryService = "chat.bitchat.test-primary"
|
||||||
|
var visitedServices: [String] = []
|
||||||
|
|
||||||
|
let partialCleanup = KeychainManager
|
||||||
|
.deleteApplicationOwnedKeychainServices(
|
||||||
|
primaryService: primaryService
|
||||||
|
) { service in
|
||||||
|
visitedServices.append(service)
|
||||||
|
return service == "chat.bitchat.outbox"
|
||||||
|
? errSecInteractionNotAllowed
|
||||||
|
: errSecSuccess
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(!partialCleanup)
|
||||||
|
#expect(visitedServices.first == primaryService)
|
||||||
|
#expect(Set(visitedServices).isSuperset(of: [
|
||||||
|
"chat.bitchat.nostr",
|
||||||
|
"chat.bitchat.favorites",
|
||||||
|
"chat.bitchat.outbox"
|
||||||
|
]))
|
||||||
|
#expect(Set(visitedServices).count == visitedServices.count)
|
||||||
|
|
||||||
|
let emptyCleanup = KeychainManager
|
||||||
|
.deleteApplicationOwnedKeychainServices(
|
||||||
|
primaryService: primaryService
|
||||||
|
) { _ in errSecItemNotFound }
|
||||||
|
#expect(emptyCleanup)
|
||||||
|
#expect(KeychainManager.completedApplicationGroupDelete(status: -34018))
|
||||||
|
#expect(!KeychainManager.completedApplicationGroupDelete(
|
||||||
|
status: errSecInteractionNotAllowed
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
@Test("Preview keychain manager stores identity and service-scoped data in memory")
|
@Test("Preview keychain manager stores identity and service-scoped data in memory")
|
||||||
func previewKeychainManagerRoundTripsData() {
|
func previewKeychainManagerRoundTripsData() {
|
||||||
let manager = PreviewKeychainManager()
|
let manager = PreviewKeychainManager()
|
||||||
@@ -51,4 +144,132 @@ struct PreviewKeychainManagerTests {
|
|||||||
Issue.record("Expected preview keychain to be empty after deleteAllKeychainData")
|
Issue.record("Expected preview keychain to be empty after deleteAllKeychainData")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test("Failed reinstall cleanup blocks stale data until a successful retry")
|
||||||
|
func failedReinstallCleanupBlocksEveryNamespaceUntilSuccessfulRetry() {
|
||||||
|
let gate = KeychainInstallAccessGate()
|
||||||
|
var cleanupCanComplete = false
|
||||||
|
var reconciliationAttempts = 0
|
||||||
|
var manager: PreviewKeychainManager!
|
||||||
|
manager = PreviewKeychainManager(
|
||||||
|
installAccessGate: gate
|
||||||
|
) {
|
||||||
|
reconciliationAttempts += 1
|
||||||
|
guard cleanupCanComplete else { return false }
|
||||||
|
return manager.deleteAllKeychainData()
|
||||||
|
}
|
||||||
|
|
||||||
|
let staleIdentity = Data([1, 2, 3])
|
||||||
|
let staleFavorite = Data([4, 5, 6])
|
||||||
|
let staleOutbox = Data([7, 8, 9])
|
||||||
|
let staleCustom = Data([10, 11, 12])
|
||||||
|
#expect(manager.saveIdentityKey(
|
||||||
|
staleIdentity,
|
||||||
|
forKey: "noiseStaticKey"
|
||||||
|
))
|
||||||
|
#expect(manager.saveIdentityKey(
|
||||||
|
staleIdentity,
|
||||||
|
forKey: "identity_noiseStaticKey"
|
||||||
|
))
|
||||||
|
#expect(manager.verifyIdentityKeyExists())
|
||||||
|
manager.save(
|
||||||
|
key: "favorite",
|
||||||
|
data: staleFavorite,
|
||||||
|
service: "chat.bitchat.favorites",
|
||||||
|
accessible: nil
|
||||||
|
)
|
||||||
|
manager.save(
|
||||||
|
key: "outbox",
|
||||||
|
data: staleOutbox,
|
||||||
|
service: "chat.bitchat.outbox",
|
||||||
|
accessible: nil
|
||||||
|
)
|
||||||
|
manager.save(
|
||||||
|
key: "custom",
|
||||||
|
data: staleCustom,
|
||||||
|
service: "chat.bitchat.future-custom",
|
||||||
|
accessible: nil
|
||||||
|
)
|
||||||
|
|
||||||
|
gate.block()
|
||||||
|
|
||||||
|
#expect(manager.getIdentityKey(forKey: "noiseStaticKey") == nil)
|
||||||
|
#expect(!manager.verifyIdentityKeyExists())
|
||||||
|
if case .accessDenied = manager.getIdentityKeyWithResult(
|
||||||
|
forKey: "noiseStaticKey"
|
||||||
|
) {
|
||||||
|
} else {
|
||||||
|
Issue.record("Expected blocked identity read to fail closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
for (key, service) in [
|
||||||
|
("favorite", "chat.bitchat.favorites"),
|
||||||
|
("outbox", "chat.bitchat.outbox"),
|
||||||
|
("custom", "chat.bitchat.future-custom")
|
||||||
|
] {
|
||||||
|
#expect(manager.load(key: key, service: service) == nil)
|
||||||
|
if case .accessDenied = manager.loadWithResult(
|
||||||
|
key: key,
|
||||||
|
service: service
|
||||||
|
) {
|
||||||
|
} else {
|
||||||
|
Issue.record(
|
||||||
|
"Expected blocked \(service) read to fail closed"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(!manager.saveIdentityKey(
|
||||||
|
Data([13]),
|
||||||
|
forKey: "replacement"
|
||||||
|
))
|
||||||
|
if case .accessDenied = manager.saveIdentityKeyWithResult(
|
||||||
|
Data([14]),
|
||||||
|
forKey: "replacement"
|
||||||
|
) {
|
||||||
|
} else {
|
||||||
|
Issue.record("Expected blocked identity save to fail closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
let failedAttempts = reconciliationAttempts
|
||||||
|
#expect(failedAttempts > 0)
|
||||||
|
cleanupCanComplete = true
|
||||||
|
|
||||||
|
// The first access retries cleanup synchronously. It must not return
|
||||||
|
// any surviving value from before the reinstall.
|
||||||
|
#expect(manager.getIdentityKey(forKey: "noiseStaticKey") == nil)
|
||||||
|
#expect(reconciliationAttempts == failedAttempts + 1)
|
||||||
|
#expect(manager.load(
|
||||||
|
key: "favorite",
|
||||||
|
service: "chat.bitchat.favorites"
|
||||||
|
) == nil)
|
||||||
|
#expect(manager.load(
|
||||||
|
key: "outbox",
|
||||||
|
service: "chat.bitchat.outbox"
|
||||||
|
) == nil)
|
||||||
|
#expect(manager.load(
|
||||||
|
key: "custom",
|
||||||
|
service: "chat.bitchat.future-custom"
|
||||||
|
) == nil)
|
||||||
|
|
||||||
|
let replacementIdentity = Data([21, 22, 23])
|
||||||
|
let replacementCustom = Data([24, 25, 26])
|
||||||
|
#expect(manager.saveIdentityKey(
|
||||||
|
replacementIdentity,
|
||||||
|
forKey: "noiseStaticKey"
|
||||||
|
))
|
||||||
|
#expect(manager.getIdentityKey(
|
||||||
|
forKey: "noiseStaticKey"
|
||||||
|
) == replacementIdentity)
|
||||||
|
manager.save(
|
||||||
|
key: "custom",
|
||||||
|
data: replacementCustom,
|
||||||
|
service: "chat.bitchat.future-custom",
|
||||||
|
accessible: nil
|
||||||
|
)
|
||||||
|
#expect(manager.load(
|
||||||
|
key: "custom",
|
||||||
|
service: "chat.bitchat.future-custom"
|
||||||
|
) == replacementCustom)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import Testing
|
|||||||
struct BLEAnnounceThrottleTests {
|
struct BLEAnnounceThrottleTests {
|
||||||
@Test
|
@Test
|
||||||
func firstAnnounceIsAllowed() {
|
func firstAnnounceIsAllowed() {
|
||||||
let throttle = BLEAnnounceThrottle(normalMinimumInterval: 10, forcedMinimumInterval: 2)
|
var throttle = BLEAnnounceThrottle(normalMinimumInterval: 10, forcedMinimumInterval: 2)
|
||||||
|
|
||||||
let shouldSend = throttle.shouldSend(force: false, now: Date(timeIntervalSince1970: 100))
|
let shouldSend = throttle.shouldSend(force: false, now: Date(timeIntervalSince1970: 100))
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ struct BLEAnnounceThrottleTests {
|
|||||||
@Test
|
@Test
|
||||||
func regularAnnounceUsesNormalMinimumInterval() {
|
func regularAnnounceUsesNormalMinimumInterval() {
|
||||||
let now = Date(timeIntervalSince1970: 100)
|
let now = Date(timeIntervalSince1970: 100)
|
||||||
let throttle = BLEAnnounceThrottle(normalMinimumInterval: 10, forcedMinimumInterval: 2)
|
var throttle = BLEAnnounceThrottle(normalMinimumInterval: 10, forcedMinimumInterval: 2)
|
||||||
|
|
||||||
let first = throttle.shouldSend(force: false, now: now)
|
let first = throttle.shouldSend(force: false, now: now)
|
||||||
let suppressed = throttle.shouldSend(force: false, now: now.addingTimeInterval(9.9))
|
let suppressed = throttle.shouldSend(force: false, now: now.addingTimeInterval(9.9))
|
||||||
@@ -29,7 +29,7 @@ struct BLEAnnounceThrottleTests {
|
|||||||
@Test
|
@Test
|
||||||
func forcedAnnounceUsesShorterMinimumInterval() {
|
func forcedAnnounceUsesShorterMinimumInterval() {
|
||||||
let now = Date(timeIntervalSince1970: 100)
|
let now = Date(timeIntervalSince1970: 100)
|
||||||
let throttle = BLEAnnounceThrottle(normalMinimumInterval: 10, forcedMinimumInterval: 2)
|
var throttle = BLEAnnounceThrottle(normalMinimumInterval: 10, forcedMinimumInterval: 2)
|
||||||
|
|
||||||
let first = throttle.shouldSend(force: false, now: now)
|
let first = throttle.shouldSend(force: false, now: now)
|
||||||
let suppressed = throttle.shouldSend(force: true, now: now.addingTimeInterval(1.9))
|
let suppressed = throttle.shouldSend(force: true, now: now.addingTimeInterval(1.9))
|
||||||
@@ -43,40 +43,10 @@ struct BLEAnnounceThrottleTests {
|
|||||||
@Test
|
@Test
|
||||||
func elapsedReportsTimeSinceAcceptedSend() {
|
func elapsedReportsTimeSinceAcceptedSend() {
|
||||||
let now = Date(timeIntervalSince1970: 100)
|
let now = Date(timeIntervalSince1970: 100)
|
||||||
let throttle = BLEAnnounceThrottle(normalMinimumInterval: 10, forcedMinimumInterval: 2)
|
var throttle = BLEAnnounceThrottle(normalMinimumInterval: 10, forcedMinimumInterval: 2)
|
||||||
|
|
||||||
_ = throttle.shouldSend(force: false, now: now)
|
_ = throttle.shouldSend(force: false, now: now)
|
||||||
|
|
||||||
#expect(throttle.elapsed(since: now.addingTimeInterval(3)) == 3)
|
#expect(throttle.elapsed(since: now.addingTimeInterval(3)) == 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
func concurrentRequestsAdmitOnlyOneAnnounce() {
|
|
||||||
let now = Date(timeIntervalSince1970: 100)
|
|
||||||
let throttle = BLEAnnounceThrottle(
|
|
||||||
normalMinimumInterval: 10,
|
|
||||||
forcedMinimumInterval: 2
|
|
||||||
)
|
|
||||||
let accepted = LockedCounter()
|
|
||||||
|
|
||||||
DispatchQueue.concurrentPerform(iterations: 1_000) { _ in
|
|
||||||
if throttle.shouldSend(force: false, now: now) {
|
|
||||||
accepted.increment()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#expect(accepted.value == 1)
|
|
||||||
#expect(throttle.elapsed(since: now.addingTimeInterval(3)) == 3)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final class LockedCounter: @unchecked Sendable {
|
|
||||||
private let lock = NSLock()
|
|
||||||
private var count = 0
|
|
||||||
|
|
||||||
var value: Int { lock.withLock { count } }
|
|
||||||
|
|
||||||
func increment() {
|
|
||||||
lock.withLock { count += 1 }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -370,6 +370,132 @@ struct BLEFileTransferHandlerTests {
|
|||||||
#expect(!FileManager.default.fileExists(atPath: evictable.path))
|
#expect(!FileManager.default.fileExists(atPath: evictable.path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func panicWipeDeletesEveryManagedMediaFileAndRecreatesEmptyDirectories() throws {
|
||||||
|
let base = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent("panic-media-wipe-\(UUID().uuidString)", isDirectory: true)
|
||||||
|
defer { try? FileManager.default.removeItem(at: base) }
|
||||||
|
let store = BLEIncomingFileStore(baseDirectory: base)
|
||||||
|
let subdirectories = [
|
||||||
|
"voicenotes/incoming",
|
||||||
|
"voicenotes/outgoing",
|
||||||
|
"images/incoming",
|
||||||
|
"images/outgoing",
|
||||||
|
"files/incoming",
|
||||||
|
"files/outgoing"
|
||||||
|
]
|
||||||
|
|
||||||
|
for subdirectory in subdirectories {
|
||||||
|
let directory = base
|
||||||
|
.appendingPathComponent("files", isDirectory: true)
|
||||||
|
.appendingPathComponent(subdirectory, isDirectory: true)
|
||||||
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
||||||
|
try Data("secret".utf8).write(to: directory.appendingPathComponent("artifact.bin"))
|
||||||
|
}
|
||||||
|
let unmanaged = base.appendingPathComponent("files/legacy/secret.bin")
|
||||||
|
try FileManager.default.createDirectory(at: unmanaged.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||||||
|
try Data("legacy".utf8).write(to: unmanaged)
|
||||||
|
|
||||||
|
try store.panicWipe()
|
||||||
|
|
||||||
|
#expect(!FileManager.default.fileExists(atPath: unmanaged.path))
|
||||||
|
for subdirectory in subdirectories {
|
||||||
|
let directory = base
|
||||||
|
.appendingPathComponent("files", isDirectory: true)
|
||||||
|
.appendingPathComponent(subdirectory, isDirectory: true)
|
||||||
|
var isDirectory: ObjCBool = false
|
||||||
|
#expect(FileManager.default.fileExists(atPath: directory.path, isDirectory: &isDirectory))
|
||||||
|
#expect(isDirectory.boolValue)
|
||||||
|
#expect(try FileManager.default.contentsOfDirectory(atPath: directory.path).isEmpty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func panicWipeAttemptsDeletionWhenMarkerPersistenceFails() throws {
|
||||||
|
enum MarkerFailure: Error { case unavailable }
|
||||||
|
|
||||||
|
let base = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent(
|
||||||
|
"panic-marker-failure-\(UUID().uuidString)",
|
||||||
|
isDirectory: true
|
||||||
|
)
|
||||||
|
defer { try? FileManager.default.removeItem(at: base) }
|
||||||
|
let secret = base
|
||||||
|
.appendingPathComponent("files/images/outgoing", isDirectory: true)
|
||||||
|
.appendingPathComponent("secret.jpg")
|
||||||
|
try FileManager.default.createDirectory(
|
||||||
|
at: secret.deletingLastPathComponent(),
|
||||||
|
withIntermediateDirectories: true
|
||||||
|
)
|
||||||
|
try Data("secret".utf8).write(to: secret)
|
||||||
|
let store = BLEIncomingFileStore(
|
||||||
|
baseDirectory: base,
|
||||||
|
panicMarkerWriter: { _, _ in throw MarkerFailure.unavailable }
|
||||||
|
)
|
||||||
|
|
||||||
|
do {
|
||||||
|
try store.panicWipe(hasDurablePendingMarker: false)
|
||||||
|
Issue.record("Expected the missing durable marker to fail closed")
|
||||||
|
} catch {
|
||||||
|
// The marker error is reported only after the deletion attempt.
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(!FileManager.default.fileExists(atPath: secret.path))
|
||||||
|
#expect(
|
||||||
|
FileManager.default.fileExists(
|
||||||
|
atPath: secret.deletingLastPathComponent().path
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func externalMarkerAllowsDeletionToCommitWhenFileMarkerFails() throws {
|
||||||
|
enum MarkerFailure: Error { case unavailable }
|
||||||
|
|
||||||
|
let base = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent(
|
||||||
|
"panic-external-marker-\(UUID().uuidString)",
|
||||||
|
isDirectory: true
|
||||||
|
)
|
||||||
|
defer { try? FileManager.default.removeItem(at: base) }
|
||||||
|
let secret = base
|
||||||
|
.appendingPathComponent("files/voicenotes/incoming", isDirectory: true)
|
||||||
|
.appendingPathComponent("secret.m4a")
|
||||||
|
try FileManager.default.createDirectory(
|
||||||
|
at: secret.deletingLastPathComponent(),
|
||||||
|
withIntermediateDirectories: true
|
||||||
|
)
|
||||||
|
try Data("secret".utf8).write(to: secret)
|
||||||
|
let store = BLEIncomingFileStore(
|
||||||
|
baseDirectory: base,
|
||||||
|
panicMarkerWriter: { _, _ in throw MarkerFailure.unavailable }
|
||||||
|
)
|
||||||
|
|
||||||
|
try store.panicWipe(hasDurablePendingMarker: true)
|
||||||
|
|
||||||
|
#expect(!FileManager.default.fileExists(atPath: secret.path))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func panicRecoveryMarkerPersistsUntilExplicitCommit() throws {
|
||||||
|
let base = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent(
|
||||||
|
"panic-recovery-marker-\(UUID().uuidString)",
|
||||||
|
isDirectory: true
|
||||||
|
)
|
||||||
|
defer { try? FileManager.default.removeItem(at: base) }
|
||||||
|
let store = BLEIncomingFileStore(baseDirectory: base)
|
||||||
|
|
||||||
|
try store.markPanicRecoveryPending()
|
||||||
|
#expect(try store.isPanicRecoveryPending())
|
||||||
|
try store.panicWipe(hasDurablePendingMarker: true)
|
||||||
|
#expect(try store.isPanicRecoveryPending())
|
||||||
|
|
||||||
|
try store.completePanicRecovery()
|
||||||
|
|
||||||
|
#expect(try !store.isPanicRecoveryPending())
|
||||||
|
}
|
||||||
|
|
||||||
private func expectNoSideEffects(_ recorder: Recorder) {
|
private func expectNoSideEffects(_ recorder: Recorder) {
|
||||||
#expect(recorder.signedNameQueries.isEmpty)
|
#expect(recorder.signedNameQueries.isEmpty)
|
||||||
#expect(recorder.trackedPackets.isEmpty)
|
#expect(recorder.trackedPackets.isEmpty)
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
import BitFoundation
|
|
||||||
import Foundation
|
|
||||||
import Testing
|
|
||||||
@testable import bitchat
|
|
||||||
|
|
||||||
struct BLELocalIdentityStateStoreTests {
|
|
||||||
@Test
|
|
||||||
func identityReplacementUpdatesWireBytesAtomically() throws {
|
|
||||||
let initial = PeerID(str: "0011223344556677")
|
|
||||||
let replacement = PeerID(str: "8899aabbccddeeff")
|
|
||||||
let store = BLELocalIdentityStateStore(peerID: initial, nickname: "alice")
|
|
||||||
|
|
||||||
store.replacePeerIdentity(with: replacement)
|
|
||||||
|
|
||||||
let snapshot = store.snapshot()
|
|
||||||
#expect(snapshot.peerID == replacement)
|
|
||||||
#expect(snapshot.peerIDData == Data(hexString: replacement.id))
|
|
||||||
#expect(snapshot.nickname == "alice")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
func concurrentReadsNeverObserveSplitIdentityState() {
|
|
||||||
let peerIDs = [
|
|
||||||
PeerID(str: "0011223344556677"),
|
|
||||||
PeerID(str: "8899aabbccddeeff")
|
|
||||||
]
|
|
||||||
let store = BLELocalIdentityStateStore(peerID: peerIDs[0], nickname: "alice")
|
|
||||||
let failures = LockedFailureRecorder()
|
|
||||||
|
|
||||||
DispatchQueue.concurrentPerform(iterations: 2_000) { index in
|
|
||||||
if index.isMultiple(of: 2) {
|
|
||||||
store.replacePeerIdentity(with: peerIDs[index % peerIDs.count])
|
|
||||||
} else {
|
|
||||||
store.setNickname(index.isMultiple(of: 3) ? "alice" : "bob")
|
|
||||||
}
|
|
||||||
|
|
||||||
let snapshot = store.snapshot()
|
|
||||||
let expectedWireID = Data(hexString: snapshot.peerID.id) ?? Data()
|
|
||||||
if snapshot.peerIDData != expectedWireID {
|
|
||||||
failures.record()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#expect(!failures.hasFailure)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final class LockedFailureRecorder: @unchecked Sendable {
|
|
||||||
private let lock = NSLock()
|
|
||||||
private var failed = false
|
|
||||||
|
|
||||||
var hasFailure: Bool { lock.withLock { failed } }
|
|
||||||
|
|
||||||
func record() {
|
|
||||||
lock.withLock { failed = true }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -76,6 +76,7 @@ final class GeohashPresenceServiceTests: XCTestCase {
|
|||||||
burstMaxDelay: 0
|
burstMaxDelay: 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
service.start()
|
||||||
service.performHeartbeat()
|
service.performHeartbeat()
|
||||||
|
|
||||||
let sentAllAllowedChannels = await waitUntil { sentGeohashes.count == 3 }
|
let sentAllAllowedChannels = await waitUntil { sentGeohashes.count == 3 }
|
||||||
@@ -83,7 +84,7 @@ final class GeohashPresenceServiceTests: XCTestCase {
|
|||||||
XCTAssertEqual(Set(sentGeohashes), Set(["9q", "9q8y", "9q8yy"]))
|
XCTAssertEqual(Set(sentGeohashes), Set(["9q", "9q8y", "9q8yy"]))
|
||||||
XCTAssertEqual(Set(lookedUpGeohashes), Set(["9q", "9q8y", "9q8yy"]))
|
XCTAssertEqual(Set(lookedUpGeohashes), Set(["9q", "9q8y", "9q8yy"]))
|
||||||
XCTAssertEqual(sleptNanoseconds.count, 3)
|
XCTAssertEqual(sleptNanoseconds.count, 3)
|
||||||
XCTAssertEqual(scheduler.intervals, [17])
|
XCTAssertEqual(scheduler.intervals, [17, 17])
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_performHeartbeat_skipsBroadcastWhenTorIsNotReady() async {
|
func test_performHeartbeat_skipsBroadcastWhenTorIsNotReady() async {
|
||||||
@@ -97,11 +98,12 @@ final class GeohashPresenceServiceTests: XCTestCase {
|
|||||||
loopMaxInterval: 21
|
loopMaxInterval: 21
|
||||||
)
|
)
|
||||||
|
|
||||||
|
service.start()
|
||||||
service.performHeartbeat()
|
service.performHeartbeat()
|
||||||
try? await Task.sleep(nanoseconds: 20_000_000)
|
try? await Task.sleep(nanoseconds: 20_000_000)
|
||||||
|
|
||||||
XCTAssertEqual(sendCount, 0)
|
XCTAssertEqual(sendCount, 0)
|
||||||
XCTAssertEqual(scheduler.intervals, [21])
|
XCTAssertEqual(scheduler.intervals, [21, 21])
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_performHeartbeat_skipsBroadcastWhenAppIsBackgrounded() async {
|
func test_performHeartbeat_skipsBroadcastWhenAppIsBackgrounded() async {
|
||||||
@@ -115,11 +117,45 @@ final class GeohashPresenceServiceTests: XCTestCase {
|
|||||||
loopMaxInterval: 22
|
loopMaxInterval: 22
|
||||||
)
|
)
|
||||||
|
|
||||||
|
service.start()
|
||||||
service.performHeartbeat()
|
service.performHeartbeat()
|
||||||
try? await Task.sleep(nanoseconds: 20_000_000)
|
try? await Task.sleep(nanoseconds: 20_000_000)
|
||||||
|
|
||||||
XCTAssertEqual(sendCount, 0)
|
XCTAssertEqual(sendCount, 0)
|
||||||
XCTAssertEqual(scheduler.intervals, [22])
|
XCTAssertEqual(scheduler.intervals, [22, 22])
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_stopForPanic_cancelsTimerAndSuppressesDelayedBroadcast() async throws {
|
||||||
|
let identity = try NostrIdentity.generate()
|
||||||
|
let scheduler = MockGeohashPresenceScheduler()
|
||||||
|
var sleeperContinuation: CheckedContinuation<Void, Never>?
|
||||||
|
var sendCount = 0
|
||||||
|
let service = makeService(
|
||||||
|
scheduler: scheduler,
|
||||||
|
deriveIdentity: { _ in identity },
|
||||||
|
relaySender: { _, _ in sendCount += 1 },
|
||||||
|
sleeper: { _ in
|
||||||
|
await withCheckedContinuation { continuation in
|
||||||
|
sleeperContinuation = continuation
|
||||||
|
}
|
||||||
|
},
|
||||||
|
burstMinDelay: 1,
|
||||||
|
burstMaxDelay: 1
|
||||||
|
)
|
||||||
|
|
||||||
|
service.start()
|
||||||
|
service.performHeartbeat()
|
||||||
|
let delayStarted = await waitUntil {
|
||||||
|
sleeperContinuation != nil
|
||||||
|
}
|
||||||
|
XCTAssertTrue(delayStarted)
|
||||||
|
|
||||||
|
service.stopForPanic()
|
||||||
|
sleeperContinuation?.resume()
|
||||||
|
try? await Task.sleep(nanoseconds: 20_000_000)
|
||||||
|
|
||||||
|
XCTAssertEqual(sendCount, 0)
|
||||||
|
XCTAssertEqual(scheduler.timers.first?.invalidateCallCount, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_broadcastPresence_skipsSendWhenNoRelaysAreAvailable() async throws {
|
func test_broadcastPresence_skipsSendWhenNoRelaysAreAvailable() async throws {
|
||||||
|
|||||||
@@ -91,6 +91,53 @@ final class NetworkActivationServiceTests: XCTestCase {
|
|||||||
XCTAssertGreaterThanOrEqual(context.relayController.connectCallCount, 1)
|
XCTAssertGreaterThanOrEqual(context.relayController.connectCallCount, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func test_stopForPanic_synchronouslyStopsAndIgnoresPublisherUpdates() async {
|
||||||
|
let context = makeService(permission: .authorized, favorites: [])
|
||||||
|
|
||||||
|
context.service.start()
|
||||||
|
context.service.stopForPanic()
|
||||||
|
let connectCountAfterStop = context.relayController.connectCallCount
|
||||||
|
let startCountAfterStop = context.torController.startIfNeededCallCount
|
||||||
|
|
||||||
|
context.favoritesSubject.send([Data([0x01])])
|
||||||
|
context.reachability.set(false)
|
||||||
|
context.reachability.set(true)
|
||||||
|
try? await Task.sleep(nanoseconds: 30_000_000)
|
||||||
|
|
||||||
|
XCTAssertFalse(context.service.activationAllowed)
|
||||||
|
XCTAssertEqual(context.reachability.stopCallCount, 1)
|
||||||
|
XCTAssertEqual(context.torController.autoStartAllowedValues.last, false)
|
||||||
|
XCTAssertEqual(context.proxyController.proxyModes.last, false)
|
||||||
|
XCTAssertGreaterThanOrEqual(
|
||||||
|
context.torController.shutdownCompletelyCallCount,
|
||||||
|
1
|
||||||
|
)
|
||||||
|
XCTAssertGreaterThanOrEqual(
|
||||||
|
context.relayController.disconnectCallCount,
|
||||||
|
1
|
||||||
|
)
|
||||||
|
XCTAssertEqual(
|
||||||
|
context.relayController.connectCallCount,
|
||||||
|
connectCountAfterStop
|
||||||
|
)
|
||||||
|
XCTAssertEqual(
|
||||||
|
context.torController.startIfNeededCallCount,
|
||||||
|
startCountAfterStop
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_start_afterPanicStop_reestablishesSubscriptions() {
|
||||||
|
let context = makeService(permission: .authorized, favorites: [])
|
||||||
|
|
||||||
|
context.service.start()
|
||||||
|
context.service.stopForPanic()
|
||||||
|
context.service.start()
|
||||||
|
|
||||||
|
XCTAssertTrue(context.service.activationAllowed)
|
||||||
|
XCTAssertEqual(context.reachability.startCallCount, 2)
|
||||||
|
XCTAssertEqual(context.relayController.connectCallCount, 2)
|
||||||
|
}
|
||||||
|
|
||||||
private func makeService(
|
private func makeService(
|
||||||
permission: LocationChannelManager.PermissionState,
|
permission: LocationChannelManager.PermissionState,
|
||||||
favorites: Set<Data>
|
favorites: Set<Data>
|
||||||
@@ -104,6 +151,7 @@ final class NetworkActivationServiceTests: XCTestCase {
|
|||||||
let torController = MockNetworkActivationTorController()
|
let torController = MockNetworkActivationTorController()
|
||||||
let relayController = MockNetworkActivationRelayController()
|
let relayController = MockNetworkActivationRelayController()
|
||||||
let proxyController = MockNetworkActivationProxyController()
|
let proxyController = MockNetworkActivationProxyController()
|
||||||
|
let reachability = MockNetworkActivationReachability()
|
||||||
let notificationCenter = NotificationCenter()
|
let notificationCenter = NotificationCenter()
|
||||||
let service = NetworkActivationService(
|
let service = NetworkActivationService(
|
||||||
storage: storage,
|
storage: storage,
|
||||||
@@ -111,7 +159,7 @@ final class NetworkActivationServiceTests: XCTestCase {
|
|||||||
mutualFavoritesPublisher: favoritesSubject.eraseToAnyPublisher(),
|
mutualFavoritesPublisher: favoritesSubject.eraseToAnyPublisher(),
|
||||||
permissionProvider: { permissionSubject.value },
|
permissionProvider: { permissionSubject.value },
|
||||||
mutualFavoritesProvider: { favoritesSubject.value },
|
mutualFavoritesProvider: { favoritesSubject.value },
|
||||||
reachabilityMonitor: AlwaysReachableMonitor(),
|
reachabilityMonitor: reachability,
|
||||||
torController: torController,
|
torController: torController,
|
||||||
relayController: relayController,
|
relayController: relayController,
|
||||||
proxyController: proxyController,
|
proxyController: proxyController,
|
||||||
@@ -121,6 +169,7 @@ final class NetworkActivationServiceTests: XCTestCase {
|
|||||||
service: service,
|
service: service,
|
||||||
storage: storage,
|
storage: storage,
|
||||||
favoritesSubject: favoritesSubject,
|
favoritesSubject: favoritesSubject,
|
||||||
|
reachability: reachability,
|
||||||
torController: torController,
|
torController: torController,
|
||||||
relayController: relayController,
|
relayController: relayController,
|
||||||
proxyController: proxyController,
|
proxyController: proxyController,
|
||||||
@@ -148,12 +197,38 @@ private struct NetworkActivationTestContext {
|
|||||||
let service: NetworkActivationService
|
let service: NetworkActivationService
|
||||||
let storage: UserDefaults
|
let storage: UserDefaults
|
||||||
let favoritesSubject: CurrentValueSubject<Set<Data>, Never>
|
let favoritesSubject: CurrentValueSubject<Set<Data>, Never>
|
||||||
|
let reachability: MockNetworkActivationReachability
|
||||||
let torController: MockNetworkActivationTorController
|
let torController: MockNetworkActivationTorController
|
||||||
let relayController: MockNetworkActivationRelayController
|
let relayController: MockNetworkActivationRelayController
|
||||||
let proxyController: MockNetworkActivationProxyController
|
let proxyController: MockNetworkActivationProxyController
|
||||||
let notificationCenter: NotificationCenter
|
let notificationCenter: NotificationCenter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
private final class MockNetworkActivationReachability:
|
||||||
|
NetworkReachabilityMonitoring {
|
||||||
|
private let subject = CurrentValueSubject<Bool, Never>(true)
|
||||||
|
private(set) var startCallCount = 0
|
||||||
|
private(set) var stopCallCount = 0
|
||||||
|
|
||||||
|
var isReachable: Bool { subject.value }
|
||||||
|
var reachabilityPublisher: AnyPublisher<Bool, Never> {
|
||||||
|
subject.removeDuplicates().dropFirst().eraseToAnyPublisher()
|
||||||
|
}
|
||||||
|
|
||||||
|
func start() {
|
||||||
|
startCallCount += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func stop() {
|
||||||
|
stopCallCount += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func set(_ reachable: Bool) {
|
||||||
|
subject.send(reachable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
private final class MockNetworkActivationTorController: NetworkActivationTorControlling {
|
private final class MockNetworkActivationTorController: NetworkActivationTorControlling {
|
||||||
private(set) var autoStartAllowedValues: [Bool] = []
|
private(set) var autoStartAllowedValues: [Bool] = []
|
||||||
|
|||||||
@@ -213,6 +213,7 @@ private final class ControllableReachabilityMonitor: NetworkReachabilityMonitori
|
|||||||
subject.removeDuplicates().dropFirst().eraseToAnyPublisher()
|
subject.removeDuplicates().dropFirst().eraseToAnyPublisher()
|
||||||
}
|
}
|
||||||
func start() { startCalled = true }
|
func start() { startCalled = true }
|
||||||
|
func stop() { startCalled = false }
|
||||||
func set(_ reachable: Bool) { subject.send(reachable) }
|
func set(_ reachable: Bool) { subject.send(reachable) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ private final class GatedVoiceCaptureSession: VoiceCaptureSession {
|
|||||||
private let startError: Error?
|
private let startError: Error?
|
||||||
private(set) var finishStarted = false
|
private(set) var finishStarted = false
|
||||||
private(set) var cancelCount = 0
|
private(set) var cancelCount = 0
|
||||||
|
private(set) var panicCancelCount = 0
|
||||||
private var finishContinuation: CheckedContinuation<URL?, Never>?
|
private var finishContinuation: CheckedContinuation<URL?, Never>?
|
||||||
|
|
||||||
init(startError: Error? = nil) {
|
init(startError: Error? = nil) {
|
||||||
@@ -83,6 +84,10 @@ private final class GatedVoiceCaptureSession: VoiceCaptureSession {
|
|||||||
cancelCount += 1
|
cancelCount += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func panicCancelSynchronously() {
|
||||||
|
panicCancelCount += 1
|
||||||
|
}
|
||||||
|
|
||||||
func resolveFinish(with url: URL?) {
|
func resolveFinish(with url: URL?) {
|
||||||
let continuation = finishContinuation
|
let continuation = finishContinuation
|
||||||
finishContinuation = nil
|
finishContinuation = nil
|
||||||
@@ -204,4 +209,57 @@ struct VoiceCaptureSessionTests {
|
|||||||
}
|
}
|
||||||
#expect(viewModel.state == .idle)
|
#expect(viewModel.state == .idle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test func panicSynchronouslyCancelsActiveCaptureAndResetsUI() async {
|
||||||
|
let session = GatedVoiceCaptureSession()
|
||||||
|
let viewModel = VoiceRecordingViewModel()
|
||||||
|
viewModel.sessionProvider = { session }
|
||||||
|
|
||||||
|
viewModel.start(shouldShow: true)
|
||||||
|
await waitUntil { self.isRecording(viewModel.state) }
|
||||||
|
|
||||||
|
viewModel.panicWipe()
|
||||||
|
|
||||||
|
#expect(session.panicCancelCount == 1)
|
||||||
|
#expect(viewModel.state == .idle)
|
||||||
|
#expect(!viewModel.isLiveStreaming)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func panicInvalidatesARecordingAlreadyFinalizing() async throws {
|
||||||
|
let session = GatedVoiceCaptureSession()
|
||||||
|
let viewModel = VoiceRecordingViewModel()
|
||||||
|
viewModel.sessionProvider = { session }
|
||||||
|
let url = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent("voice-panic-\(UUID().uuidString).m4a")
|
||||||
|
try Data([0x01]).write(to: url)
|
||||||
|
var delivered = false
|
||||||
|
|
||||||
|
viewModel.start(shouldShow: true)
|
||||||
|
await waitUntil { self.isRecording(viewModel.state) }
|
||||||
|
viewModel.finish { _ in delivered = true }
|
||||||
|
await waitUntil { session.finishStarted }
|
||||||
|
|
||||||
|
viewModel.panicWipe()
|
||||||
|
session.resolveFinish(with: url)
|
||||||
|
await waitUntil {
|
||||||
|
!FileManager.default.fileExists(atPath: url.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(!delivered)
|
||||||
|
#expect(viewModel.state == .idle)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func liveSessionPanicStopsCaptureWithoutSendingControl() {
|
||||||
|
let capture = StubPTTCapture(stopResult: (nil, 0))
|
||||||
|
var sentPackets: [Data] = []
|
||||||
|
let session = PTTLiveVoiceSession(
|
||||||
|
sendPacket: { sentPackets.append($0) },
|
||||||
|
capture: capture
|
||||||
|
)
|
||||||
|
|
||||||
|
session.panicCancelSynchronously()
|
||||||
|
|
||||||
|
#expect(capture.cancelCount == 1)
|
||||||
|
#expect(sentPackets.isEmpty)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -360,6 +360,35 @@ struct VoiceRecorderTests {
|
|||||||
#expect(FileManager.default.fileExists(atPath: secondURL.path))
|
#expect(FileManager.default.fileExists(atPath: secondURL.path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test func classicSessionPanicStopsRecorderAndDeletesFileBeforeReturning() async throws {
|
||||||
|
let directory = try makeTemporaryDirectory()
|
||||||
|
defer { try? FileManager.default.removeItem(at: directory) }
|
||||||
|
|
||||||
|
let rawSession = VoiceRecorderTestSession()
|
||||||
|
let coordinator = AudioSessionCoordinator(session: rawSession)
|
||||||
|
let factory = TestVoiceAudioRecorderFactory(plans: [.success])
|
||||||
|
let voiceRecorder = VoiceRecorder(
|
||||||
|
sessionCoordinator: coordinator,
|
||||||
|
recorderFactory: factory,
|
||||||
|
permissionGranted: { true },
|
||||||
|
paddingInterval: 0,
|
||||||
|
outputDirectory: directory
|
||||||
|
)
|
||||||
|
let capture = VoiceNoteCaptureSession(recorder: voiceRecorder)
|
||||||
|
|
||||||
|
try await capture.start()
|
||||||
|
let url = try #require(factory.urls.first)
|
||||||
|
let recorder = try #require(factory.recorders.first)
|
||||||
|
|
||||||
|
capture.panicCancelSynchronously()
|
||||||
|
|
||||||
|
#expect(recorder.stopCallCount == 1)
|
||||||
|
#expect(!recorder.isRecording)
|
||||||
|
#expect(!FileManager.default.fileExists(atPath: url.path))
|
||||||
|
await coordinator.drain()
|
||||||
|
#expect(rawSession.activationCalls == [true, false])
|
||||||
|
}
|
||||||
|
|
||||||
private func verifyFailedStart(
|
private func verifyFailedStart(
|
||||||
firstPlan: TestVoiceAudioRecorderFactory.Plan,
|
firstPlan: TestVoiceAudioRecorderFactory.Plan,
|
||||||
expectedPrepareCalls: Int,
|
expectedPrepareCalls: Int,
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ Residual risk: private-message metadata such as timing, radio adjacency, ciphert
|
|||||||
- Recent signed public mesh messages are archived in Application Support for up to 15 minutes so gossip sync survives a relaunch and can cross mesh partitions.
|
- Recent signed public mesh messages are archived in Application Support for up to 15 minutes so gossip sync survives a relaunch and can cross mesh partitions.
|
||||||
- Signed public board posts and tombstones persist until author-selected expiry, at most seven days. Stores are bounded by global and per-author quotas.
|
- Signed public board posts and tombstones persist until author-selected expiry, at most seven days. Stores are bounded by global and per-author quotas.
|
||||||
- Group metadata (name, roster, creator, epoch) persists as protected JSON; group keys live in the keychain until leave/removal/wipe.
|
- Group metadata (name, roster, creator, epoch) persists as protected JSON; group keys live in the keychain until leave/removal/wipe.
|
||||||
- Voice notes and images are stored in Application Support. Incoming media has a 100 MB oldest-first quota; outgoing media does not have an equivalent automatic lifetime and remains until cleanup, panic wipe, or app removal.
|
- Voice notes and images are stored in Application Support. Incoming media has a 100 MB oldest-first quota; outgoing media does not have an equivalent automatic lifetime and remains until cleanup, panic wipe, or app removal. Panic wipe invalidates detached preparation work, cancels active transfers, closes live capture files, and removes the managed media tree before returning.
|
||||||
|
|
||||||
Public archives contain content already intended for public mesh/board distribution, but a seized unlocked device can reveal it. Group metadata and media can reveal relationships or content even when the in-memory chat timeline has gone away.
|
Public archives contain content already intended for public mesh/board distribution, but a seized unlocked device can reveal it. Group metadata and media can reveal relationships or content even when the in-memory chat timeline has gone away.
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ Residual risk: Nostr relay retention and logging are outside project control. Pu
|
|||||||
|
|
||||||
## Panic Wipe Coverage
|
## Panic Wipe Coverage
|
||||||
|
|
||||||
The panic action clears identity/session state, preferences, location state, groups, prekeys, outbox mail, courier mail, bridge dedup state, gossip archive, board data, managed media, and active subscriptions/transports. New persistent stores must add an explicit wipe hook and a regression test.
|
The panic action clears identity/session state, preferences, location state, groups, prekeys, outbox mail, courier mail, bridge dedup state, gossip archive, board data, managed media, and active subscriptions/transports. Managed media deletion completes synchronously, after active media work has been invalidated. Keychain secrets use device-only accessibility, and an install marker detects and clears app keys that survive uninstall before a later reinstall can use them. New persistent stores must add an explicit wipe hook and a regression test.
|
||||||
|
|
||||||
## Release Review Checklist
|
## Release Review Checklist
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user