Skip to content

Commit

Permalink
[+]Add simple view for process list
Browse files Browse the repository at this point in the history
  • Loading branch information
Muska-Ami committed Dec 24, 2023
1 parent 56c44dd commit 35eac44
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
24 changes: 24 additions & 0 deletions lib/controller/dconsole.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyalcf/util/frpc/ProcessManager.dart';

class ConsoleController extends GetxController {

final process_list = FrpcProcessManager.process_list;

var widgets = <DataRow>[].obs;

load() {
widgets.value = <DataRow>[];
process_list.forEach((element) {
final process = element['process'];
final proxy_id = element['proxy_id'];

widgets.add(DataRow(cells: [
DataCell(Text(process.pid.toString())),
DataCell(Text(proxy_id.toString())),
]));
widgets.refresh();
});
}
}
6 changes: 3 additions & 3 deletions lib/controller/proxies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ProxiesController extends GetxController {
load(username, token) async {
var proxies = await ProxiesGetDio().get(username, token);
if (proxies is List<ProxyInfo>) {
List<DataRow> widgets = <DataRow>[];
proxies.forEach((element) => widgets.add(DataRow(cells: <DataCell>[
proxiesListWidgets.value = <DataRow>[];
proxies.forEach((element) => proxiesListWidgets.add(DataRow(cells: <DataCell>[
DataCell(
Container(
width: 150.0,
Expand Down Expand Up @@ -67,7 +67,7 @@ class ProxiesController extends GetxController {
),
),
])));
proxiesListWidgets.value = widgets;
proxiesListWidgets.refresh();
} else {
proxiesListWidgets.value = <DataRow>[
DataRow(cells: <DataCell>[
Expand Down
25 changes: 25 additions & 0 deletions lib/ui/model/ProcessListDialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyalcf/controller/dconsole.dart';

class ProcessListDialogX {
ProcessListDialogX({required this.context});

final context;
final ConsoleController c_c = Get.find();

Widget build() {
return SimpleDialog(
title: const Text('Frpc进程列表'),
children: <Widget>[
DataTable(
columns: [
DataColumn(label: Text('进程PID')),
DataColumn(label: Text('隧道ID')),
],
rows: c_c.widgets,
),
],
);
}
}
21 changes: 18 additions & 3 deletions lib/ui/panel/console.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyalcf/controller/dconsole.dart';
import 'package:nyalcf/controller/frpc.dart';
import 'package:nyalcf/controller/user.dart';
import 'package:nyalcf/ui/model/AccountDialog.dart';
import 'package:nyalcf/ui/model/AppbarActions.dart';
import 'package:nyalcf/ui/model/Drawer.dart';
import 'package:nyalcf/ui/model/FloatingActionButton.dart';
import 'package:nyalcf/ui/model/ProcessListDialog.dart';
import 'package:nyalcf/util/frpc/ProcessManager.dart';

class PanelConsole extends StatelessWidget {
PanelConsole({super.key, required this.title});

final UserController c = Get.find();
final FrpcController f_c = Get.find();
final ConsoleController c_c = Get.put(ConsoleController());
final String title;

@override
Widget build(BuildContext context) {
c_c.load();
return Scaffold(
appBar: AppBar(
title:
Text('$title - 仪表板', style: const TextStyle(color: Colors.white)),
backgroundColor: Colors.pink[100],
//automaticallyImplyLeading: false,
actions: AppbarActionsX(append: <Widget>[
Obx(() => IconButton(
Obx(
() => IconButton(
onPressed: () {
showDialog(
context: context,
builder: (x) {
c_c.load();
return AccountDialogX(context: context).build();
});
},
Expand All @@ -38,7 +44,9 @@ class PanelConsole extends StatelessWidget {
'${c.avatar}',
width: 35,
),
))),
),
),
),
], context: context)
.actions(),
),
Expand Down Expand Up @@ -67,7 +75,14 @@ class PanelConsole extends StatelessWidget {
child: Row(
children: [
ElevatedButton(
onPressed: () {},
onPressed: () {
showDialog(
context: context,
builder: (x) {
return ProcessListDialogX(context: context)
.build();
});
},
child: Text('查看进程列表'),
),
ElevatedButton(
Expand Down
22 changes: 13 additions & 9 deletions lib/util/frpc/ProcessManager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import 'package:nyalcf/io/frpcManagerStorage.dart';
class FrpcProcessManager {
final FrpcController f_c = Get.find();

final List<Process> process_list = <Process>[];
static final List<Map> process_list = <Map>[];
final frpc_work_path = FrpcManagerStorage.getRunPath('0.51.3');

void nwprcs({
required String frp_token,
required int proxy_id,
}) async {
final Map<String, dynamic> p_map = Map();
final process = await Process.start(
await FrpcManagerStorage.getFilePath('0.51.3'),
[
Expand All @@ -25,15 +26,17 @@ class FrpcProcessManager {
],
workingDirectory: await FrpcManagerStorage.getRunPath('0.51.3'),
);
process_list.add(process);
// Process [stdout, stderr]
p_map['process'] = process;
p_map['proxy_id'] = proxy_id;
process_list.add(p_map);
/// Process [stdout, stderr]
process.stdout.forEach((element) {
final fmt_str = utf8.decode(element).trim();
if (fmt_str.contains('stopped') || fmt_str.contains('启动失败')) {
print('[${proxy_id}][FRPC][WARN] ${fmt_str}');
f_c.appendWarnLog(fmt_str);
process.kill();
process_list.remove(process);
process_list.remove(p_map);
} else {
print('[${proxy_id}][FRPC][INFO] ${fmt_str}');
f_c.appendInfoLog(fmt_str);
Expand All @@ -46,7 +49,7 @@ class FrpcProcessManager {
print('[${proxy_id}][FRPC][ERROR] ${fmt_str}');
f_c.appendErrorLog(fmt_str);
process.kill();
process_list.remove(process);
process_list.remove(p_map);
});

//print('Process length: ${process_list.length}');
Expand All @@ -57,11 +60,12 @@ class FrpcProcessManager {
print('Process length: ${process_list.length}');
f_c.appendInfoLog('[SYSTEM][INFO] Killing all process...');
process_list.forEach((element) {
print('Killing frpc process, pid: ${element.pid}');
f_c.appendInfoLog('[SYSTEM][INFO] Killing process, pid: ${element.pid}');
element.kill();
process_list.remove(element);
print('Killing frpc process, pid: ${element['process'].pid}');
f_c.appendInfoLog(
'[SYSTEM][INFO] Killing process, pid: ${element['process'].pid}');
element['process'].kill();
});
process_list.clear();

print('Process length: ${process_list.length}');
}
Expand Down

0 comments on commit 35eac44

Please sign in to comment.