版本名 1.1.3,构建号 6。应用标识和签名未改。包含自动更新、覆盖安装、目标历史、IPv4/IPv6 开关、HTTP(含 HTTP/3)以及中心服务端一键添加节点。
72 lines
2.2 KiB
Dart
72 lines
2.2 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../models/session_config.dart';
|
|
import '../models/target_history.dart';
|
|
import 'token_vault.dart';
|
|
|
|
class AppStore {
|
|
static const _key = 'netquality.settings.v2';
|
|
static const _legacy = 'netquality.settings.v1';
|
|
static const historyKey = 'netquality.target-history.v1';
|
|
|
|
Future<({SessionConfig config, String legacyEcho})> load() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(_key);
|
|
if (raw != null && raw.isNotEmpty) {
|
|
try {
|
|
final decoded = jsonDecode(raw);
|
|
if (decoded is Map) {
|
|
return (
|
|
config: SessionConfig.fromJson(Map<String, dynamic>.from(decoded)),
|
|
legacyEcho: '',
|
|
);
|
|
}
|
|
} on FormatException {
|
|
return (config: SessionConfig.defaults, legacyEcho: '');
|
|
}
|
|
}
|
|
final legacy = prefs.getString(_legacy);
|
|
if (legacy != null && legacy.isNotEmpty) {
|
|
try {
|
|
final migrated = migrateV1(legacy);
|
|
await save(migrated.config);
|
|
await prefs.remove(_legacy);
|
|
return (config: migrated.config, legacyEcho: migrated.echoToken);
|
|
} on FormatException {
|
|
return (config: SessionConfig.defaults, legacyEcho: '');
|
|
}
|
|
}
|
|
return (config: SessionConfig.defaults, legacyEcho: '');
|
|
}
|
|
|
|
Future<void> save(SessionConfig config) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(_key, jsonEncode(config.toJson()));
|
|
}
|
|
|
|
Future<TargetHistory> loadHistory() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
return TargetHistory.decode(prefs.getString(historyKey) ?? '');
|
|
}
|
|
|
|
Future<void> saveHistory(TargetHistory history) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(historyKey, history.encode());
|
|
}
|
|
}
|
|
|
|
Future<TokenVault> openTokenVault() async {
|
|
try {
|
|
final dir = await getApplicationSupportDirectory();
|
|
final vault = FileTokenVault(File('${dir.path}/node-tokens.json'));
|
|
await vault.load();
|
|
return vault;
|
|
} on Object {
|
|
return MemoryTokenVault();
|
|
}
|
|
}
|