Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f26c459f6c | ||
|
|
8cd450d5b1 |
No files matched your search
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
enum ConnectionMode { tun, proxy }
|
enum ConnectionMode { tun, proxy }
|
||||||
@@ -18,23 +19,37 @@ abstract final class ConnectionSettings {
|
|||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
final storedMode = prefs.getString(_modeKey);
|
final storedMode = prefs.getString(_modeKey);
|
||||||
|
|
||||||
// Any unrecognized value (including the pre-V3 'auto') is TUN. 'proxy' is
|
// Only the explicit current proxy value restores SOCKS5. Unknown and
|
||||||
// the only persisted value that selects the SOCKS5 mode.
|
// 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
|
_mode = storedMode == ConnectionMode.proxy.name
|
||||||
? ConnectionMode.proxy
|
? ConnectionMode.proxy
|
||||||
: ConnectionMode.tun;
|
: ConnectionMode.tun;
|
||||||
|
|
||||||
if (storedMode != _mode.name) {
|
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;
|
_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;
|
_mode = mode;
|
||||||
_initialized = true;
|
_initialized = true;
|
||||||
final prefs = await SharedPreferences.getInstance();
|
return true;
|
||||||
await prefs.setString(_modeKey, mode.name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,8 +51,14 @@ class _ConnectionModeTileState extends State<ConnectionModeTile> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await ConnectionSettings.setMode(next);
|
final saved = await ConnectionSettings.setMode(next);
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
|
if (!saved) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('Could not save connection mode.')),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_mode = next;
|
_mode = next;
|
||||||
|
|||||||
Reference in new issue
Block a user