fix(app): fail closed on invalid connection mode state
Restore SOCKS5 only from the current proxy value and keep UI state unchanged when persistence fails.
This commit is contained in:
1 parent
b6ed976346
commit
3f7aead263
2 files changed
+22
-16
No files matched your search
@@ -18,23 +18,32 @@ 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 former 'auto'/'ass' labels, 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);
|
||||
final migrated = await prefs.setString(_modeKey, _mode.name);
|
||||
if (!migrated) {
|
||||
throw StateError('Could not persist migrated connection 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user