feat: GitHub Gist sync (#854)

This commit is contained in:
lollipopkit🏳️‍⚧️
2025-08-14 23:21:33 +08:00
committed by GitHub
parent 1d553eccd5
commit bc1b6e5a4a
21 changed files with 538 additions and 197 deletions

View File

@@ -1 +0,0 @@
extensions:

View File

@@ -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

View File

@@ -96,20 +96,25 @@ 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) {
if (intros.isNotEmpty) { child = const Scaffold(body: Center(child: CircularProgressIndicator()));
child = _IntroPage(intros); } else {
final intros = snapshot.data ?? [];
if (intros.isNotEmpty) {
child = _IntroPage(intros);
} else {
child = const HomePage();
}
} }
child = const HomePage();
return VirtualWindowFrame(title: BuildData.name, child: child); return VirtualWindowFrame(title: BuildData.name, child: child);
}, },
), ),

View File

@@ -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();
return MergeableUtils.fromJsonString(content).$1; final pwd = await SecureStoreProps.bakPwd.read();
try {
if (Cryptor.isEncrypted(content)) {
return MergeableUtils.fromJsonString(content, pwd).$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;
} }
} }

View File

@@ -6,17 +6,14 @@ import 'package:server_box/data/model/server/system.dart';
/// Helper class for detecting remote system types /// Helper class for detecting remote system types
class SystemDetector { class SystemDetector {
/// Detects the system type of a remote server /// Detects the system type of a remote server
/// ///
/// First checks if a custom system type is configured in [spi]. /// First checks if a custom system type is configured in [spi].
/// If not, attempts to detect the system by running commands: /// If not, attempts to detect the system by running commands:
/// 1. 'ver' command to detect Windows /// 1. 'ver' command to detect Windows
/// 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) {
@@ -54,4 +51,4 @@ class SystemDetector {
dprint('Defaulting to Linux system type for ${spi.oldId}'); dprint('Defaulting to Linux system type for ${spi.oldId}');
return detectedSystemType; return detectedSystemType;
} }
} }

View File

