mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 04:05:20 +00:00
Fix timestamp bug causing peer discovery failures
- Fixed BitchatPacket convenience initializer to use milliseconds (was using seconds) - Fixed BitchatMessage binary encoding to use milliseconds for consistency - Updated decoding logic to properly handle millisecond timestamps - This resolves the issue where packets were being dropped due to timestamp validation failures All timestamps are now consistently in milliseconds throughout the codebase.
This commit is contained in:
@@ -196,11 +196,11 @@ extension BitchatMessage {
|
|||||||
|
|
||||||
data.append(flags)
|
data.append(flags)
|
||||||
|
|
||||||
// Timestamp
|
// Timestamp (in milliseconds)
|
||||||
let timestampSeconds = UInt64(timestamp.timeIntervalSince1970)
|
let timestampMillis = UInt64(timestamp.timeIntervalSince1970 * 1000)
|
||||||
// Encode as 8 bytes, big-endian
|
// Encode as 8 bytes, big-endian
|
||||||
for i in (0..<8).reversed() {
|
for i in (0..<8).reversed() {
|
||||||
data.append(UInt8((timestampSeconds >> (i * 8)) & 0xFF))
|
data.append(UInt8((timestampMillis >> (i * 8)) & 0xFF))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ID
|
// ID
|
||||||
@@ -290,11 +290,11 @@ extension BitchatMessage {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
let timestampData = dataCopy[offset..<offset+8]
|
let timestampData = dataCopy[offset..<offset+8]
|
||||||
let timestampSeconds = timestampData.reduce(0) { result, byte in
|
let timestampMillis = timestampData.reduce(0) { result, byte in
|
||||||
(result << 8) | UInt64(byte)
|
(result << 8) | UInt64(byte)
|
||||||
}
|
}
|
||||||
offset += 8
|
offset += 8
|
||||||
let timestamp = Date(timeIntervalSince1970: TimeInterval(timestampSeconds))
|
let timestamp = Date(timeIntervalSince1970: TimeInterval(timestampMillis) / 1000.0)
|
||||||
|
|
||||||
// ID
|
// ID
|
||||||
guard offset < dataCopy.count else {
|
guard offset < dataCopy.count else {
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ struct BitchatPacket: Codable {
|
|||||||
self.type = type
|
self.type = type
|
||||||
self.senderID = senderID.data(using: .utf8)!
|
self.senderID = senderID.data(using: .utf8)!
|
||||||
self.recipientID = nil
|
self.recipientID = nil
|
||||||
self.timestamp = UInt64(Date().timeIntervalSince1970)
|
self.timestamp = UInt64(Date().timeIntervalSince1970 * 1000) // milliseconds
|
||||||
self.payload = payload
|
self.payload = payload
|
||||||
self.signature = nil
|
self.signature = nil
|
||||||
self.ttl = ttl
|
self.ttl = ttl
|
||||||
|
|||||||
@@ -1053,7 +1053,7 @@ class BluetoothMeshService: NSObject {
|
|||||||
let currentTime = UInt64(Date().timeIntervalSince1970 * 1000) // milliseconds
|
let currentTime = UInt64(Date().timeIntervalSince1970 * 1000) // milliseconds
|
||||||
let timeDiff = abs(Int64(currentTime) - Int64(packet.timestamp))
|
let timeDiff = abs(Int64(currentTime) - Int64(packet.timestamp))
|
||||||
if timeDiff > 300000 { // 5 minutes in milliseconds
|
if timeDiff > 300000 { // 5 minutes in milliseconds
|
||||||
print("[SECURITY] Dropping packet with timestamp too far from current time: \(timeDiff/1000) seconds")
|
print("[SECURITY] Dropping packet from \(peerID) type:\(packet.type) - timestamp diff: \(timeDiff/1000)s (packet:\(packet.timestamp) vs current:\(currentTime))")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user