// Persistence: saved servers in SharedPreferences, device tokens in the // platform's secure storage (Android Keystore). library; import 'dart:convert'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:shared_preferences/shared_preferences.dart'; class Connection { final String id; final String label; final String url; final String proxy; final bool insecure; final String fingerprint; final int lastUsedAt; Connection({ required this.id, required this.label, required this.url, this.proxy = '', this.insecure = false, this.fingerprint = '', this.lastUsedAt = 0, }); factory Connection.fromJson(Map j) => Connection( id: j['id'] as String? ?? '', label: j['label'] as String? ?? '', url: j['url'] as String? ?? '', proxy: j['proxy'] as String? ?? '', insecure: j['insecure'] as bool? ?? false, fingerprint: j['fingerprint'] as String? ?? '', lastUsedAt: (j['lastUsedAt'] as num?)?.toInt() ?? 0, ); Map toJson() => { 'id': id, 'label': label, 'url': url, 'proxy': proxy, 'insecure': insecure, 'fingerprint': fingerprint, 'lastUsedAt': lastUsedAt, }; Connection copyWith({ String? label, String? url, String? proxy, bool? insecure, String? fingerprint, int? lastUsedAt, }) => Connection( id: id, label: label ?? this.label, url: url ?? this.url, proxy: proxy ?? this.proxy, insecure: insecure ?? this.insecure, fingerprint: fingerprint ?? this.fingerprint, lastUsedAt: lastUsedAt ?? this.lastUsedAt, ); } class Storage { static const _connsKey = 'openchamber.connections'; static const _installKey = 'openchamber.installId'; static const _terminalPrefix = 'openchamber.terminalSessions.'; // v11 encrypts with AES-GCM and an Android Keystore-wrapped key by default. final _secure = const FlutterSecureStorage(); Future> connections() async { final prefs = await SharedPreferences.getInstance(); final raw = prefs.getString(_connsKey); if (raw == null || raw.isEmpty) return []; try { final list = jsonDecode(raw) as List; final out = list .map((e) => Connection.fromJson(e as Map)) .toList(); out.sort((a, b) => b.lastUsedAt.compareTo(a.lastUsedAt)); return out; } catch (_) { return []; } } Future saveConnections(List list) async { final sorted = [...list]..sort((a, b) => b.lastUsedAt.compareTo(a.lastUsedAt)); final prefs = await SharedPreferences.getInstance(); await prefs.setString(_connsKey, jsonEncode(sorted.map((e) => e.toJson()).toList())); } // ------------------------------------------------------------- terminals // The last terminal session per directory, so reopening the terminal after // an app restart can reattach to the shell the server still holds instead of // starting another one. Ids are meaningless across servers, hence one map // per connection. static String _terminalKey(String connectionId) => '$_terminalPrefix$connectionId'; Future> _terminalSessions(String connectionId) async { if (connectionId.isEmpty) return {}; final prefs = await SharedPreferences.getInstance(); final raw = prefs.getString(_terminalKey(connectionId)); if (raw == null || raw.isEmpty) return {}; try { final decoded = jsonDecode(raw); if (decoded is! Map) return {}; return { for (final entry in decoded.entries) if (entry.key is String && entry.value is String) entry.key as String: entry.value as String, }; } catch (_) { return {}; } } Future terminalSession(String connectionId, String directory) async => (await _terminalSessions(connectionId))[directory]; Future saveTerminalSession( String connectionId, String directory, String sessionId) async { if (connectionId.isEmpty || directory.isEmpty || sessionId.isEmpty) return; final prefs = await SharedPreferences.getInstance(); final map = await _terminalSessions(connectionId); map[directory] = sessionId; await prefs.setString(_terminalKey(connectionId), jsonEncode(map)); } Future clearTerminalSession( String connectionId, String directory) async { if (connectionId.isEmpty) return; final prefs = await SharedPreferences.getInstance(); final map = await _terminalSessions(connectionId); if (map.remove(directory) == null) return; if (map.isEmpty) { await prefs.remove(_terminalKey(connectionId)); } else { await prefs.setString(_terminalKey(connectionId), jsonEncode(map)); } } Future installId() async { final prefs = await SharedPreferences.getInstance(); var id = prefs.getString(_installKey); if (id == null || id.isEmpty) { id = _newId(); await prefs.setString(_installKey, id); } return id; } Future loadToken(String connectionId) async { return _secure.read(key: _tokenKey(connectionId)); } Future saveToken(String connectionId, String token) async { await _secure.write(key: _tokenKey(connectionId), value: token); } Future clearToken(String connectionId) async { await _secure.delete(key: _tokenKey(connectionId)); } String _tokenKey(String id) { final digest = _sha1(id); return 'openchamber.token.$digest'; } static String _newId() => 'srv_${DateTime.now().microsecondsSinceEpoch.toRadixString(36)}${(_rand() & 0xffffff).toRadixString(36).padLeft(6, '0')}'; static int _rand() => DateTime.now().microsecondsSinceEpoch % 0xffffffff; } String _sha1(String s) { // A small, dependency-free FNV-ish key is fine for a storage key: it only // names the secret, never secures it. var h = 0x811c9dc5; for (final c in s.codeUnits) { h ^= c; h = (h * 0x01000193) & 0xffffffff; } return h.toRadixString(16).padLeft(8, '0'); }