Address review: consent race, unsigned advert bytes, antimeridian
- Uploads carry a consent generation: nothing is sent if sharing is turned off while a batch is being prepared, and a late response never touches a queue that was cleared and refilled - Parse only the signed 32 advert app-data bytes, as the firmware does, so appended unsigned bytes can't add the hidden marker or change the name - Split online map requests that cross the antimeridian into two valid boxes and merge the results - README: link the network map next to the website
This commit is contained in:
1 parent
49582d3f07
commit
e614fb32e2
8 files changed
+239
-14
No files matched your search
@@ -25,6 +25,32 @@ class NetworkMapBbox {
|
||||
maxLon - minLon <= networkMapMaxBboxSpan &&
|
||||
maxLat - minLat <= networkMapMaxBboxSpan;
|
||||
|
||||
/// Valid request boxes for a visible area, or null when it is larger than
|
||||
/// [networkMapMaxBboxSpan]. [west] and [east] may be unwrapped (beyond
|
||||
/// ±180) or wrapped across the antimeridian (west > east); an area that
|
||||
/// crosses ±180 is split into two boxes, one on each side.
|
||||
static List<NetworkMapBbox>? forVisibleArea({
|
||||
required double west,
|
||||
required double south,
|
||||
required double east,
|
||||
required double north,
|
||||
}) {
|
||||
final minLat = south.clamp(-90.0, 90.0);
|
||||
final maxLat = north.clamp(-90.0, 90.0);
|
||||
var span = east - west;
|
||||
if (span < 0) span += 360;
|
||||
if (span > networkMapMaxBboxSpan ||
|
||||
maxLat - minLat > networkMapMaxBboxSpan) {
|
||||
return null;
|
||||
}
|
||||
final w = (west + 180) % 360 - 180; // Dart % is non-negative here.
|
||||
final e = w + span;
|
||||
NetworkMapBbox box(double a, double b) =>
|
||||
NetworkMapBbox(minLon: a, minLat: minLat, maxLon: b, maxLat: maxLat);
|
||||
if (e <= 180) return [box(w, e)];
|
||||
return [box(w, 180), box(-180, e - 360)];
|
||||
}
|
||||
|
||||
String get query => [
|
||||
minLon,
|
||||
minLat,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'dart:math';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
@@ -288,7 +289,13 @@ Future<NetworkMapAdvertRecord?> buildAdvertRecord(
|
||||
const appDataStart = _advertPubKeySize + 4 + _advertSignatureSize;
|
||||
if (payload.length <= appDataStart) return null;
|
||||
|
||||
final appData = payload.sublist(appDataStart);
|
||||
// Only the first 32 app-data bytes are signed (firmware Mesh.cpp truncates
|
||||
// to MAX_ADVERT_DATA_SIZE before verifying and parsing), so bytes appended
|
||||
// after them must never reach the name, location or 🚫 check.
|
||||
final appData = payload.sublist(
|
||||
appDataStart,
|
||||
min(payload.length, appDataStart + _maxAdvertDataSize),
|
||||
);
|
||||
final flags = appData[0];
|
||||
final type = flags & 0x0F;
|
||||
if (type != advTypeRepeater && type != advTypeRoom) return null;
|
||||
|
||||
@@ -140,6 +140,12 @@ class NetworkMapUploadService extends ChangeNotifier {
|
||||
StreamSubscription<Uint8List>? _frameSub;
|
||||
Timer? _timer;
|
||||
bool _uploading = false;
|
||||
|
||||
/// Bumped whenever sharing is turned off. An upload started under an older
|
||||
/// generation is never sent and its result is ignored, so nothing leaves the
|
||||
/// phone after consent is withdrawn and a late response can't touch a queue
|
||||
/// that was cleared and refilled in the meantime.
|
||||
int _consentGeneration = 0;
|
||||
int _failures = 0;
|
||||
Duration _retryAfter = Duration.zero;
|
||||
Duration _nextInterval = uploadInterval;
|
||||
@@ -189,7 +195,10 @@ class NetworkMapUploadService extends ChangeNotifier {
|
||||
}
|
||||
|
||||
void _sync() {
|
||||
if (!enabled) _queue.clear();
|
||||
if (!enabled && (isActive || !_queue.isEmpty || _uploading)) {
|
||||
_queue.clear();
|
||||
_consentGeneration++;
|
||||
}
|
||||
// The store-review demo radio is simulated; its traffic must not reach
|
||||
// the real map.
|
||||
final shouldListen =
|
||||
@@ -263,6 +272,8 @@ class NetworkMapUploadService extends ChangeNotifier {
|
||||
|
||||
Future<void> _upload() async {
|
||||
_uploading = true;
|
||||
final generation = _consentGeneration;
|
||||
bool stale() => generation != _consentGeneration || !enabled;
|
||||
_lastAttemptAt = DateTime.now();
|
||||
try {
|
||||
_accrueListenTime();
|
||||
@@ -289,6 +300,7 @@ class NetworkMapUploadService extends ChangeNotifier {
|
||||
packets: batch.packets,
|
||||
adverts: batch.adverts,
|
||||
);
|
||||
if (stale()) return;
|
||||
final jsonBytes = utf8.encode(jsonEncode(body));
|
||||
final gzipped = gzipEncode(jsonBytes);
|
||||
final response = await _client
|
||||
@@ -301,6 +313,7 @@ class NetworkMapUploadService extends ChangeNotifier {
|
||||
body: gzipped ?? jsonBytes,
|
||||
)
|
||||
.timeout(const Duration(seconds: 30));
|
||||
if (stale()) return;
|
||||
final status = response.statusCode;
|
||||
if (status == 202 || status == 200) {
|
||||
_queue.remove(batch.packets.length, batch.adverts.length);
|
||||
@@ -318,6 +331,7 @@ class NetworkMapUploadService extends ChangeNotifier {
|
||||
_failures = 0;
|
||||
}
|
||||
} catch (e) {
|
||||
if (stale()) return;
|
||||
appLogger.warn('Network map upload failed: $e', tag: 'NetworkMap');
|
||||
_onFailed(null, null);
|
||||
} finally {
|
||||
@@ -363,6 +377,16 @@ class NetworkMapUploadService extends ChangeNotifier {
|
||||
: Duration(seconds: min(retryAfter, maxBackoff.inSeconds));
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
int get queuedPacketCount => _queue.packetCount;
|
||||
|
||||
@visibleForTesting
|
||||
void queuePacketForTest(NetworkMapPacketRecord record) =>
|
||||
_queue.addPacket(record, DateTime.now());
|
||||
|
||||
@visibleForTesting
|
||||
Future<void> uploadNowForTest() => _upload();
|
||||
|
||||
Future<String> _appString() async {
|
||||
if (_appId != null) return _appId!;
|
||||
var version = '';
|
||||
|
||||
@@ -52,26 +52,33 @@ class NetworkMapOnlineController extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> _load(LatLngBounds bounds) async {
|
||||
final bbox = NetworkMapBbox(
|
||||
minLon: bounds.west.clamp(-180.0, 180.0),
|
||||
minLat: bounds.south.clamp(-90.0, 90.0),
|
||||
maxLon: bounds.east.clamp(-180.0, 180.0),
|
||||
maxLat: bounds.north.clamp(-90.0, 90.0),
|
||||
final boxes = NetworkMapBbox.forVisibleArea(
|
||||
west: bounds.west,
|
||||
south: bounds.south,
|
||||
east: bounds.east,
|
||||
north: bounds.north,
|
||||
);
|
||||
final generation = ++_generation;
|
||||
if (!bbox.isWithinLimit) {
|
||||
if (boxes == null) {
|
||||
_setState(NetworkMapLoadState.zoomIn);
|
||||
return;
|
||||
}
|
||||
_setState(NetworkMapLoadState.loading);
|
||||
try {
|
||||
final results = await Future.wait([
|
||||
_client.fetchRepeaters(bbox),
|
||||
_client.fetchLinks(bbox),
|
||||
Future.wait(boxes.map(_client.fetchRepeaters)),
|
||||
Future.wait(boxes.map((b) => _client.fetchLinks(b))),
|
||||
]);
|
||||
if (_disposed || generation != _generation) return;
|
||||
_repeaters = results[0] as List<NetworkMapRepeater>;
|
||||
_links = results[1] as List<NetworkMapLink>;
|
||||
// Two boxes (antimeridian) can both return items on the seam.
|
||||
_repeaters = {
|
||||
for (final r in results[0].expand((l) => l as List<NetworkMapRepeater>))
|
||||
r.key: r,
|
||||
}.values.toList();
|
||||
_links = {
|
||||
for (final l in results[1].expand((l) => l as List<NetworkMapLink>))
|
||||
'${l.a}:${l.b}': l,
|
||||
}.values.toList();
|
||||
_setState(NetworkMapLoadState.ready);
|
||||
} catch (_) {
|
||||
if (_disposed || generation != _generation) return;
|
||||
|
||||
Reference in new issue
Block a user