@@ -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(

View File

@@ -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 '''

View File

@@ -32,14 +32,14 @@ class ScriptConstants {
/// Parse script output into command-specific map /// Parse script output into command-specific map
static Map<String, String> parseScriptOutput(String raw) { static Map<String, String> parseScriptOutput(String raw) {
final result = <String, String>{}; final result = <String, String>{};
if (raw.isEmpty) return result; if (raw.isEmpty) return result;
// Parse line by line to properly handle command-specific separators // Parse line by line to properly handle command-specific separators
final lines = raw.split('\n'); final lines = raw.split('\n');
String? currentCmd; String? currentCmd;
final buffer = StringBuffer(); final buffer = StringBuffer();
for (final line in lines) { for (final line in lines) {
if (line.startsWith('$separator.')) { if (line.startsWith('$separator.')) {
// Save previous command content // Save previous command content
@@ -61,12 +61,12 @@ class ScriptConstants {
buffer.writeln(line); buffer.writeln(line);
} }
} }
// Don't forget the last command // Don't forget the last command
if (currentCmd != null) { if (currentCmd != null) {
result[currentCmd] = buffer.toString().trim(); result[currentCmd] = buffer.toString().trim();
} }
return result; return result;
} }

View File

@@ -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);

View File

@@ -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
/// ///
@@ -214,14 +205,14 @@ final _freebsdCpuPercentReg = RegExp(
/// - Generic BSD: fallback to percentage extraction /// - Generic BSD: fallback to percentage extraction
Cpus parseBsdCpu(String raw) { Cpus parseBsdCpu(String raw) {
final init = InitStatus.cpus; final init = InitStatus.cpus;
// Try macOS format first // Try macOS format first
final macMatch = _macCpuPercentReg.firstMatch(raw); final macMatch = _macCpuPercentReg.firstMatch(raw);
if (macMatch != null) { if (macMatch != null) {
final userPercent = double.parse(macMatch.group(1)!).toInt(); final userPercent = double.parse(macMatch.group(1)!).toInt();
final sysPercent = double.parse(macMatch.group(2)!).toInt(); final sysPercent = double.parse(macMatch.group(2)!).toInt();
final idlePercent = double.parse(macMatch.group(3)!).toInt(); final idlePercent = double.parse(macMatch.group(3)!).toInt();
init.add([ init.add([
SingleCpuCore( SingleCpuCore(
'cpu0', 'cpu0',
@@ -236,7 +227,7 @@ Cpus parseBsdCpu(String raw) {
]); ]);
return init; return init;
} }
// Try FreeBSD format // Try FreeBSD format
final freebsdMatch = _freebsdCpuPercentReg.firstMatch(raw); final freebsdMatch = _freebsdCpuPercentReg.firstMatch(raw);
if (freebsdMatch != null) { if (freebsdMatch != null) {
@@ -245,7 +236,7 @@ Cpus parseBsdCpu(String raw) {
final sysPercent = double.parse(freebsdMatch.group(3)!).toInt(); final sysPercent = double.parse(freebsdMatch.group(3)!).toInt();
final irqPercent = double.parse(freebsdMatch.group(4)!).toInt(); final irqPercent = double.parse(freebsdMatch.group(4)!).toInt();
final idlePercent = double.parse(freebsdMatch.group(5)!).toInt(); final idlePercent = double.parse(freebsdMatch.group(5)!).toInt();
init.add([ init.add([
SingleCpuCore( SingleCpuCore(
'cpu0', 'cpu0',
@@ -260,20 +251,28 @@ Cpus parseBsdCpu(String raw) {
]); ]);
return init; return init;
} }
// 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) {
// Validate that percentages are reasonable (0-100 range) // Validate that percentages are reasonable (0-100 range)
final validPercents = percents.where((p) => p >= 0 && p <= 100).toList(); final validPercents = percents.where((p) => p >= 0 && p <= 100).toList();
if (validPercents.length != percents.length) { if (validPercents.length != percents.length) {
Loggers.app.warning('BSD CPU fallback parsing found invalid percentages in: $raw'); Loggers.app.warning('BSD CPU fallback parsing found invalid percentages in: $raw');
} }
init.add([ init.add([
SingleCpuCore( SingleCpuCore(
'cpu0', 'cpu0',
@@ -288,10 +287,12 @@ 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');
} }
return init; return init;
} }

View File

@@ -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);
} }
@@ -36,14 +32,13 @@ class Memory {
final memItemReg = RegExp(r'([A-Z].+:)\s+([0-9]+) kB'); final memItemReg = RegExp(r'([A-Z].+:)\s+([0-9]+) kB');
/// Parse BSD/macOS memory from top output /// Parse BSD/macOS memory from top output
/// ///
/// Supports formats like: /// Supports formats like:
/// - macOS: "PhysMem: 32G used (1536M wired), 64G unused." /// - macOS: "PhysMem: 32G used (1536M wired), 64G unused."
/// - 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);
} }

View File

@@ -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,
),
); );
} }
} }
@@ -597,7 +578,7 @@ List<NetSpeedPart> _parseWindowsNetwork(String raw, int currentTime) {
} }
String _extractInterfaceName(String path) { String _extractInterfaceName(String path) {
// Extract interface name from path like // Extract interface name from path like
// "\\Computer\\NetworkInterface(Interface Name)\\..." // "\\Computer\\NetworkInterface(Interface Name)\\..."
final match = RegExp(r'\\NetworkInterface\(([^)]+)\)\\').firstMatch(path); final match = RegExp(r'\\NetworkInterface\(([^)]+)\)\\').firstMatch(path);
return match?.group(1) ?? ''; return match?.group(1) ?? '';
@@ -632,7 +613,7 @@ List<DiskIOPiece> _parseWindowsDiskIO(String raw, int currentTime) {
} }
} }
// Create DiskIOPiece for each disk - convert bytes to sectors // Create DiskIOPiece for each disk - convert bytes to sectors
// (assuming 512 bytes per sector) // (assuming 512 bytes per sector)
for (final diskName in diskReads.keys) { for (final diskName in diskReads.keys) {
final readBytes = diskReads[diskName] ?? 0; final readBytes = diskReads[diskName] ?? 0;
@@ -659,7 +640,7 @@ List<DiskIOPiece> _parseWindowsDiskIO(String raw, int currentTime) {
} }
String _extractDiskName(String path) { String _extractDiskName(String path) {
// Extract disk name from path like // Extract disk name from path like
// "\\Computer\\PhysicalDisk(Disk Name)\\..." // "\\Computer\\PhysicalDisk(Disk Name)\\..."
final match = RegExp(r'\\PhysicalDisk\(([^)]+)\)\\').firstMatch(path); final match = RegExp(r'\\PhysicalDisk\(([^)]+)\)\\').firstMatch(path);
return match?.group(1) ?? ''; return match?.group(1) ?? '';
@@ -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;
} }
@@ -689,7 +668,7 @@ void _parseWindowsTemperatures(Temperatures temps, String raw) {
if (temperature != null) { if (temperature != null) {
// Convert to the format expected by the existing parse method // Convert to the format expected by the existing parse method
typeLines.add('/sys/class/thermal/thermal_zone$i/$typeName'); typeLines.add('/sys/class/thermal/thermal_zone$i/$typeName');
// Convert to millicelsius (multiply by 1000) // Convert to millicelsius (multiply by 1000)
// as expected by Linux parsing // as expected by Linux parsing
valueLines.add((temperature * 1000).round().toString()); valueLines.add((temperature * 1000).round().toString());
} }

View File

@@ -14,16 +14,14 @@ enum SystemType {
static const windowsSign = '__windows'; static const windowsSign = '__windows';
/// Used for parsing system types from shell output. /// Used for parsing system types from shell output.
/// ///
/// This method looks for specific system signatures in the shell output /// This method looks for specific system signatures in the shell output
/// and returns the corresponding SystemType. If no signature is found, /// and returns the corresponding SystemType. If no signature is found,
/// 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');
return SystemType.windows; return SystemType.windows;
@@ -32,24 +30,23 @@ enum SystemType {
Loggers.app.info('System detected as BSD from signature in: $truncatedValue'); Loggers.app.info('System detected as BSD from signature in: $truncatedValue');
return SystemType.bsd; return SystemType.bsd;
} }
// Log when falling back to Linux detection // Log when falling back to Linux detection
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');
} }
return SystemType.linux; return SystemType.linux;
} }
} }

View File

@@ -8,7 +8,7 @@ import 'package:server_box/data/model/server/memory.dart';
import 'package:server_box/data/model/server/server.dart'; import 'package:server_box/data/model/server/server.dart';
/// Windows-specific status parsing utilities /// Windows-specific status parsing utilities
/// ///
/// This module handles parsing of Windows PowerShell command outputs /// This module handles parsing of Windows PowerShell command outputs
/// for server monitoring. It extracts the Windows parsing logic /// for server monitoring. It extracts the Windows parsing logic
/// to improve maintainability and readability. /// to improve maintainability and readability.
@@ -36,15 +36,13 @@ 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');
return null; return null;
} }
// Try multiple date formats to handle different Windows locale/version outputs // Try multiple date formats to handle different Windows locale/version outputs
final formatters = [ final formatters = [
DateFormat('EEEE, MMMM d, yyyy h:mm:ss a', 'en_US'), // Original format DateFormat('EEEE, MMMM d, yyyy h:mm:ss a', 'en_US'), // Original format
@@ -56,24 +54,27 @@ class WindowsParser {
DateFormat('d/M/yyyy h:mm:ss a', 'en_US'), // Short European format DateFormat('d/M/yyyy h:mm:ss a', 'en_US'), // Short European format
DateFormat('dd/MM/yyyy h:mm:ss a', 'en_US'), // Short European format with zero padding DateFormat('dd/MM/yyyy h:mm:ss a', 'en_US'), // Short European format with zero padding
]; ];
DateTime? dateTime; DateTime? dateTime;
for (final formatter in formatters) { for (final formatter in formatters) {
dateTime = formatter.tryParseLoose(cleanedInput); dateTime = formatter.tryParseLoose(cleanedInput);
if (dateTime != null) break; if (dateTime != null) break;
} }
if (dateTime == null) { if (dateTime == null) {
Loggers.app.warning('Windows uptime parsing: could not parse date format for: $cleanedInput'); Loggers.app.warning('Windows uptime parsing: could not parse date format for: $cleanedInput');
return null; return null;
} }
final now = DateTime.now(); final now = DateTime.now();
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;
} }
@@ -168,8 +169,8 @@ class WindowsParser {
} }
/// Parse Windows memory information from PowerShell output /// Parse Windows memory information from PowerShell output
/// ///
/// NOTE: Windows Win32_OperatingSystem properties TotalVisibleMemorySize /// NOTE: Windows Win32_OperatingSystem properties TotalVisibleMemorySize
/// and FreePhysicalMemory are returned in KB units. /// and FreePhysicalMemory are returned in KB units.
static Memory? parseMemory(String raw) { static Memory? parseMemory(String raw) {
try { try {
@@ -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(
@@ -245,4 +243,4 @@ class WindowsParser {
return []; return [];
} }
} }
} }

View File

@@ -298,7 +298,7 @@ enum ContainerCmdType {
.map((e) => e.exec(type, sudo: sudo, includeStats: includeStats)) .map((e) => e.exec(type, sudo: sudo, includeStats: includeStats))
.join('\necho ${ScriptConstants.separator}\n'); .join('\necho ${ScriptConstants.separator}\n');
} }
/// Find out the required segment from [segments] /// Find out the required segment from [segments]
String find(List<String> segments) { String find(List<String> segments) {
return segments[index]; return segments[index];

View File

@@ -144,7 +144,7 @@ class ServerProvider extends Provider {
// Start a new update operation // Start a new update operation
final updateFuture = _updateServer(s.spi); final updateFuture = _updateServer(s.spi);
_serverIdsUpdating[s.spi.id] = updateFuture; _serverIdsUpdating[s.spi.id] = updateFuture;
try { try {
await updateFuture; await updateFuture;
} finally { } finally {
@@ -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));
@@ -396,7 +400,7 @@ class ServerProvider extends Provider {
sv.status.err = err; sv.status.err = err;
Loggers.app.warning(err); Loggers.app.warning(err);
_setServerState(s, ServerConn.failed); _setServerState(s, ServerConn.failed);
// Update SSH session status to disconnected // Update SSH session status to disconnected
final sessionId = 'ssh_${spi.id}'; final sessionId = 'ssh_${spi.id}';
TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected); TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected);
@@ -407,7 +411,7 @@ class ServerProvider extends Provider {
sv.status.err = err; sv.status.err = err;
Loggers.app.warning(err); Loggers.app.warning(err);
_setServerState(s, ServerConn.failed); _setServerState(s, ServerConn.failed);
// Update SSH session status to disconnected // Update SSH session status to disconnected
final sessionId = 'ssh_${spi.id}'; final sessionId = 'ssh_${spi.id}';
TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected); TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected);
@@ -419,7 +423,7 @@ class ServerProvider extends Provider {
sv.status.err = err; sv.status.err = err;
Loggers.app.warning(err); Loggers.app.warning(err);
_setServerState(s, ServerConn.failed); _setServerState(s, ServerConn.failed);
// Update SSH session status to disconnected // Update SSH session status to disconnected
final sessionId = 'ssh_${spi.id}'; final sessionId = 'ssh_${spi.id}';
TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected); TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected);
@@ -452,7 +456,7 @@ class ServerProvider extends Provider {
TryLimiter.inc(sid); TryLimiter.inc(sid);
sv.status.err = SSHErr(type: SSHErrType.segements, message: 'Seperate segments failed, raw:\n$raw'); sv.status.err = SSHErr(type: SSHErrType.segements, message: 'Seperate segments failed, raw:\n$raw');
_setServerState(s, ServerConn.failed); _setServerState(s, ServerConn.failed);
// Update SSH session status to disconnected on segments error // Update SSH session status to disconnected on segments error
final sessionId = 'ssh_${spi.id}'; final sessionId = 'ssh_${spi.id}';
TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected); TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected);
@@ -463,7 +467,7 @@ class ServerProvider extends Provider {
sv.status.err = SSHErr(type: SSHErrType.getStatus, message: e.toString()); sv.status.err = SSHErr(type: SSHErrType.getStatus, message: e.toString());
_setServerState(s, ServerConn.failed); _setServerState(s, ServerConn.failed);
Loggers.app.warning('Get status from ${spi.name} failed', e); Loggers.app.warning('Get status from ${spi.name} failed', e);
// Update SSH session status to disconnected on status error // Update SSH session status to disconnected on status error
final sessionId = 'ssh_${spi.id}'; final sessionId = 'ssh_${spi.id}';
TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected); TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected);
@@ -473,7 +477,7 @@ class ServerProvider extends Provider {
try { try {
// Parse script output into command-specific map // Parse script output into command-specific map
final parsedOutput = ScriptConstants.parseScriptOutput(raw); final parsedOutput = ScriptConstants.parseScriptOutput(raw);
final req = ServerStatusUpdateReq( final req = ServerStatusUpdateReq(
ss: sv.status, ss: sv.status,
parsedOutput: parsedOutput, parsedOutput: parsedOutput,
@@ -486,7 +490,7 @@ class ServerProvider extends Provider {
sv.status.err = SSHErr(type: SSHErrType.getStatus, message: 'Parse failed: $e\n\n$raw'); sv.status.err = SSHErr(type: SSHErrType.getStatus, message: 'Parse failed: $e\n\n$raw');
_setServerState(s, ServerConn.failed); _setServerState(s, ServerConn.failed);
Loggers.app.warning('Server status', e, trace); Loggers.app.warning('Server status', e, trace);
// Update SSH session status to disconnected on parse error // Update SSH session status to disconnected on parse error
final sessionId = 'ssh_${spi.id}'; final sessionId = 'ssh_${spi.id}';
TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected); TermSessionManager.updateStatus(sessionId, TermSessionStatus.disconnected);

View File

@@ -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 = '';

View File

@@ -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;

View File

@@ -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());

View File

@@ -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"

View File

@@ -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: