|
@@ -2,8 +2,10 @@ import 'dart:convert';
|
|
|
import 'dart:io';
|
|
|
import 'dart:async';
|
|
|
import 'package:get/get.dart';
|
|
|
+import 'package:yaml/yaml.dart';
|
|
|
import 'package:yaml_edit/yaml_edit.dart';
|
|
|
import 'package:path/path.dart' as path;
|
|
|
+import '../../common/LogHelper.dart';
|
|
|
import '../../common/constants.dart';
|
|
|
import '../../const/const.dart';
|
|
|
import '../../controller/controllers.dart';
|
|
@@ -80,10 +82,16 @@ class ClashService extends GetxController {
|
|
|
dnsHijack: ['any:53'],
|
|
|
),
|
|
|
proxies: [],
|
|
|
-
|
|
|
+ proxyGroups: [
|
|
|
+ ProxyGroup(
|
|
|
+ name: 'proxy',
|
|
|
+ type: 'select',
|
|
|
+ proxies: ['DIRECT'],
|
|
|
+ ),
|
|
|
+ ],
|
|
|
rules: [
|
|
|
'GEOIP,CN,DIRECT',
|
|
|
- 'MATCH,DIRECT'
|
|
|
+ 'MATCH,proxy'
|
|
|
]
|
|
|
);
|
|
|
try {
|
|
@@ -190,27 +198,46 @@ class ClashService extends GetxController {
|
|
|
|
|
|
Future<void> reloadClashCore() async {
|
|
|
try {
|
|
|
- // if(coreStatus.value == RunningState.stoped){
|
|
|
- // await updatePorts();
|
|
|
- // }
|
|
|
-
|
|
|
controllers.config.config.value.selected = Files.makeProxyConfig.path;
|
|
|
if (coreStatus.value == RunningState.running) {
|
|
|
controllers.global.updateMsg("切换配置...");
|
|
|
await controllers.config.readClashCoreApi();
|
|
|
controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
|
|
|
- await controllers.core.changeConfig(path.join(Paths.config.path, controllers.config.config.value.selected));
|
|
|
+
|
|
|
+ // 在切换配置前确保配置文件有效
|
|
|
+ final configFile = File(path.join(Paths.config.path, controllers.config.config.value.selected));
|
|
|
+ if (!await configFile.exists()) {
|
|
|
+ throw Exception("配置文件不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保配置文件包含必要的代理组
|
|
|
+ final configStr = await configFile.readAsString();
|
|
|
+ final yamlEditor = YamlEditor(configStr);
|
|
|
+ final configMap = yamlEditor.parseAt([]) as YamlMap;
|
|
|
+
|
|
|
+ if (configMap['proxy-groups'] == null ||
|
|
|
+ (configMap['proxy-groups'] as YamlList).isEmpty) {
|
|
|
+ throw Exception("配置文件缺少代理组");
|
|
|
+ }
|
|
|
+
|
|
|
+ await controllers.core.changeConfig(configFile.path);
|
|
|
controllers.global.updateMsg("fetchReloadConfig${controllers.config.clashCoreApiAddress.value}...");
|
|
|
}
|
|
|
} catch (e) {
|
|
|
- // if(coreStatus.value == RunningState.stoped){
|
|
|
- // await updatePorts();
|
|
|
- // }
|
|
|
-
|
|
|
- controllers.global.updateMsg("重新配置...");
|
|
|
- await controllers.config.readClashCoreApi();
|
|
|
- controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
|
|
|
- await controllers.core.changeConfig(path.join(Paths.config.path, controllers.config.config.value.selected));
|
|
|
+ LogHelper().e("切换配置失败: $e");
|
|
|
+
|
|
|
+ // 如果切换失败,尝试回退到初始配置
|
|
|
+ try {
|
|
|
+ controllers.config.config.value.selected = Files.makeInitProxyConfig.path;
|
|
|
+ await controllers.config.readClashCoreApi();
|
|
|
+ controllers.core.setApi(controllers.config.clashCoreApiAddress.value, controllers.config.clashCoreApiSecret.value);
|
|
|
+ await controllers.core.changeConfig(
|
|
|
+ path.join(Paths.config.path, controllers.config.config.value.selected)
|
|
|
+ );
|
|
|
+ } catch (fallbackError) {
|
|
|
+ LogHelper().e("回退到初始配置失败: $fallbackError");
|
|
|
+ throw Exception("配置切换失败");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|