mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 15:24:35 +01:00
feat: GitHub Gist sync (#854)
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
extensions:
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
variables:
|
|
||||||
output: dist/
|
|
||||||
releases:
|
|
||||||
- name: linux
|
|
||||||
jobs:
|
|
||||||
- name: release-linux-deb
|
|
||||||
package:
|
|
||||||
platform: linux
|
|
||||||
target: deb
|
|
||||||
- name: release-linux-rpm
|
|
||||||
package:
|
|
||||||
platform: linux
|
|
||||||
target: rpm
|
|
||||||
15
lib/app.dart
15
lib/app.dart
@@ -96,19 +96,24 @@ class MyApp extends StatelessWidget {
|
|||||||
themeMode: themeMode,
|
themeMode: themeMode,
|
||||||
theme: light.fixWindowsFont,
|
theme: light.fixWindowsFont,
|
||||||
darkTheme: (tMode < 3 ? dark : dark.toAmoled).fixWindowsFont,
|
darkTheme: (tMode < 3 ? dark : dark.toAmoled).fixWindowsFont,
|
||||||
home: Builder(
|
home: FutureBuilder<List<IntroPageBuilder>>(
|
||||||
builder: (context) {
|
future: _IntroPage.builders,
|
||||||
|
builder: (context, snapshot) {
|
||||||
context.setLibL10n();
|
context.setLibL10n();
|
||||||
final appL10n = AppLocalizations.of(context);
|
final appL10n = AppLocalizations.of(context);
|
||||||
if (appL10n != null) l10n = appL10n;
|
if (appL10n != null) l10n = appL10n;
|
||||||
|
|
||||||
Widget child;
|
Widget child;
|
||||||
final intros = _IntroPage.builders;
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
|
child = const Scaffold(body: Center(child: CircularProgressIndicator()));
|
||||||
|
} else {
|
||||||
|
final intros = snapshot.data ?? [];
|
||||||
if (intros.isNotEmpty) {
|
if (intros.isNotEmpty) {
|
||||||
child = _IntroPage(intros);
|
child = _IntroPage(intros);
|
||||||
}
|
} else {
|
||||||
|
|
||||||
child = const HomePage();
|
child = const HomePage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return VirtualWindowFrame(title: BuildData.name, child: child);
|
return VirtualWindowFrame(title: BuildData.name, child: child);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -12,12 +12,28 @@ final class BakSyncer extends SyncIface {
|
|||||||
const BakSyncer._() : super();
|
const BakSyncer._() : super();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> saveToFile() => BackupV2.backup();
|
Future<void> saveToFile() async {
|
||||||
|
final pwd = await SecureStoreProps.bakPwd.read();
|
||||||
|
if (pwd == null || pwd.isEmpty) {
|
||||||
|
// Enforce password for non-clipboard backups
|
||||||
|
throw Exception('Backup password not set');
|
||||||
|
}
|
||||||
|
await BackupV2.backup(null, pwd);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Mergeable> fromFile(String path) async {
|
Future<Mergeable> fromFile(String path) async {
|
||||||
final content = await File(path).readAsString();
|
final content = await File(path).readAsString();
|
||||||
|
final pwd = await SecureStoreProps.bakPwd.read();
|
||||||
|
try {
|
||||||
|
if (Cryptor.isEncrypted(content)) {
|
||||||
|
return MergeableUtils.fromJsonString(content, pwd).$1;
|
||||||
|
}
|
||||||
return MergeableUtils.fromJsonString(content).$1;
|
return MergeableUtils.fromJsonString(content).$1;
|
||||||
|
} catch (_) {
|
||||||
|
// Fallback: try without password if detection failed
|
||||||
|
return MergeableUtils.fromJsonString(content).$1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -28,6 +44,9 @@ final class BakSyncer extends SyncIface {
|
|||||||
final webdavEnabled = PrefProps.webdavSync.get();
|
final webdavEnabled = PrefProps.webdavSync.get();
|
||||||
if (webdavEnabled) return Webdav.shared;
|
if (webdavEnabled) return Webdav.shared;
|
||||||
|
|
||||||
|
final gistEnabled = PrefProps.gistSync.get();
|
||||||
|
if (gistEnabled) return GistRs.shared;
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,7 @@ class SystemDetector {
|
|||||||
/// 2. 'uname -a' command to detect Linux/BSD/Darwin
|
/// 2. 'uname -a' command to detect Linux/BSD/Darwin
|
||||||
///
|
///
|
||||||
/// Returns [SystemType.linux] as default if detection fails.
|
/// Returns [SystemType.linux] as default if detection fails.
|
||||||
static Future<SystemType> detect(
|
static Future<SystemType> detect(SSHClient client, Spi spi) async {
|
||||||
SSHClient client,
|
|
||||||
Spi spi,
|
|
||||||
) async {
|
|
||||||
// First, check if custom system type is defined
|
// First, check if custom system type is defined
|
||||||
SystemType? detectedSystemType = spi.customSystemType;
|
SystemType? detectedSystemType = spi.customSystemType;
|
||||||
if (detectedSystemType != null) {
|
if (detectedSystemType != null) {
|
||||||
|
|||||||
@@ -5,20 +5,35 @@ import 'package:server_box/core/extension/context/locale.dart';
|
|||||||
import 'package:server_box/data/model/app/bak/backup2.dart';
|
import 'package:server_box/data/model/app/bak/backup2.dart';
|
||||||
import 'package:server_box/data/model/app/bak/backup_source.dart';
|
import 'package:server_box/data/model/app/bak/backup_source.dart';
|
||||||
import 'package:server_box/data/model/app/bak/utils.dart';
|
import 'package:server_box/data/model/app/bak/utils.dart';
|
||||||
import 'package:server_box/data/res/store.dart';
|
|
||||||
|
|
||||||
/// Service class for handling backup operations
|
/// Service class for handling backup operations
|
||||||
class BackupService {
|
class BackupService {
|
||||||
/// Perform backup operation with the given source
|
/// Perform backup operation with the given source
|
||||||
static Future<void> backup(BuildContext context, BackupSource source) async {
|
static Future<void> backup(BuildContext context, BackupSource source) async {
|
||||||
final password = await _getBackupPassword(context);
|
|
||||||
if (password == null) return;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
String? password;
|
||||||
|
|
||||||
|
if (source is ClipboardBackupSource) {
|
||||||
|
// Clipboard backup: allow optional password
|
||||||
|
password = await _getClipboardPassword(context);
|
||||||
|
if (password == null) return; // canceled
|
||||||
|
} else {
|
||||||
|
// All other backups require pre-set bakPwd (SecureStore)
|
||||||
|
final saved = await SecureStoreProps.bakPwd.read();
|
||||||
|
if (saved == null || saved.isEmpty) {
|
||||||
|
// Prompt to set before proceeding
|
||||||
|
password = await _showPasswordDialog(context, hint: l10n.backupPasswordTip);
|
||||||
|
if (password == null || password.isEmpty) return; // Not set
|
||||||
|
await SecureStoreProps.bakPwd.write(password);
|
||||||
|
context.showSnackBar(l10n.backupPasswordSet);
|
||||||
|
} else {
|
||||||
|
password = saved;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final path = await BackupV2.backup(null, password.isEmpty ? null : password);
|
final path = await BackupV2.backup(null, password.isEmpty ? null : password);
|
||||||
await source.saveContent(path);
|
await source.saveContent(path);
|
||||||
|
|
||||||
// Show success message for clipboard source
|
|
||||||
if (source is ClipboardBackupSource) {
|
if (source is ClipboardBackupSource) {
|
||||||
context.showSnackBar(libL10n.success);
|
context.showSnackBar(libL10n.success);
|
||||||
}
|
}
|
||||||
@@ -42,12 +57,12 @@ class BackupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Handle password dialog for backup operations
|
/// Handle password dialog for backup operations
|
||||||
static Future<String?> _getBackupPassword(BuildContext context) async {
|
static Future<String?> _getClipboardPassword(BuildContext context) async {
|
||||||
final savedPassword = await Stores.setting.backupasswd.read();
|
// Use saved bakPwd as default for clipboard flow if exists, but allow empty/custom
|
||||||
|
final savedPassword = await SecureStoreProps.bakPwd.read();
|
||||||
String? password;
|
String? password;
|
||||||
|
|
||||||
if (savedPassword != null && savedPassword.isNotEmpty) {
|
if (savedPassword != null && savedPassword.isNotEmpty) {
|
||||||
// Use saved password or ask for custom password
|
|
||||||
final useCustom = await context.showRoundDialog<bool>(
|
final useCustom = await context.showRoundDialog<bool>(
|
||||||
title: l10n.backupPassword,
|
title: l10n.backupPassword,
|
||||||
child: Text(l10n.backupPasswordTip),
|
child: Text(l10n.backupPasswordTip),
|
||||||
@@ -57,19 +72,15 @@ class BackupService {
|
|||||||
TextButton(onPressed: () => context.pop(true), child: Text(libL10n.custom)),
|
TextButton(onPressed: () => context.pop(true), child: Text(libL10n.custom)),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
if (useCustom == null) return null;
|
if (useCustom == null) return null;
|
||||||
|
|
||||||
if (useCustom) {
|
if (useCustom) {
|
||||||
password = await _showPasswordDialog(context, initial: savedPassword);
|
password = await _showPasswordDialog(context, initial: savedPassword);
|
||||||
} else {
|
} else {
|
||||||
password = savedPassword;
|
password = savedPassword;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// No saved password, ask if user wants to set one
|
|
||||||
password = await _showPasswordDialog(context);
|
password = await _showPasswordDialog(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +106,7 @@ class BackupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try with saved password first
|
// Try with saved password first
|
||||||
final savedPassword = await Stores.setting.backupasswd.read();
|
final savedPassword = await SecureStoreProps.bakPwd.read();
|
||||||
if (savedPassword != null && savedPassword.isNotEmpty) {
|
if (savedPassword != null && savedPassword.isNotEmpty) {
|
||||||
try {
|
try {
|
||||||
final (backup, err) = await context.showLoadingDialog(
|
final (backup, err) = await context.showLoadingDialog(
|
||||||
|
|||||||
@@ -196,10 +196,14 @@ esac''');
|
|||||||
/// Get Unix status command with OS detection
|
/// Get Unix status command with OS detection
|
||||||
String _getUnixStatusCommand({required List<String> disabledCmdTypes}) {
|
String _getUnixStatusCommand({required List<String> disabledCmdTypes}) {
|
||||||
// Generate command lists with command-specific separators, filtering disabled commands
|
// Generate command lists with command-specific separators, filtering disabled commands
|
||||||
final filteredLinuxCmdTypes = StatusCmdType.values.where((e) => !disabledCmdTypes.contains(e.displayName));
|
final filteredLinuxCmdTypes = StatusCmdType.values.where(
|
||||||
|
(e) => !disabledCmdTypes.contains(e.displayName),
|
||||||
|
);
|
||||||
final linuxCommands = filteredLinuxCmdTypes.map((e) => '${e.divider}${e.cmd}').join('').trimRight();
|
final linuxCommands = filteredLinuxCmdTypes.map((e) => '${e.divider}${e.cmd}').join('').trimRight();
|
||||||
|
|
||||||
final filteredBsdCmdTypes = BSDStatusCmdType.values.where((e) => !disabledCmdTypes.contains(e.displayName));
|
final filteredBsdCmdTypes = BSDStatusCmdType.values.where(
|
||||||
|
(e) => !disabledCmdTypes.contains(e.displayName),
|
||||||
|
);
|
||||||
final bsdCommands = filteredBsdCmdTypes.map((e) => '${e.divider}${e.cmd}').join('').trimRight();
|
final bsdCommands = filteredBsdCmdTypes.map((e) => '${e.divider}${e.cmd}').join('').trimRight();
|
||||||
|
|
||||||
return '''
|
return '''
|
||||||
|
|||||||
@@ -93,7 +93,11 @@ class ShellFuncManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Generate complete script based on system type
|
/// Generate complete script based on system type
|
||||||
static String allScript(Map<String, String>? customCmds, {SystemType? systemType, List<String>? disabledCmdTypes}) {
|
static String allScript(
|
||||||
|
Map<String, String>? customCmds, {
|
||||||
|
SystemType? systemType,
|
||||||
|
List<String>? disabledCmdTypes,
|
||||||
|
}) {
|
||||||
final isWindows = systemType == SystemType.windows;
|
final isWindows = systemType == SystemType.windows;
|
||||||
final builder = ScriptBuilderFactory.getBuilder(isWindows);
|
final builder = ScriptBuilderFactory.getBuilder(isWindows);
|
||||||
|
|
||||||
|
|||||||
@@ -142,16 +142,7 @@ class SingleCpuCore extends TimeSeqIface<SingleCpuCore> {
|
|||||||
final int irq;
|
final int irq;
|
||||||
final int softirq;
|
final int softirq;
|
||||||
|
|
||||||
SingleCpuCore(
|
SingleCpuCore(this.id, this.user, this.sys, this.nice, this.idle, this.iowait, this.irq, this.softirq);
|
||||||
this.id,
|
|
||||||
this.user,
|
|
||||||
this.sys,
|
|
||||||
this.nice,
|
|
||||||
this.idle,
|
|
||||||
this.iowait,
|
|
||||||
this.irq,
|
|
||||||
this.softirq,
|
|
||||||
);
|
|
||||||
|
|
||||||
int get total => user + sys + nice + idle + iowait + irq + softirq;
|
int get total => user + sys + nice + idle + iowait + irq + softirq;
|
||||||
|
|
||||||
@@ -200,11 +191,11 @@ final class CpuBrand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final _bsdCpuPercentReg = RegExp(r'(\d+\.\d+)%');
|
final _bsdCpuPercentReg = RegExp(r'(\d+\.\d+)%');
|
||||||
final _macCpuPercentReg = RegExp(
|
final _macCpuPercentReg = RegExp(r'CPU usage: ([\d.]+)% user, ([\d.]+)% sys, ([\d.]+)% idle');
|
||||||
r'CPU usage: ([\d.]+)% user, ([\d.]+)% sys, ([\d.]+)% idle');
|
|
||||||
final _freebsdCpuPercentReg = RegExp(
|
final _freebsdCpuPercentReg = RegExp(
|
||||||
r'CPU: ([\d.]+)% user, ([\d.]+)% nice, ([\d.]+)% system, '
|
r'CPU: ([\d.]+)% user, ([\d.]+)% nice, ([\d.]+)% system, '
|
||||||
r'([\d.]+)% interrupt, ([\d.]+)% idle');
|
r'([\d.]+)% interrupt, ([\d.]+)% idle',
|
||||||
|
);
|
||||||
|
|
||||||
/// Parse CPU status on BSD system with support for different BSD variants
|
/// Parse CPU status on BSD system with support for different BSD variants
|
||||||
///
|
///
|
||||||
@@ -264,7 +255,15 @@ Cpus parseBsdCpu(String raw) {
|
|||||||
// Fallback to generic percentage extraction
|
// Fallback to generic percentage extraction
|
||||||
final percents = _bsdCpuPercentReg
|
final percents = _bsdCpuPercentReg
|
||||||
.allMatches(raw)
|
.allMatches(raw)
|
||||||
.map((e) => double.parse(e.group(1) ?? '0'))
|
.map((e) {
|
||||||
|
final valueStr = e.group(1) ?? '0';
|
||||||
|
final value = double.tryParse(valueStr);
|
||||||
|
if (value == null) {
|
||||||
|
dprint('Warning: Failed to parse CPU percentage from "$valueStr"');
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
})
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
if (percents.length >= 3) {
|
if (percents.length >= 3) {
|
||||||
@@ -288,7 +287,9 @@ Cpus parseBsdCpu(String raw) {
|
|||||||
]);
|
]);
|
||||||
return init;
|
return init;
|
||||||
} else if (percents.isNotEmpty) {
|
} else if (percents.isNotEmpty) {
|
||||||
Loggers.app.warning('BSD CPU fallback parsing found ${percents.length} percentages (expected at least 3) in: $raw');
|
Loggers.app.warning(
|
||||||
|
'BSD CPU fallback parsing found ${percents.length} percentages (expected at least 3) in: $raw',
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
Loggers.app.warning('BSD CPU fallback parsing found no percentages in: $raw');
|
Loggers.app.warning('BSD CPU fallback parsing found no percentages in: $raw');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,15 +19,11 @@ class Memory {
|
|||||||
static Memory parse(String raw) {
|
static Memory parse(String raw) {
|
||||||
final items = raw.split('\n').map((e) => memItemReg.firstMatch(e)).toList();
|
final items = raw.split('\n').map((e) => memItemReg.firstMatch(e)).toList();
|
||||||
|
|
||||||
final total = int.tryParse(
|
final total =
|
||||||
items.firstWhereOrNull((e) => e?.group(1) == 'MemTotal:')
|
int.tryParse(items.firstWhereOrNull((e) => e?.group(1) == 'MemTotal:')?.group(2) ?? '1') ?? 1;
|
||||||
?.group(2) ?? '1') ?? 1;
|
final free = int.tryParse(items.firstWhereOrNull((e) => e?.group(1) == 'MemFree:')?.group(2) ?? '0') ?? 0;
|
||||||
final free = int.tryParse(
|
final available =
|
||||||
items.firstWhereOrNull((e) => e?.group(1) == 'MemFree:')
|
int.tryParse(items.firstWhereOrNull((e) => e?.group(1) == 'MemAvailable:')?.group(2) ?? '0') ?? 0;
|
||||||
?.group(2) ?? '0') ?? 0;
|
|
||||||
final available = int.tryParse(
|
|
||||||
items.firstWhereOrNull((e) => e?.group(1) == 'MemAvailable:')
|
|
||||||
?.group(2) ?? '0') ?? 0;
|
|
||||||
|
|
||||||
return Memory(total: total, free: free, avail: available);
|
return Memory(total: total, free: free, avail: available);
|
||||||
}
|
}
|
||||||
@@ -42,8 +38,7 @@ final memItemReg = RegExp(r'([A-Z].+:)\s+([0-9]+) kB');
|
|||||||
/// - FreeBSD: "Mem: 456M Active, 2918M Inact, 1127M Wired, 187M Cache, 829M Buf, 3535M Free"
|
/// - FreeBSD: "Mem: 456M Active, 2918M Inact, 1127M Wired, 187M Cache, 829M Buf, 3535M Free"
|
||||||
Memory parseBsdMemory(String raw) {
|
Memory parseBsdMemory(String raw) {
|
||||||
// Try macOS format first: "PhysMem: 32G used (1536M wired), 64G unused."
|
// Try macOS format first: "PhysMem: 32G used (1536M wired), 64G unused."
|
||||||
final macMemReg = RegExp(
|
final macMemReg = RegExp(r'PhysMem:\s*([\d.]+)([KMGT])\s*used.*?,\s*([\d.]+)([KMGT])\s*unused');
|
||||||
r'PhysMem:\s*([\d.]+)([KMGT])\s*used.*?,\s*([\d.]+)([KMGT])\s*unused');
|
|
||||||
final macMatch = macMemReg.firstMatch(raw);
|
final macMatch = macMemReg.firstMatch(raw);
|
||||||
|
|
||||||
if (macMatch != null) {
|
if (macMatch != null) {
|
||||||
@@ -58,8 +53,7 @@ Memory parseBsdMemory(String raw) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try FreeBSD format: "Mem: 456M Active, 2918M Inact, 1127M Wired, 187M Cache, 829M Buf, 3535M Free"
|
// Try FreeBSD format: "Mem: 456M Active, 2918M Inact, 1127M Wired, 187M Cache, 829M Buf, 3535M Free"
|
||||||
final freeBsdReg = RegExp(
|
final freeBsdReg = RegExp(r'(\d+)([KMGT])\s+(Active|Inact|Wired|Cache|Buf|Free)', caseSensitive: false);
|
||||||
r'(\d+)([KMGT])\s+(Active|Inact|Wired|Cache|Buf|Free)', caseSensitive: false);
|
|
||||||
final matches = freeBsdReg.allMatches(raw);
|
final matches = freeBsdReg.allMatches(raw);
|
||||||
|
|
||||||
if (matches.isNotEmpty) {
|
if (matches.isNotEmpty) {
|
||||||
@@ -72,7 +66,11 @@ Memory parseBsdMemory(String raw) {
|
|||||||
final kb = _convertToKB(amount, unit);
|
final kb = _convertToKB(amount, unit);
|
||||||
|
|
||||||
// Only sum known keywords
|
// Only sum known keywords
|
||||||
if (keyword == 'active' || keyword == 'inact' || keyword == 'wired' || keyword == 'cache' || keyword == 'buf') {
|
if (keyword == 'active' ||
|
||||||
|
keyword == 'inact' ||
|
||||||
|
keyword == 'wired' ||
|
||||||
|
keyword == 'cache' ||
|
||||||
|
keyword == 'buf') {
|
||||||
usedKB += kb;
|
usedKB += kb;
|
||||||
} else if (keyword == 'free') {
|
} else if (keyword == 'free') {
|
||||||
freeKB += kb;
|
freeKB += kb;
|
||||||
@@ -121,15 +119,12 @@ class Swap {
|
|||||||
static Swap parse(String raw) {
|
static Swap parse(String raw) {
|
||||||
final items = raw.split('\n').map((e) => memItemReg.firstMatch(e)).toList();
|
final items = raw.split('\n').map((e) => memItemReg.firstMatch(e)).toList();
|
||||||
|
|
||||||
final total = int.tryParse(
|
final total =
|
||||||
items.firstWhereOrNull((e) => e?.group(1) == 'SwapTotal:')
|
int.tryParse(items.firstWhereOrNull((e) => e?.group(1) == 'SwapTotal:')?.group(2) ?? '1') ?? 0;
|
||||||
?.group(2) ?? '1') ?? 0;
|
final free =
|
||||||
final free = int.tryParse(
|
int.tryParse(items.firstWhereOrNull((e) => e?.group(1) == 'SwapFree:')?.group(2) ?? '1') ?? 0;
|
||||||
items.firstWhereOrNull((e) => e?.group(1) == 'SwapFree:')
|
final cached =
|
||||||
?.group(2) ?? '1') ?? 0;
|
int.tryParse(items.firstWhereOrNull((e) => e?.group(1) == 'SwapCached:')?.group(2) ?? '0') ?? 0;
|
||||||
final cached = int.tryParse(
|
|
||||||
items.firstWhereOrNull((e) => e?.group(1) == 'SwapCached:')
|
|
||||||
?.group(2) ?? '0') ?? 0;
|
|
||||||
|
|
||||||
return Swap(total: total, free: free, cached: cached);
|
return Swap(total: total, free: free, cached: cached);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ Future<ServerStatus> getStatus(ServerStatusUpdateReq req) async {
|
|||||||
Future<ServerStatus> _getLinuxStatus(ServerStatusUpdateReq req) async {
|
Future<ServerStatus> _getLinuxStatus(ServerStatusUpdateReq req) async {
|
||||||
final parsedOutput = req.parsedOutput;
|
final parsedOutput = req.parsedOutput;
|
||||||
|
|
||||||
final time = int.tryParse(StatusCmdType.time.findInMap(parsedOutput)) ??
|
final time =
|
||||||
|
int.tryParse(StatusCmdType.time.findInMap(parsedOutput)) ??
|
||||||
DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -259,9 +260,7 @@ String? _parseUpTime(String raw) {
|
|||||||
if (splitedComma.length >= 2) {
|
if (splitedComma.length >= 2) {
|
||||||
final timePart = splitedComma[1].trim();
|
final timePart = splitedComma[1].trim();
|
||||||
// Check if it's in HH:MM format
|
// Check if it's in HH:MM format
|
||||||
if (timePart.contains(':') &&
|
if (timePart.contains(':') && !timePart.contains('user') && !timePart.contains('load')) {
|
||||||
!timePart.contains('user') &&
|
|
||||||
!timePart.contains('load')) {
|
|
||||||
return '$firstPart, $timePart';
|
return '$firstPart, $timePart';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -269,9 +268,7 @@ String? _parseUpTime(String raw) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Case 2: "2:34" (hours:minutes) - already in good format
|
// Case 2: "2:34" (hours:minutes) - already in good format
|
||||||
if (firstPart.contains(':') &&
|
if (firstPart.contains(':') && !firstPart.contains('user') && !firstPart.contains('load')) {
|
||||||
!firstPart.contains('user') &&
|
|
||||||
!firstPart.contains('load')) {
|
|
||||||
return firstPart;
|
return firstPart;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,7 +300,8 @@ String? _parseHostName(String raw) {
|
|||||||
// Windows status parsing implementation
|
// Windows status parsing implementation
|
||||||
Future<ServerStatus> _getWindowsStatus(ServerStatusUpdateReq req) async {
|
Future<ServerStatus> _getWindowsStatus(ServerStatusUpdateReq req) async {
|
||||||
final parsedOutput = req.parsedOutput;
|
final parsedOutput = req.parsedOutput;
|
||||||
final time = int.tryParse(WindowsStatusCmdType.time.findInMap(parsedOutput)) ??
|
final time =
|
||||||
|
int.tryParse(WindowsStatusCmdType.time.findInMap(parsedOutput)) ??
|
||||||
DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||||
|
|
||||||
// Parse all different resource types using helper methods
|
// Parse all different resource types using helper methods
|
||||||
@@ -372,10 +370,7 @@ void _parseWindowsCpuData(ServerStatusUpdateReq req, Map<String, String> parsedO
|
|||||||
try {
|
try {
|
||||||
// Windows CPU parsing - JSON format from PowerShell
|
// Windows CPU parsing - JSON format from PowerShell
|
||||||
final cpuRaw = WindowsStatusCmdType.cpu.findInMap(parsedOutput);
|
final cpuRaw = WindowsStatusCmdType.cpu.findInMap(parsedOutput);
|
||||||
if (cpuRaw.isNotEmpty &&
|
if (cpuRaw.isNotEmpty && cpuRaw != 'null' && !cpuRaw.contains('error') && !cpuRaw.contains('Exception')) {
|
||||||
cpuRaw != 'null' &&
|
|
||||||
!cpuRaw.contains('error') &&
|
|
||||||
!cpuRaw.contains('Exception')) {
|
|
||||||
final cpus = WindowsParser.parseCpu(cpuRaw, req.ss);
|
final cpus = WindowsParser.parseCpu(cpuRaw, req.ss);
|
||||||
if (cpus.isNotEmpty) {
|
if (cpus.isNotEmpty) {
|
||||||
req.ss.cpu.update(cpus);
|
req.ss.cpu.update(cpus);
|
||||||
@@ -397,10 +392,7 @@ void _parseWindowsCpuData(ServerStatusUpdateReq req, Map<String, String> parsedO
|
|||||||
void _parseWindowsMemoryData(ServerStatusUpdateReq req, Map<String, String> parsedOutput) {
|
void _parseWindowsMemoryData(ServerStatusUpdateReq req, Map<String, String> parsedOutput) {
|
||||||
try {
|
try {
|
||||||
final memRaw = WindowsStatusCmdType.mem.findInMap(parsedOutput);
|
final memRaw = WindowsStatusCmdType.mem.findInMap(parsedOutput);
|
||||||
if (memRaw.isNotEmpty &&
|
if (memRaw.isNotEmpty && memRaw != 'null' && !memRaw.contains('error') && !memRaw.contains('Exception')) {
|
||||||
memRaw != 'null' &&
|
|
||||||
!memRaw.contains('error') &&
|
|
||||||
!memRaw.contains('Exception')) {
|
|
||||||
final memory = WindowsParser.parseMemory(memRaw);
|
final memory = WindowsParser.parseMemory(memRaw);
|
||||||
if (memory != null) {
|
if (memory != null) {
|
||||||
req.ss.mem = memory;
|
req.ss.mem = memory;
|
||||||
@@ -506,7 +498,6 @@ void _parseWindowsGpuData(ServerStatusUpdateReq req, Map<String, String> parsedO
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
List<Battery> _parseWindowsBatteries(String raw) {
|
List<Battery> _parseWindowsBatteries(String raw) {
|
||||||
try {
|
try {
|
||||||
final dynamic jsonData = json.decode(raw);
|
final dynamic jsonData = json.decode(raw);
|
||||||
@@ -515,24 +506,19 @@ List<Battery> _parseWindowsBatteries(String raw) {
|
|||||||
final batteryList = jsonData is List ? jsonData : [jsonData];
|
final batteryList = jsonData is List ? jsonData : [jsonData];
|
||||||
|
|
||||||
for (final batteryData in batteryList) {
|
for (final batteryData in batteryList) {
|
||||||
final chargeRemaining =
|
final chargeRemaining = batteryData['EstimatedChargeRemaining'] as int? ?? 0;
|
||||||
batteryData['EstimatedChargeRemaining'] as int? ?? 0;
|
|
||||||
final batteryStatus = batteryData['BatteryStatus'] as int? ?? 0;
|
final batteryStatus = batteryData['BatteryStatus'] as int? ?? 0;
|
||||||
|
|
||||||
// Windows battery status: 1=Other, 2=Unknown, 3=Full, 4=Low,
|
// Windows battery status: 1=Other, 2=Unknown, 3=Full, 4=Low,
|
||||||
// 5=Critical, 6=Charging, 7=ChargingAndLow, 8=ChargingAndCritical,
|
// 5=Critical, 6=Charging, 7=ChargingAndLow, 8=ChargingAndCritical,
|
||||||
// 9=Undefined, 10=PartiallyCharged
|
// 9=Undefined, 10=PartiallyCharged
|
||||||
final isCharging = batteryStatus == 6 ||
|
final isCharging = batteryStatus == 6 || batteryStatus == 7 || batteryStatus == 8;
|
||||||
batteryStatus == 7 ||
|
|
||||||
batteryStatus == 8;
|
|
||||||
|
|
||||||
batteries.add(
|
batteries.add(
|
||||||
Battery(
|
Battery(
|
||||||
name: 'Battery',
|
name: 'Battery',
|
||||||
percent: chargeRemaining,
|
percent: chargeRemaining,
|
||||||
status: isCharging
|
status: isCharging ? BatteryStatus.charging : BatteryStatus.discharging,
|
||||||
? BatteryStatus.charging
|
|
||||||
: BatteryStatus.discharging,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -579,12 +565,7 @@ List<NetSpeedPart> _parseWindowsNetwork(String raw, int currentTime) {
|
|||||||
final tx = interfaceTx[interfaceName] ?? 0;
|
final tx = interfaceTx[interfaceName] ?? 0;
|
||||||
|
|
||||||
netParts.add(
|
netParts.add(
|
||||||
NetSpeedPart(
|
NetSpeedPart(interfaceName, BigInt.from(rx.toInt()), BigInt.from(tx.toInt()), currentTime),
|
||||||
interfaceName,
|
|
||||||
BigInt.from(rx.toInt()),
|
|
||||||
BigInt.from(tx.toInt()),
|
|
||||||
currentTime,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -668,9 +649,7 @@ String _extractDiskName(String path) {
|
|||||||
void _parseWindowsTemperatures(Temperatures temps, String raw) {
|
void _parseWindowsTemperatures(Temperatures temps, String raw) {
|
||||||
try {
|
try {
|
||||||
// Handle error output
|
// Handle error output
|
||||||
if (raw.contains('Error') ||
|
if (raw.contains('Error') || raw.contains('Exception') || raw.contains('The term')) {
|
||||||
raw.contains('Exception') ||
|
|
||||||
raw.contains('The term')) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,7 @@ enum SystemType {
|
|||||||
/// it defaults to Linux but logs the detection failure for debugging.
|
/// it defaults to Linux but logs the detection failure for debugging.
|
||||||
static SystemType parse(String value) {
|
static SystemType parse(String value) {
|
||||||
// Log the raw value for debugging purposes (truncated to avoid spam)
|
// Log the raw value for debugging purposes (truncated to avoid spam)
|
||||||
final truncatedValue = value.length > 100
|
final truncatedValue = value.length > 100 ? '${value.substring(0, 100)}...' : value;
|
||||||
? '${value.substring(0, 100)}...'
|
|
||||||
: value;
|
|
||||||
|
|
||||||
if (value.contains(windowsSign)) {
|
if (value.contains(windowsSign)) {
|
||||||
Loggers.app.info('System detected as Windows from signature in: $truncatedValue');
|
Loggers.app.info('System detected as Windows from signature in: $truncatedValue');
|
||||||
@@ -37,13 +35,13 @@ enum SystemType {
|
|||||||
if (value.trim().isEmpty) {
|
if (value.trim().isEmpty) {
|
||||||
Loggers.app.warning(
|
Loggers.app.warning(
|
||||||
'System detection received empty input, defaulting to Linux. '
|
'System detection received empty input, defaulting to Linux. '
|
||||||
'This may indicate a script execution issue.'
|
'This may indicate a script execution issue.',
|
||||||
);
|
);
|
||||||
} else if (!value.contains(linuxSign)) {
|
} else if (!value.contains(linuxSign)) {
|
||||||
Loggers.app.warning(
|
Loggers.app.warning(
|
||||||
'System detection could not find any known signatures (Windows: $windowsSign, '
|
'System detection could not find any known signatures (Windows: $windowsSign, '
|
||||||
'BSD: $bsdSign, Linux: $linuxSign) in output: "$truncatedValue". '
|
'BSD: $bsdSign, Linux: $linuxSign) in output: "$truncatedValue". '
|
||||||
'Defaulting to Linux, but this may cause incorrect parsing.'
|
'Defaulting to Linux, but this may cause incorrect parsing.',
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
Loggers.app.info('System detected as Linux from signature in: $truncatedValue');
|
Loggers.app.info('System detected as Linux from signature in: $truncatedValue');
|
||||||
@@ -51,5 +49,4 @@ enum SystemType {
|
|||||||
|
|
||||||
return SystemType.linux;
|
return SystemType.linux;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,9 +36,7 @@ class WindowsParser {
|
|||||||
static String? parseUpTime(String raw) {
|
static String? parseUpTime(String raw) {
|
||||||
try {
|
try {
|
||||||
// Clean the input - trim whitespace and get the first non-empty line
|
// Clean the input - trim whitespace and get the first non-empty line
|
||||||
final cleanedInput = raw.trim().split('\n')
|
final cleanedInput = raw.trim().split('\n').where((line) => line.trim().isNotEmpty).firstOrNull;
|
||||||
.where((line) => line.trim().isNotEmpty)
|
|
||||||
.firstOrNull;
|
|
||||||
|
|
||||||
if (cleanedInput == null || cleanedInput.isEmpty) {
|
if (cleanedInput == null || cleanedInput.isEmpty) {
|
||||||
Loggers.app.warning('Windows uptime parsing: empty or null input');
|
Loggers.app.warning('Windows uptime parsing: empty or null input');
|
||||||
@@ -72,8 +70,11 @@ class WindowsParser {
|
|||||||
final uptime = now.difference(dateTime);
|
final uptime = now.difference(dateTime);
|
||||||
|
|
||||||
// Validate that the uptime is reasonable (not negative, not too far in the future)
|
// Validate that the uptime is reasonable (not negative, not too far in the future)
|
||||||
if (uptime.isNegative || uptime.inDays > 3650) { // More than 10 years seems unreasonable
|
if (uptime.isNegative || uptime.inDays > 3650) {
|
||||||
Loggers.app.warning('Windows uptime parsing: unreasonable uptime calculated: ${uptime.inDays} days for date: $cleanedInput');
|
// More than 10 years seems unreasonable
|
||||||
|
Loggers.app.warning(
|
||||||
|
'Windows uptime parsing: unreasonable uptime calculated: ${uptime.inDays} days for date: $cleanedInput',
|
||||||
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,22 +201,19 @@ class WindowsParser {
|
|||||||
|
|
||||||
for (final diskData in diskList) {
|
for (final diskData in diskList) {
|
||||||
final deviceId = diskData['DeviceID']?.toString() ?? '';
|
final deviceId = diskData['DeviceID']?.toString() ?? '';
|
||||||
final size =
|
final size = BigInt.tryParse(diskData['Size']?.toString() ?? '0') ?? BigInt.zero;
|
||||||
BigInt.tryParse(diskData['Size']?.toString() ?? '0') ?? BigInt.zero;
|
final freeSpace = BigInt.tryParse(diskData['FreeSpace']?.toString() ?? '0') ?? BigInt.zero;
|
||||||
final freeSpace =
|
|
||||||
BigInt.tryParse(diskData['FreeSpace']?.toString() ?? '0') ??
|
|
||||||
BigInt.zero;
|
|
||||||
final fileSystem = diskData['FileSystem']?.toString() ?? '';
|
final fileSystem = diskData['FileSystem']?.toString() ?? '';
|
||||||
|
|
||||||
// Validate all required fields
|
// Validate all required fields
|
||||||
final hasRequiredFields = deviceId.isNotEmpty &&
|
final hasRequiredFields =
|
||||||
size != BigInt.zero &&
|
deviceId.isNotEmpty && size != BigInt.zero && freeSpace != BigInt.zero && fileSystem.isNotEmpty;
|
||||||
freeSpace != BigInt.zero &&
|
|
||||||
fileSystem.isNotEmpty;
|
|
||||||
|
|
||||||
if (!hasRequiredFields) {
|
if (!hasRequiredFields) {
|
||||||
Loggers.app.warning('Windows disk parsing: skipping disk with missing required fields. '
|
Loggers.app.warning(
|
||||||
'DeviceID: $deviceId, Size: $size, FreeSpace: $freeSpace, FileSystem: $fileSystem');
|
'Windows disk parsing: skipping disk with missing required fields. '
|
||||||
|
'DeviceID: $deviceId, Size: $size, FreeSpace: $freeSpace, FileSystem: $fileSystem',
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,7 +221,7 @@ class WindowsParser {
|
|||||||
final freeKB = freeSpace ~/ BigInt.from(1024);
|
final freeKB = freeSpace ~/ BigInt.from(1024);
|
||||||
final usedKB = sizeKB - freeKB;
|
final usedKB = sizeKB - freeKB;
|
||||||
final usedPercent = sizeKB > BigInt.zero
|
final usedPercent = sizeKB > BigInt.zero
|
||||||
? ((usedKB * BigInt.from(100)) ~/ sizeKB).toInt()
|
? ((usedKB * BigInt.from(100)) ~/ sizeKB).toInt().clamp(0, 100)
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
disks.add(
|
disks.add(
|
||||||
|
|||||||
@@ -382,7 +382,11 @@ class ServerProvider extends Provider {
|
|||||||
sv.status.system = detectedSystemType;
|
sv.status.system = detectedSystemType;
|
||||||
|
|
||||||
final (_, writeScriptResult) = await sv.client!.exec((session) async {
|
final (_, writeScriptResult) = await sv.client!.exec((session) async {
|
||||||
final scriptRaw = ShellFuncManager.allScript(spi.custom?.cmds, systemType: detectedSystemType, disabledCmdTypes: spi.disabledCmdTypes).uint8List;
|
final scriptRaw = ShellFuncManager.allScript(
|
||||||
|
spi.custom?.cmds,
|
||||||
|
systemType: detectedSystemType,
|
||||||
|
disabledCmdTypes: spi.disabledCmdTypes,
|
||||||
|
).uint8List;
|
||||||
session.stdin.add(scriptRaw);
|
session.stdin.add(scriptRaw);
|
||||||
session.stdin.close();
|
session.stdin.close();
|
||||||
}, entry: ShellFuncManager.getInstallShellCmd(spi.id, systemType: detectedSystemType));
|
}, entry: ShellFuncManager.getInstallShellCmd(spi.id, systemType: detectedSystemType));
|
||||||
|
|||||||
@@ -81,10 +81,7 @@ done
|
|||||||
|
|
||||||
final parsedUnits = <SystemdUnit>[];
|
final parsedUnits = <SystemdUnit>[];
|
||||||
for (final unit in units.where((e) => e.trim().isNotEmpty)) {
|
for (final unit in units.where((e) => e.trim().isNotEmpty)) {
|
||||||
final parts = unit
|
final parts = unit.split('\n').where((e) => e.trim().isNotEmpty).toList();
|
||||||
.split('\n')
|
|
||||||
.where((e) => e.trim().isNotEmpty)
|
|
||||||
.toList();
|
|
||||||
if (parts.isEmpty) continue;
|
if (parts.isEmpty) continue;
|
||||||
var name = '';
|
var name = '';
|
||||||
var type = '';
|
var type = '';
|
||||||
|
|||||||
120
lib/intro.dart
120
lib/intro.dart
@@ -5,7 +5,7 @@ final class _IntroPage extends StatelessWidget {
|
|||||||
|
|
||||||
const _IntroPage(this.pages);
|
const _IntroPage(this.pages);
|
||||||
|
|
||||||
static const _builders = {1: _buildAppSettings};
|
static const _builders = {1: _buildAppSettings, 2: _buildBackupPasswordMigration};
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -33,6 +33,43 @@ final class _IntroPage extends StatelessWidget {
|
|||||||
SizedBox(height: padTop),
|
SizedBox(height: padTop),
|
||||||
IntroPage.title(text: l10n.init, big: true),
|
IntroPage.title(text: l10n.init, big: true),
|
||||||
SizedBox(height: padTop),
|
SizedBox(height: padTop),
|
||||||
|
// Prompt to set backup password after migration or on first launch
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.lock),
|
||||||
|
title: Text(l10n.backupPassword),
|
||||||
|
subtitle: Text(l10n.backupPasswordTip, style: UIs.textGrey),
|
||||||
|
trailing: const Icon(Icons.keyboard_arrow_right),
|
||||||
|
onTap: () async {
|
||||||
|
final currentPwd = await SecureStoreProps.bakPwd.read();
|
||||||
|
final controller = TextEditingController(text: currentPwd ?? '');
|
||||||
|
final result = await ctx.showRoundDialog<bool>(
|
||||||
|
title: l10n.backupPassword,
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Text(l10n.backupPasswordTip, style: UIs.textGrey),
|
||||||
|
UIs.height13,
|
||||||
|
Input(
|
||||||
|
label: l10n.backupPassword,
|
||||||
|
controller: controller,
|
||||||
|
obscureText: true,
|
||||||
|
onSubmitted: (_) => ctx.pop(true),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: Btnx.oks,
|
||||||
|
);
|
||||||
|
if (result == true) {
|
||||||
|
final pwd = controller.text.trim();
|
||||||
|
if (pwd.isEmpty) {
|
||||||
|
ctx.showSnackBar(libL10n.empty);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await SecureStoreProps.bakPwd.write(pwd);
|
||||||
|
ctx.showSnackBar(l10n.backupPasswordSet);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
).cardx,
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(IonIcons.language),
|
leading: const Icon(IonIcons.language),
|
||||||
title: Text(libL10n.language),
|
title: Text(libL10n.language),
|
||||||
@@ -76,9 +113,86 @@ final class _IntroPage extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<IntroPageBuilder> get builders {
|
static Widget _buildBackupPasswordMigration(BuildContext ctx, double padTop) {
|
||||||
|
return ListView(
|
||||||
|
padding: _introListPad,
|
||||||
|
children: [
|
||||||
|
SizedBox(height: padTop),
|
||||||
|
IntroPage.title(text: l10n.backupPassword, big: true),
|
||||||
|
SizedBox(height: padTop * 0.5),
|
||||||
|
Text(
|
||||||
|
'${l10n.backupTip}\n\n${l10n.backupPasswordTip}',
|
||||||
|
style: const TextStyle(fontSize: 16),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
SizedBox(height: padTop * 0.5),
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.lock, color: Colors.orange),
|
||||||
|
title: Text(l10n.backupPassword),
|
||||||
|
subtitle: Text(l10n.backupPasswordTip, style: UIs.textGrey),
|
||||||
|
trailing: const Icon(Icons.keyboard_arrow_right),
|
||||||
|
onTap: () async {
|
||||||
|
final controller = TextEditingController();
|
||||||
|
final result = await ctx.showRoundDialog<bool>(
|
||||||
|
title: l10n.backupPassword,
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Text(l10n.backupPasswordTip, style: UIs.textGrey),
|
||||||
|
UIs.height13,
|
||||||
|
Input(
|
||||||
|
label: l10n.backupPassword,
|
||||||
|
controller: controller,
|
||||||
|
obscureText: true,
|
||||||
|
onSubmitted: (_) => ctx.pop(true),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(onPressed: () => ctx.pop(false), child: Text(libL10n.cancel)),
|
||||||
|
TextButton(onPressed: () => ctx.pop(true), child: Text(libL10n.ok)),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
if (result == true) {
|
||||||
|
final pwd = controller.text.trim();
|
||||||
|
if (pwd.isNotEmpty) {
|
||||||
|
await SecureStoreProps.bakPwd.write(pwd);
|
||||||
|
ctx.showSnackBar(l10n.backupPasswordSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
).cardx,
|
||||||
|
SizedBox(height: padTop),
|
||||||
|
Text(
|
||||||
|
'This step is recommended for secure backup functionality.',
|
||||||
|
style: UIs.textGrey,
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
UIs.height77,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<List<IntroPageBuilder>> get builders async {
|
||||||
final storedVer = _setting.introVer.fetch();
|
final storedVer = _setting.introVer.fetch();
|
||||||
return _builders.entries.where((e) => e.key > storedVer).map((e) => e.value).toList();
|
final lastVer = _setting.lastVer.fetch();
|
||||||
|
|
||||||
|
// If user is upgrading from older version and doesn't have backup password set,
|
||||||
|
// show the backup password migration page
|
||||||
|
final hasBackupPwd = (await SecureStoreProps.bakPwd.read())?.isNotEmpty == true;
|
||||||
|
final isUpgrading = lastVer > 0 && storedVer < 2; // lastVer > 0 means not first install
|
||||||
|
|
||||||
|
final builders = _builders.entries
|
||||||
|
.where((e) {
|
||||||
|
if (e.key == 2 && (!isUpgrading || hasBackupPwd)) {
|
||||||
|
return false; // Skip backup password migration if not upgrading or already has password
|
||||||
|
}
|
||||||
|
return e.key > storedVer;
|
||||||
|
})
|
||||||
|
.map((e) => e.value)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
return builders;
|
||||||
}
|
}
|
||||||
|
|
||||||
static final _setting = Stores.setting;
|
static final _setting = Stores.setting;
|
||||||
|
|||||||
@@ -28,10 +28,12 @@ class BackupPage extends StatefulWidget {
|
|||||||
|
|
||||||
final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveClientMixin {
|
final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveClientMixin {
|
||||||
final webdavLoading = false.vn;
|
final webdavLoading = false.vn;
|
||||||
|
final gistLoading = false.vn;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
webdavLoading.dispose();
|
webdavLoading.dispose();
|
||||||
|
gistLoading.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,8 +50,10 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
|
|||||||
[
|
[
|
||||||
CenterGreyTitle(libL10n.sync),
|
CenterGreyTitle(libL10n.sync),
|
||||||
_buildTip,
|
_buildTip,
|
||||||
|
_buildBakPwd,
|
||||||
if (isMacOS || isIOS) _buildIcloud,
|
if (isMacOS || isIOS) _buildIcloud,
|
||||||
_buildWebdav,
|
_buildWebdav,
|
||||||
|
_buildGist,
|
||||||
_buildFile,
|
_buildFile,
|
||||||
_buildClipboard,
|
_buildClipboard,
|
||||||
],
|
],
|
||||||
@@ -58,6 +62,82 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget get _buildBakPwd {
|
||||||
|
return FutureBuilder<String?>(
|
||||||
|
future: SecureStoreProps.bakPwd.read(),
|
||||||
|
builder: (context, snapshot) {
|
||||||
|
final hasPwd = snapshot.data?.isNotEmpty == true;
|
||||||
|
return CardX(
|
||||||
|
child: ListTile(
|
||||||
|
leading: const Icon(Icons.lock),
|
||||||
|
title: Text(l10n.backupPassword),
|
||||||
|
subtitle: Text(hasPwd ? l10n.backupEncrypted : l10n.backupNotEncrypted, style: UIs.textGrey),
|
||||||
|
trailing: Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
TextButton(onPressed: () async => _onTapSetBakPwd(context), child: Text(libL10n.setting)),
|
||||||
|
if (hasPwd) ...[
|
||||||
|
UIs.width7,
|
||||||
|
TextButton(
|
||||||
|
onPressed: () async {
|
||||||
|
await SecureStoreProps.bakPwd.write(null);
|
||||||
|
context.showSnackBar(l10n.backupPasswordRemoved);
|
||||||
|
setState(() {});
|
||||||
|
},
|
||||||
|
child: Text(libL10n.delete),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
),
|
||||||
|
onTap: () async => _onTapSetBakPwd(context),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onTapSetBakPwd(BuildContext context) async {
|
||||||
|
final currentPwd = await SecureStoreProps.bakPwd.read();
|
||||||
|
final controller = TextEditingController(text: currentPwd ?? '');
|
||||||
|
final node = FocusNode();
|
||||||
|
final result = await context.showRoundDialog<bool>(
|
||||||
|
title: l10n.backupPassword,
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Text(l10n.backupPasswordTip, style: UIs.textGrey),
|
||||||
|
UIs.height13,
|
||||||
|
Input(
|
||||||
|
label: l10n.backupPassword,
|
||||||
|
controller: controller,
|
||||||
|
node: node,
|
||||||
|
obscureText: true,
|
||||||
|
onSubmitted: (_) => context.pop(true),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: Btnx.oks,
|
||||||
|
);
|
||||||
|
if (result == true) {
|
||||||
|
final pwd = controller.text.trim();
|
||||||
|
if (pwd.isEmpty) {
|
||||||
|
context.showSnackBar(libL10n.empty);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await SecureStoreProps.bakPwd.write(pwd);
|
||||||
|
context.showSnackBar(l10n.backupPasswordSet);
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> _ensureBakPwd(BuildContext context) async {
|
||||||
|
final saved = await SecureStoreProps.bakPwd.read();
|
||||||
|
if (saved != null && saved.isNotEmpty) return true;
|
||||||
|
await _onTapSetBakPwd(context);
|
||||||
|
final after = await SecureStoreProps.bakPwd.read();
|
||||||
|
return after != null && after.isNotEmpty;
|
||||||
|
}
|
||||||
|
|
||||||
Widget get _buildTip {
|
Widget get _buildTip {
|
||||||
return CardX(
|
return CardX(
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
@@ -102,6 +182,10 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
|
|||||||
context.showSnackBar(l10n.autoBackupConflict);
|
context.showSnackBar(l10n.autoBackupConflict);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (p0) {
|
||||||
|
final ok = await _ensureBakPwd(context);
|
||||||
|
if (!ok) return false;
|
||||||
|
}
|
||||||
if (p0) {
|
if (p0) {
|
||||||
await bakSync.sync(rs: icloud);
|
await bakSync.sync(rs: icloud);
|
||||||
}
|
}
|
||||||
@@ -133,6 +217,10 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
|
|||||||
context.showSnackBar(l10n.autoBackupConflict);
|
context.showSnackBar(l10n.autoBackupConflict);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (p0) {
|
||||||
|
final ok = await _ensureBakPwd(context);
|
||||||
|
if (!ok) return false;
|
||||||
|
}
|
||||||
if (p0) {
|
if (p0) {
|
||||||
final url = PrefProps.webdavUrl.get();
|
final url = PrefProps.webdavUrl.get();
|
||||||
final user = PrefProps.webdavUser.get();
|
final user = PrefProps.webdavUser.get();
|
||||||
@@ -178,6 +266,67 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget get _buildGist {
|
||||||
|
return CardX(
|
||||||
|
child: ExpandTile(
|
||||||
|
leading: const Icon(Icons.code),
|
||||||
|
title: const Text('GitHub Gist'),
|
||||||
|
initiallyExpanded: false,
|
||||||
|
children: [
|
||||||
|
ListTile(
|
||||||
|
title: Text(libL10n.setting),
|
||||||
|
trailing: const Icon(Icons.settings),
|
||||||
|
onTap: () async => _onTapGistSetting(context),
|
||||||
|
),
|
||||||
|
ListTile(
|
||||||
|
title: Text(libL10n.auto),
|
||||||
|
trailing: StoreSwitch(
|
||||||
|
prop: PrefProps.gistSync,
|
||||||
|
validator: (p0) async {
|
||||||
|
if (p0 && (PrefProps.icloudSync.get() || PrefProps.webdavSync.get())) {
|
||||||
|
context.showSnackBar(l10n.autoBackupConflict);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (p0) {
|
||||||
|
final ok = await _ensureBakPwd(context);
|
||||||
|
if (!ok) return false;
|
||||||
|
}
|
||||||
|
if (p0) {
|
||||||
|
final token = PrefProps.githubToken.get();
|
||||||
|
// Allow empty gistId (will create one on first upload)
|
||||||
|
final hasToken = token != null && token.isNotEmpty;
|
||||||
|
if (!hasToken) {
|
||||||
|
context.showSnackBar('Token or Gist ID is empty');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
gistLoading.value = true;
|
||||||
|
await bakSync.sync(rs: GistRs.shared);
|
||||||
|
gistLoading.value = false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
ListTile(
|
||||||
|
title: Text(l10n.manual),
|
||||||
|
trailing: gistLoading.listenVal((loading) {
|
||||||
|
if (loading) return SizedLoading.small;
|
||||||
|
|
||||||
|
return Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
TextButton(onPressed: () async => _onTapGistDl(context), child: Text(libL10n.restore)),
|
||||||
|
UIs.width7,
|
||||||
|
TextButton(onPressed: () async => _onTapGistUp(context), child: Text(libL10n.backup)),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Widget get _buildClipboard {
|
Widget get _buildClipboard {
|
||||||
return CardX(
|
return CardX(
|
||||||
child: ExpandTile(
|
child: ExpandTile(
|
||||||
@@ -289,7 +438,9 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
|
|||||||
final date = DateTime.now().ymdhms(ymdSep: '-', hmsSep: '-', sep: '-');
|
final date = DateTime.now().ymdhms(ymdSep: '-', hmsSep: '-', sep: '-');
|
||||||
final bakName = '$date-${Miscs.bakFileName}';
|
final bakName = '$date-${Miscs.bakFileName}';
|
||||||
try {
|
try {
|
||||||
final savedPassword = await Stores.setting.backupasswd.read();
|
final ok = await _ensureBakPwd(context);
|
||||||
|
if (!ok) return;
|
||||||
|
final savedPassword = await SecureStoreProps.bakPwd.read();
|
||||||
await BackupV2.backup(bakName, savedPassword);
|
await BackupV2.backup(bakName, savedPassword);
|
||||||
await Webdav.shared.upload(relativePath: bakName);
|
await Webdav.shared.upload(relativePath: bakName);
|
||||||
Loggers.app.info('Upload webdav backup success');
|
Loggers.app.info('Upload webdav backup success');
|
||||||
@@ -301,6 +452,85 @@ final class _BackupPageState extends State<BackupPage> with AutomaticKeepAliveCl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _onTapGistDl(BuildContext context) async {
|
||||||
|
gistLoading.value = true;
|
||||||
|
try {
|
||||||
|
final files = await GistRs.shared.list();
|
||||||
|
if (files.isEmpty) return context.showSnackBar(l10n.dirEmpty);
|
||||||
|
|
||||||
|
final fileName = await context.showPickSingleDialog(title: libL10n.restore, items: files);
|
||||||
|
if (fileName == null) return;
|
||||||
|
|
||||||
|
await GistRs.shared.download(relativePath: fileName);
|
||||||
|
final dlFile = await File('${Paths.doc}/$fileName').readAsString();
|
||||||
|
await BackupService.restoreFromText(context, dlFile);
|
||||||
|
} catch (e, s) {
|
||||||
|
context.showErrDialog(e, s, libL10n.restore);
|
||||||
|
Loggers.app.warning('Download gist backup failed', e, s);
|
||||||
|
} finally {
|
||||||
|
gistLoading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onTapGistUp(BuildContext context) async {
|
||||||
|
gistLoading.value = true;
|
||||||
|
final date = DateTime.now().ymdhms(ymdSep: '-', hmsSep: '-', sep: '-');
|
||||||
|
final bakName = '$date-${Miscs.bakFileName}';
|
||||||
|
try {
|
||||||
|
final ok = await _ensureBakPwd(context);
|
||||||
|
if (!ok) return;
|
||||||
|
final savedPassword = await SecureStoreProps.bakPwd.read();
|
||||||
|
await BackupV2.backup(bakName, savedPassword);
|
||||||
|
await GistRs.shared.upload(relativePath: bakName);
|
||||||
|
Loggers.app.info('Upload gist backup success');
|
||||||
|
} catch (e, s) {
|
||||||
|
context.showErrDialog(e, s, l10n.upload);
|
||||||
|
Loggers.app.warning('Upload gist backup failed', e, s);
|
||||||
|
} finally {
|
||||||
|
gistLoading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onTapGistSetting(BuildContext context) async {
|
||||||
|
final tokenCtrl = TextEditingController(text: PrefProps.githubToken.get());
|
||||||
|
final gistIdCtrl = TextEditingController(text: PrefProps.gistId.get());
|
||||||
|
final nodeToken = FocusNode();
|
||||||
|
final result = await context.showRoundDialog<bool>(
|
||||||
|
title: 'GitHub Gist',
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Input(label: 'Token', controller: tokenCtrl, suggestion: false, node: nodeToken),
|
||||||
|
Input(
|
||||||
|
label: 'Gist ID (optional)',
|
||||||
|
controller: gistIdCtrl,
|
||||||
|
suggestion: false,
|
||||||
|
onSubmitted: (_) => context.pop(true),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: Btnx.oks,
|
||||||
|
);
|
||||||
|
if (result == true) {
|
||||||
|
try {
|
||||||
|
final token_ = tokenCtrl.text.trim();
|
||||||
|
final gistId_ = gistIdCtrl.text.trim();
|
||||||
|
|
||||||
|
await GistRs.test(token: token_, gistId: gistId_.isEmpty ? null : gistId_);
|
||||||
|
context.showSnackBar(libL10n.success);
|
||||||
|
|
||||||
|
await PrefProps.githubToken.set(token_);
|
||||||
|
if (gistId_.isEmpty) {
|
||||||
|
await PrefProps.gistId.remove();
|
||||||
|
} else {
|
||||||
|
await PrefProps.gistId.set(gistId_);
|
||||||
|
}
|
||||||
|
} catch (e, s) {
|
||||||
|
context.showErrDialog(e, s, 'Gist');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _onTapWebdavSetting(BuildContext context) async {
|
Future<void> _onTapWebdavSetting(BuildContext context) async {
|
||||||
final url = TextEditingController(text: PrefProps.webdavUrl.get());
|
final url = TextEditingController(text: PrefProps.webdavUrl.get());
|
||||||
final user = TextEditingController(text: PrefProps.webdavUser.get());
|
final user = TextEditingController(text: PrefProps.webdavUser.get());
|
||||||
|
|||||||
@@ -497,8 +497,8 @@ packages:
|
|||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
path: "."
|
path: "."
|
||||||
ref: "v1.0.329"
|
ref: "v1.0.333"
|
||||||
resolved-ref: "1620fb055986dfcf7587d7a16eb994a39c0d5772"
|
resolved-ref: d73f37c7e7232847d29507ac0df24d38db64a9dc
|
||||||
url: "https://github.com/lppcg/fl_lib"
|
url: "https://github.com/lppcg/fl_lib"
|
||||||
source: git
|
source: git
|
||||||
version: "0.0.1"
|
version: "0.0.1"
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ dependencies:
|
|||||||
fl_lib:
|
fl_lib:
|
||||||
git:
|
git:
|
||||||
url: https://github.com/lppcg/fl_lib
|
url: https://github.com/lppcg/fl_lib
|
||||||
ref: v1.0.329
|
ref: v1.0.333
|
||||||
|
|
||||||
dependency_overrides:
|
dependency_overrides:
|
||||||
# webdav_client_plus:
|
# webdav_client_plus:
|
||||||
|
|||||||
Reference in New Issue
Block a user