Import netquality 1.1.1
Initial import of the 1.1.1 source tree. Corresponds to Origin commit f9d97e368818acfe7dd44fa9fe5088b9629d0dff. Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
This commit is contained in:
1 parent
948594e7c0
commit
f6ffbde518
93 files changed
+10570
-1
No files matched your search
@@ -0,0 +1,59 @@
|
||||
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 'token_vault.dart';
|
||||
|
||||
class AppStore {
|
||||
static const _key = 'netquality.settings.v2';
|
||||
static const _legacy = 'netquality.settings.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<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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
/// Stores node-control tokens and the target echo token outside preferences.
|
||||
///
|
||||
/// The file lives in the app's private support directory. On Unix it is
|
||||
/// created with mode 0600. Tokens are never written into URLs or logs.
|
||||
abstract class TokenVault {
|
||||
Future<String?> read(String key);
|
||||
Future<void> write(String key, String value);
|
||||
Future<void> delete(String key);
|
||||
}
|
||||
|
||||
class MemoryTokenVault implements TokenVault {
|
||||
final Map<String, String> _values = {};
|
||||
|
||||
@override
|
||||
Future<void> delete(String key) async {
|
||||
_values.remove(key);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> read(String key) async => _values[key];
|
||||
|
||||
@override
|
||||
Future<void> write(String key, String value) async {
|
||||
if (value.isEmpty) {
|
||||
_values.remove(key);
|
||||
return;
|
||||
}
|
||||
_values[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
class FileTokenVault implements TokenVault {
|
||||
FileTokenVault(this.file);
|
||||
|
||||
final File file;
|
||||
final Map<String, String> _values = {};
|
||||
bool _loaded = false;
|
||||
|
||||
Future<void> load() async {
|
||||
if (_loaded) {
|
||||
return;
|
||||
}
|
||||
_loaded = true;
|
||||
if (!await file.exists()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final decoded = jsonDecode(await file.readAsString());
|
||||
if (decoded is Map) {
|
||||
decoded.forEach((key, value) {
|
||||
if (key is String && value is String && value.isNotEmpty) {
|
||||
_values[key] = value;
|
||||
}
|
||||
});
|
||||
}
|
||||
} on FormatException {
|
||||
_values.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> delete(String key) async {
|
||||
await load();
|
||||
_values.remove(key);
|
||||
await _flush();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> read(String key) async {
|
||||
await load();
|
||||
return _values[key];
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> write(String key, String value) async {
|
||||
await load();
|
||||
if (value.isEmpty) {
|
||||
_values.remove(key);
|
||||
} else {
|
||||
_values[key] = value;
|
||||
}
|
||||
await _flush();
|
||||
}
|
||||
|
||||
Future<void> _flush() async {
|
||||
await file.parent.create(recursive: true);
|
||||
await file.writeAsString(jsonEncode(_values), flush: true);
|
||||
if (!Platform.isWindows) {
|
||||
try {
|
||||
await Process.run('chmod', ['600', file.path]);
|
||||
} on ProcessException {
|
||||
// The app directory is already private on Android.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const echoTokenKey = 'target-echo-token';
|
||||
|
||||
String nodeTokenKey(String nodeId) => 'node-token.$nodeId';
|
||||
Reference in new issue
Block a user