fix(app): fail closed on invalid connection mode state #7

Open
Naveax wants to merge 2 commits from Naveax/pr/fix-connection-mode-persistence into main
pull from: Naveax/pr/fix-connection-mode-persistence
2 changed files with 28 additions and 7 deletions

No files matched your search

+21 -6
View File
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
enum ConnectionMode { tun, proxy }
@@ -18,23 +19,37 @@ abstract final class ConnectionSettings {
final prefs = await SharedPreferences.getInstance();
final storedMode = prefs.getString(_modeKey);
// Any unrecognized value (including the pre-V3 'auto') is TUN. 'proxy' is
// the only persisted value that selects the SOCKS5 mode.
// Only the explicit current proxy value restores SOCKS5. Unknown and
// obsolete values, including the pre-V3 'auto' label, fail closed to
// full-tunnel TUN instead of silently selecting a weaker route.
_mode = storedMode == ConnectionMode.proxy.name
? ConnectionMode.proxy
: ConnectionMode.tun;
if (storedMode != _mode.name) {
await prefs.setString(_modeKey, _mode.name);
// Best-effort. _mode is already the safe TUN default in memory, so a
// failed write leaves nothing insecure — it only means the migration
// is retried next launch. initialize() runs from main() before
// runApp(), with no zone guard, so throwing here would black-screen
// the app on the fresh-install path for no security gain.
final migrated = await prefs.setString(_modeKey, _mode.name);
if (!migrated) {
debugPrint('[ConnectionSettings] Could not persist migrated mode.');
}
}
_initialized = true;
}
static Future<void> setMode(ConnectionMode mode) async {
static Future<bool> setMode(ConnectionMode mode) async {
if (_initialized && _mode == mode) return true;
final prefs = await SharedPreferences.getInstance();
final saved = await prefs.setString(_modeKey, mode.name);
if (!saved) return false;
_mode = mode;
_initialized = true;
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_modeKey, mode.name);
return true;
}
}
@@ -51,8 +51,14 @@ class _ConnectionModeTileState extends State<ConnectionModeTile> {
return;
}
await ConnectionSettings.setMode(next);
final saved = await ConnectionSettings.setMode(next);
if (!mounted) return;
if (!saved) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Could not save connection mode.')),
);
return;
}
setState(() {
_mode = next;