mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-29 20:34:07 +01:00
Compare commits
No commits in common. "c093c22ae044e3a68c9b25d947d270f06725f906" and "55fade1e15229416b4787598dfb2f7bb838bcf4b" have entirely different histories.
c093c22ae0
...
55fade1e15
@ -55,16 +55,6 @@ Future<void> main(List<String> args) async {
|
|||||||
await server.start(port);
|
await server.start(port);
|
||||||
stdout.writeln('diet_guard desktop serving on http://localhost:$port');
|
stdout.writeln('diet_guard desktop serving on http://localhost:$port');
|
||||||
|
|
||||||
// Provisioning is a setup step, not a session: load the app once in a
|
|
||||||
// headless browser so its own code can write the account into the
|
|
||||||
// origin-keyed localStorage only the page can reach, then exit. No window
|
|
||||||
// is ever mapped, so this never steals focus or lands on the wrong monitor.
|
|
||||||
if (args.contains('--provision-sync-account')) {
|
|
||||||
final ok = await _provisionSyncAccount(home, port);
|
|
||||||
await server.stop();
|
|
||||||
exit(ok ? 0 : 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// A bare flag, not a valued option: requiring a dummy value meant passing a
|
// A bare flag, not a valued option: requiring a dummy value meant passing a
|
||||||
// stray positional, which the AOT runtime tries to interpret as a snapshot.
|
// stray positional, which the AOT runtime tries to interpret as a snapshot.
|
||||||
if (!args.contains('--no-browser')) {
|
if (!args.contains('--no-browser')) {
|
||||||
@ -88,45 +78,7 @@ Future<void> main(List<String> args) async {
|
|||||||
/// Launches the app in a Chrome-family browser with a **stable** profile
|
/// Launches the app in a Chrome-family browser with a **stable** profile
|
||||||
/// directory, since the food log, photos and settings live in that profile.
|
/// directory, since the food log, photos and settings live in that profile.
|
||||||
/// Returns true when the browser ran long enough to have owned the session.
|
/// Returns true when the browser ran long enough to have owned the session.
|
||||||
/// Loads the app once headlessly so it provisions its own sync account.
|
Future<bool> _launchBrowser(String home, int port) async {
|
||||||
///
|
|
||||||
/// The account lives in origin-keyed localStorage, which only code running in
|
|
||||||
/// the page can write -- so the page has to run. `--headless` means it runs
|
|
||||||
/// without ever mapping a window.
|
|
||||||
Future<bool> _provisionSyncAccount(String home, int port) async {
|
|
||||||
final browser = _findBrowser();
|
|
||||||
if (browser.isEmpty) {
|
|
||||||
stderr.writeln('No Chrome-family browser found; cannot provision.');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
final process = await Process.start(browser, [
|
|
||||||
'--headless=new',
|
|
||||||
'--disable-gpu',
|
|
||||||
'--user-data-dir=${_profileDir(home)}',
|
|
||||||
'--virtual-time-budget=45000',
|
|
||||||
'--dump-dom',
|
|
||||||
'http://localhost:$port',
|
|
||||||
]);
|
|
||||||
final code = await process.exitCode;
|
|
||||||
if (code != 0) {
|
|
||||||
stderr.writeln('Headless provisioning run exited with $code.');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
stdout.writeln(
|
|
||||||
'Provisioning run complete. Launch normally to confirm it is connected.',
|
|
||||||
);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The Chrome profile holding this app's IndexedDB and localStorage.
|
|
||||||
///
|
|
||||||
/// Must stay as stable as the port: IndexedDB is keyed by origin and lives
|
|
||||||
/// inside this profile, so a changing path silently hides the whole food log.
|
|
||||||
String _profileDir(String home) =>
|
|
||||||
p.join(home, '.local', 'share', 'diet-guard-desktop', 'profile');
|
|
||||||
|
|
||||||
/// Returns the first Chrome-family browser on this machine, or ''.
|
|
||||||
String _findBrowser() {
|
|
||||||
// Ordered by preference, and deliberately broad: this machine runs Thorium
|
// Ordered by preference, and deliberately broad: this machine runs Thorium
|
||||||
// behind /opt/google/chrome, and has a policy that uninstalls the `chromium`
|
// behind /opt/google/chrome, and has a policy that uninstalls the `chromium`
|
||||||
// package, so assuming any single browser is wrong. DIET_GUARD_BROWSER
|
// package, so assuming any single browser is wrong. DIET_GUARD_BROWSER
|
||||||
@ -139,23 +91,29 @@ String _findBrowser() {
|
|||||||
'/usr/bin/chromium',
|
'/usr/bin/chromium',
|
||||||
'/usr/bin/brave',
|
'/usr/bin/brave',
|
||||||
];
|
];
|
||||||
return candidates.firstWhere(
|
final browser = candidates.firstWhere(
|
||||||
(path) => path.isNotEmpty && File(path).existsSync(),
|
(path) => path.isNotEmpty && File(path).existsSync(),
|
||||||
orElse: () => '',
|
orElse: () => '',
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool> _launchBrowser(String home, int port) async {
|
|
||||||
final browser = _findBrowser();
|
|
||||||
if (browser.isEmpty) {
|
if (browser.isEmpty) {
|
||||||
stderr.writeln(
|
stderr.writeln(
|
||||||
'No Chrome-family browser found; open http://localhost:$port manually.',
|
'No Chrome-family browser found; open http://localhost:$port manually.',
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// The profile directory must stay as stable as the port: IndexedDB is keyed
|
||||||
|
// by origin and lives inside this profile, so a changing path silently
|
||||||
|
// hides the entire food log.
|
||||||
|
final profile = p.join(
|
||||||
|
home,
|
||||||
|
'.local',
|
||||||
|
'share',
|
||||||
|
'diet-guard-desktop',
|
||||||
|
'profile',
|
||||||
|
);
|
||||||
final process = await Process.start(browser, [
|
final process = await Process.start(browser, [
|
||||||
'--app=http://localhost:$port',
|
'--app=http://localhost:$port',
|
||||||
'--user-data-dir=${_profileDir(home)}',
|
'--user-data-dir=$profile',
|
||||||
// Sets WM_CLASS, which the .desktop entry matches on via StartupWMClass.
|
// Sets WM_CLASS, which the .desktop entry matches on via StartupWMClass.
|
||||||
// Without it the window inherits the browser's class and the taskbar shows
|
// Without it the window inherits the browser's class and the taskbar shows
|
||||||
// a browser icon instead of diet_guard's.
|
// a browser icon instead of diet_guard's.
|
||||||
|
|||||||
@ -35,20 +35,7 @@ class WrapperServer {
|
|||||||
required this.webRoot,
|
required this.webRoot,
|
||||||
required this.dataDir,
|
required this.dataDir,
|
||||||
required this.gitHubProxy,
|
required this.gitHubProxy,
|
||||||
bool? serveSyncAccount,
|
});
|
||||||
String? syncConfigDir,
|
|
||||||
}) : serveSyncAccount =
|
|
||||||
serveSyncAccount ??
|
|
||||||
(Platform.environment[kSyncAccountEnvVar] ?? '').isNotEmpty,
|
|
||||||
syncConfigDir =
|
|
||||||
syncConfigDir ??
|
|
||||||
p.join(Platform.environment['HOME'] ?? '', '.config', 'crdt-sync');
|
|
||||||
|
|
||||||
/// Whether the sync-account route answers; off unless explicitly enabled.
|
|
||||||
final bool serveSyncAccount;
|
|
||||||
|
|
||||||
/// Directory holding `firebase.json` and `password`.
|
|
||||||
final String syncConfigDir;
|
|
||||||
|
|
||||||
/// Directory holding the built Flutter web assets.
|
/// Directory holding the built Flutter web assets.
|
||||||
final String webRoot;
|
final String webRoot;
|
||||||
@ -127,12 +114,17 @@ class WrapperServer {
|
|||||||
/// the same reasoning that keeps the GitHub token in [gitHubProxy] instead
|
/// the same reasoning that keeps the GitHub token in [gitHubProxy] instead
|
||||||
/// of the browser.
|
/// of the browser.
|
||||||
Future<void> _syncAccount(HttpRequest request) async {
|
Future<void> _syncAccount(HttpRequest request) async {
|
||||||
if (!serveSyncAccount) {
|
if ((Platform.environment[kSyncAccountEnvVar] ?? '').isEmpty) {
|
||||||
request.response.statusCode = HttpStatus.notFound;
|
request.response.statusCode = HttpStatus.notFound;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final configFile = File(p.join(syncConfigDir, 'firebase.json'));
|
final configDir = p.join(
|
||||||
final passwordFile = File(p.join(syncConfigDir, 'password'));
|
Platform.environment['HOME'] ?? '',
|
||||||
|
'.config',
|
||||||
|
'crdt-sync',
|
||||||
|
);
|
||||||
|
final configFile = File(p.join(configDir, 'firebase.json'));
|
||||||
|
final passwordFile = File(p.join(configDir, 'password'));
|
||||||
if (!configFile.existsSync() || !passwordFile.existsSync()) {
|
if (!configFile.existsSync() || !passwordFile.existsSync()) {
|
||||||
stderr.writeln(
|
stderr.writeln(
|
||||||
'Sync account requested but ~/.config/crdt-sync/ is incomplete — '
|
'Sync account requested but ~/.config/crdt-sync/ is incomplete — '
|
||||||
|
|||||||
@ -120,11 +120,7 @@ class _LogMealScreenState extends State<LogMealScreen>
|
|||||||
_autoSyncing = true;
|
_autoSyncing = true;
|
||||||
try {
|
try {
|
||||||
final settings = await SyncSettings.load();
|
final settings = await SyncSettings.load();
|
||||||
// Either backend counts. `isConfigured` means "has a GitHub token", so
|
if (!settings.isConfigured) return;
|
||||||
// gating on it alone silently skips every sync on a device connected
|
|
||||||
// only to Firebase -- and once the mirror is retired that is every
|
|
||||||
// device. Same fix workout_app's push() already carries.
|
|
||||||
if (!settings.isConfigured && await openFirebase() == null) return;
|
|
||||||
final client = createGitHubClient(
|
final client = createGitHubClient(
|
||||||
settings,
|
settings,
|
||||||
httpClient: widget.httpClient,
|
httpClient: widget.httpClient,
|
||||||
|
|||||||
@ -7,7 +7,6 @@ import 'dart:io';
|
|||||||
|
|
||||||
import 'package:diet_guard_app/desktop/github_proxy.dart';
|
import 'package:diet_guard_app/desktop/github_proxy.dart';
|
||||||
import 'package:diet_guard_app/desktop/wrapper_server.dart';
|
import 'package:diet_guard_app/desktop/wrapper_server.dart';
|
||||||
import 'package:crdt_sync/crdt_sync.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:http/testing.dart';
|
import 'package:http/testing.dart';
|
||||||
@ -21,10 +20,7 @@ void main() {
|
|||||||
late List<http.Request> outbound;
|
late List<http.Request> outbound;
|
||||||
late Map<String, http.Response> canned;
|
late Map<String, http.Response> canned;
|
||||||
|
|
||||||
Future<void> startServer({
|
Future<void> startServer() async {
|
||||||
bool serveSyncAccount = false,
|
|
||||||
String? syncConfigDir,
|
|
||||||
}) async {
|
|
||||||
outbound = [];
|
outbound = [];
|
||||||
canned = {};
|
canned = {};
|
||||||
final proxy = GitHubProxy(
|
final proxy = GitHubProxy(
|
||||||
@ -40,8 +36,6 @@ void main() {
|
|||||||
webRoot: webRoot,
|
webRoot: webRoot,
|
||||||
dataDir: dataDir,
|
dataDir: dataDir,
|
||||||
gitHubProxy: proxy,
|
gitHubProxy: proxy,
|
||||||
serveSyncAccount: serveSyncAccount,
|
|
||||||
syncConfigDir: syncConfigDir,
|
|
||||||
);
|
);
|
||||||
await server.start(0);
|
await server.start(0);
|
||||||
}
|
}
|
||||||
@ -291,59 +285,4 @@ void main() {
|
|||||||
expect(typeOf('data.bin').mimeType, 'application/octet-stream');
|
expect(typeOf('data.bin').mimeType, 'application/octet-stream');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('sync-account provisioning', () {
|
|
||||||
/// Writes [files] into a temp config dir and restarts with the route on.
|
|
||||||
Future<String> enableWith(Map<String, String> files) async {
|
|
||||||
final configDir = Directory(p.join(tempDir.path, 'crdt-sync'))
|
|
||||||
..createSync(recursive: true);
|
|
||||||
files.forEach((name, contents) {
|
|
||||||
File(p.join(configDir.path, name)).writeAsStringSync(contents);
|
|
||||||
});
|
|
||||||
await server.stop();
|
|
||||||
await startServer(serveSyncAccount: true, syncConfigDir: configDir.path);
|
|
||||||
return 'http://localhost:${server.port}';
|
|
||||||
}
|
|
||||||
|
|
||||||
test('is 404 when not enabled', () async {
|
|
||||||
// The default, and the whole security posture: a credential route must
|
|
||||||
// not be reachable just because the wrapper is running.
|
|
||||||
final response = await http.get(url(kSyncAccountPath));
|
|
||||||
|
|
||||||
expect(response.statusCode, HttpStatus.notFound);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('serves the account when enabled', () async {
|
|
||||||
final origin = await enableWith({
|
|
||||||
'firebase.json': '{"email":"a@b.c"}',
|
|
||||||
'password': 'pw\n',
|
|
||||||
});
|
|
||||||
|
|
||||||
final response = await http.get(Uri.parse('$origin$kSyncAccountPath'));
|
|
||||||
final account = FirebaseAccount.tryParse(response.body);
|
|
||||||
|
|
||||||
expect(response.statusCode, HttpStatus.ok);
|
|
||||||
expect(account?.email, 'a@b.c');
|
|
||||||
expect(account?.password, 'pw');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('is 404 when the config files are absent', () async {
|
|
||||||
final origin = await enableWith({});
|
|
||||||
|
|
||||||
final response = await http.get(Uri.parse('$origin$kSyncAccountPath'));
|
|
||||||
|
|
||||||
expect(response.statusCode, HttpStatus.notFound);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('is 404 when firebase.json has no usable email', () async {
|
|
||||||
final origin = await enableWith({
|
|
||||||
'firebase.json': '{"apiKey":"x"}',
|
|
||||||
'password': 'pw',
|
|
||||||
});
|
|
||||||
|
|
||||||
final response = await http.get(Uri.parse('$origin$kSyncAccountPath'));
|
|
||||||
|
|
||||||
expect(response.statusCode, HttpStatus.notFound);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user