mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 23:34:24 +01:00
opt. proj struct
This commit is contained in:
@@ -1,19 +1,3 @@
|
||||
get initOneTimeCpuStatus => OneTimeCpuStatus(
|
||||
'cpu',
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
get initCpuStatus => CpuStatus(
|
||||
[initOneTimeCpuStatus],
|
||||
[initOneTimeCpuStatus],
|
||||
'',
|
||||
);
|
||||
|
||||
class CpuStatus {
|
||||
List<OneTimeCpuStatus> _pre;
|
||||
List<OneTimeCpuStatus> _now;
|
||||
@@ -107,3 +91,51 @@ class OneTimeCpuStatus {
|
||||
|
||||
int get total => user + sys + nice + idle + iowait + irq + softirq;
|
||||
}
|
||||
|
||||
List<OneTimeCpuStatus> parseCPU(String raw) {
|
||||
final List<OneTimeCpuStatus> cpus = [];
|
||||
|
||||
for (var item in raw.split('\n')) {
|
||||
if (item == '') break;
|
||||
final id = item.split(' ').first;
|
||||
final matches = item.replaceFirst(id, '').trim().split(' ');
|
||||
cpus.add(OneTimeCpuStatus(
|
||||
id,
|
||||
int.parse(matches[0]),
|
||||
int.parse(matches[1]),
|
||||
int.parse(matches[2]),
|
||||
int.parse(matches[3]),
|
||||
int.parse(matches[4]),
|
||||
int.parse(matches[5]),
|
||||
int.parse(matches[6])));
|
||||
}
|
||||
return cpus;
|
||||
}
|
||||
|
||||
final cpuTempReg = RegExp(r'(x86_pkg_temp|cpu_thermal)');
|
||||
|
||||
String parseCPUTemp(List<String> segments) {
|
||||
const noMatch = "/sys/class/thermal/thermal_zone*/type";
|
||||
final type = segments[0];
|
||||
final value = segments[1];
|
||||
// Not support to get CPU temperature
|
||||
if (value.contains(noMatch) ||
|
||||
type.contains(noMatch) ||
|
||||
value.isEmpty ||
|
||||
type.isEmpty) {
|
||||
return '';
|
||||
}
|
||||
final split = type.split('\n');
|
||||
int idx = 0;
|
||||
for (var item in split) {
|
||||
if (item.contains(cpuTempReg)) {
|
||||
break;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
final valueSplited = value.split('\n');
|
||||
if (idx >= valueSplited.length) return '';
|
||||
final temp = int.tryParse(valueSplited[idx].trim());
|
||||
if (temp == null) return '';
|
||||
return '${(temp / 1000).toStringAsFixed(1)}°C';
|
||||
}
|
||||
|
||||
@@ -1,11 +1,3 @@
|
||||
get initMemory => Memory(
|
||||
total: 1,
|
||||
used: 0,
|
||||
free: 1,
|
||||
cache: 0,
|
||||
avail: 1,
|
||||
);
|
||||
|
||||
class Memory {
|
||||
int total;
|
||||
int used;
|
||||
@@ -19,3 +11,23 @@ class Memory {
|
||||
required this.cache,
|
||||
required this.avail});
|
||||
}
|
||||
|
||||
final memItemReg = RegExp(r'([A-Z].+:)\s+([0-9]+) kB');
|
||||
|
||||
Memory parseMem(String raw) {
|
||||
final items = raw.split('\n').map((e) => memItemReg.firstMatch(e)).toList();
|
||||
final total = int.parse(
|
||||
items.firstWhere((e) => e?.group(1) == 'MemTotal:')?.group(2) ?? '1');
|
||||
final free = int.parse(
|
||||
items.firstWhere((e) => e?.group(1) == 'MemFree:')?.group(2) ?? '0');
|
||||
final cached = int.parse(
|
||||
items.firstWhere((e) => e?.group(1) == 'Cached:')?.group(2) ?? '0');
|
||||
final available = int.parse(
|
||||
items.firstWhere((e) => e?.group(1) == 'MemAvailable:')?.group(2) ?? '0');
|
||||
return Memory(
|
||||
total: total,
|
||||
used: total - available,
|
||||
free: free,
|
||||
cache: cached,
|
||||
avail: available);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,5 @@
|
||||
import 'package:toolbox/core/extension/numx.dart';
|
||||
|
||||
get initNetSpeedPart => NetSpeedPart(
|
||||
'',
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
get initNetSpeed => NetSpeed(
|
||||
[initNetSpeedPart],
|
||||
[initNetSpeedPart],
|
||||
);
|
||||
|
||||
class NetSpeedPart {
|
||||
String device;
|
||||
int bytesIn;
|
||||
@@ -69,3 +58,23 @@ class NetSpeed {
|
||||
String buildStandardOutput(double speed) =>
|
||||
'${speed.convertBytes.toLowerCase()}/s';
|
||||
}
|
||||
|
||||
List<NetSpeedPart> parseNetSpeed(String raw) {
|
||||
final split = raw.split('\n');
|
||||
if (split.length < 4) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final time = int.parse(split[split.length - 1]);
|
||||
final results = <NetSpeedPart>[];
|
||||
for (final item in split.sublist(2, split.length - 1)) {
|
||||
final data = item.trim().split(':');
|
||||
final device = data.first;
|
||||
final bytes = data.last.trim().split(' ');
|
||||
bytes.removeWhere((element) => element == '');
|
||||
final bytesIn = int.parse(bytes.first);
|
||||
final bytesOut = int.parse(bytes[8]);
|
||||
results.add(NetSpeedPart(device, bytesIn, bytesOut, time));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
@@ -8,16 +8,6 @@ import 'package:toolbox/data/model/server/tcp_status.dart';
|
||||
/// Code generated by jsonToDartModel https://ashamp.github.io/jsonToDartModel/
|
||||
///
|
||||
|
||||
get initStatus => ServerStatus(
|
||||
initCpuStatus,
|
||||
initMemory,
|
||||
'Loading...',
|
||||
'',
|
||||
[DiskInfo('/', '/', 0, '0', '0', '0')],
|
||||
TcpStatus(0, 0, 0, 0),
|
||||
initNetSpeed,
|
||||
);
|
||||
|
||||
class ServerStatus {
|
||||
/*
|
||||
{
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import '../../../core/extension/stringx.dart';
|
||||
|
||||
///
|
||||
/// Code generated by jsonToDartModel https://ashamp.github.io/jsonToDartModel/
|
||||
///
|
||||
@@ -39,3 +41,16 @@ class TcpStatus {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
final numReg = RegExp(r'\s{1,}');
|
||||
|
||||
TcpStatus? parseTcp(String raw) {
|
||||
final lines = raw.split('\n');
|
||||
final idx = lines.lastWhere((element) => element.startsWith('Tcp:'),
|
||||
orElse: () => '');
|
||||
if (idx != '') {
|
||||
final vals = idx.split(numReg);
|
||||
return TcpStatus(vals[5].i, vals[6].i, vals[7].i, vals[8].i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
26
lib/data/model/ssh/virtual_key.dart
Normal file
26
lib/data/model/ssh/virtual_key.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:xterm/core.dart';
|
||||
|
||||
class VirtualKey {
|
||||
final TerminalKey key;
|
||||
final String text;
|
||||
final bool toggleable;
|
||||
final IconData? icon;
|
||||
|
||||
VirtualKey(this.key, this.text, {this.toggleable = false, this.icon});
|
||||
}
|
||||
|
||||
var virtualKeys = [
|
||||
VirtualKey(TerminalKey.escape, 'Esc'),
|
||||
VirtualKey(TerminalKey.alt, 'Alt', toggleable: true),
|
||||
VirtualKey(TerminalKey.pageUp, 'PgUp'),
|
||||
VirtualKey(TerminalKey.arrowUp, 'Up', icon: Icons.arrow_upward),
|
||||
VirtualKey(TerminalKey.pageDown, 'PgDn'),
|
||||
VirtualKey(TerminalKey.end, 'End'),
|
||||
VirtualKey(TerminalKey.tab, 'Tab'),
|
||||
VirtualKey(TerminalKey.control, 'Ctrl', toggleable: true),
|
||||
VirtualKey(TerminalKey.arrowLeft, 'Left', icon: Icons.arrow_back),
|
||||
VirtualKey(TerminalKey.arrowDown, 'Down', icon: Icons.arrow_downward),
|
||||
VirtualKey(TerminalKey.arrowRight, 'Right', icon: Icons.arrow_forward),
|
||||
VirtualKey(TerminalKey.home, 'Home'),
|
||||
];
|
||||
@@ -3,29 +3,23 @@ import 'dart:async';
|
||||
import 'package:dartssh2/dartssh2.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:toolbox/core/extension/stringx.dart';
|
||||
import 'package:toolbox/core/extension/uint8list.dart';
|
||||
import 'package:toolbox/core/provider_base.dart';
|
||||
import 'package:toolbox/data/model/server/cpu_status.dart';
|
||||
import 'package:toolbox/data/model/server/memory.dart';
|
||||
import 'package:toolbox/data/model/server/net_speed.dart';
|
||||
import 'package:toolbox/data/model/server/disk_info.dart';
|
||||
import 'package:toolbox/data/model/server/server.dart';
|
||||
import 'package:toolbox/data/model/server/server_private_info.dart';
|
||||
import 'package:toolbox/data/model/server/server_status.dart';
|
||||
import 'package:toolbox/data/model/server/snippet.dart';
|
||||
import 'package:toolbox/data/model/server/tcp_status.dart';
|
||||
import 'package:toolbox/data/store/private_key.dart';
|
||||
import 'package:toolbox/data/store/server.dart';
|
||||
import 'package:toolbox/data/store/setting.dart';
|
||||
import 'package:toolbox/locator.dart';
|
||||
|
||||
/// Must put this func out of any Class.
|
||||
/// Because of this function is called by [compute] in [ServerProvider.genClient].
|
||||
/// https://stackoverflow.com/questions/51998995/invalid-arguments-illegal-argument-in-isolate-message-object-is-a-closure
|
||||
List<SSHKeyPair> loadIndentity(String key) {
|
||||
return SSHKeyPair.fromPem(key);
|
||||
}
|
||||
import '../../core/extension/uint8list.dart';
|
||||
import '../../core/provider_base.dart';
|
||||
import '../../core/utils.dart';
|
||||
import '../../locator.dart';
|
||||
import '../model/server/cpu_status.dart';
|
||||
import '../model/server/disk_info.dart';
|
||||
import '../model/server/memory.dart';
|
||||
import '../model/server/net_speed.dart';
|
||||
import '../model/server/server.dart';
|
||||
import '../model/server/server_private_info.dart';
|
||||
import '../model/server/snippet.dart';
|
||||
import '../model/server/tcp_status.dart';
|
||||
import '../res/status.dart';
|
||||
import '../store/private_key.dart';
|
||||
import '../store/server.dart';
|
||||
import '../store/setting.dart';
|
||||
|
||||
const seperator = 'A====A';
|
||||
const shellCmd = "export LANG=en_US.utf-8 \necho '$seperator' \n"
|
||||
@@ -39,9 +33,6 @@ const shellCmd = "export LANG=en_US.utf-8 \necho '$seperator' \n"
|
||||
"cat /sys/class/thermal/thermal_zone*/type \necho $seperator \n"
|
||||
"cat /sys/class/thermal/thermal_zone*/temp";
|
||||
const shellPath = '.serverbox.sh';
|
||||
final cpuTempReg = RegExp(r'(x86_pkg_temp|cpu_thermal)');
|
||||
final numReg = RegExp(r'\s{1,}');
|
||||
final memItemReg = RegExp(r'([A-Z].+:)\s+([0-9]+) kB');
|
||||
|
||||
class ServerProvider extends BusyProvider {
|
||||
List<Server> _servers = [];
|
||||
@@ -221,7 +212,7 @@ class ServerProvider extends BusyProvider {
|
||||
/// 1635752901
|
||||
Future<void> _getNetSpeed(ServerPrivateInfo spi, String raw) async {
|
||||
final info = _servers.firstWhere((e) => e.spi == spi);
|
||||
info.status.netSpeed.update(await compute(_parseNetSpeed, raw));
|
||||
info.status.netSpeed.update(await compute(parseNetSpeed, raw));
|
||||
}
|
||||
|
||||
void _getSysVer(ServerPrivateInfo spi, String raw) {
|
||||
@@ -235,11 +226,11 @@ class ServerProvider extends BusyProvider {
|
||||
Future<void> _getCPU(ServerPrivateInfo spi, String raw, String tempType,
|
||||
String tempValue) async {
|
||||
final info = _servers.firstWhere((e) => e.spi == spi);
|
||||
final cpus = await compute(_parseCPU, raw);
|
||||
final cpus = await compute(parseCPU, raw);
|
||||
|
||||
if (cpus.isNotEmpty) {
|
||||
info.status.cpu
|
||||
.update(cpus, await compute(_getCPUTemp, [tempType, tempValue]));
|
||||
.update(cpus, await compute(parseCPUTemp, [tempType, tempValue]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,7 +241,7 @@ class ServerProvider extends BusyProvider {
|
||||
|
||||
Future<void> _getTcp(ServerPrivateInfo spi, String raw) async {
|
||||
final info = _servers.firstWhere((e) => e.spi == spi);
|
||||
final status = await compute(_parseTcp, raw);
|
||||
final status = await compute(parseTcp, raw);
|
||||
if (status != null) {
|
||||
info.status.tcp = status;
|
||||
}
|
||||
@@ -273,7 +264,7 @@ class ServerProvider extends BusyProvider {
|
||||
|
||||
Future<void> _getMem(ServerPrivateInfo spi, String raw) async {
|
||||
final info = _servers.firstWhere((e) => e.spi == spi);
|
||||
final mem = await compute(_parseMem, raw);
|
||||
final mem = await compute(parseMem, raw);
|
||||
info.status.mem = mem;
|
||||
}
|
||||
|
||||
@@ -286,96 +277,3 @@ class ServerProvider extends BusyProvider {
|
||||
return await client.run(snippet.script).string;
|
||||
}
|
||||
}
|
||||
|
||||
Memory _parseMem(String raw) {
|
||||
final items = raw.split('\n').map((e) => memItemReg.firstMatch(e)).toList();
|
||||
final total = int.parse(
|
||||
items.firstWhere((e) => e?.group(1) == 'MemTotal:')?.group(2) ?? '1');
|
||||
final free = int.parse(
|
||||
items.firstWhere((e) => e?.group(1) == 'MemFree:')?.group(2) ?? '0');
|
||||
final cached = int.parse(
|
||||
items.firstWhere((e) => e?.group(1) == 'Cached:')?.group(2) ?? '0');
|
||||
final available = int.parse(
|
||||
items.firstWhere((e) => e?.group(1) == 'MemAvailable:')?.group(2) ?? '0');
|
||||
return Memory(
|
||||
total: total,
|
||||
used: total - available,
|
||||
free: free,
|
||||
cache: cached,
|
||||
avail: available);
|
||||
}
|
||||
|
||||
TcpStatus? _parseTcp(String raw) {
|
||||
final lines = raw.split('\n');
|
||||
final idx = lines.lastWhere((element) => element.startsWith('Tcp:'),
|
||||
orElse: () => '');
|
||||
if (idx != '') {
|
||||
final vals = idx.split(numReg);
|
||||
return TcpStatus(vals[5].i, vals[6].i, vals[7].i, vals[8].i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
List<OneTimeCpuStatus> _parseCPU(String raw) {
|
||||
final List<OneTimeCpuStatus> cpus = [];
|
||||
|
||||
for (var item in raw.split('\n')) {
|
||||
if (item == '') break;
|
||||
final id = item.split(' ').first;
|
||||
final matches = item.replaceFirst(id, '').trim().split(' ');
|
||||
cpus.add(OneTimeCpuStatus(
|
||||
id,
|
||||
int.parse(matches[0]),
|
||||
int.parse(matches[1]),
|
||||
int.parse(matches[2]),
|
||||
int.parse(matches[3]),
|
||||
int.parse(matches[4]),
|
||||
int.parse(matches[5]),
|
||||
int.parse(matches[6])));
|
||||
}
|
||||
return cpus;
|
||||
}
|
||||
|
||||
String _getCPUTemp(List<String> segments) {
|
||||
const noMatch = "/sys/class/thermal/thermal_zone*/type";
|
||||
final type = segments[0];
|
||||
final value = segments[1];
|
||||
// Not support to get CPU temperature
|
||||
if (value.contains(noMatch) ||
|
||||
type.contains(noMatch) ||
|
||||
value.isEmpty ||
|
||||
type.isEmpty) {
|
||||
return '';
|
||||
}
|
||||
final split = type.split('\n');
|
||||
int idx = 0;
|
||||
for (var item in split) {
|
||||
if (item.contains(cpuTempReg)) {
|
||||
break;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
final valueSplited = value.split('\n');
|
||||
if (idx >= valueSplited.length) return '';
|
||||
final temp = int.tryParse(valueSplited[idx].trim());
|
||||
if (temp == null) return '';
|
||||
return '${(temp / 1000).toStringAsFixed(1)}°C';
|
||||
}
|
||||
|
||||
List<NetSpeedPart> _parseNetSpeed(String raw) {
|
||||
final split = raw.split('\n');
|
||||
final deviceCount = split.length - 3;
|
||||
if (deviceCount < 1) return [];
|
||||
final time = int.parse(split[split.length - 1]);
|
||||
final results = <NetSpeedPart>[];
|
||||
for (int idx = 2; idx < deviceCount; idx++) {
|
||||
final data = split[idx].trim().split(':');
|
||||
final device = data.first;
|
||||
final bytes = data.last.trim().split(' ');
|
||||
bytes.removeWhere((element) => element == '');
|
||||
final bytesIn = int.parse(bytes.first);
|
||||
final bytesOut = int.parse(bytes[8]);
|
||||
results.add(NetSpeedPart(device, bytesIn, bytesOut, time));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
29
lib/data/provider/virtual_keyboard.dart
Normal file
29
lib/data/provider/virtual_keyboard.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:xterm/core.dart';
|
||||
|
||||
class VirtualKeyboard extends TerminalInputHandler with ChangeNotifier {
|
||||
VirtualKeyboard();
|
||||
|
||||
bool ctrl = false;
|
||||
bool alt = false;
|
||||
|
||||
void reset(TerminalKeyboardEvent e) {
|
||||
if (e.ctrl) {
|
||||
ctrl = false;
|
||||
}
|
||||
if (e.alt) {
|
||||
alt = false;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
String? call(TerminalKeyboardEvent event) {
|
||||
final e = event.copyWith(
|
||||
ctrl: event.ctrl || ctrl,
|
||||
alt: event.alt || alt,
|
||||
);
|
||||
reset(e);
|
||||
return defaultInputHandler.call(e);
|
||||
}
|
||||
}
|
||||
48
lib/data/res/status.dart
Normal file
48
lib/data/res/status.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import '../model/server/cpu_status.dart';
|
||||
import '../model/server/disk_info.dart';
|
||||
import '../model/server/memory.dart';
|
||||
import '../model/server/net_speed.dart';
|
||||
import '../model/server/server_status.dart';
|
||||
import '../model/server/tcp_status.dart';
|
||||
|
||||
get _initMemory => Memory(
|
||||
total: 1,
|
||||
used: 0,
|
||||
free: 1,
|
||||
cache: 0,
|
||||
avail: 1,
|
||||
);
|
||||
get _initOneTimeCpuStatus => OneTimeCpuStatus(
|
||||
'cpu',
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
get initCpuStatus => CpuStatus(
|
||||
[_initOneTimeCpuStatus],
|
||||
[_initOneTimeCpuStatus],
|
||||
'',
|
||||
);
|
||||
get _initNetSpeedPart => NetSpeedPart(
|
||||
'',
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
get initNetSpeed => NetSpeed(
|
||||
[_initNetSpeedPart],
|
||||
[_initNetSpeedPart],
|
||||
);
|
||||
get initStatus => ServerStatus(
|
||||
initCpuStatus,
|
||||
_initMemory,
|
||||
'Loading...',
|
||||
'',
|
||||
[DiskInfo('/', '/', 0, '0', '0', '0')],
|
||||
TcpStatus(0, 0, 0, 0),
|
||||
initNetSpeed,
|
||||
);
|
||||
Reference in New Issue
Block a user