Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions lib/core/extension/ssh_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';
import 'package:dartssh2/dartssh2.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/widgets.dart';
import 'package:server_box/data/helper/ssh_decoder.dart';
import 'package:server_box/data/model/server/system.dart';

import 'package:server_box/data/res/misc.dart';
Expand Down Expand Up @@ -170,4 +171,98 @@ extension SSHClientX on SSHClient {
);
return ret.$2;
}

/// Runs a command and decodes output safely with encoding fallback
///
/// [systemType] - The system type (affects encoding choice)
/// Runs a command and safely decodes the result
Future<String> runSafe(
String command, {
SystemType? systemType,
String? context,
}) async {
// Let SSH errors propagate with their original type (e.g., SSHError subclasses)
final result = await run(command);

// Only catch decoding failures and add context
try {
return SSHDecoder.decode(
result,
isWindows: systemType == SystemType.windows,
context: context,
);
} on FormatException catch (e) {
throw Exception(
'Failed to decode command output${context != null ? " [$context]" : ""}: $e',
);
}
}

/// Executes a command with stdin and safely decodes stdout/stderr
Future<(String stdout, String stderr)> execSafe(
void Function(SSHSession session) callback, {
required String entry,
SystemType? systemType,
String? context,
}) async {
final stdoutBuilder = BytesBuilder(copy: false);
final stderrBuilder = BytesBuilder(copy: false);
final stdoutDone = Completer<void>();
final stderrDone = Completer<void>();

final session = await execute(entry);

session.stdout.listen(
(e) {
stdoutBuilder.add(e);
},
onDone: stdoutDone.complete,
onError: stdoutDone.completeError,
);

session.stderr.listen(
(e) {
stderrBuilder.add(e);
},
onDone: stderrDone.complete,
onError: stderrDone.completeError,
);

callback(session);

await stdoutDone.future;
await stderrDone.future;

final stdoutBytes = stdoutBuilder.takeBytes();
final stderrBytes = stderrBuilder.takeBytes();

// Only catch decoding failures, let other errors propagate
String stdout;
try {
stdout = SSHDecoder.decode(
stdoutBytes,
isWindows: systemType == SystemType.windows,
context: context != null ? '$context (stdout)' : 'stdout',
);
} on FormatException catch (e) {
throw Exception(
'Failed to decode stdout${context != null ? " [$context]" : ""}: $e',
);
}

String stderr;
try {
stderr = SSHDecoder.decode(
stderrBytes,
isWindows: systemType == SystemType.windows,
context: context != null ? '$context (stderr)' : 'stderr',
);
} on FormatException catch (e) {
throw Exception(
'Failed to decode stderr${context != null ? " [$context]" : ""}: $e',
);
}

return (stdout, stderr);
}
}
66 changes: 66 additions & 0 deletions lib/data/helper/ssh_decoder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'dart:convert';

import 'package:fl_lib/fl_lib.dart';
import 'package:flutter_gbk2utf8/flutter_gbk2utf8.dart';

/// Utility class for decoding SSH command output with encoding fallback
class SSHDecoder {
/// Decodes bytes to string with multiple encoding fallback strategies
///
/// Tries in order:
/// 1. UTF-8 (with allowMalformed for lenient parsing)
/// - Windows PowerShell scripts now set UTF-8 output encoding by default
/// 2. GBK (for Windows Chinese systems)
/// - In some cases, Windows will still revert to GBK.
/// - Only attempted if UTF-8 produces replacement characters (�)
static String decode(
List<int> bytes, {
bool isWindows = false,
String? context,
}) {
if (bytes.isEmpty) return '';

// Try UTF-8 first with allowMalformed
try {
final result = utf8.decode(bytes, allowMalformed: true);
// Check if there are replacement characters indicating decode failure
// For non-Windows systems, always use UTF-8 result
if (!result.contains('�') || !isWindows) {
return result;
}
// For Windows with replacement chars, log and try GBK fallback
if (isWindows && result.contains('�')) {
final contextInfo = context != null ? ' [$context]' : '';
Loggers.app.info('UTF-8 decode has replacement chars$contextInfo, trying GBK fallback');
}
} catch (e) {
final contextInfo = context != null ? ' [$context]' : '';
Loggers.app.warning('UTF-8 decode failed$contextInfo: $e');
}

// For Windows or when UTF-8 has replacement chars, try GBK
try {
return gbk.decode(bytes);
} catch (e) {
final contextInfo = context != null ? ' [$context]' : '';
Loggers.app.warning('GBK decode failed$contextInfo: $e');
// Return empty string if all decoding attempts fail
return '';
}
}

/// Encodes string to bytes for SSH command input
///
/// Uses GBK for Windows, UTF-8 for others
static List<int> encode(String text, {bool isWindows = false}) {
if (isWindows) {
try {
return gbk.encode(text);
} catch (e) {
Loggers.app.warning('GBK encode failed: $e, falling back to UTF-8');
return utf8.encode(text);
}
}
return utf8.encode(text);
}
}
16 changes: 12 additions & 4 deletions lib/data/helper/system_detector.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:dartssh2/dartssh2.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:server_box/core/extension/ssh_client.dart';
import 'package:server_box/data/model/server/server_private_info.dart';
import 'package:server_box/data/model/server/system.dart';

