Skip to content

Commit

Permalink
Merge pull request #10 from Muska-Ami/dev
Browse files Browse the repository at this point in the history
添加高级启动功能 #1
  • Loading branch information
Muska-Ami authored Dec 31, 2023
2 parents 1c0c355 + 364e8e3 commit a346fdb
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 47 deletions.
6 changes: 3 additions & 3 deletions lib/controller/dsetting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class DSettingController extends GetxController {
));
}
} else if (frpc_download_cancel is Response) {
Navigator.of(context).pop();
Get.close(0);
/*await showDialog(
context: context,
builder: (context) {
Expand Down Expand Up @@ -117,8 +117,8 @@ class DSettingController extends GetxController {
);
}
/// 关闭对话框
Navigator.of(context).pop();
Navigator.of(context).pop();
Get.close(0);
Get.close(0);
});
} else {
frpc_download_show.clear();
Expand Down
155 changes: 121 additions & 34 deletions lib/controller/proxies.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyalcf/controller/user.dart';
import 'package:nyalcf/dio/proxies/configuration.dart';
import 'package:nyalcf/dio/proxies/get.dart';
import 'package:nyalcf/io/frpcConfigurationStorage.dart';
import 'package:nyalcf/model/ProxyInfo.dart';
import 'package:nyalcf/ui/model/FrpcConfigurationEditorDialog.dart';
import 'package:nyalcf/util/frpc/ProcessManager.dart';

import 'frpc.dart';
Expand All @@ -11,8 +16,13 @@ import 'frpc.dart';
* 代理 GetX 状态控制器
*/
class ProxiesController extends GetxController {
ProxiesController({required this.context});

final context;

final FrpcController f_c = Get.find();
final UserController c = Get.find();

var proxiesListWidgets = <DataRow>[
DataRow(cells: <DataCell>[
DataCell(SizedBox(
Expand All @@ -38,42 +48,32 @@ class ProxiesController extends GetxController {
var proxies = await ProxiesGetDio().get(username, token);
if (proxies is List<ProxyInfo>) {
proxiesListWidgets.value = <DataRow>[];
proxies.forEach((element) =>
proxiesListWidgets.add(DataRow(cells: <DataCell>[
DataCell(
Container(
width: 150.0,
height: 30.0,
child: SelectableText(element.proxy_name),
proxies.forEach(
(element) async => proxiesListWidgets.add(
DataRow(
cells: <DataCell>[
DataCell(
Container(
width: 150.0,
height: 30.0,
child: SelectableText(element.proxy_name),
),
),
DataCell(SelectableText(element.id.toString())),
DataCell(SelectableText(element.node.toString())),
DataCell(SelectableText(element.proxy_type)),
DataCell(SelectableText(element.local_ip)),
DataCell(
SelectableText(
'${element.local_port} -> ${element.remote_port}'),
),
),
DataCell(SelectableText(element.id.toString())),
DataCell(SelectableText(element.node.toString())),
DataCell(SelectableText(element.proxy_type)),
DataCell(SelectableText(element.local_ip)),
DataCell(
SelectableText('${element.local_port} -> ${element.remote_port}'),
),
DataCell(
Row(
children: [
IconButton(
icon: Icon(Icons.play_circle),
onPressed: () async => {
FrpcProcessManager().nwprcs(
frp_token: c.frp_token.value, proxy_id: element.id)
},
),
IconButton(
icon: Icon(Icons.edit),
onPressed: () async {
Get.snackbar('谁让你点了?', '还没写,爬去面板编辑喵喵喵!');
},
),
],
DataCell(
Row(children: await _buildActions(element)),
),
),
])));
],
),
),
);
proxiesListWidgets.refresh();
} else {
proxiesListWidgets.value = <DataRow>[
Expand All @@ -96,6 +96,93 @@ class ProxiesController extends GetxController {
}
}

_buildActions(element) async {
final List<Widget> list = <Widget>[
IconButton(
icon: Icon(Icons.play_circle),
tooltip: '启动',
onPressed: () async => {
FrpcProcessManager()
.nwprcs(frp_token: c.frp_token.value, proxy_id: element.id)
},
),
IconButton(
icon: Icon(Icons.edit),
tooltip: '编辑配置文件',
onPressed: () async {
/// 展示编辑框
void showDialogX(text) {
Get.dialog(
FrpcConfigEditorDialogX(context: context).dialog(
text,
proxy_id: element.id,
),
barrierDismissible: false,
);
}

final fp = await FrpcConfigurationStorage.getConfigPath(element.id);
String text = '';

/// 判空
if (fp != null) {
/// 配置已存在
final f = File(fp);
text = await f.readAsString();
showDialogX(text);
} else {
/// 配置不存在,获取写入
Get.dialog(FrpcConfigEditorDialogX(context: context).loading(),
barrierDismissible: false);
final res = await ProxiesConfigurationDio()
.get(c.frp_token.value, element.id);
if (res is String) {
print('Successfully get config ini');
text = res;
FrpcConfigurationStorage.setConfig(element.id, res);
Get.close(0);
showDialogX(text);
} else if (res == null) {
Get.snackbar(
'获取配置文件失败',
'返回值无效',
snackPosition: SnackPosition.BOTTOM,
animationDuration: Duration(milliseconds: 300),
);
Get.close(0);
} else {
print(res);
Get.snackbar(
'获取配置文件失败',
res.toString(),
snackPosition: SnackPosition.BOTTOM,
animationDuration: Duration(milliseconds: 300),
);
Get.close(0);
}
// Get.snackbar('谁让你点了?', '还没写,爬去面板编辑喵喵喵!');
}
},
),
];

final fcsp = await FrpcConfigurationStorage.getConfigPath(element.id);

if (fcsp != null)
list.add(
IconButton(
icon: Icon(Icons.remove),
tooltip: '移除自定义配置文件',
onPressed: () async {
File(fcsp).delete();
reload(c.user, c.token);
},
),
);

return list;
}

/**
* 重新加载代理列表
*/
Expand Down
1 change: 1 addition & 0 deletions lib/dio/basicConfig.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class basicConfig {
static final api_v1_url = 'https://api.locyanfrp.cn';
static final api_v2_url = 'https://api-v2.locyanfrp.cn/api/v2';
static final frpc_config_url = 'https://www.locyanfrp.cn/api';
static final github_api_url = 'https://api.github.com';
static final github_main_url = 'https://github.com';
// static final frpc_release_repo = 'LoCyan-Team/LoCyanFrpPureApp';
Expand Down
35 changes: 35 additions & 0 deletions lib/dio/proxies/configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:nyalcf/dio/basicConfig.dart';

class ProxiesConfigurationDio {
final dio = Dio();

Future<dynamic> get(String frp_token, int proxy_id) async {
try {
Map<String, dynamic> params_map = Map();
params_map['action'] = 'getcfg';
params_map['token'] = frp_token;
params_map['id'] = proxy_id;

final res = await dio.get('${basicConfig.frpc_config_url}',
queryParameters: params_map);
try {
final Map<String, dynamic> resData = jsonDecode(res.data);
print(res);
if (resData['success']) {
return resData['cfg'].replaceAll('\\n', '''
''');
} else {
return null;
}
} catch (ie) {
print(ie);
return null;
}
} catch (e) {
return e;
}
}
}
32 changes: 32 additions & 0 deletions lib/io/frpcConfigurationStorage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:io';

import 'package:nyalcf/util/FileIO.dart';

class FrpcConfigurationStorage {
static var _path = FileIO.support_path;
/// 配置文件目录路径
static get _configDir async {
final dir = Directory('${await _path}/frpc/.confini');
if (!await dir.exists())
await dir.create();
return '${await _path}/frpc/.confini';
}
/// 配置文件路径
static _configPath(int proxy_id) async =>
'${await _configDir}/${proxy_id}.ini';
/// 设置配置文件
static setConfig(int proxy_id, String ini) async {
final String cp = await _configPath(proxy_id);
final f = File(cp);
if (!(await f.exists())) f.create();
f.writeAsString(ini);
}
/// 获取配置文件路径
static Future<String?> getConfigPath(int proxy_id) async {
final String cp = await _configPath(proxy_id);
if (await File(cp).exists())
return cp;
else
return null;
}
}
6 changes: 3 additions & 3 deletions lib/ui/model/Drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ class DrawerX {
leading: Icon(Icons.dashboard),
title: const Text('仪表板'),
onTap: () {
Navigator.pop(context);
Get.close(0);
Get.toNamed('/panel/home');
},
),
ListTile(
leading: Icon(Icons.list),
title: const Text('隧道列表'),
onTap: () {
Navigator.pop(context);
Get.close(0);
Get.toNamed('/panel/proxies');
},
),
ListTile(
leading: Icon(Icons.last_page),
title: const Text('控制台'),
onTap: () {
Navigator.pop(context);
Get.close(0);
Get.toNamed('/panel/console');
},
)
Expand Down
Loading

0 comments on commit a346fdb

Please sign in to comment.