Skip to content

Commit

Permalink
[+]Allow user kill single frpc process
Browse files Browse the repository at this point in the history
  • Loading branch information
Muska-Ami committed Dec 25, 2023
1 parent 35eac44 commit b8a0355
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
26 changes: 23 additions & 3 deletions lib/controller/dconsole.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ import 'package:nyalcf/util/frpc/ProcessManager.dart';

class ConsoleController extends GetxController {

final process_list = FrpcProcessManager.process_list;

var widgets = <DataRow>[].obs;
var process_list = <Map>[].obs;

addProcess(p_map) {
process_list.add(p_map);
process_list.refresh();
}
removeProcess(p_map) {
process_list.remove(p_map);
process_list.refresh();
}
clearProcess() {
process_list.clear();
process_list.refresh();
}

load() {
widgets.value = <DataRow>[];
Expand All @@ -17,8 +29,16 @@ class ConsoleController extends GetxController {
widgets.add(DataRow(cells: [
DataCell(Text(process.pid.toString())),
DataCell(Text(proxy_id.toString())),
DataCell(
IconButton(
icon: Icon(Icons.close),
onPressed: () {
FrpcProcessManager().kill(element);
},
),
),
]));
widgets.refresh();
});
}
}
}
1 change: 1 addition & 0 deletions lib/ui/model/ProcessListDialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ProcessListDialogX {
columns: [
DataColumn(label: Text('进程PID')),
DataColumn(label: Text('隧道ID')),
DataColumn(label: Text('操作')),
],
rows: c_c.widgets,
),
Expand Down
7 changes: 3 additions & 4 deletions lib/ui/panel/console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PanelConsole extends StatelessWidget {

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

@override
Expand All @@ -34,7 +34,6 @@ class PanelConsole extends StatelessWidget {
showDialog(
context: context,
builder: (x) {
c_c.load();
return AccountDialogX(context: context).build();
});
},
Expand Down Expand Up @@ -79,8 +78,8 @@ class PanelConsole extends StatelessWidget {
showDialog(
context: context,
builder: (x) {
return ProcessListDialogX(context: context)
.build();
return Obx(() =>
ProcessListDialogX(context: context).build());
});
},
child: Text('查看进程列表'),
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/panel/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:get/get.dart';
import 'package:nyalcf/controller/dconsole.dart';
import 'package:nyalcf/controller/dpanel.dart';
import 'package:nyalcf/controller/user.dart';
import 'package:nyalcf/ui/model/AccountDialog.dart';
Expand All @@ -16,6 +17,7 @@ class PanelHome extends StatelessWidget {

final UserController c = Get.find();
final DPanelController dp_c = Get.put(DPanelController());
final ConsoleController c_c = Get.put(ConsoleController());
final String title;

@override
Expand Down
26 changes: 18 additions & 8 deletions lib/util/frpc/ProcessManager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import 'dart:convert';
import 'dart:io';

import 'package:get/get.dart';
import 'package:nyalcf/controller/dconsole.dart';
import 'package:nyalcf/controller/frpc.dart';
import 'package:nyalcf/io/frpcManagerStorage.dart';

class FrpcProcessManager {
final FrpcController f_c = Get.find();
final ConsoleController c_c = Get.find();

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

void nwprcs({
Expand All @@ -28,15 +29,15 @@ class FrpcProcessManager {
);
p_map['process'] = process;
p_map['proxy_id'] = proxy_id;
process_list.add(p_map);
c_c.addProcess(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(p_map);
c_c.removeProcess(p_map);
} else {
print('[${proxy_id}][FRPC][INFO] ${fmt_str}');
f_c.appendInfoLog(fmt_str);
Expand All @@ -49,24 +50,33 @@ class FrpcProcessManager {
print('[${proxy_id}][FRPC][ERROR] ${fmt_str}');
f_c.appendErrorLog(fmt_str);
process.kill();
process_list.remove(p_map);
c_c.removeProcess(p_map);
});

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

void killAll() {
print('Killing all process');
print('Process length: ${process_list.length}');
print('Process length: ${c_c.process_list.length}');
f_c.appendInfoLog('[SYSTEM][INFO] Killing all process...');
process_list.forEach((element) {
c_c.process_list.forEach((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();
c_c.clearProcess();

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

void kill(prs) {
print('Killing frpc process, pid: ${prs['process'].pid}');
f_c.appendInfoLog('[SYSTEM][INFO] Killing process, pid: ${prs['process'].pid}');
prs['process'].kill();
c_c.removeProcess(prs);

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

0 comments on commit b8a0355

Please sign in to comment.