Expand All @@ -23,7 +24,10 @@ class SystemDetector {

try {
// Try to detect Unix/Linux/BSD systems first (more reliable and doesn't create files)
final unixResult = await client.run('uname -a 2>/dev/null').string;
final unixResult = await client.runSafe(
'uname -a 2>/dev/null',
context: 'uname detection for ${spi.oldId}',
);
if (unixResult.contains('Linux')) {
detectedSystemType = SystemType.linux;
dprint('Detected Linux system type for ${spi.oldId}');
Expand All @@ -35,15 +39,19 @@ class SystemDetector {
}

// If uname fails, try to detect Windows systems
final powershellResult = await client.run('ver 2>nul').string;
final powershellResult = await client.runSafe(
'ver 2>nul',
systemType: SystemType.windows,
context: 'ver detection for ${spi.oldId}',
);
if (powershellResult.isNotEmpty &&
(powershellResult.contains('Windows') || powershellResult.contains('NT'))) {
detectedSystemType = SystemType.windows;
dprint('Detected Windows system type for ${spi.oldId}');
return detectedSystemType;
}
} catch (e) {
Loggers.app.warning('System detection failed for ${spi.oldId}: $e');
} catch (e, stackTrace) {
Loggers.app.warning('System detection failed for ${spi.oldId}: $e\n$stackTrace');
}

// Default fallback
Expand Down
53 changes: 31 additions & 22 deletions lib/data/model/app/scripts/cmd_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,34 @@ enum WindowsStatusCmdType implements ShellCmdType {
echo('echo ${SystemType.windowsSign}'),
time('[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()'),

/// Get network interface statistics using Windows Performance Counters
/// Get network interface statistics using WMI
///
/// Uses Get-Counter to collect network I/O metrics from all network interfaces:
/// - Collects bytes received and sent per second for all network interfaces
/// Uses WMI Win32_PerfRawData_Tcpip_NetworkInterface for cross-language compatibility:
/// - Takes 2 samples with 1 second interval to calculate rates
/// - Outputs results in JSON format for easy parsing
/// - Counter paths use double backslashes to escape PowerShell string literals
net(
r'Get-Counter -Counter '
r'"\\NetworkInterface(*)\\Bytes Received/sec", '
r'"\\NetworkInterface(*)\\Bytes Sent/sec" '
r'-SampleInterval 1 -MaxSamples 2 | ConvertTo-Json',
r'$s1 = @(Get-WmiObject Win32_PerfRawData_Tcpip_NetworkInterface | '
r'Select-Object Name, BytesReceivedPersec, BytesSentPersec, Timestamp_Sys100NS); '
r'Start-Sleep -Seconds 1; '
r'$s2 = @(Get-WmiObject Win32_PerfRawData_Tcpip_NetworkInterface | '
r'Select-Object Name, BytesReceivedPersec, BytesSentPersec, Timestamp_Sys100NS); '
r'@($s1, $s2) | ConvertTo-Json -Depth 5',
),
sys('(Get-ComputerInfo).OsName'),
cpu(
'Get-WmiObject -Class Win32_Processor | '
'Select-Object Name, LoadPercentage | ConvertTo-Json',
'Select-Object Name, LoadPercentage, NumberOfCores, NumberOfLogicalProcessors | ConvertTo-Json',
),

/// Get system uptime by calculating time since last boot
///
/// Calculates uptime directly in PowerShell to avoid date format parsing issues:
/// - Gets LastBootUpTime from Win32_OperatingSystem
/// - Calculates difference from current time
/// - Returns pre-formatted string: "X days, H:MM" or "H:MM" (if less than 1 day)
/// - Uses ToString('00') for zero-padding to avoid quote escaping issues
uptime(
r"""$up = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime; if ($up.Days -gt 0) { "$($up.Days) days, $($up.Hours):$($up.Minutes.ToString('00'))" } else { "$($up.Hours):$($up.Minutes.ToString('00'))" }""",
),
uptime('(Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime'),
conn('(netstat -an | findstr ESTABLISHED | Measure-Object -Line).Count'),
disk(
'Get-WmiObject -Class Win32_LogicalDisk | '
Expand Down Expand Up @@ -213,19 +222,19 @@ enum WindowsStatusCmdType implements ShellCmdType {
),
host(r'Write-Output $env:COMPUTERNAME'),

/// Get disk I/O statistics using Windows Performance Counters
/// Get disk I/O statistics using WMI
///
/// Uses Get-Counter to collect disk I/O metrics from all physical disks:
/// Uses WMI Win32_PerfRawData_PerfDisk_PhysicalDisk:
/// - Monitors read and write bytes per second for all physical disks
/// - Takes 2 samples with 1 second interval to calculate I/O rates
/// - Physical disk counters provide hardware-level I/O statistics
/// - Outputs results in JSON format for parsing
/// - Counter names use wildcard (*) to capture all disk instances
/// - Takes 2 samples with 1 second interval to calculate rates
/// - DiskReadBytesPersec and DiskWriteBytesPersec are cumulative counters
diskio(
r'Get-Counter -Counter '
r'"\\PhysicalDisk(*)\\Disk Read Bytes/sec", '
r'"\\PhysicalDisk(*)\\Disk Write Bytes/sec" '
r'-SampleInterval 1 -MaxSamples 2 | ConvertTo-Json',
r'$s1 = @(Get-WmiObject Win32_PerfRawData_PerfDisk_PhysicalDisk | '
r'Select-Object Name, DiskReadBytesPersec, DiskWriteBytesPersec, Timestamp_Sys100NS); '
r'Start-Sleep -Seconds 1; '
r'$s2 = @(Get-WmiObject Win32_PerfRawData_PerfDisk_PhysicalDisk | '
r'Select-Object Name, DiskReadBytesPersec, DiskWriteBytesPersec, Timestamp_Sys100NS); '
r'@($s1, $s2) | ConvertTo-Json -Depth 5',
),
battery(
'Get-WmiObject -Class Win32_Battery | '
Expand Down Expand Up @@ -287,7 +296,7 @@ enum WindowsStatusCmdType implements ShellCmdType {
String get separator => ScriptConstants.getCmdSeparator(name);

@override
String get divider => ScriptConstants.getCmdDivider(name);
String get divider => ScriptConstants.getWindowsCmdDivider(name);

@override
CmdTypeSys get sysType => CmdTypeSys.windows;
Expand Down
4 changes: 4 additions & 0 deletions lib/data/model/app/scripts/script_consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class ScriptConstants {
/// Generate command-specific divider
static String getCmdDivider(String cmdName) => '\necho ${getCmdSeparator(cmdName)}\n\t';

/// Generate command-specific divider for Windows PowerShell
static String getWindowsCmdDivider(String cmdName) => '\n Write-Host "${getCmdSeparator(cmdName)}"\n ';

/// Parse script output into command-specific map
static Map<String, String> parseScriptOutput(String raw) {
final result = <String, String>{};
Expand Down Expand Up @@ -102,6 +105,7 @@ exec 2>/dev/null
# DO NOT delete this file while app is running

\$ErrorActionPreference = "SilentlyContinue"
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

''';
}
Expand Down
11 changes: 9 additions & 2 deletions lib/data/model/server/cpu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ import 'package:server_box/data/res/status.dart';
/// Capacity of the FIFO queue
const _kCap = 30;

class Cpus extends TimeSeq<List<SingleCpuCore>> {
class Cpus extends TimeSeq<SingleCpuCore> {
Cpus(super.init1, super.init2);

final Map<String, int> brand = {};

@override
void onUpdate() {
_coresCount = now.length;
if (pre.isEmpty || now.isEmpty || pre.length != now.length) {
_totalDelta = 0;
_user = 0;
_sys = 0;
_iowait = 0;
_idle = 0;
return;
}
_totalDelta = now[0].total - pre[0].total;
_user = _getUser();
_sys = _getSys();
_iowait = _getIowait();
_idle = _getIdle();
_updateSpots();
//_updateRange();
}

double usedPercent({int coreIdx = 0}) {
Expand Down
2 changes: 1 addition & 1 deletion lib/data/model/server/disk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class Disk with EquatableMixin {
];
}

class DiskIO extends TimeSeq<List<DiskIOPiece>> {
class DiskIO extends TimeSeq<DiskIOPiece> {
DiskIO(super.init1, super.init2);

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/data/model/server/net_speed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NetSpeedPart extends TimeSeqIface<NetSpeedPart> {

typedef CachedNetVals = ({String sizeIn, String sizeOut, String speedIn, String speedOut});

class NetSpeed extends TimeSeq<List<NetSpeedPart>> {
class NetSpeed extends TimeSeq<NetSpeedPart> {
NetSpeed(super.init1, super.init2);

@override
Expand Down
Loading