feat(#1206): one log package for Share, Save and Send
LogPackage gathers every log file and the Android exit traces, re-runs the redactor over each line oldest first (so logs written by earlier builds lose their passwords too), and adds manifest.json with the app version, commit, platform, OS and radio model, firmware and caps. Share and Save now hand over these redacted copies, never the files in the log folder. zip() is what Send to Offband will upload. archive moves from transitive to direct at the locked 4.0.9. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>docs/1232-region-scope-guide
parent
3834971ef5
commit
2cc5135e46
@ -0,0 +1,156 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:archive/archive.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../connector/meshcore_connector.dart';
|
||||
import '../utils/app_logger.dart';
|
||||
import '../utils/build_info.dart';
|
||||
import '../utils/log_redactor.dart';
|
||||
import 'app_exit_info_service.dart';
|
||||
import 'file_log_service.dart';
|
||||
|
||||
/// The log package every send uses (#1192): every log file and Android exit
|
||||
/// trace, with passwords and PINs removed, plus `manifest.json` naming the
|
||||
/// build, platform and radio. Share/Save hands over these files; Send to
|
||||
/// Offband zips them.
|
||||
///
|
||||
/// Lines written before the redactor existed are redacted here too, so an
|
||||
/// older rotated log cannot carry a password out.
|
||||
class LogPackage {
|
||||
LogPackage._(this.directory, this.files);
|
||||
|
||||
static const String manifestName = 'manifest.json';
|
||||
static const String tracesDir = 'traces';
|
||||
|
||||
/// Holds the redacted copies. Delete with [delete] once sent.
|
||||
final Directory directory;
|
||||
|
||||
/// The manifest first, then the logs oldest first, then the traces.
|
||||
final List<File> files;
|
||||
|
||||
/// [logs] is newest first, as [FileLogService.flushAndGetAllFiles] returns
|
||||
/// them. Both lists default to what is on disk now.
|
||||
static Future<LogPackage> build({
|
||||
MeshCoreConnector? radio,
|
||||
List<File>? logs,
|
||||
List<File>? traces,
|
||||
Directory? into,
|
||||
DateTime Function() now = DateTime.now,
|
||||
}) async {
|
||||
final logFiles =
|
||||
logs ?? await FileLogService.instance.flushAndGetAllFiles();
|
||||
final traceFiles =
|
||||
traces ??
|
||||
(defaultTargetPlatform == TargetPlatform.android
|
||||
? await AppExitInfoService.traceFiles()
|
||||
: const <File>[]);
|
||||
final dir = into ?? await _freshTempDir();
|
||||
final sep = Platform.pathSeparator;
|
||||
final redactor = LogRedactor();
|
||||
final written = <File>[];
|
||||
|
||||
for (final log in logFiles.reversed) {
|
||||
if (!await log.exists()) continue;
|
||||
final text = utf8.decode(await log.readAsBytes(), allowMalformed: true);
|
||||
final redacted = const LineSplitter()
|
||||
.convert(text)
|
||||
.map(redactor.line)
|
||||
.join('\n');
|
||||
final out = File('${dir.path}$sep${_name(log)}');
|
||||
await out.writeAsString('$redacted\n');
|
||||
written.add(out);
|
||||
}
|
||||
if (traceFiles.isNotEmpty) {
|
||||
final traceDir = await Directory('${dir.path}$sep$tracesDir').create();
|
||||
for (final trace in traceFiles) {
|
||||
if (!await trace.exists()) continue;
|
||||
written.add(await trace.copy('${traceDir.path}$sep${_name(trace)}'));
|
||||
}
|
||||
}
|
||||
|
||||
final manifest = File('${dir.path}$sep$manifestName');
|
||||
await manifest.writeAsString(
|
||||
const JsonEncoder.withIndent(' ').convert(
|
||||
manifestFor(
|
||||
radio: radio,
|
||||
createdAt: now(),
|
||||
files: [for (final f in written) _relative(dir, f)],
|
||||
),
|
||||
),
|
||||
);
|
||||
return LogPackage._(dir, [manifest, ...written]);
|
||||
}
|
||||
|
||||
static Map<String, Object?> manifestFor({
|
||||
MeshCoreConnector? radio,
|
||||
required DateTime createdAt,
|
||||
required List<String> files,
|
||||
}) => {
|
||||
'format': 1,
|
||||
'createdAt': createdAt.toUtc().toIso8601String(),
|
||||
'app': appLogger.appVersion,
|
||||
'commit': BuildInfo.gitSha,
|
||||
'branch': BuildInfo.gitBranch,
|
||||
'buildTime': BuildInfo.buildTime,
|
||||
'platform': kIsWeb ? 'web' : defaultTargetPlatform.name,
|
||||
'os': kIsWeb ? null : Platform.operatingSystemVersion,
|
||||
'radio': radio == null
|
||||
? null
|
||||
: {
|
||||
'connected': radio.isConnected,
|
||||
'transport': radio.activeTransport.name,
|
||||
'model': radio.deviceModel,
|
||||
'firmware': radio.firmwareVersion,
|
||||
'firmwareVerCode': radio.firmwareVerCode,
|
||||
'offbandCaps': radio.offbandCaps,
|
||||
'offbandCaps2': radio.offbandCaps2,
|
||||
},
|
||||
'files': files,
|
||||
};
|
||||
|
||||
/// Every file, at its path inside the package, in one zip.
|
||||
Future<Uint8List> zip() async {
|
||||
final archive = Archive();
|
||||
for (final f in files) {
|
||||
archive.addFile(
|
||||
ArchiveFile.bytes(_relative(directory, f), await f.readAsBytes()),
|
||||
);
|
||||
}
|
||||
return ZipEncoder().encodeBytes(archive);
|
||||
}
|
||||
|
||||
Future<void> delete() async {
|
||||
try {
|
||||
await directory.delete(recursive: true);
|
||||
} catch (e) {
|
||||
appLogger.warn(
|
||||
'Removing ${directory.path} failed: $e',
|
||||
tag: 'LogPackage',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A new temp folder, after removing the ones earlier packages left: a
|
||||
/// share sheet reads its files after `share` returns, so they cannot be
|
||||
/// removed straight after sending.
|
||||
static Future<Directory> _freshTempDir() async {
|
||||
final temp = await getTemporaryDirectory();
|
||||
await for (final old in temp.list()) {
|
||||
if (old is Directory && _name(old).startsWith(_tempPrefix)) {
|
||||
await old.delete(recursive: true).catchError((_) => old);
|
||||
}
|
||||
}
|
||||
return temp.createTemp(_tempPrefix);
|
||||
}
|
||||
|
||||
static const _tempPrefix = 'offband-logs-';
|
||||
|
||||
static String _name(FileSystemEntity f) =>
|
||||
f.uri.pathSegments.lastWhere((s) => s.isNotEmpty);
|
||||
|
||||
static String _relative(Directory dir, File f) =>
|
||||
f.path.substring(dir.path.length + 1).replaceAll('\\', '/');
|
||||
}
|
||||
@ -0,0 +1,114 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:archive/archive.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/cli_command_codec.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/services/log_package.dart';
|
||||
import 'package:meshcore_open/utils/log_redactor.dart';
|
||||
|
||||
final _peer = Uint8List.fromList(List.generate(32, (i) => 0xA0 + i));
|
||||
|
||||
String _hex(Uint8List f) =>
|
||||
f.map((b) => b.toRadixString(16).padLeft(2, '0')).join(' ');
|
||||
|
||||
String _frameLine(String dir, String label, Uint8List f) =>
|
||||
'2026-11-25T10:00:00.000 $dir $label ${_hex(f)}';
|
||||
|
||||
void main() {
|
||||
late Directory tmp;
|
||||
setUp(() async => tmp = await Directory.systemTemp.createTemp('logpkg'));
|
||||
tearDown(() async => tmp.delete(recursive: true));
|
||||
|
||||
Future<File> write(String name, List<String> lines) =>
|
||||
File('${tmp.path}/$name').writeAsString('${lines.join('\n')}\n');
|
||||
|
||||
test('lines written before the redactor lose their passwords', () async {
|
||||
final login = _frameLine(
|
||||
'TX',
|
||||
'CMD_SEND_LOGIN',
|
||||
buildSendLoginFrame(_peer, 'hunter2'),
|
||||
);
|
||||
final get = _frameLine(
|
||||
'TX',
|
||||
'CODE_66',
|
||||
buildRunCliCommandFrame('get guest.password', prefix: 'b2|'),
|
||||
);
|
||||
final reply = _frameLine(
|
||||
'RX',
|
||||
'CODE_29',
|
||||
Uint8List.fromList([respCodeCliReply, ...latin1.encode('b2|> secret')]),
|
||||
);
|
||||
const late =
|
||||
'2026-11-25T10:00:01.000 [WARN/RepeaterCommand] Late reply to '
|
||||
'"password hunter2" from abcd arrived 900ms after its window closed: '
|
||||
'password now: hunter2';
|
||||
const ordinary = '2026-11-25T10:00:02.000 [INFO/App] connected';
|
||||
|
||||
final older = await write('offband.1.log', [login, get]);
|
||||
final newer = await write('offband.log', [reply, late, ordinary]);
|
||||
final out = await Directory('${tmp.path}/out').create();
|
||||
final package = await LogPackage.build(
|
||||
logs: [newer, older],
|
||||
traces: const [],
|
||||
into: out,
|
||||
);
|
||||
|
||||
final text = [
|
||||
for (final f in package.files.skip(1)) await f.readAsString(),
|
||||
].join();
|
||||
expect(text, isNot(contains(_hex(latin1.encode('hunter2')))));
|
||||
expect(text, isNot(contains('hunter2')));
|
||||
expect(text, isNot(contains(_hex(latin1.encode('secret')))));
|
||||
expect(text, contains(ordinary));
|
||||
expect(text, contains('password now: *******'));
|
||||
expect(package.files.skip(1).map((f) => f.uri.pathSegments.last), [
|
||||
'offband.1.log',
|
||||
'offband.log',
|
||||
]);
|
||||
});
|
||||
|
||||
test('frame lines without secrets come out byte for byte', () {
|
||||
final r = LogRedactor();
|
||||
final psk = Uint8List.fromList(List.generate(16, (i) => i));
|
||||
final line = _frameLine(
|
||||
'TX',
|
||||
'CMD_SET_CHANNEL',
|
||||
buildSetChannelFrame(2, 'Ops', psk),
|
||||
);
|
||||
expect(r.line(line), line);
|
||||
final redacted = r.line(
|
||||
_frameLine('TX', 'CMD_SEND_LOGIN', buildSendLoginFrame(_peer, 'x')),
|
||||
);
|
||||
expect(r.line(redacted), redacted);
|
||||
});
|
||||
|
||||
test('the manifest names the files, and the zip holds them all', () async {
|
||||
final log = await write('offband.log', ['one']);
|
||||
final trace = await write('anr-1.txt', ['trace']);
|
||||
final out = await Directory('${tmp.path}/out').create();
|
||||
final package = await LogPackage.build(
|
||||
logs: [log, File('${tmp.path}/gone.log')],
|
||||
traces: [trace],
|
||||
into: out,
|
||||
now: () => DateTime.utc(2026, 11, 25, 12),
|
||||
);
|
||||
|
||||
final manifest =
|
||||
jsonDecode(await package.files.first.readAsString())
|
||||
as Map<String, dynamic>;
|
||||
expect(manifest['format'], 1);
|
||||
expect(manifest['createdAt'], '2026-11-25T12:00:00.000Z');
|
||||
expect(manifest['files'], ['offband.log', 'traces/anr-1.txt']);
|
||||
expect(manifest.keys, containsAll(['app', 'commit', 'platform', 'os']));
|
||||
|
||||
final zip = ZipDecoder().decodeBytes(await package.zip());
|
||||
expect(zip.files.map((f) => f.name), [
|
||||
LogPackage.manifestName,
|
||||
'offband.log',
|
||||
'traces/anr-1.txt',
|
||||
]);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue