diff --git a/lib/logic/connection_settings.dart b/lib/logic/connection_settings.dart index 6189360..658e9c2 100644 --- a/lib/logic/connection_settings.dart +++ b/lib/logic/connection_settings.dart @@ -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 setMode(ConnectionMode mode) async { + static Future 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; } } diff --git a/lib/screens/settings/in_settings/connection_mode.dart b/lib/screens/settings/in_settings/connection_mode.dart index 021c3b0..46519d7 100644 --- a/lib/screens/settings/in_settings/connection_mode.dart +++ b/lib/screens/settings/in_settings/connection_mode.dart @@ -51,8 +51,14 @@ class _ConnectionModeTileState extends State { 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;